-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathArrayCompareTest.php
More file actions
645 lines (522 loc) · 17.4 KB
/
ArrayCompareTest.php
File metadata and controls
645 lines (522 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
<?php
use PHPUnit\Framework\TestCase;
use Rogervila\ArrayDiffMultidimensional;
class ArrayCompareTest extends TestCase
{
/** @test */
public function it_returns_an_array()
{
$diff = new ArrayDiffMultidimensional();
$this->assertTrue(is_array($diff->compare([], [])));
}
/** @test */
public function it_fails_if_first_argument_is_not_an_array()
{
if (method_exists($this, 'expectException')) {
$this->expectException(\InvalidArgumentException::class);
$diff = new ArrayDiffMultidimensional();
$diff->compare('this should be an array', 'whatever');
} else {
var_dump('Skipped since current PHPUnit version does not support expectException');
$this->assertTrue(true);
}
}
/** @test */
public function it_does_not_change_if_second_argument_is_not_an_array()
{
$diff = new ArrayDiffMultidimensional();
$old = [
'a' => 'b',
'c' => [
'd' => 'e',
'ff' => [
'test'
]
],
];
$new = 'anything except an array';
$this->assertEquals($old, $diff->compare($old, $new));
}
/** @test */
public function it_detects_the_difference_on_string_value()
{
$diff = new ArrayDiffMultidimensional();
$old = [
'a' => 'b',
'c' => uniqid(),
];
$new = [
'a' => 'b',
'c' => uniqid(),
];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertTrue(isset($diff->compare($new, $old)['c']));
$this->assertTrue(is_string($diff->compare($new, $old)['c']));
$this->assertFalse(isset($diff->compare($new, $old)['a']));
}
/** @test */
public function it_detects_change_from_string_to_array()
{
$diff = new ArrayDiffMultidimensional();
$new = [
'a' => 'b',
'c' => [
'd' => uniqid(),
'e' => uniqid(),
],
];
$old = [
'a' => 'b',
'c' => uniqid(),
];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertTrue(is_array($diff->compare($new, $old)['c']));
$this->assertFalse(isset($diff->compare($new, $old)['a']));
}
/** @test */
public function it_detects_changes_on_nested_arrays()
{
$diff = new ArrayDiffMultidimensional();
$new = [
'a' => 'b',
'c' => [
'd' => 'e',
'f' => uniqid(),
],
];
$old = [
'a' => 'b',
'c' => [
'd' => 'e',
'f' => uniqid(),
],
];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertTrue(isset($diff->compare($new, $old)['c']['f']));
$this->assertFalse(isset($diff->compare($new, $old)['a']));
}
/** @test */
public function it_detects_change_from_float_to_array()
{
$diff = new ArrayDiffMultidimensional();
$newfloat = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0000000000002;
$oldfloat = 1.0000000000004;
$new = [
'a' => 'b',
'c' => $newfloat,
];
$old = [
'a' => 'b',
'c' => $oldfloat,
];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertEquals($newfloat, $diff->compare($new, $old)['c']);
$this->assertTrue(is_float($diff->compare($new, $old)['c']));
$this->assertFalse(isset($diff->compare($new, $old)['a']));
}
/** @test */
public function it_detects_floats_do_not_change()
{
$diff = new ArrayDiffMultidimensional();
$floatval = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0000000000005;
$new = [
'a' => 'b',
'c' => $floatval,
];
$old = [
'a' => 'd',
'c' => $floatval,
];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertEquals('b', $diff->compare($new, $old)['a']);
$this->assertFalse(isset($diff->compare($new, $old)['c']));
}
/** @test */
public function it_works_with_deep_levels()
{
$diff = new ArrayDiffMultidimensional();
$old = [
'a' => 'b',
'c' => [
'd' => [
'e' => [
'f' => [
'g' => [
'h' => 'old'
]
]
]
]
],
];
$new = [
'a' => 'b',
'c' => [
'd' => [
'e' => [
'f' => [
'g' => [
'h' => 'new'
]
]
]
]
],
];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertEquals('new', $diff->compare($new, $old)['c']['d']['e']['f']['g']['h']);
$this->assertFalse(isset($diff->compare($new, $old)['a']));
}
/** @test */
public function it_detects_new_array_items()
{
$diff = new ArrayDiffMultidimensional();
$value = 'this should be detected';
$new = [
'a' => 'b',
'c' => 'd',
'd' => $value,
];
$old = [
'a' => 'b',
'c' => 'd',
];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertTrue(isset($diff->compare($new, $old)['d']));
$this->assertEquals($value, $diff->compare($new, $old)['d']);
$this->assertFalse(isset($diff->compare($new, $old)['a']));
$this->assertFalse(isset($diff->compare($new, $old)['c']));
}
/** @test */
public function it_detects_loose_changes_with_strict_mode()
{
$diff = new ArrayDiffMultidimensional();
$new = [
'a' => 'b',
'c' => 1714,
];
$old = [
'a' => 'b',
'c' => '1714',
];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertTrue(isset($diff->compare($new, $old)['c']));
$this->assertEquals(1714, $diff->compare($new, $old)['c']);
$this->assertEquals(1, count($diff->compare($new, $old, true)));
$this->assertTrue(isset($diff->compare($new, $old, true)['c']));
$this->assertEquals(1714, $diff->compare($new, $old, true)['c']);
$this->assertEquals(1, count($diff->strictComparison($new, $old)));
$this->assertTrue(isset($diff->strictComparison($new, $old)['c']));
$this->assertEquals(1714, $diff->strictComparison($new, $old)['c']);
}
/** @test */
public function it_does_not_detect_loose_changes_without_strict_mode()
{
$diff = new ArrayDiffMultidimensional();
$new = [
'a' => 'b',
'c' => 1714,
];
$old = [
'a' => 'b',
'c' => '1714',
];
$this->assertEquals(0, count($diff->compare($new, $old, false)));
$this->assertFalse(isset($diff->compare($new, $old, false)['c']));
$this->assertEquals(0, count($diff->looseComparison($new, $old)));
$this->assertFalse(isset($diff->looseComparison($new, $old)['c']));
}
/** @test */
public function it_detects_epsilon_change_with_strict_mode()
{
$epsilon = defined('PHP_FLOAT_EPSILON') ? PHP_FLOAT_EPSILON : 2.2204460492503E-16;
$diff = new ArrayDiffMultidimensional();
$new = [123];
$old = [$epsilon + 123];
$this->assertEquals(1, count($diff->compare($new, $old)));
$this->assertTrue(isset($diff->compare($new, $old)[0]));
$this->assertTrue(is_int($diff->compare($new, $old)[0]));
$this->assertEquals(123, $diff->compare($new, $old)[0]);
}
/** @test */
public function it_does_not_detect_epsilon_change_with_strict_mode()
{
$epsilon = defined('PHP_FLOAT_EPSILON') ? PHP_FLOAT_EPSILON : 2.2204460492503E-16;
$diff = new ArrayDiffMultidimensional();
$new = [123];
$old = [$epsilon + 123];
$this->assertEquals(0, count($diff->looseComparison($new, $old)));
$this->assertFalse(isset($diff->looseComparison($new, $old)[0]));
}
/** @test */
public function it_detects_empty_array_change_with_strict_mode()
{
$diff = new ArrayDiffMultidimensional();
$a = [[]];
$b = [1];
$this->assertEquals($a, $diff->compare($a, $b));
$this->assertTrue(isset($diff->compare($a, $b)[0]));
}
/** @test */
public function it_detects_empty_array_change_with_strict_mode_on_multiple_dimensions()
{
$diff = new ArrayDiffMultidimensional();
$new = [
'a' => 'b',
'c' => [
'd' => [],
]
];
$old = [
'a' => 'b',
'c' => [
'd' => 1,
]
];
$this->assertEquals([
'c' => [
'd' => [],
]
], $diff->compare($new, $old));
}
/** @test */
public function it_handles_null_values_correctly()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => null, 'b' => 'test'];
$old = ['a' => '', 'b' => 'test'];
$result = $diff->compare($new, $old);
$this->assertEquals(['a' => null], $result);
}
/** @test */
public function it_handles_null_vs_missing_key()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => null, 'b' => 'test'];
$old = ['b' => 'test'];
$result = $diff->compare($new, $old);
$this->assertEquals(['a' => null], $result);
}
/** @test */
public function it_handles_false_vs_empty_array()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => false, 'b' => []];
$old = ['a' => [], 'b' => false];
$result = $diff->compare($new, $old);
$this->assertEquals(['a' => false, 'b' => []], $result);
}
/** @test */
public function it_handles_zero_values_correctly()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => 0, 'b' => '0', 'c' => 0.0];
$old = ['a' => false, 'b' => 0, 'c' => '0'];
$result = $diff->compare($new, $old, true);
$this->assertEquals(['a' => 0, 'b' => '0', 'c' => 0.0], $result);
$result = $diff->compare($new, $old, false);
$this->assertEquals([], $result);
}
/** @test */
public function it_handles_boolean_values()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => true, 'b' => false];
$old = ['a' => 1, 'b' => 0];
$result = $diff->compare($new, $old, true);
$this->assertEquals(['a' => true, 'b' => false], $result);
$result = $diff->compare($new, $old, false);
$this->assertEquals([], $result);
}
/** @test */
public function it_handles_mixed_numeric_types()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => 123, 'b' => 123.0, 'c' => '123'];
$old = ['a' => '123', 'b' => 123, 'c' => 123.0];
$result = $diff->compare($new, $old, true);
$this->assertEquals(['a' => 123, 'b' => 123.0, 'c' => '123'], $result);
$result = $diff->compare($new, $old, false);
$this->assertEquals([], $result);
}
/** @test */
public function it_handles_very_small_float_differences()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => 1.0000000000001];
$old = ['a' => 1.0000000000002];
$result = $diff->compare($new, $old, true);
$this->assertEquals(['a' => 1.0000000000001], $result);
}
/** @test */
public function it_handles_array_with_numeric_keys()
{
$diff = new ArrayDiffMultidimensional();
$new = [0 => 'a', 1 => 'b', 2 => ['c' => 'd']];
$old = [0 => 'a', 1 => 'x', 2 => ['c' => 'e']];
$result = $diff->compare($new, $old);
$this->assertEquals([1 => 'b', 2 => ['c' => 'd']], $result);
}
/** @test */
public function it_handles_empty_vs_null_in_arrays()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => ['b' => []]];
$old = ['a' => ['b' => null]];
$result = $diff->compare($new, $old);
$this->assertEquals(['a' => ['b' => []]], $result);
}
/** @test */
public function it_handles_nested_empty_arrays()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => ['b' => ['c' => []]]];
$old = ['a' => ['b' => ['c' => ['d' => 'value']]]];
$result = $diff->compare($new, $old);
$this->assertEquals(['a' => ['b' => ['c' => []]]], $result);
}
/** @test */
public function it_handles_objects_correctly()
{
$diff = new ArrayDiffMultidimensional();
$obj1 = new \stdClass();
$obj1->prop = 'value1';
$obj2 = new \stdClass();
$obj2->prop = 'value2';
$new = ['a' => $obj1];
$old = ['a' => $obj2];
$result = $diff->compare($new, $old);
$this->assertEquals(['a' => $obj1], $result);
}
/** @test */
public function it_handles_large_nested_structures()
{
$diff = new ArrayDiffMultidimensional();
$new = [
'level1' => [
'level2' => [
'level3' => [
'level4' => [
'level5' => [
'data' => 'new_value',
'other' => 'same'
]
]
]
]
]
];
$old = [
'level1' => [
'level2' => [
'level3' => [
'level4' => [
'level5' => [
'data' => 'old_value',
'other' => 'same'
]
]
]
]
]
];
$result = $diff->compare($new, $old);
$expected = [
'level1' => [
'level2' => [
'level3' => [
'level4' => [
'level5' => [
'data' => 'new_value'
]
]
]
]
]
];
$this->assertEquals($expected, $result);
}
/** @test */
public function it_handles_array_to_scalar_changes()
{
$diff = new ArrayDiffMultidimensional();
$new = ['a' => 'scalar_value'];
$old = ['a' => ['nested' => 'array']];
$result = $diff->compare($new, $old);
$this->assertEquals(['a' => 'scalar_value'], $result);
}
/** @test */
public function it_handles_complex_mixed_types()
{
$diff = new ArrayDiffMultidimensional();
$new = [
'string' => 'test',
'int' => 42,
'float' => 3.14,
'bool' => true,
'null' => null,
'array' => ['nested' => 'value'],
'empty_array' => []
];
$old = [
'string' => 'different',
'int' => 42,
'float' => 3.14,
'bool' => false,
'null' => null,
'array' => ['nested' => 'different'],
'empty_array' => ['not_empty']
];
$result = $diff->compare($new, $old);
$expected = [
'string' => 'test',
'bool' => true,
'array' => ['nested' => 'value'],
'empty_array' => []
];
$this->assertEquals($expected, $result);
}
/** @test */
public function it_preserves_array_structure_in_results()
{
$diff = new ArrayDiffMultidimensional();
$new = [
'users' => [
0 => ['name' => 'John', 'age' => 30],
1 => ['name' => 'Jane', 'age' => 25]
]
];
$old = [
'users' => [
0 => ['name' => 'John', 'age' => 25],
1 => ['name' => 'Jane', 'age' => 25]
]
];
$result = $diff->compare($new, $old);
$expected = [
'users' => [
0 => ['age' => 30]
]
];
$this->assertEquals($expected, $result);
}
/** @test */
public function it_handles_performance_with_large_arrays()
{
$diff = new ArrayDiffMultidimensional();
// Create large arrays for performance testing
$new = [];
$old = [];
for ($i = 0; $i < 1000; $i++) {
$new[$i] = ['data' => "value_$i", 'meta' => ['id' => $i]];
$old[$i] = ['data' => "value_$i", 'meta' => ['id' => $i]];
}
// Change one value
$new[500]['data'] = 'changed_value';
$start = microtime(true);
$result = $diff->compare($new, $old);
$end = microtime(true);
$this->assertEquals([500 => ['data' => 'changed_value']], $result);
$this->assertLessThan(1.0, $end - $start, 'Performance test: should complete in less than 1 second');
}
}