Skip to content

Commit c621016

Browse files
committed
test(processor): implement comprehensive test suite for pipeline handlers
- Add ProcessorRuntimeException test coverage: * Tests for all static exception factory methods (2601-2606 error codes) * Proper error code and message assertions * Exception hierarchy validation - Enhance ProcessorPipeline test coverage: * Test all pipeline operations (add, process, clear) * Validate processor state management * Error handling scenarios * Chain processing validation - Complete ProcessorAttributeHandler test coverage: * Validation error handling * Processing error scenarios * Result collection management * Pipeline and processor interactions * Cache behavior * State reset functionality - Fix inheritance-related test issues: * Proper mock configuration for inherited methods * Correct validation scenarios * Result transfer verification * Error propagation testing
1 parent 3ba1b7b commit c621016

File tree

5 files changed

+351
-34
lines changed

5 files changed

+351
-34
lines changed

src/Handler/ProcessorAttributeHandler.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
use KaririCode\ProcessorPipeline\Processor\ProcessorValidator;
1212
use KaririCode\ProcessorPipeline\Result\ProcessingResultCollection;
1313

14-
/**
15-
* Handler for processing attributes with configured processors.
16-
*/
1714
final class ProcessorAttributeHandler extends AttributeHandler
1815
{
1916
private ProcessingResultCollection $results;
@@ -39,9 +36,6 @@ public function handleAttribute(string $propertyName, object $attribute, mixed $
3936
return $result;
4037
}
4138

42-
/**
43-
* Transfers results from parent handler to ProcessingResultCollection.
44-
*/
4539
private function transferResults(string $propertyName): void
4640
{
4741
$processedValues = parent::getProcessedPropertyValues();
@@ -78,25 +72,16 @@ public function getProcessingResultErrors(): array
7872
return $this->results->getErrors();
7973
}
8074

81-
/**
82-
* Checks if there are any processing errors.
83-
*/
8475
public function hasErrors(): bool
8576
{
8677
return $this->results->hasErrors();
8778
}
8879

89-
/**
90-
* Gets the processing results collection.
91-
*/
9280
public function getProcessingResults(): ProcessingResultCollection
9381
{
9482
return $this->results;
9583
}
9684

97-
/**
98-
* Resets the processing state.
99-
*/
10085
public function reset(): void
10186
{
10287
parent::reset();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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->assertSame(2601, $exception->getCode());
17+
$this->assertSame('PROCESSOR_CONTEXT_NOT_FOUND', $exception->getErrorCode());
18+
$this->assertSame("Processor context 'payment' not found", $exception->getMessage());
19+
}
20+
21+
public function testProcessorNotFound(): void
22+
{
23+
$exception = ProcessorRuntimeException::processorNotFound('validate', 'payment');
24+
25+
$this->assertSame(2602, $exception->getCode());
26+
$this->assertSame('PROCESSOR_NOT_FOUND', $exception->getErrorCode());
27+
$this->assertSame("Processor 'validate' not found in context 'payment'", $exception->getMessage());
28+
}
29+
30+
public function testInvalidProcessor(): void
31+
{
32+
$exception = ProcessorRuntimeException::invalidProcessor('emailValidator', 'Invalid configuration');
33+
34+
$this->assertSame(2603, $exception->getCode());
35+
$this->assertSame('PROCESSOR_INVALID', $exception->getErrorCode());
36+
$this->assertSame("Invalid processor 'emailValidator': Invalid configuration", $exception->getMessage());
37+
}
38+
39+
public function testInvalidContext(): void
40+
{
41+
$exception = ProcessorRuntimeException::invalidContext('payment', 'Context not initialized');
42+
43+
$this->assertSame(2604, $exception->getCode());
44+
$this->assertSame('PROCESSOR_CONTEXT_INVALID', $exception->getErrorCode());
45+
$this->assertSame("Invalid processor context 'payment': Context not initialized", $exception->getMessage());
46+
}
47+
48+
public function testInvalidConfiguration(): void
49+
{
50+
$exception = ProcessorRuntimeException::invalidConfiguration('emailValidator', 'Missing required fields');
51+
52+
$this->assertSame(2605, $exception->getCode());
53+
$this->assertSame('PROCESSOR_CONFIG_INVALID', $exception->getErrorCode());
54+
$this->assertSame(
55+
"Invalid processor configuration for 'emailValidator': Missing required fields",
56+
$exception->getMessage()
57+
);
58+
}
59+
60+
public function testProcessingFailed(): void
61+
{
62+
$exception = ProcessorRuntimeException::processingFailed('email');
63+
64+
$this->assertSame(2606, $exception->getCode());
65+
$this->assertSame('PROCESSOR_PROCESSING_FAILED', $exception->getErrorCode());
66+
$this->assertSame("Processing failed for property 'email'", $exception->getMessage());
67+
}
68+
}

