Skip to content

Commit 6d2e25b

Browse files
committed
test(pipeline): complete test coverage and remove unused exceptions
COVERAGE: - Complete test coverage for ProcessingResultCollection - Implement comprehensive tests for ProcessedData - Add full coverage for ProcessingError - Add tests for error hash generation - Cover all timestamp validations - Verify array conversions - Test error collections and data management CLEANUP: - Remove unused exception methods from ProcessorRuntimeException: * invalidProcessor() * invalidContext() * invalidConfiguration()
1 parent c621016 commit 6d2e25b

7 files changed

+630
-95
lines changed

src/Exception/ProcessorRuntimeException.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,6 @@ public static function processorNotFound(string $processorName, string $context)
3333
);
3434
}
3535

36-
public static function invalidProcessor(string $processorName, string $details): self
37-
{
38-
return self::createException(
39-
self::CODE_INVALID_PROCESSOR,
40-
'PROCESSOR_INVALID',
41-
"Invalid processor '{$processorName}': {$details}"
42-
);
43-
}
44-
45-
public static function invalidContext(string $context, string $details): self
46-
{
47-
return self::createException(
48-
self::CODE_INVALID_CONTEXT,
49-
'PROCESSOR_CONTEXT_INVALID',
50-
"Invalid processor context '{$context}': {$details}"
51-
);
52-
}
53-
54-
public static function invalidConfiguration(string $processorName, string $details): self
55-
{
56-
return self::createException(
57-
self::CODE_PROCESSOR_CONFIG_INVALID,
58-
'PROCESSOR_CONFIG_INVALID',
59-
"Invalid processor configuration for '{$processorName}': {$details}"
60-
);
61-
}
62-
6336
public static function processingFailed(string $property): self
6437
{
6538
return self::createException(

tests/Exception/ProcessorRuntimeExceptionTest .php

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

tests/Handler/ProcessorAttributeHandlerTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,44 @@ private function configureBasicMocks(): void
182182
return null;
183183
});
184184
}
185+
186+
public function testValidateProcessorsWithInvalidProcessor(): void
187+
{
188+
$processorsConfig = ['processor1' => []];
189+
$messages = ['processor1' => 'Validation failed for processor1'];
190+
191+
$processor = $this->createMock(ValidatableProcessor::class);
192+
$processor->method('isValid')->willReturn(false);
193+
$processor->method('getErrorKey')->willReturn('invalid_processor');
194+
195+
$this->builder->method('build')->willReturn($processor);
196+
197+
// Usar Reflection para acessar validateProcessors
198+
$reflection = new \ReflectionClass(ProcessorAttributeHandler::class);
199+
$method = $reflection->getMethod('validateProcessors');
200+
$method->setAccessible(true);
201+
202+
$errors = $method->invoke($this->handler, $processorsConfig, $messages);
203+
204+
$this->assertArrayHasKey('processor1', $errors);
205+
$this->assertEquals('invalid_processor', $errors['processor1']['errorKey']);
206+
$this->assertEquals('Validation failed for processor1', $errors['processor1']['message']);
207+
}
208+
209+
public function testProcessValueWithValidPipeline(): void
210+
{
211+
$config = ['processor1' => []];
212+
$this->pipeline->method('process')->willReturn('processed_value');
213+
214+
$this->builder->method('buildPipeline')->willReturn($this->pipeline);
215+
216+
// Usar Reflection para acessar processValue
217+
$reflection = new \ReflectionClass(ProcessorAttributeHandler::class);
218+
$method = $reflection->getMethod('processValue');
219+
$method->setAccessible(true);
220+
221+
$result = $method->invoke($this->handler, 'input_value', $config);
222+
223+
$this->assertEquals('processed_value', $result);
224+
}
185225
}

tests/Result/ProcessedDataTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\ProcessorPipeline\Tests\Result;
6+
7+
use KaririCode\ProcessorPipeline\Result\ProcessedData;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class ProcessedDataTest extends TestCase
11+
{
12+
public function testGetProperty(): void
13+
{
14+
$data = new ProcessedData('email', '[email protected]');
15+
16+
$this->assertEquals('email', $data->getProperty());
17+
}
18+
19+
public function testGetValue(): void
20+
{
21+
$data = new ProcessedData('email', '[email protected]');
22+
23+
$this->assertEquals('[email protected]', $data->getValue());
24+
}
25+
26+
public function testToArray(): void
27+
{
28+
$data = new ProcessedData('email', '[email protected]');
29+
$result = $data->toArray();
30+
31+
$this->assertIsArray($result);
32+
$this->assertArrayHasKey('value', $result);
33+
$this->assertArrayHasKey('timestamp', $result);
34+
$this->assertEquals('[email protected]', $result['value']);
35+
$this->assertIsInt($result['timestamp']);
36+
$this->assertLessThanOrEqual(time(), $result['timestamp']);
37+
}
38+
39+
/**
40+
* @dataProvider valueTypesProvider
41+
*/
42+
public function testDifferentValueTypes(mixed $value): void
43+
{
44+
$data = new ProcessedData('property', $value);
45+
46+
$this->assertSame($value, $data->getValue());
47+
$array = $data->toArray();
48+
$this->assertSame($value, $array['value']);
49+
}
50+
51+
public static function valueTypesProvider(): array
52+
{
53+
return [
54+
'string' => ['test'],
55+
'integer' => [42],
56+
'float' => [3.14],
57+
'boolean' => [true],
58+
'null' => [null],
59+
'array' => [['test' => 'value']],
60+
'object' => [new \stdClass()],
61+
];
62+
}
63+
}

0 commit comments

Comments
 (0)