4
4
5
5
namespace KaririCode \PropertyInspector \Tests ;
6
6
7
+ use KaririCode \Contract \Processor \Attribute \CustomizableMessageAttribute ;
8
+ use KaririCode \Contract \Processor \Attribute \ProcessableAttribute ;
7
9
use KaririCode \Contract \Processor \Pipeline ;
8
- use KaririCode \Contract \Processor \ProcessableAttribute ;
9
10
use KaririCode \Contract \Processor \ProcessorBuilder ;
10
11
use KaririCode \ProcessorPipeline \Exception \ProcessingException ;
11
12
use KaririCode \PropertyInspector \AttributeHandler ;
13
+ use PHPUnit \Framework \MockObject \MockObject ;
12
14
use PHPUnit \Framework \TestCase ;
13
15
16
+ interface CombinedAttribute extends
17
+ ProcessableAttribute,
18
+ CustomizableMessageAttribute
19
+ {
20
+ }
21
+
14
22
final class AttributeHandlerTest extends TestCase
15
23
{
16
24
private AttributeHandler $ attributeHandler ;
17
- private ProcessorBuilder $ processorBuilder ;
25
+ private ProcessorBuilder | MockObject $ processorBuilder ;
18
26
19
27
protected function setUp (): void
20
28
{
@@ -45,7 +53,7 @@ public function testHandleAttributeProcessesValue(): void
45
53
$ this ->assertSame ('processedValue ' , $ result );
46
54
}
47
55
48
- public function testHandleAttributeReturnsFallbackOnException (): void
56
+ public function testHandleAttributeReturnsOriginalValueOnException (): void
49
57
{
50
58
$ mockAttribute = $ this ->createMock (ProcessableAttribute::class);
51
59
$ mockPipeline = $ this ->createMock (Pipeline::class);
@@ -62,17 +70,17 @@ public function testHandleAttributeReturnsFallbackOnException(): void
62
70
->method ('getProcessors ' )
63
71
->willReturn (['processor1 ' ]);
64
72
65
- $ mockAttribute ->expects ($ this ->once ())
66
- ->method ('getFallbackValue ' )
67
- ->willReturn ('fallbackValue ' );
68
-
69
73
$ 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 ' ]);
71
79
}
72
80
73
81
public function testHandleAttributeReturnsNullWhenAttributeNotProcessable (): void
74
82
{
75
- $ nonProcessableAttribute = new \stdClass (); // Simulate a non-ProcessableAttribute object
83
+ $ nonProcessableAttribute = new \stdClass ();
76
84
$ result = $ this ->attributeHandler ->handleAttribute ('testProperty ' , $ nonProcessableAttribute , 'initialValue ' );
77
85
$ this ->assertNull ($ result );
78
86
}
@@ -127,6 +135,62 @@ public function testGetProcessedValuesReturnsProcessedData(): void
127
135
$ processedValues = $ this ->attributeHandler ->getProcessedValues ();
128
136
129
137
$ 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 ' ]);
131
195
}
132
196
}
0 commit comments