Skip to content

Commit 1372c8e

Browse files
committed
feat: add tests
1 parent 57ab762 commit 1372c8e

File tree

1 file changed

+156
-1
lines changed

1 file changed

+156
-1
lines changed

tests/Feature/JsonDiffTest.php

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ public function test_new_array_elements_added_between_jsons(): void
144144
$this->assertSame(2, $jsonDiff->getKeysAdded()->count());
145145

146146
$keysAdded = $jsonDiff->getKeysAdded();
147-
dd($keysAdded, $jsonDiff);
148147
$valuesAdded = $jsonDiff->getValuesAdded();
149148

150149
// Check that the new keys are added
@@ -611,4 +610,160 @@ public function test_json_diff_with_scalar_types(): void
611610
}
612611
}
613612
}
613+
614+
public function test_basic_array_list(): void
615+
{
616+
$originalArray = [0, 1, 2];
617+
$newArray = [0, 1, 3];
618+
$jsonDiff = new JsonDiff($originalArray, $newArray);
619+
$this->assertCount(
620+
1,
621+
$jsonDiff->getValuesChanged()
622+
);
623+
$this->assertSame(
624+
1,
625+
$jsonDiff->getNumberOfChanges()
626+
);
627+
628+
/** @var ValueChange $valueChanged */
629+
$valueChanged = $jsonDiff->getValuesChanged()->first();
630+
$this->assertSame(
631+
'2',
632+
$valueChanged->getPath()
633+
);
634+
$this->assertSame(
635+
2,
636+
$valueChanged->getOldValue()
637+
);
638+
$this->assertSame(
639+
3,
640+
$valueChanged->getNewValue()
641+
);
642+
}
643+
644+
public function test_diff_between_scalar_type_and_array(): void
645+
{
646+
$original = 'a string';
647+
$new = [0, 1, 2];
648+
649+
$jsonDiff = new JsonDiff($original, $new);
650+
651+
$this->assertSame(
652+
1,
653+
$jsonDiff->getNumberOfChanges()
654+
);
655+
656+
/** @var ValueChange $valueChange */
657+
$valueChange = $jsonDiff->getValuesChanged()->first();
658+
$this->assertSame(
659+
$original,
660+
$valueChange->getOldValue()
661+
);
662+
$this->assertSame(
663+
$new,
664+
$valueChange->getNewValue()
665+
);
666+
$this->assertSame(
667+
'',
668+
$valueChange->getPath()
669+
);
670+
671+
// Reverse the diff
672+
$original = [0, 1, 2];
673+
$new = 'a string';
674+
675+
$jsonDiff = new JsonDiff($original, $new);
676+
677+
$this->assertSame(
678+
1,
679+
$jsonDiff->getNumberOfChanges()
680+
);
681+
682+
/** @var ValueChange $valueChange */
683+
$valueChange = $jsonDiff->getValuesChanged()->first();
684+
$this->assertSame(
685+
$original,
686+
$valueChange->getOldValue()
687+
);
688+
$this->assertSame(
689+
$new,
690+
$valueChange->getNewValue()
691+
);
692+
$this->assertSame(
693+
'',
694+
$valueChange->getPath()
695+
);
696+
}
697+
698+
public function test_path_generation(): void
699+
{
700+
$dataSets = [
701+
[
702+
'original' => 'a string',
703+
'new' => 'another string',
704+
'expectedPath' => ''
705+
],
706+
[
707+
'original' => [0],
708+
'new' => [1],
709+
'expectedPath' => '0'
710+
],
711+
[
712+
'original' => [
713+
'name' => 'Bill Gates',
714+
'sports' => [
715+
'soccer',
716+
'rugby',
717+
]
718+
],
719+
'new' => [
720+
'name' => 'Bill Gates',
721+
'sports' => [
722+
'soccer',
723+
'tennis',
724+
]
725+
],
726+
'expectedPath' => 'sports.1'
727+
],
728+
[
729+
'original' => [
730+
[
731+
'name' => 'Bill Gates',
732+
'children' => [
733+
[
734+
'name' => 'Alice Gates',
735+
'sports' => [
736+
'soccer',
737+
'rugby',
738+
]
739+
]
740+
]
741+
]
742+
],
743+
'new' => [
744+
[
745+
'name' => 'Bill Gates',
746+
'children' => [
747+
[
748+
'name' => 'Alice Gates',
749+
'sports' => [
750+
'soccer',
751+
'tennis',
752+
]
753+
]
754+
]
755+
]
756+
],
757+
'expectedPath' => '0.children.0.sports.1'
758+
],
759+
];
760+
761+
foreach ($dataSets as $dataSet) {
762+
$jsonDiff = new JsonDiff($dataSet['original'], $dataSet['new']);
763+
$this->assertCount(1, $jsonDiff->getValuesChanged());
764+
/** @var ValueChange $valueChange */
765+
$valueChange = $jsonDiff->getValuesChanged()->first();
766+
$this->assertSame($dataSet['expectedPath'], $valueChange->getPath());
767+
}
768+
}
614769
}

0 commit comments

Comments
 (0)