1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Component\PhpUtils\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Oro\Component\PhpUtils\ArrayUtil; |
6
|
|
|
|
7
|
|
|
class ArrayUtilTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
public function testCreateOrderedComparator() |
10
|
|
|
{ |
11
|
|
|
$order = array_flip(['a', 'z', 'd', 'e']); |
12
|
|
|
$array = [ |
13
|
|
|
'b' => 'val b', |
14
|
|
|
'd' => 'val d', |
15
|
|
|
'z' => 'val z', |
16
|
|
|
'c' => 'val c', |
17
|
|
|
'e' => 'val e', |
18
|
|
|
]; |
19
|
|
|
$expectedResult = [ |
20
|
|
|
'z' => 'val z', |
21
|
|
|
'd' => 'val d', |
22
|
|
|
'e' => 'val e', |
23
|
|
|
'b' => 'val b', |
24
|
|
|
'c' => 'val c', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
uksort($array, ArrayUtil::createOrderedComparator($order)); |
28
|
|
|
$this->assertEquals(array_keys($expectedResult), array_keys($array)); |
29
|
|
|
$this->assertEquals(array_values($expectedResult), array_values($array)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider isAssocDataProvider |
34
|
|
|
*/ |
35
|
|
|
public function testIsAssoc($array, $expectedResult) |
36
|
|
|
{ |
37
|
|
|
$this->assertEquals($expectedResult, ArrayUtil::isAssoc($array)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function isAssocDataProvider() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
[[1, 2, 3], false], |
44
|
|
|
[[0 => 1, 1 => 2, 2 => 3], false], |
45
|
|
|
[['a' => 1, 'b' => 2, 'c' => 3], true], |
46
|
|
|
[[1, 'b' => 2, 3], true], |
47
|
|
|
[[1 => 1, 2 => 2, 3 => 3], true] |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testSortByEmpty() |
52
|
|
|
{ |
53
|
|
|
$array = []; |
54
|
|
|
|
55
|
|
|
ArrayUtil::sortBy($array); |
56
|
|
|
$this->assertSame([], $array); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSortByArrayNoOrder() |
60
|
|
|
{ |
61
|
|
|
$array = [ |
62
|
|
|
['name' => '1'], |
63
|
|
|
['name' => '2'], |
64
|
|
|
['name' => '3'], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
ArrayUtil::sortBy($array); |
68
|
|
|
$this->assertSame( |
69
|
|
|
[ |
70
|
|
|
['name' => '1'], |
71
|
|
|
['name' => '2'], |
72
|
|
|
['name' => '3'], |
73
|
|
|
], |
74
|
|
|
$array |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testSortByArrayNoOrderReverse() |
79
|
|
|
{ |
80
|
|
|
$array = [ |
81
|
|
|
['name' => '1'], |
82
|
|
|
['name' => '2'], |
83
|
|
|
['name' => '3'], |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
ArrayUtil::sortBy($array, true); |
87
|
|
|
$this->assertSame( |
88
|
|
|
[ |
89
|
|
|
['name' => '1'], |
90
|
|
|
['name' => '2'], |
91
|
|
|
['name' => '3'], |
92
|
|
|
], |
93
|
|
|
$array |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testSortByArraySameOrder() |
98
|
|
|
{ |
99
|
|
|
$array = [ |
100
|
|
|
['name' => '1', 'priority' => 100], |
101
|
|
|
['name' => '2', 'priority' => 100], |
102
|
|
|
['name' => '3', 'priority' => 100], |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
ArrayUtil::sortBy($array); |
106
|
|
|
$this->assertSame( |
107
|
|
|
[ |
108
|
|
|
['name' => '1', 'priority' => 100], |
109
|
|
|
['name' => '2', 'priority' => 100], |
110
|
|
|
['name' => '3', 'priority' => 100], |
111
|
|
|
], |
112
|
|
|
$array |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testSortByArraySameOrderReverse() |
117
|
|
|
{ |
118
|
|
|
$array = [ |
119
|
|
|
['name' => '1', 'priority' => 100], |
120
|
|
|
['name' => '2', 'priority' => 100], |
121
|
|
|
['name' => '3', 'priority' => 100], |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
ArrayUtil::sortBy($array, true); |
125
|
|
|
$this->assertSame( |
126
|
|
|
[ |
127
|
|
|
['name' => '1', 'priority' => 100], |
128
|
|
|
['name' => '2', 'priority' => 100], |
129
|
|
|
['name' => '3', 'priority' => 100], |
130
|
|
|
], |
131
|
|
|
$array |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
public function testSortByArrayNumeric() |
136
|
|
|
{ |
137
|
|
|
$array = [ |
138
|
|
|
['name' => '1'], |
139
|
|
|
['name' => '2'], |
140
|
|
|
['name' => '3', 'priority' => 100], |
141
|
|
|
['name' => '4'], |
142
|
|
|
['name' => '5', 'priority' => -100], |
143
|
|
|
['name' => '6', 'priority' => 100], |
144
|
|
|
['name' => '7', 'priority' => -100], |
145
|
|
|
['name' => '8', 'priority' => 0], |
146
|
|
|
['name' => '9'], |
147
|
|
|
]; |
148
|
|
|
|
149
|
|
|
ArrayUtil::sortBy($array); |
150
|
|
|
$this->assertSame( |
151
|
|
|
[ |
152
|
|
|
['name' => '5', 'priority' => -100], |
153
|
|
|
['name' => '7', 'priority' => -100], |
154
|
|
|
['name' => '1'], |
155
|
|
|
['name' => '2'], |
156
|
|
|
['name' => '4'], |
157
|
|
|
['name' => '8', 'priority' => 0], |
158
|
|
|
['name' => '9'], |
159
|
|
|
['name' => '3', 'priority' => 100], |
160
|
|
|
['name' => '6', 'priority' => 100], |
161
|
|
|
], |
162
|
|
|
$array |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function testSortByArrayNumericReverse() |
167
|
|
|
{ |
168
|
|
|
$array = [ |
169
|
|
|
['name' => '1'], |
170
|
|
|
['name' => '2'], |
171
|
|
|
['name' => '3', 'priority' => 100], |
172
|
|
|
['name' => '4'], |
173
|
|
|
['name' => '5', 'priority' => -100], |
174
|
|
|
['name' => '6', 'priority' => 100], |
175
|
|
|
['name' => '7', 'priority' => -100], |
176
|
|
|
['name' => '8', 'priority' => 0], |
177
|
|
|
['name' => '9'], |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
ArrayUtil::sortBy($array, true); |
181
|
|
|
$this->assertSame( |
182
|
|
|
[ |
183
|
|
|
['name' => '3', 'priority' => 100], |
184
|
|
|
['name' => '6', 'priority' => 100], |
185
|
|
|
['name' => '1'], |
186
|
|
|
['name' => '2'], |
187
|
|
|
['name' => '4'], |
188
|
|
|
['name' => '8', 'priority' => 0], |
189
|
|
|
['name' => '9'], |
190
|
|
|
['name' => '5', 'priority' => -100], |
191
|
|
|
['name' => '7', 'priority' => -100], |
192
|
|
|
], |
193
|
|
|
$array |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
View Code Duplication |
public function testSortByAssocArrayNumeric() |
198
|
|
|
{ |
199
|
|
|
$array = [ |
200
|
|
|
'i1' => ['name' => '1'], |
201
|
|
|
'i2' => ['name' => '2'], |
202
|
|
|
'i3' => ['name' => '3', 'priority' => 100], |
203
|
|
|
'i4' => ['name' => '4'], |
204
|
|
|
'i5' => ['name' => '5', 'priority' => -100], |
205
|
|
|
'i6' => ['name' => '6', 'priority' => 100], |
206
|
|
|
'i7' => ['name' => '7', 'priority' => -100], |
207
|
|
|
'i8' => ['name' => '8', 'priority' => 0], |
208
|
|
|
'i9' => ['name' => '9'], |
209
|
|
|
]; |
210
|
|
|
|
211
|
|
|
ArrayUtil::sortBy($array); |
212
|
|
|
$this->assertSame( |
213
|
|
|
[ |
214
|
|
|
'i5' => ['name' => '5', 'priority' => -100], |
215
|
|
|
'i7' => ['name' => '7', 'priority' => -100], |
216
|
|
|
'i1' => ['name' => '1'], |
217
|
|
|
'i2' => ['name' => '2'], |
218
|
|
|
'i4' => ['name' => '4'], |
219
|
|
|
'i8' => ['name' => '8', 'priority' => 0], |
220
|
|
|
'i9' => ['name' => '9'], |
221
|
|
|
'i3' => ['name' => '3', 'priority' => 100], |
222
|
|
|
'i6' => ['name' => '6', 'priority' => 100], |
223
|
|
|
], |
224
|
|
|
$array |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
View Code Duplication |
public function testSortByAssocArrayNumericReverse() |
229
|
|
|
{ |
230
|
|
|
$array = [ |
231
|
|
|
'i1' => ['name' => '1'], |
232
|
|
|
'i2' => ['name' => '2'], |
233
|
|
|
'i3' => ['name' => '3', 'priority' => 100], |
234
|
|
|
'i4' => ['name' => '4'], |
235
|
|
|
'i5' => ['name' => '5', 'priority' => -100], |
236
|
|
|
'i6' => ['name' => '6', 'priority' => 100], |
237
|
|
|
'i7' => ['name' => '7', 'priority' => -100], |
238
|
|
|
'i8' => ['name' => '8', 'priority' => 0], |
239
|
|
|
'i9' => ['name' => '9'], |
240
|
|
|
]; |
241
|
|
|
|
242
|
|
|
ArrayUtil::sortBy($array, true); |
243
|
|
|
$this->assertSame( |
244
|
|
|
[ |
245
|
|
|
'i3' => ['name' => '3', 'priority' => 100], |
246
|
|
|
'i6' => ['name' => '6', 'priority' => 100], |
247
|
|
|
'i1' => ['name' => '1'], |
248
|
|
|
'i2' => ['name' => '2'], |
249
|
|
|
'i4' => ['name' => '4'], |
250
|
|
|
'i8' => ['name' => '8', 'priority' => 0], |
251
|
|
|
'i9' => ['name' => '9'], |
252
|
|
|
'i5' => ['name' => '5', 'priority' => -100], |
253
|
|
|
'i7' => ['name' => '7', 'priority' => -100], |
254
|
|
|
], |
255
|
|
|
$array |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
View Code Duplication |
public function testSortByArrayString() |
260
|
|
|
{ |
261
|
|
|
$array = [ |
262
|
|
|
['name' => 'a'], |
263
|
|
|
['name' => 'c'], |
264
|
|
|
['name' => 'b'], |
265
|
|
|
]; |
266
|
|
|
|
267
|
|
|
ArrayUtil::sortBy($array, false, 'name', SORT_STRING); |
268
|
|
|
$this->assertSame( |
269
|
|
|
[ |
270
|
|
|
['name' => 'a'], |
271
|
|
|
['name' => 'b'], |
272
|
|
|
['name' => 'c'], |
273
|
|
|
], |
274
|
|
|
$array |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
View Code Duplication |
public function testSortByArrayStringReverse() |
279
|
|
|
{ |
280
|
|
|
$array = [ |
281
|
|
|
['name' => 'a'], |
282
|
|
|
['name' => 'c'], |
283
|
|
|
['name' => 'b'], |
284
|
|
|
]; |
285
|
|
|
|
286
|
|
|
ArrayUtil::sortBy($array, true, 'name', SORT_STRING); |
287
|
|
|
$this->assertSame( |
288
|
|
|
[ |
289
|
|
|
['name' => 'c'], |
290
|
|
|
['name' => 'b'], |
291
|
|
|
['name' => 'a'], |
292
|
|
|
], |
293
|
|
|
$array |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
View Code Duplication |
public function testSortByArrayStringCaseInsensitive() |
298
|
|
|
{ |
299
|
|
|
$array = [ |
300
|
|
|
['name' => 'a'], |
301
|
|
|
['name' => 'C'], |
302
|
|
|
['name' => 'B'], |
303
|
|
|
]; |
304
|
|
|
|
305
|
|
|
ArrayUtil::sortBy($array, false, 'name', SORT_STRING | SORT_FLAG_CASE); |
306
|
|
|
$this->assertSame( |
307
|
|
|
[ |
308
|
|
|
['name' => 'a'], |
309
|
|
|
['name' => 'B'], |
310
|
|
|
['name' => 'C'], |
311
|
|
|
], |
312
|
|
|
$array |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
View Code Duplication |
public function testSortByArrayStringCaseInsensitiveReverse() |
317
|
|
|
{ |
318
|
|
|
$array = [ |
319
|
|
|
['name' => 'a'], |
320
|
|
|
['name' => 'C'], |
321
|
|
|
['name' => 'B'], |
322
|
|
|
]; |
323
|
|
|
|
324
|
|
|
ArrayUtil::sortBy($array, true, 'name', SORT_STRING | SORT_FLAG_CASE); |
325
|
|
|
$this->assertSame( |
326
|
|
|
[ |
327
|
|
|
['name' => 'C'], |
328
|
|
|
['name' => 'B'], |
329
|
|
|
['name' => 'a'], |
330
|
|
|
], |
331
|
|
|
$array |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testSortByArrayPath() |
336
|
|
|
{ |
337
|
|
|
$array = [ |
338
|
|
|
['name' => '1', 'child' => ['priority' => 1]], |
339
|
|
|
['name' => '2', 'child' => ['priority' => 3]], |
340
|
|
|
['name' => '3', 'child' => ['priority' => 2]], |
341
|
|
|
]; |
342
|
|
|
|
343
|
|
|
ArrayUtil::sortBy($array, false, '[child][priority]'); |
344
|
|
|
$this->assertSame( |
345
|
|
|
[ |
346
|
|
|
['name' => '1', 'child' => ['priority' => 1]], |
347
|
|
|
['name' => '3', 'child' => ['priority' => 2]], |
348
|
|
|
['name' => '2', 'child' => ['priority' => 3]], |
349
|
|
|
], |
350
|
|
|
$array |
351
|
|
|
); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
View Code Duplication |
public function testSortByObject() |
355
|
|
|
{ |
356
|
|
|
$obj1 = $this->createObject(['name' => '1', 'priority' => null]); |
357
|
|
|
$obj2 = $this->createObject(['name' => '2', 'priority' => 100]); |
358
|
|
|
$obj3 = $this->createObject(['name' => '3', 'priority' => 0]); |
359
|
|
|
$array = [ |
360
|
|
|
$obj1, |
361
|
|
|
$obj2, |
362
|
|
|
$obj3, |
363
|
|
|
]; |
364
|
|
|
|
365
|
|
|
ArrayUtil::sortBy($array); |
366
|
|
|
$this->assertSame( |
367
|
|
|
[ |
368
|
|
|
$obj1, |
369
|
|
|
$obj3, |
370
|
|
|
$obj2, |
371
|
|
|
], |
372
|
|
|
$array |
373
|
|
|
); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function testSortByObjectPath() |
377
|
|
|
{ |
378
|
|
|
$obj1 = $this->createObject( |
379
|
|
|
['name' => '1', 'child' => $this->createObject(['priority' => null])] |
380
|
|
|
); |
381
|
|
|
$obj2 = $this->createObject( |
382
|
|
|
['name' => '2', 'child' => $this->createObject(['priority' => 100])] |
383
|
|
|
); |
384
|
|
|
$obj3 = $this->createObject( |
385
|
|
|
['name' => '3', 'child' => $this->createObject(['priority' => 0])] |
386
|
|
|
); |
387
|
|
|
$array = [ |
388
|
|
|
$obj1, |
389
|
|
|
$obj2, |
390
|
|
|
$obj3, |
391
|
|
|
]; |
392
|
|
|
|
393
|
|
|
ArrayUtil::sortBy($array, false, 'child.priority'); |
394
|
|
|
$this->assertSame( |
395
|
|
|
[ |
396
|
|
|
$obj1, |
397
|
|
|
$obj3, |
398
|
|
|
$obj2, |
399
|
|
|
], |
400
|
|
|
$array |
401
|
|
|
); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
View Code Duplication |
public function testSortByClosure() |
405
|
|
|
{ |
406
|
|
|
$obj1 = $this->createObject(['name' => '1', 'priority' => null]); |
407
|
|
|
$obj2 = $this->createObject(['name' => '2', 'priority' => 100]); |
408
|
|
|
$obj3 = $this->createObject(['name' => '3', 'priority' => 0]); |
409
|
|
|
$array = [ |
410
|
|
|
$obj1, |
411
|
|
|
$obj2, |
412
|
|
|
$obj3, |
413
|
|
|
]; |
414
|
|
|
|
415
|
|
|
ArrayUtil::sortBy( |
416
|
|
|
$array, |
417
|
|
|
false, |
418
|
|
|
function ($item) { |
419
|
|
|
return $item->priority; |
420
|
|
|
} |
421
|
|
|
); |
422
|
|
|
$this->assertSame( |
423
|
|
|
[ |
424
|
|
|
$obj1, |
425
|
|
|
$obj3, |
426
|
|
|
$obj2, |
427
|
|
|
], |
428
|
|
|
$array |
429
|
|
|
); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
View Code Duplication |
public function testSortByCallable() |
433
|
|
|
{ |
434
|
|
|
$obj1 = $this->createObject(['name' => '1', 'priority' => null]); |
435
|
|
|
$obj2 = $this->createObject(['name' => '2', 'priority' => 100]); |
436
|
|
|
$obj3 = $this->createObject(['name' => '3', 'priority' => 0]); |
437
|
|
|
$array = [ |
438
|
|
|
$obj1, |
439
|
|
|
$obj2, |
440
|
|
|
$obj3, |
441
|
|
|
]; |
442
|
|
|
|
443
|
|
|
ArrayUtil::sortBy( |
444
|
|
|
$array, |
445
|
|
|
false, |
446
|
|
|
[$this, 'getObjectPriority'] |
447
|
|
|
); |
448
|
|
|
$this->assertSame( |
449
|
|
|
[ |
450
|
|
|
$obj1, |
451
|
|
|
$obj3, |
452
|
|
|
$obj2, |
453
|
|
|
], |
454
|
|
|
$array |
455
|
|
|
); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @dataProvider someProvider |
460
|
|
|
*/ |
461
|
|
|
public function testSome(callable $callback, array $array, $expectedResult) |
462
|
|
|
{ |
463
|
|
|
$this->assertSame($expectedResult, ArrayUtil::some($callback, $array)); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
public function someProvider() |
467
|
|
|
{ |
468
|
|
|
return [ |
469
|
|
|
[ |
470
|
|
|
function ($item) { |
471
|
|
|
return $item === 1; |
472
|
|
|
}, |
473
|
|
|
[0, 1, 2, 3, 4], |
474
|
|
|
true, |
475
|
|
|
], |
476
|
|
|
[ |
477
|
|
|
function ($item) { |
478
|
|
|
return $item === 0; |
479
|
|
|
}, |
480
|
|
|
[0, 1, 2, 3, 4], |
481
|
|
|
true, |
482
|
|
|
], |
483
|
|
|
[ |
484
|
|
|
function ($item) { |
485
|
|
|
return $item === 4; |
486
|
|
|
}, |
487
|
|
|
[0, 1, 2, 3, 4], |
488
|
|
|
true, |
489
|
|
|
], |
490
|
|
|
[ |
491
|
|
|
function ($item) { |
492
|
|
|
return $item === 5; |
493
|
|
|
}, |
494
|
|
|
[0, 1, 2, 3, 4], |
495
|
|
|
false, |
496
|
|
|
], |
497
|
|
|
]; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* @dataProvider findProvider |
502
|
|
|
*/ |
503
|
|
|
public function testFind(callable $callback, array $array, $expectedResult) |
504
|
|
|
{ |
505
|
|
|
$this->assertSame($expectedResult, ArrayUtil::find($callback, $array)); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
public function findProvider() |
509
|
|
|
{ |
510
|
|
|
return [ |
511
|
|
|
[ |
512
|
|
|
function ($item) { |
513
|
|
|
return $item === 1; |
514
|
|
|
}, |
515
|
|
|
[0, 1, 2, 3, 4], |
516
|
|
|
1, |
517
|
|
|
], |
518
|
|
|
[ |
519
|
|
|
function ($item) { |
520
|
|
|
return $item === 0; |
521
|
|
|
}, |
522
|
|
|
[0, 1, 2, 3, 4], |
523
|
|
|
0, |
524
|
|
|
], |
525
|
|
|
[ |
526
|
|
|
function ($item) { |
527
|
|
|
return $item === 4; |
528
|
|
|
}, |
529
|
|
|
[0, 1, 2, 3, 4], |
530
|
|
|
4, |
531
|
|
|
], |
532
|
|
|
[ |
533
|
|
|
function ($item) { |
534
|
|
|
return $item === 5; |
535
|
|
|
}, |
536
|
|
|
[0, 1, 2, 3, 4], |
537
|
|
|
null, |
538
|
|
|
], |
539
|
|
|
]; |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* @dataProvider dropWhileProvider |
544
|
|
|
*/ |
545
|
|
|
public function testDropWhile(callable $callback, array $array, $expectedResult) |
546
|
|
|
{ |
547
|
|
|
$this->assertEquals($expectedResult, ArrayUtil::dropWhile($callback, $array)); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
public function dropWhileProvider() |
551
|
|
|
{ |
552
|
|
|
return [ |
553
|
|
|
[ |
554
|
|
|
function ($item) { |
555
|
|
|
return $item !== 2; |
556
|
|
|
}, |
557
|
|
|
[], |
558
|
|
|
[], |
559
|
|
|
], |
560
|
|
|
[ |
561
|
|
|
function ($item) { |
562
|
|
|
return $item !== 2; |
563
|
|
|
}, |
564
|
|
|
[0, 1, 2, 3, 4, 5], |
565
|
|
|
[2, 3, 4, 5], |
566
|
|
|
], |
567
|
|
|
[ |
568
|
|
|
function ($item) { |
569
|
|
|
return $item !== 0; |
570
|
|
|
}, |
571
|
|
|
[0, 1, 2, 3, 4, 5], |
572
|
|
|
[0, 1, 2, 3, 4, 5], |
573
|
|
|
], |
574
|
|
|
[ |
575
|
|
|
function ($item) { |
576
|
|
|
return $item !== 6; |
577
|
|
|
}, |
578
|
|
|
[0, 1, 2, 3, 4, 5], |
579
|
|
|
[], |
580
|
|
|
], |
581
|
|
|
]; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* @dataProvider shiftRangeProvider |
586
|
|
|
*/ |
587
|
|
|
public function testShiftRange(array $sortedUniqueInts, $expectedResult, $expectedShiftedUniqueInts) |
588
|
|
|
{ |
589
|
|
|
$this->assertEquals($expectedResult, ArrayUtil::shiftRange($sortedUniqueInts)); |
590
|
|
|
$this->assertEquals($expectedShiftedUniqueInts, $sortedUniqueInts); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
public function shiftRangeProvider() |
594
|
|
|
{ |
595
|
|
|
return [ |
596
|
|
|
'empty' => [ |
597
|
|
|
[], |
598
|
|
|
false, |
599
|
|
|
[], |
600
|
|
|
], |
601
|
|
|
'1 item' => [ |
602
|
|
|
[5], |
603
|
|
|
[5, 5], |
604
|
|
|
[], |
605
|
|
|
], |
606
|
|
|
'2 items' => [ |
607
|
|
|
[5, 6], |
608
|
|
|
[5, 6], |
609
|
|
|
[], |
610
|
|
|
], |
611
|
|
|
'first' => [ |
612
|
|
|
[1, 3, 5], |
613
|
|
|
[1, 1], |
614
|
|
|
[3, 5], |
615
|
|
|
], |
616
|
|
|
'first to last' => [ |
617
|
|
|
[1, 2, 3, 4, 5], |
618
|
|
|
[1, 5], |
619
|
|
|
[], |
620
|
|
|
], |
621
|
|
|
'first to gap' => [ |
622
|
|
|
[1, 2, 3, 5, 6], |
623
|
|
|
[1, 3], |
624
|
|
|
[5, 6], |
625
|
|
|
], |
626
|
|
|
]; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* @dataProvider intRangesProvider |
631
|
|
|
*/ |
632
|
|
|
public function testIntRanges($ints, array $expectedResult) |
633
|
|
|
{ |
634
|
|
|
$this->assertEquals($expectedResult, ArrayUtil::intRanges($ints)); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
public function intRangesProvider() |
638
|
|
|
{ |
639
|
|
|
return [ |
640
|
|
|
[ |
641
|
|
|
[], |
642
|
|
|
[], |
643
|
|
|
], |
644
|
|
|
[ |
645
|
|
|
[1], |
646
|
|
|
[ |
647
|
|
|
[1, 1], |
648
|
|
|
] |
649
|
|
|
], |
650
|
|
|
[ |
651
|
|
|
[5, 5, 3, 1, 6, 4, 100], |
652
|
|
|
[ |
653
|
|
|
[1, 1], |
654
|
|
|
[3, 6], |
655
|
|
|
[100, 100], |
656
|
|
|
] |
657
|
|
|
] |
658
|
|
|
]; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
/** |
662
|
|
|
* @param array $array |
663
|
|
|
* @param mixed $columnKey |
664
|
|
|
* @param mixed $indexKey |
665
|
|
|
* @param array $expected |
666
|
|
|
* |
667
|
|
|
* @dataProvider arrayColumnProvider |
668
|
|
|
*/ |
669
|
|
|
public function testArrayColumn(array $array, $columnKey, $indexKey, array $expected) |
670
|
|
|
{ |
671
|
|
|
$this->assertEquals( |
672
|
|
|
$expected, |
673
|
|
|
ArrayUtil::arrayColumn($array, $columnKey, $indexKey) |
674
|
|
|
); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* @return array |
679
|
|
|
*/ |
680
|
|
|
public function arrayColumnProvider() |
681
|
|
|
{ |
682
|
|
|
return [ |
683
|
|
|
'empty' => [[], 'value', 'value', []], |
684
|
|
|
'no_index' => [ |
685
|
|
|
[ |
686
|
|
|
[ |
687
|
|
|
'id' => 'id1', |
688
|
|
|
'value' => 'value2' |
689
|
|
|
] |
690
|
|
|
], |
691
|
|
|
'value', |
692
|
|
|
null, |
693
|
|
|
['value2'] |
694
|
|
|
], |
695
|
|
|
'index' => [ |
696
|
|
|
[ |
697
|
|
|
[ |
698
|
|
|
'id' => 'id1', |
699
|
|
|
'value' => 'value2' |
700
|
|
|
] |
701
|
|
|
], |
702
|
|
|
'value', |
703
|
|
|
'id', |
704
|
|
|
['id1' => 'value2'] |
705
|
|
|
], |
706
|
|
|
'wrong_index' => [ |
707
|
|
|
[ |
708
|
|
|
['value' => 'value2'] |
709
|
|
|
], |
710
|
|
|
'value', |
711
|
|
|
'id', |
712
|
|
|
[] |
713
|
|
|
], |
714
|
|
|
'wrong_column' => [ |
715
|
|
|
[ |
716
|
|
|
['value' => 'value2'] |
717
|
|
|
], |
718
|
|
|
'id', |
719
|
|
|
null, |
720
|
|
|
[] |
721
|
|
|
], |
722
|
|
|
|
723
|
|
|
]; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* @param array $array |
728
|
|
|
* @param mixed $columnKey |
729
|
|
|
* @param mixed $indexKey |
730
|
|
|
* @param string $expectedMessage |
731
|
|
|
* |
732
|
|
|
* @dataProvider arrayColumnInputData |
733
|
|
|
*/ |
734
|
|
|
public function testArrayColumnInputData(array $array, $columnKey, $indexKey, $expectedMessage) |
735
|
|
|
{ |
736
|
|
|
$this->setExpectedException( |
737
|
|
|
'\InvalidArgumentException', |
738
|
|
|
$expectedMessage |
739
|
|
|
); |
740
|
|
|
|
741
|
|
|
ArrayUtil::arrayColumn($array, $columnKey, $indexKey); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* @return array |
746
|
|
|
*/ |
747
|
|
|
public function arrayColumnInputData() |
748
|
|
|
{ |
749
|
|
|
return [ |
750
|
|
|
'empty_column_key' => [ |
751
|
|
|
[ |
752
|
|
|
['id' => 'value'] |
753
|
|
|
], |
754
|
|
|
null, |
755
|
|
|
null, |
756
|
|
|
'Column key is empty' |
757
|
|
|
] |
758
|
|
|
]; |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* @dataProvider unsetPathDataProvider |
763
|
|
|
*/ |
764
|
|
|
public function testUnsetPath(array $array, array $path, array $expectedValue) |
765
|
|
|
{ |
766
|
|
|
$this->assertEquals($expectedValue, ArrayUtil::unsetPath($array, $path)); |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
public function unsetPathDataProvider() |
770
|
|
|
{ |
771
|
|
|
return [ |
772
|
|
|
'unset with empty path' => [ |
773
|
|
|
['a' => 'aval'], |
774
|
|
|
[], |
775
|
|
|
['a' => 'aval'], |
776
|
|
|
], |
777
|
|
|
'unset with path having 1 element' => [ |
778
|
|
|
['a' => 'aval'], |
779
|
|
|
['a'], |
780
|
|
|
[], |
781
|
|
|
], |
782
|
|
|
'unset with invalid path having 1 element' => [ |
783
|
|
|
['a' => 'aval'], |
784
|
|
|
['b'], |
785
|
|
|
['a' => 'aval'], |
786
|
|
|
], |
787
|
|
|
'unset with path having more elements' => [ |
788
|
|
|
[ |
789
|
|
|
'a' => 'aval', |
790
|
|
|
'b' => [ |
791
|
|
|
'c' => 'cval', |
792
|
|
|
'd' => [ |
793
|
|
|
'e' => 'eval', |
794
|
|
|
], |
795
|
|
|
], |
796
|
|
|
], |
797
|
|
|
['b', 'c'], |
798
|
|
|
[ |
799
|
|
|
'a' => 'aval', |
800
|
|
|
'b' => [ |
801
|
|
|
'd' => [ |
802
|
|
|
'e' => 'eval', |
803
|
|
|
], |
804
|
|
|
], |
805
|
|
|
], |
806
|
|
|
], |
807
|
|
|
'unset with invalid path having more elements' => [ |
808
|
|
|
[ |
809
|
|
|
'a' => 'aval', |
810
|
|
|
'b' => [ |
811
|
|
|
'c' => 'cval', |
812
|
|
|
'd' => [ |
813
|
|
|
'e' => 'eval', |
814
|
|
|
], |
815
|
|
|
], |
816
|
|
|
], |
817
|
|
|
['a', 'b', 'c'], |
818
|
|
|
[ |
819
|
|
|
'a' => 'aval', |
820
|
|
|
'b' => [ |
821
|
|
|
'c' => 'cval', |
822
|
|
|
'd' => [ |
823
|
|
|
'e' => 'eval', |
824
|
|
|
], |
825
|
|
|
], |
826
|
|
|
], |
827
|
|
|
], |
828
|
|
|
]; |
829
|
|
|
} |
830
|
|
|
|
831
|
|
|
/** |
832
|
|
|
* @dataProvider getInDataProvider |
833
|
|
|
*/ |
834
|
|
|
public function testGetIn(array $array, array $path, $defaultValue, $expectedValue) |
835
|
|
|
{ |
836
|
|
|
$this->assertEquals($expectedValue, ArrayUtil::getIn($array, $path, $defaultValue)); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
public function getInDataProvider() |
840
|
|
|
{ |
841
|
|
|
return [ |
842
|
|
|
'reading non existing key from empty array' => [ |
843
|
|
|
[], |
844
|
|
|
['k2', 'k2.2', 'nonExistent'], |
845
|
|
|
null, |
846
|
|
|
null, |
847
|
|
|
], |
848
|
|
|
'reading non existing key from array' => [ |
849
|
|
|
['k1' => 'v1', 'k2' => ['k2.1' => 'v2.1', 'k2.2' => 'v2.2']], |
850
|
|
|
['k2', 'k2.2', 'nonExistent'], |
851
|
|
|
null, |
852
|
|
|
null, |
853
|
|
|
], |
854
|
|
|
'reading non existing key from array with overwritten default value' => [ |
855
|
|
|
['k1' => 'v1', 'k2' => ['k2.1' => 'v2.1', 'k2.2' => 'v2.2']], |
856
|
|
|
['k2', 'k2.2', 'nonExistent'], |
857
|
|
|
'default', |
858
|
|
|
'default', |
859
|
|
|
], |
860
|
|
|
'reading simple key from array' => [ |
861
|
|
|
['k1' => 'v1', 'k2' => ['k2.1' => 'v2.1', 'k2.2' => 'v2.2']], |
862
|
|
|
['k1'], |
863
|
|
|
null, |
864
|
|
|
'v1', |
865
|
|
|
], |
866
|
|
|
'reading multivalue key from array' => [ |
867
|
|
|
['k1' => 'v1', 'k2' => ['k2.1' => 'v2.1', 'k2.2' => 'v2.2']], |
868
|
|
|
['k2', 'k2.2'], |
869
|
|
|
null, |
870
|
|
|
'v2.2', |
871
|
|
|
], |
872
|
|
|
]; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
/** |
876
|
|
|
* @dataProvider mergeDataProvider |
877
|
|
|
* |
878
|
|
|
* @param array $expected |
879
|
|
|
* @param array $first |
880
|
|
|
* @param array $second |
881
|
|
|
*/ |
882
|
|
|
public function testArrayMergeRecursiveDistinct(array $expected, array $first, array $second) |
883
|
|
|
{ |
884
|
|
|
$this->assertEquals($expected, ArrayUtil::arrayMergeRecursiveDistinct($first, $second)); |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
/** |
888
|
|
|
* @return array |
889
|
|
|
*/ |
890
|
|
|
public function mergeDataProvider() |
891
|
|
|
{ |
892
|
|
|
return [ |
893
|
|
|
[ |
894
|
|
|
[ |
895
|
|
|
'a', |
896
|
|
|
'b', |
897
|
|
|
'c' => [ |
898
|
|
|
'd' => 'd2', |
899
|
|
|
'e' => 'e1' |
900
|
|
|
] |
901
|
|
|
], |
902
|
|
|
['a', 'c' => ['d' => 'd1', 'e' => 'e1']], |
903
|
|
|
['b', 'c' => ['d' => 'd2']] |
904
|
|
|
], |
905
|
|
|
[ |
906
|
|
|
[ |
907
|
|
|
'a', |
908
|
|
|
'b', |
909
|
|
|
'c' => ['e1','e2'] |
910
|
|
|
], |
911
|
|
|
[ |
912
|
|
|
'a', |
913
|
|
|
'c' => 'e1', |
914
|
|
|
], |
915
|
|
|
[ |
916
|
|
|
'b', |
917
|
|
|
'c' => ['e1','e2'] |
918
|
|
|
], |
919
|
|
|
] |
920
|
|
|
]; |
921
|
|
|
} |
922
|
|
|
|
923
|
|
|
/** |
924
|
|
|
* @param object $obj |
925
|
|
|
* |
926
|
|
|
* @return mixed |
927
|
|
|
*/ |
928
|
|
|
public function getObjectPriority($obj) |
929
|
|
|
{ |
930
|
|
|
return $obj->priority; |
931
|
|
|
} |
932
|
|
|
|
933
|
|
|
/** |
934
|
|
|
* @param array $properties |
935
|
|
|
* |
936
|
|
|
* @return object |
937
|
|
|
*/ |
938
|
|
|
protected function createObject($properties) |
939
|
|
|
{ |
940
|
|
|
$obj = new \stdClass(); |
941
|
|
|
foreach ($properties as $name => $val) { |
942
|
|
|
$obj->$name = $val; |
943
|
|
|
} |
944
|
|
|
|
945
|
|
|
return $obj; |
946
|
|
|
} |
947
|
|
|
} |
948
|
|
|
|