Skip to content

Commit 3d7e726

Browse files
authored
chore: Additional tests (#58)
1 parent 919afb8 commit 3d7e726

File tree

4 files changed

+1131
-0
lines changed

4 files changed

+1131
-0
lines changed

tests/ArrayCompareTest.php

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,306 @@ public function it_detects_empty_array_change_with_strict_mode_on_multiple_dimen
347347
]
348348
], $diff->compare($new, $old));
349349
}
350+
351+
/** @test */
352+
public function it_handles_null_values_correctly()
353+
{
354+
$diff = new ArrayDiffMultidimensional();
355+
356+
$new = ['a' => null, 'b' => 'test'];
357+
$old = ['a' => '', 'b' => 'test'];
358+
359+
$result = $diff->compare($new, $old);
360+
$this->assertEquals(['a' => null], $result);
361+
}
362+
363+
/** @test */
364+
public function it_handles_null_vs_missing_key()
365+
{
366+
$diff = new ArrayDiffMultidimensional();
367+
368+
$new = ['a' => null, 'b' => 'test'];
369+
$old = ['b' => 'test'];
370+
371+
$result = $diff->compare($new, $old);
372+
$this->assertEquals(['a' => null], $result);
373+
}
374+
375+
/** @test */
376+
public function it_handles_false_vs_empty_array()
377+
{
378+
$diff = new ArrayDiffMultidimensional();
379+
380+
$new = ['a' => false, 'b' => []];
381+
$old = ['a' => [], 'b' => false];
382+
383+
$result = $diff->compare($new, $old);
384+
$this->assertEquals(['a' => false, 'b' => []], $result);
385+
}
386+
387+
/** @test */
388+
public function it_handles_zero_values_correctly()
389+
{
390+
$diff = new ArrayDiffMultidimensional();
391+
392+
$new = ['a' => 0, 'b' => '0', 'c' => 0.0];
393+
$old = ['a' => false, 'b' => 0, 'c' => '0'];
394+
395+
$result = $diff->compare($new, $old, true);
396+
$this->assertEquals(['a' => 0, 'b' => '0', 'c' => 0.0], $result);
397+
398+
$result = $diff->compare($new, $old, false);
399+
$this->assertEquals([], $result);
400+
}
401+
402+
/** @test */
403+
public function it_handles_boolean_values()
404+
{
405+
$diff = new ArrayDiffMultidimensional();
406+
407+
$new = ['a' => true, 'b' => false];
408+
$old = ['a' => 1, 'b' => 0];
409+
410+
$result = $diff->compare($new, $old, true);
411+
$this->assertEquals(['a' => true, 'b' => false], $result);
412+
413+
$result = $diff->compare($new, $old, false);
414+
$this->assertEquals([], $result);
415+
}
416+
417+
/** @test */
418+
public function it_handles_mixed_numeric_types()
419+
{
420+
$diff = new ArrayDiffMultidimensional();
421+
422+
$new = ['a' => 123, 'b' => 123.0, 'c' => '123'];
423+
$old = ['a' => '123', 'b' => 123, 'c' => 123.0];
424+
425+
$result = $diff->compare($new, $old, true);
426+
$this->assertEquals(['a' => 123, 'b' => 123.0, 'c' => '123'], $result);
427+
428+
$result = $diff->compare($new, $old, false);
429+
$this->assertEquals([], $result);
430+
}
431+
432+
/** @test */
433+
public function it_handles_very_small_float_differences()
434+
{
435+
$diff = new ArrayDiffMultidimensional();
436+
437+
$new = ['a' => 1.0000000000001];
438+
$old = ['a' => 1.0000000000002];
439+
440+
$result = $diff->compare($new, $old, true);
441+
$this->assertEquals(['a' => 1.0000000000001], $result);
442+
}
443+
444+
/** @test */
445+
public function it_handles_array_with_numeric_keys()
446+
{
447+
$diff = new ArrayDiffMultidimensional();
448+
449+
$new = [0 => 'a', 1 => 'b', 2 => ['c' => 'd']];
450+
$old = [0 => 'a', 1 => 'x', 2 => ['c' => 'e']];
451+
452+
$result = $diff->compare($new, $old);
453+
$this->assertEquals([1 => 'b', 2 => ['c' => 'd']], $result);
454+
}
455+
456+
/** @test */
457+
public function it_handles_empty_vs_null_in_arrays()
458+
{
459+
$diff = new ArrayDiffMultidimensional();
460+
461+
$new = ['a' => ['b' => []]];
462+
$old = ['a' => ['b' => null]];
463+
464+
$result = $diff->compare($new, $old);
465+
$this->assertEquals(['a' => ['b' => []]], $result);
466+
}
467+
468+
/** @test */
469+
public function it_handles_nested_empty_arrays()
470+
{
471+
$diff = new ArrayDiffMultidimensional();
472+
473+
$new = ['a' => ['b' => ['c' => []]]];
474+
$old = ['a' => ['b' => ['c' => ['d' => 'value']]]];
475+
476+
$result = $diff->compare($new, $old);
477+
$this->assertEquals(['a' => ['b' => ['c' => []]]], $result);
478+
}
479+
480+
/** @test */
481+
public function it_handles_objects_correctly()
482+
{
483+
$diff = new ArrayDiffMultidimensional();
484+
485+
$obj1 = new \stdClass();
486+
$obj1->prop = 'value1';
487+
488+
$obj2 = new \stdClass();
489+
$obj2->prop = 'value2';
490+
491+
$new = ['a' => $obj1];
492+
$old = ['a' => $obj2];
493+
494+
$result = $diff->compare($new, $old);
495+
$this->assertEquals(['a' => $obj1], $result);
496+
}
497+
498+
/** @test */
499+
public function it_handles_large_nested_structures()
500+
{
501+
$diff = new ArrayDiffMultidimensional();
502+
503+
$new = [
504+
'level1' => [
505+
'level2' => [
506+
'level3' => [
507+
'level4' => [
508+
'level5' => [
509+
'data' => 'new_value',
510+
'other' => 'same'
511+
]
512+
]
513+
]
514+
]
515+
]
516+
];
517+
518+
$old = [
519+
'level1' => [
520+
'level2' => [
521+
'level3' => [
522+
'level4' => [
523+
'level5' => [
524+
'data' => 'old_value',
525+
'other' => 'same'
526+
]
527+
]
528+
]
529+
]
530+
]
531+
];
532+
533+
$result = $diff->compare($new, $old);
534+
$expected = [
535+
'level1' => [
536+
'level2' => [
537+
'level3' => [
538+
'level4' => [
539+
'level5' => [
540+
'data' => 'new_value'
541+
]
542+
]
543+
]
544+
]
545+
]
546+
];
547+
548+
$this->assertEquals($expected, $result);
549+
}
550+
551+
/** @test */
552+
public function it_handles_array_to_scalar_changes()
553+
{
554+
$diff = new ArrayDiffMultidimensional();
555+
556+
$new = ['a' => 'scalar_value'];
557+
$old = ['a' => ['nested' => 'array']];
558+
559+
$result = $diff->compare($new, $old);
560+
$this->assertEquals(['a' => 'scalar_value'], $result);
561+
}
562+
563+
/** @test */
564+
public function it_handles_complex_mixed_types()
565+
{
566+
$diff = new ArrayDiffMultidimensional();
567+
568+
$new = [
569+
'string' => 'test',
570+
'int' => 42,
571+
'float' => 3.14,
572+
'bool' => true,
573+
'null' => null,
574+
'array' => ['nested' => 'value'],
575+
'empty_array' => []
576+
];
577+
578+
$old = [
579+
'string' => 'different',
580+
'int' => 42,
581+
'float' => 3.14,
582+
'bool' => false,
583+
'null' => null,
584+
'array' => ['nested' => 'different'],
585+
'empty_array' => ['not_empty']
586+
];
587+
588+
$result = $diff->compare($new, $old);
589+
$expected = [
590+
'string' => 'test',
591+
'bool' => true,
592+
'array' => ['nested' => 'value'],
593+
'empty_array' => []
594+
];
595+
596+
$this->assertEquals($expected, $result);
597+
}
598+
599+
/** @test */
600+
public function it_preserves_array_structure_in_results()
601+
{
602+
$diff = new ArrayDiffMultidimensional();
603+
604+
$new = [
605+
'users' => [
606+
0 => ['name' => 'John', 'age' => 30],
607+
1 => ['name' => 'Jane', 'age' => 25]
608+
]
609+
];
610+
611+
$old = [
612+
'users' => [
613+
0 => ['name' => 'John', 'age' => 25],
614+
1 => ['name' => 'Jane', 'age' => 25]
615+
]
616+
];
617+
618+
$result = $diff->compare($new, $old);
619+
$expected = [
620+
'users' => [
621+
0 => ['age' => 30]
622+
]
623+
];
624+
625+
$this->assertEquals($expected, $result);
626+
}
627+
628+
/** @test */
629+
public function it_handles_performance_with_large_arrays()
630+
{
631+
$diff = new ArrayDiffMultidimensional();
632+
633+
// Create large arrays for performance testing
634+
$new = [];
635+
$old = [];
636+
637+
for ($i = 0; $i < 1000; $i++) {
638+
$new[$i] = ['data' => "value_$i", 'meta' => ['id' => $i]];
639+
$old[$i] = ['data' => "value_$i", 'meta' => ['id' => $i]];
640+
}
641+
642+
// Change one value
643+
$new[500]['data'] = 'changed_value';
644+
645+
$start = microtime(true);
646+
$result = $diff->compare($new, $old);
647+
$end = microtime(true);
648+
649+
$this->assertEquals([500 => ['data' => 'changed_value']], $result);
650+
$this->assertLessThan(1.0, $end - $start, 'Performance test: should complete in less than 1 second');
651+
}
350652
}

0 commit comments

Comments
 (0)