tests/AttributeHandlerTest.php renamed to tests/Handler/AttributeHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace KaririCode\PropertyInspector\Tests;
5+
namespace KaririCode\PropertyInspector\Tests\Handler;
66

77
use KaririCode\Contract\Processor\Attribute\CustomizableMessageAttribute;
88
use KaririCode\Contract\Processor\Attribute\ProcessableAttribute;
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\ProcessorPipeline\Tests\Handler;
6+
7+
use KaririCode\Contract\Processor\Attribute\ProcessableAttribute;
8+
use KaririCode\Contract\Processor\Pipeline;
9+
use KaririCode\Contract\Processor\ProcessorBuilder;
10+
use KaririCode\Contract\Processor\ValidatableProcessor;
11+
use KaririCode\ProcessorPipeline\Handler\ProcessorAttributeHandler;
12+
use KaririCode\ProcessorPipeline\Processor\ProcessorConfigBuilder;
13+
use KaririCode\ProcessorPipeline\Processor\ProcessorValidator;
14+
use KaririCode\ProcessorPipeline\Result\ProcessingResultCollection;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
final class ProcessorAttributeHandlerTest extends TestCase
19+
{
20+
private ProcessorAttributeHandler $handler;
21+
private ProcessorBuilder|MockObject $builder;
22+
private ProcessorValidator|MockObject $validator;
23+
private ProcessorConfigBuilder|MockObject $configBuilder;
24+
private ValidatableProcessor|MockObject $processor;
25+
private Pipeline|MockObject $pipeline;
26+
27+
protected function setUp(): void
28+
{
29+
$this->builder = $this->createMock(ProcessorBuilder::class);
30+
$this->validator = $this->createMock(ProcessorValidator::class);
31+
$this->configBuilder = $this->createMock(ProcessorConfigBuilder::class);
32+
$this->processor = $this->createMock(ValidatableProcessor::class);
33+
$this->pipeline = $this->createMock(Pipeline::class);
34+
35+
$this->handler = new ProcessorAttributeHandler(
36+
'validator',
37+
$this->builder,
38+
$this->validator,
39+
$this->configBuilder
40+
);
41+
}
42+
43+
public function testHandleAttributeWithValidProcessor(): void
44+
{
45+
$attribute = $this->createMock(ProcessableAttribute::class);
46+
47+
$this->configureBasicMocks();
48+
$this->processor->method('isValid')->willReturn(true);
49+
$this->pipeline->method('process')->willReturn('processed');
50+
51+
$result = $this->handler->handleAttribute('property', $attribute, 'value');
52+
53+
$this->assertEquals('processed', $result);
54+
$this->assertFalse($this->handler->hasErrors());
55+
}
56+
57+
public function testHandleAttributeWithValidationError(): void
58+
{
59+
$attribute = $this->createMock(ProcessableAttribute::class);
60+
$attribute->method('getProcessors')
61+
->willReturn(['processor1' => []]);
62+
63+
// Configurar processador para falhar validação
64+
$processor = $this->createMock(ValidatableProcessor::class);
65+
$processor->method('isValid')->willReturn(false);
66+
$processor->method('getErrorKey')->willReturn('validation_failed');
67+
68+
// Configurar pipeline
69+
$pipeline = $this->createMock(Pipeline::class);
70+
$pipeline->method('process')->willReturn('processed');
71+
72+
// Configurar builder
73+
$this->builder->method('build')
74+
->with('validator', 'processor1')
75+
->willReturn($processor);
76+
$this->builder->method('buildPipeline')
77+
->willReturn($pipeline);
78+
79+
// Configurar config builder
80+
$this->configBuilder->method('build')
81+
->willReturn(['processor1' => []]);
82+
83+
// Configurar validator para retornar erro
84+
$this->validator->method('validate')
85+
->willReturn([
86+
'errorKey' => 'validation_failed',
87+
'message' => 'Validation failed',
88+
]);
89+
90+
$this->handler->handleAttribute('property', $attribute, 'value');
91+
92+
$this->assertTrue($this->handler->hasErrors());
93+
$errors = $this->handler->getProcessingResultErrors();
94+
$this->assertArrayHasKey('property', $errors);
95+
$this->assertIsArray($errors['property']);
96+
$this->assertNotEmpty($errors['property']);
97+
}
98+
99+
public function testHandleAttributeWithProcessingError(): void
100+
{
101+
$attribute = $this->createMock(ProcessableAttribute::class);
102+
103+
$this->configureBasicMocks();
104+
$this->pipeline->method('process')
105+
->willThrowException(new \Exception('Processing error'));
106+
107+
$result = $this->handler->handleAttribute('property', $attribute, 'value');
108+
109+
$this->assertEquals('value', $result);
110+
$this->assertArrayHasKey('property', $this->handler->getProcessingResultErrors());
111+
}
112+
113+
public function testGetProcessingResults(): void
114+
{
115+
$attribute = $this->createMock(ProcessableAttribute::class);
116+
117+
$this->configureBasicMocks();
118+
$this->pipeline->method('process')->willReturn('processed');
119+
120+
$this->handler->handleAttribute('property', $attribute, 'value');
121+
122+
$results = $this->handler->getProcessingResults();
123+
$this->assertInstanceOf(ProcessingResultCollection::class, $results);
124+
$processedData = $results->getProcessedData();
125+
$this->assertArrayHasKey('property', $processedData);
126+
$this->assertEquals('processed', $processedData['property']);
127+
}
128+
129+
public function testReset(): void
130+
{
131+
$attribute = $this->createMock(ProcessableAttribute::class);
132+
133+
$this->configureBasicMocks();
134+
$this->pipeline->method('process')->willReturn('processed');
135+
136+
$this->handler->handleAttribute('property', $attribute, 'value');
137+
$this->handler->reset();
138+
139+
$this->assertEmpty($this->handler->getProcessedPropertyValues()['values']);
140+
$this->assertEmpty($this->handler->getProcessingResultErrors());
141+
$this->assertFalse($this->handler->hasErrors());
142+
}
143+
144+
public function testGetProcessedPropertyValues(): void
145+
{
146+
$attribute = $this->createMock(ProcessableAttribute::class);
147+
148+
$this->configureBasicMocks();
149+
$this->pipeline->method('process')->willReturn('processed');
150+
151+
$this->handler->handleAttribute('property', $attribute, 'value');
152+
153+
$values = $this->handler->getProcessedPropertyValues();
154+
$this->assertArrayHasKey('values', $values);
155+
$this->assertArrayHasKey('timestamp', $values);
156+
$this->assertEquals(['property' => 'processed'], $values['values']);
157+
}
158+
159+
private function configureBasicMocks(): void
160+
{
161+
// ConfigBuilder setup
162+
$this->configBuilder->method('build')
163+
->willReturn(['processor1' => ['config' => 'value']]);
164+
165+
// Builder setup
166+
$this->builder->method('build')
167+
->willReturn($this->processor);
168+
169+
$this->builder->method('buildPipeline')
170+
->willReturn($this->pipeline);
171+
172+
// Validator setup
173+
$this->validator->method('validate')
174+
->willReturnCallback(function ($processor) {
175+
if ($processor instanceof ValidatableProcessor && !$processor->isValid()) {
176+
return [
177+
'errorKey' => $processor->getErrorKey(),
178+
'message' => 'Validation failed',
179+
];
180+
}
181+
182+
return null;
183+
});
184+
}
185+
}

0 commit comments

Comments
 (0)