|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace KaririCode\ProcessorPipeline\Tests\Exception; |
| 6 | + |
| 7 | +use KaririCode\ProcessorPipeline\Exception\ProcessorRuntimeException; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +final class ProcessorRuntimeExceptionTest extends TestCase |
| 11 | +{ |
| 12 | + public function testContextNotFound(): void |
| 13 | + { |
| 14 | + $exception = ProcessorRuntimeException::contextNotFound('payment'); |
| 15 | + |
| 16 | + $this->assertInstanceOf(ProcessorRuntimeException::class, $exception); |
| 17 | + $this->assertEquals(2601, $exception->getCode()); |
| 18 | + $this->assertEquals('PROCESSOR_CONTEXT_NOT_FOUND', $exception->getErrorCode()); |
| 19 | + $this->assertEquals("Processor context 'payment' not found", $exception->getMessage()); |
| 20 | + $this->assertNull($exception->getPrevious()); |
| 21 | + } |
| 22 | + |
| 23 | + public function testProcessorNotFound(): void |
| 24 | + { |
| 25 | + $exception = ProcessorRuntimeException::processorNotFound('validate', 'payment'); |
| 26 | + |
| 27 | + $this->assertInstanceOf(ProcessorRuntimeException::class, $exception); |
| 28 | + $this->assertEquals(2602, $exception->getCode()); |
| 29 | + $this->assertEquals('PROCESSOR_NOT_FOUND', $exception->getErrorCode()); |
| 30 | + $this->assertEquals("Processor 'validate' not found in context 'payment'", $exception->getMessage()); |
| 31 | + $this->assertNull($exception->getPrevious()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testProcessingFailed(): void |
| 35 | + { |
| 36 | + $exception = ProcessorRuntimeException::processingFailed('email'); |
| 37 | + |
| 38 | + $this->assertInstanceOf(ProcessorRuntimeException::class, $exception); |
| 39 | + $this->assertEquals(2606, $exception->getCode()); |
| 40 | + $this->assertEquals('PROCESSOR_PROCESSING_FAILED', $exception->getErrorCode()); |
| 41 | + $this->assertEquals( |
| 42 | + "Processing failed for property 'email'", |
| 43 | + $exception->getMessage() |
| 44 | + ); |
| 45 | + $this->assertNull($exception->getPrevious()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dataProvider specialValuesProvider |
| 50 | + */ |
| 51 | + public function testWithSpecialValues(string $context, string $processor, string $details): void |
| 52 | + { |
| 53 | + $exceptionContext = ProcessorRuntimeException::contextNotFound($context); |
| 54 | + $this->assertStringContainsString($context, $exceptionContext->getMessage()); |
| 55 | + |
| 56 | + $exceptionProcessor = ProcessorRuntimeException::processorNotFound($processor, $context); |
| 57 | + $this->assertStringContainsString($processor, $exceptionProcessor->getMessage()); |
| 58 | + $this->assertStringContainsString($context, $exceptionProcessor->getMessage()); |
| 59 | + } |
| 60 | + |
| 61 | + public static function specialValuesProvider(): array |
| 62 | + { |
| 63 | + return [ |
| 64 | + 'empty values' => ['', '', ''], |
| 65 | + 'special characters' => ['payment!@#', 'validator$%^', 'error&*()'], |
| 66 | + 'unicode characters' => ['pagaménto', 'validação', 'erro'], |
| 67 | + 'very long values' => [ |
| 68 | + str_repeat('a', 100), |
| 69 | + str_repeat('b', 100), |
| 70 | + str_repeat('c', 100), |
| 71 | + ], |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + public function testExceptionHierarchy(): void |
| 76 | + { |
| 77 | + $exception = ProcessorRuntimeException::contextNotFound('payment'); |
| 78 | + |
| 79 | + $this->assertInstanceOf(\Exception::class, $exception); |
| 80 | + $this->assertInstanceOf(\Throwable::class, $exception); |
| 81 | + } |
| 82 | + |
| 83 | + public function testExceptionWithPreviousException(): void |
| 84 | + { |
| 85 | + $previous = new \Exception('Original error'); |
| 86 | + |
| 87 | + $reflection = new \ReflectionClass(ProcessorRuntimeException::class); |
| 88 | + $method = $reflection->getMethod('createException'); |
| 89 | + $method->setAccessible(true); |
| 90 | + |
| 91 | + $exception = $method->invokeArgs(null, [ |
| 92 | + 2601, |
| 93 | + 'PROCESSOR_CONTEXT_NOT_FOUND', |
| 94 | + 'Test message', |
| 95 | + $previous, |
| 96 | + ]); |
| 97 | + |
| 98 | + $this->assertInstanceOf(ProcessorRuntimeException::class, $exception); |
| 99 | + $this->assertEquals(2601, $exception->getCode()); |
| 100 | + $this->assertEquals('PROCESSOR_CONTEXT_NOT_FOUND', $exception->getErrorCode()); |
| 101 | + $this->assertEquals('Test message', $exception->getMessage()); |
| 102 | + $this->assertSame($previous, $exception->getPrevious()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @dataProvider invalidPropertyValuesProvider |
| 107 | + */ |
| 108 | + public function testProcessingFailedWithDifferentPropertyTypes($property): void |
| 109 | + { |
| 110 | + $exception = ProcessorRuntimeException::processingFailed($property); |
| 111 | + $message = $exception->getMessage(); |
| 112 | + |
| 113 | + $this->assertIsString($message); |
| 114 | + $this->assertStringContainsString((string) $property, $message); |
| 115 | + } |
| 116 | + |
| 117 | + public static function invalidPropertyValuesProvider(): array |
| 118 | + { |
| 119 | + return [ |
| 120 | + 'valid string' => ['email'], |
| 121 | + ]; |
| 122 | + } |
| 123 | +} |
0 commit comments