13
13
use Wrkflow \GetValue \GetValue ;
14
14
use Wrkflow \GetValue \Transformers \ArrayItemGetterTransformer ;
15
15
16
- class ArrayItemGetterTransformerTest extends AbstractTransformerTestCase
16
+ final class ArrayItemGetterTransformerTest extends AbstractTransformerTestCase
17
17
{
18
- final public const KeyValue = 'key ' ;
18
+ public const KeyValue = 'key ' ;
19
19
20
- final public const ValueToReturnNull = 'return_null ' ;
20
+ public const ValueToReturnNull = 'return_null ' ;
21
21
22
22
public function testExampleArray (): void
23
23
{
@@ -62,14 +62,25 @@ public function testExampleXML(): void
62
62
CODE_SAMPLE
63
63
)));
64
64
65
- $ transformer = new ArrayItemGetterTransformer (fn (GetValue $ value , string $ key ): string => implode (' ' , [
66
- $ value ->getRequiredString ('name ' ),
67
- $ value ->getRequiredString ('surname ' ),
68
- $ value ->getXMLAttributesGetter (['surname ' ])->getRequiredInt ('number ' ),
69
- ]));
65
+ $ expectedKeys = ['names.0 ' , 'names.1 ' ];
66
+
67
+ $ transformer = new ArrayItemGetterTransformer (function (GetValue $ value , string $ key ) use (
68
+ &$ expectedKeys
69
+ ): string {
70
+ $ expectedKey = array_shift ($ expectedKeys );
71
+
72
+ $ this ->assertEquals ($ expectedKey , $ key , 'Key does not match up ' );
73
+
74
+ return implode (' ' , [
75
+ $ value ->getRequiredString ('name ' ),
76
+ $ value ->getRequiredString ('surname ' ),
77
+ $ value ->getXMLAttributesGetter (['surname ' ])->getRequiredInt ('number ' ),
78
+ ]);
79
+ });
70
80
71
81
$ values = $ data ->getArray ('names ' , transformers: [$ transformer ]);
72
82
$ this ->assertEquals (['Marco Polo 3 ' , 'Martin Way 2 ' ], $ values );
83
+ $ this ->assertEmpty ($ expectedKeys , 'Expected key match should be empty - loop did not go through all keys ' );
73
84
}
74
85
75
86
public function dataToTest (): array
@@ -82,7 +93,7 @@ public function dataToTest(): array
82
93
*/
83
94
public function testBeforeValidation (TransformerExpectationEntity $ entity ): void
84
95
{
85
- $ this ->assertValue ($ this ->getBeforeValidationTransformer (), $ entity );
96
+ $ this ->assertValue ($ this ->getBeforeValidationTransformer ($ entity ), $ entity );
86
97
}
87
98
88
99
public function dataToTestBeforeValidation (): array
@@ -95,7 +106,7 @@ public function dataToTestBeforeValidation(): array
95
106
*/
96
107
public function testAfterValidationForce (TransformerExpectationEntity $ entity ): void
97
108
{
98
- $ this ->assertValue ($ this ->getForceAfterValidation (), $ entity );
109
+ $ this ->assertValue ($ this ->getForceAfterValidation ($ entity ), $ entity );
99
110
}
100
111
101
112
public function dataToAfterValidationForce (): array
@@ -105,8 +116,14 @@ public function dataToAfterValidationForce(): array
105
116
106
117
public function testSupportsEmptyArray (): void
107
118
{
108
- $ transformer = new ArrayItemGetterTransformer (onItem: function (GetValue $ value , string $ key ): array {
109
- $ this ->assertEquals ('test ' , $ key , 'Key does not match up ' );
119
+ $ expectedKeys = ['test.0 ' , 'test.1 ' ];
120
+
121
+ $ transformer = new ArrayItemGetterTransformer (onItem: function (GetValue $ value , string $ key ) use (
122
+ &$ expectedKeys
123
+ ): array {
124
+ $ expectedKey = array_shift ($ expectedKeys );
125
+
126
+ $ this ->assertEquals ($ expectedKey , $ key , 'Key does not match up ' );
110
127
111
128
return [
112
129
'original ' => $ value ->data ->get (),
@@ -124,8 +141,9 @@ public function testSupportsEmptyArray(): void
124
141
], [
125
142
'original ' => $ testValue ,
126
143
]],
127
- expectedValueBeforeValidation: $ value
144
+ expectedValueBeforeValidation: $ value,
128
145
));
146
+ $ this ->assertEmpty ($ expectedKeys , 'Expected key match should be empty - loop did not go through all keys ' );
129
147
}
130
148
131
149
/**
@@ -135,7 +153,7 @@ public function testBeforeValidationLeaveNull(TransformerExpectationEntity $enti
135
153
{
136
154
$ this ->assertValue (
137
155
new ArrayItemGetterTransformer (
138
- onItem: $ this ->getClosure (),
156
+ onItem: $ this ->getClosure ($ entity ),
139
157
beforeValidation: true ,
140
158
ignoreNullResult: false
141
159
),
@@ -155,7 +173,7 @@ public function testAfterValidationForceLeaveNull(TransformerExpectationEntity $
155
173
{
156
174
$ this ->assertValue (
157
175
new ArrayItemGetterTransformer (
158
- onItem: $ this ->getClosure (),
176
+ onItem: $ this ->getClosure ($ entity ),
159
177
beforeValidation: false ,
160
178
ignoreNullResult: false
161
179
),
@@ -174,7 +192,7 @@ public function dataToAfterValidationForceLeaveNull(): array
174
192
public function testTransformLeaveNull (TransformerExpectationEntity $ entity ): void
175
193
{
176
194
$ this ->assertValue (
177
- new ArrayItemGetterTransformer (onItem: $ this ->getClosure (), ignoreNullResult: false ),
195
+ new ArrayItemGetterTransformer (onItem: $ this ->getClosure ($ entity ), ignoreNullResult: false ),
178
196
$ entity
179
197
);
180
198
}
@@ -189,10 +207,11 @@ protected function dataAfterValidationForTransformer(): array
189
207
return $ this ->createData (true , false );
190
208
}
191
209
192
- protected function getClosure (): Closure
210
+ protected function getClosure (TransformerExpectationEntity $ entity ): Closure
193
211
{
194
- return function (GetValue $ value , string $ key ): ?array {
195
- $ this ->assertEquals ('test ' , $ key , 'Key does not match up ' );
212
+ return function (GetValue $ value , string $ key ) use (&$ entity ): ?array {
213
+ $ expectedKey = array_shift ($ entity ->expectedKey );
214
+ $ this ->assertEquals ($ expectedKey , $ key , 'Key does not match up ' );
196
215
197
216
$ value = $ value ->getRequiredString (self ::KeyValue, transformers: []);
198
217
@@ -207,22 +226,22 @@ protected function getClosure(): Closure
207
226
};
208
227
}
209
228
210
- protected function getTransformer (): TransformerContract
229
+ protected function getTransformer (TransformerExpectationEntity $ entity ): TransformerContract
211
230
{
212
- return new ArrayItemGetterTransformer (onItem: $ this ->getClosure ());
231
+ return new ArrayItemGetterTransformer (onItem: $ this ->getClosure ($ entity ));
213
232
}
214
233
215
- protected function getBeforeValidationTransformer (): TransformerContract
234
+ private function getBeforeValidationTransformer (TransformerExpectationEntity $ entity ): TransformerContract
216
235
{
217
- return new ArrayItemGetterTransformer (onItem: $ this ->getClosure (), beforeValidation: true );
236
+ return new ArrayItemGetterTransformer (onItem: $ this ->getClosure ($ entity ), beforeValidation: true );
218
237
}
219
238
220
- protected function getForceAfterValidation (): TransformerContract
239
+ private function getForceAfterValidation (TransformerExpectationEntity $ entity ): TransformerContract
221
240
{
222
- return new ArrayItemGetterTransformer (onItem: $ this ->getClosure (), beforeValidation: false );
241
+ return new ArrayItemGetterTransformer (onItem: $ this ->getClosure ($ entity ), beforeValidation: false );
223
242
}
224
243
225
- protected function createData (bool $ beforeValueIsSameAsValue , bool $ leaveNull ): array
244
+ private function createData (bool $ beforeValueIsSameAsValue , bool $ leaveNull ): array
226
245
{
227
246
return [
228
247
[
@@ -235,7 +254,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
235
254
]],
236
255
expectedValueBeforeValidation: $ beforeValueIsSameAsValue ? [[
237
256
self ::KeyValue => '' ,
238
- ]] : null
257
+ ]] : null ,
258
+ expectedKey: 'test.0 ' ,
239
259
),
240
260
],
241
261
[
@@ -248,7 +268,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
248
268
]],
249
269
expectedValueBeforeValidation: $ beforeValueIsSameAsValue ? [[
250
270
self ::KeyValue => ' ' ,
251
- ]] : null
271
+ ]] : null ,
272
+ expectedKey: 'test.0 ' ,
252
273
),
253
274
],
254
275
[
@@ -261,7 +282,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
261
282
]],
262
283
expectedValueBeforeValidation: $ beforeValueIsSameAsValue ? [[
263
284
self ::KeyValue => ' asd ' ,
264
- ]] : null
285
+ ]] : null ,
286
+ expectedKey: 'test.0 ' ,
265
287
),
266
288
],
267
289
[
@@ -274,7 +296,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
274
296
]],
275
297
expectedValueBeforeValidation: $ beforeValueIsSameAsValue ? [[
276
298
self ::KeyValue => 'asd ' ,
277
- ]] : null
299
+ ]] : null ,
300
+ expectedKey: 'test.0 ' ,
278
301
),
279
302
],
280
303
[
@@ -287,7 +310,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
287
310
]],
288
311
expectedValueBeforeValidation: $ beforeValueIsSameAsValue ? [[
289
312
self ::KeyValue => 'asd mix ' ,
290
- ]] : null
313
+ ]] : null ,
314
+ expectedKey: 'test.0 ' ,
291
315
),
292
316
],
293
317
[
@@ -306,7 +330,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
306
330
'test ' => [
307
331
self ::KeyValue => 'asd mix ' ,
308
332
],
309
- ] : null
333
+ ] : null ,
334
+ expectedKey: 'test.test ' ,
310
335
),
311
336
],
312
337
[
@@ -317,7 +342,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
317
342
expectedValue: $ leaveNull ? [null ] : [],
318
343
expectedValueBeforeValidation: $ beforeValueIsSameAsValue ? [[
319
344
self ::KeyValue => self ::ValueToReturnNull,
320
- ]] : null
345
+ ]] : null ,
346
+ expectedKey: 'test.0 ' ,
321
347
),
322
348
],
323
349
[
@@ -334,14 +360,17 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
334
360
'test ' => [
335
361
self ::KeyValue => self ::ValueToReturnNull,
336
362
],
337
- ] : null
363
+ ] : null ,
364
+ expectedKey: 'test.test ' ,
338
365
),
339
366
],
340
367
[new TransformerExpectationEntity (value: null , expectedValue: null )],
341
368
[
342
- new TransformerExpectationEntity (value: [
343
- 'test ' ,
344
- ], expectedValue: null , expectException: NotSupportedDataException::class),
369
+ new TransformerExpectationEntity (
370
+ value: ['test ' ],
371
+ expectedValue: null ,
372
+ expectException: NotSupportedDataException::class,
373
+ ),
345
374
],
346
375
];
347
376
}
0 commit comments