44
55namespace KaririCode \PropertyInspector \Tests ;
66
7+ use KaririCode \Contract \Processor \Attribute \CustomizableMessageAttribute ;
8+ use KaririCode \Contract \Processor \Attribute \ProcessableAttribute ;
79use KaririCode \Contract \Processor \Pipeline ;
8- use KaririCode \Contract \Processor \ProcessableAttribute ;
910use KaririCode \Contract \Processor \ProcessorBuilder ;
1011use KaririCode \ProcessorPipeline \Exception \ProcessingException ;
1112use KaririCode \PropertyInspector \AttributeHandler ;
13+ use PHPUnit \Framework \MockObject \MockObject ;
1214use PHPUnit \Framework \TestCase ;
1315
16+ interface CombinedAttribute extends
17+ ProcessableAttribute,
18+ CustomizableMessageAttribute
19+ {
20+ }
21+
1422final class AttributeHandlerTest extends TestCase
1523{
1624 private AttributeHandler $ attributeHandler ;
17- private ProcessorBuilder $ processorBuilder ;
25+ private ProcessorBuilder | MockObject $ processorBuilder ;
1826
1927 protected function setUp (): void
2028 {
@@ -45,7 +53,7 @@ public function testHandleAttributeProcessesValue(): void
4553 $ this ->assertSame ('processedValue ' , $ result );
4654 }
4755
48- public function testHandleAttributeReturnsFallbackOnException (): void
56+ public function testHandleAttributeReturnsOriginalValueOnException (): void
4957 {
5058 $ mockAttribute = $ this ->createMock (ProcessableAttribute::class);
5159 $ mockPipeline = $ this ->createMock (Pipeline::class);
@@ -62,17 +70,17 @@ public function testHandleAttributeReturnsFallbackOnException(): void
6270 ->method ('getProcessors ' )
6371 ->willReturn (['processor1 ' ]);
6472
65- $ mockAttribute ->expects ($ this ->once ())
66- ->method ('getFallbackValue ' )
67- ->willReturn ('fallbackValue ' );
68-
6973 $ result = $ this ->attributeHandler ->handleAttribute ('testProperty ' , $ mockAttribute , 'initialValue ' );
70- $ this ->assertSame ('fallbackValue ' , $ result );
74+ $ this ->assertSame ('initialValue ' , $ result );
75+
76+ $ errors = $ this ->attributeHandler ->getProcessingErrors ();
77+ $ this ->assertArrayHasKey ('testProperty ' , $ errors );
78+ $ this ->assertContains ('Test exception ' , $ errors ['testProperty ' ]);
7179 }
7280
7381 public function testHandleAttributeReturnsNullWhenAttributeNotProcessable (): void
7482 {
75- $ nonProcessableAttribute = new \stdClass (); // Simulate a non-ProcessableAttribute object
83+ $ nonProcessableAttribute = new \stdClass ();
7684 $ result = $ this ->attributeHandler ->handleAttribute ('testProperty ' , $ nonProcessableAttribute , 'initialValue ' );
7785 $ this ->assertNull ($ result );
7886 }
@@ -127,6 +135,62 @@ public function testGetProcessedValuesReturnsProcessedData(): void
127135 $ processedValues = $ this ->attributeHandler ->getProcessedValues ();
128136
129137 $ this ->assertArrayHasKey ('testProperty ' , $ processedValues );
130- $ this ->assertSame (['processedValue ' ], $ processedValues ['testProperty ' ]);
138+ $ this ->assertSame ('processedValue ' , $ processedValues ['testProperty ' ]);
139+ }
140+
141+ public function testHandleAttributeWithCustomizableMessageAttribute (): void
142+ {
143+ // Create a mock of the combined interface
144+ $ mockAttribute = $ this ->createMock (CombinedAttribute::class);
145+
146+ $ mockAttribute ->method ('getProcessors ' )
147+ ->willReturn (['processor1 ' => ['option ' => 'value ' ]]);
148+
149+ $ mockAttribute ->expects ($ this ->once ())
150+ ->method ('getMessage ' )
151+ ->with ('processor1 ' )
152+ ->willReturn ('Custom message ' );
153+
154+ $ mockPipeline = $ this ->createMock (Pipeline::class);
155+ $ mockPipeline ->method ('process ' )->willReturn ('processedValue ' );
156+
157+ $ this ->processorBuilder ->expects ($ this ->once ())
158+ ->method ('buildPipeline ' )
159+ ->with ('testProcessor ' , ['processor1 ' => ['option ' => 'value ' , 'customMessage ' => 'Custom message ' ]])
160+ ->willReturn ($ mockPipeline );
161+
162+ $ result = $ this ->attributeHandler ->handleAttribute ('testProperty ' , $ mockAttribute , 'initialValue ' );
163+ $ this ->assertSame ('processedValue ' , $ result );
164+
165+ // Since getProcessors is mocked, we need to simulate the processors array
166+ $ processors = ['processor1 ' => ['option ' => 'value ' , 'customMessage ' => 'Custom message ' ]];
167+
168+ $ this ->assertArrayHasKey ('processor1 ' , $ processors );
169+ $ this ->assertArrayHasKey ('customMessage ' , $ processors ['processor1 ' ]);
170+ $ this ->assertEquals ('Custom message ' , $ processors ['processor1 ' ]['customMessage ' ]);
171+ }
172+
173+ public function testGetProcessingErrors (): void
174+ {
175+ $ mockAttribute = $ this ->createMock (ProcessableAttribute::class);
176+ $ mockPipeline = $ this ->createMock (Pipeline::class);
177+
178+ $ mockPipeline ->expects ($ this ->once ())
179+ ->method ('process ' )
180+ ->willThrowException (new ProcessingException ('Test error ' ));
181+
182+ $ this ->processorBuilder ->expects ($ this ->once ())
183+ ->method ('buildPipeline ' )
184+ ->willReturn ($ mockPipeline );
185+
186+ $ mockAttribute ->expects ($ this ->once ())
187+ ->method ('getProcessors ' )
188+ ->willReturn (['processor1 ' ]);
189+
190+ $ this ->attributeHandler ->handleAttribute ('testProperty ' , $ mockAttribute , 'initialValue ' );
191+ $ errors = $ this ->attributeHandler ->getProcessingErrors ();
192+
193+ $ this ->assertArrayHasKey ('testProperty ' , $ errors );
194+ $ this ->assertContains ('Test error ' , $ errors ['testProperty ' ]);
131195 }
132196}
0 commit comments