From bbfe55bc94d55e41235b13ead1c74d3561e437d1 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Thu, 14 Apr 2022 15:43:40 +0300 Subject: [PATCH] fix for internal array comparison logic --- src/Comparator/RecursiveArrayComparator.php | 8 +- .../RecursiveLtrArrayComparator.php | 11 +-- .../RecursiveArrayComparatorTest.php | 75 +++++++++++++++++++ .../RecursiveArrayLtrComparatorTest.php | 33 ++++++++ 4 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 tests/src/Comparator/RecursiveArrayComparatorTest.php diff --git a/src/Comparator/RecursiveArrayComparator.php b/src/Comparator/RecursiveArrayComparator.php index eb800a3..94cc626 100644 --- a/src/Comparator/RecursiveArrayComparator.php +++ b/src/Comparator/RecursiveArrayComparator.php @@ -50,8 +50,12 @@ class RecursiveArrayComparator implements ComparatorInterface } foreach ($first as $key => $value) { - if (is_array($value) && !self::recursiveCompareArrays($value, $second[$key])) { - return false; + if (is_array($value)) { + if (!is_array($second[$key]) || !self::recursiveCompareArrays($value, $second[$key])) { + return false; + } + + continue; } if ($value !== $second[$key]) { diff --git a/src/Comparator/RecursiveLtrArrayComparator.php b/src/Comparator/RecursiveLtrArrayComparator.php index de67a5e..2dfa48a 100644 --- a/src/Comparator/RecursiveLtrArrayComparator.php +++ b/src/Comparator/RecursiveLtrArrayComparator.php @@ -47,11 +47,12 @@ class RecursiveLtrArrayComparator extends RecursiveArrayComparator } foreach ($needle as $key => $value) { - if ( - is_array($value) && - (!is_array($haystack[$key]) || !self::recursiveCompareArrays($value, $haystack[$key])) - ) { - return false; + if (is_array($value)) { + if (!is_array($haystack[$key]) || !self::recursiveCompareArrays($value, $haystack[$key])) { + return false; + } + + continue; } if ($value !== $haystack[$key]) { diff --git a/tests/src/Comparator/RecursiveArrayComparatorTest.php b/tests/src/Comparator/RecursiveArrayComparatorTest.php new file mode 100644 index 0000000..da44e09 --- /dev/null +++ b/tests/src/Comparator/RecursiveArrayComparatorTest.php @@ -0,0 +1,75 @@ + [ + 'createdAtFrom' => '2020-01-01 00:00:00', + 'createdAtTo' => '2021-08-01 00:00:00', + ], + 'test' => '' + ]; + $haystack = [ + 'filter' => [ + 'createdAtFrom' => '2020-01-01 00:00:00', + 'createdAtTo' => '2021-08-01 00:00:00', + ], + 'test' => '' + ]; + + self::assertTrue(ComparatorLocator::get(RecursiveArrayComparator::class)->compare($needle, $haystack)); + } + + public function testNotMatches(): void + { + $needle = [ + 'filter' => [ + 'createdAtFrom' => '2020-01-01 00:00:00', + 'createdAtTo' => '2021-08-01 00:00:00', + ], + 'test2' => [1] + ]; + $haystack = [ + 'filter' => [ + 'createdAtFrom' => '2020-01-01 00:00:00', + 'createdAtTo' => '2021-08-01 00:00:00', + ], + 'test2' => 1 + ]; + + self::assertFalse(ComparatorLocator::get(RecursiveArrayComparator::class)->compare($needle, $haystack)); + } + + public function testNotMatchesKeyPositions(): void + { + $needle = [ + 'source' => json_decode('{"medium":"tiktok","source":"Test Ad","campaign":"Test Campaign"}', true) + ]; + $haystack = [ + 'source' => json_decode('{"source":"Test Ad","medium":"tiktok","campaign":"Test Campaign"}', true) + ]; + + self::assertTrue(ComparatorLocator::get(RecursiveArrayComparator::class)->compare($needle, $haystack)); + } +} diff --git a/tests/src/Comparator/RecursiveArrayLtrComparatorTest.php b/tests/src/Comparator/RecursiveArrayLtrComparatorTest.php index c72a5bc..51db6e1 100644 --- a/tests/src/Comparator/RecursiveArrayLtrComparatorTest.php +++ b/tests/src/Comparator/RecursiveArrayLtrComparatorTest.php @@ -39,4 +39,37 @@ class RecursiveArrayLtrComparatorTest extends TestCase self::assertTrue(ComparatorLocator::get(RecursiveLtrArrayComparator::class)->compare($needle, $haystack)); } + + public function testNotMatches(): void + { + $needle = [ + 'filter' => [ + 'createdAtFrom' => '2020-01-01 00:00:00', + 'createdAtTo' => '2021-08-01 00:00:00', + ], + 'test2' => [1] + ]; + $haystack = [ + 'filter' => [ + 'createdAtFrom' => '2020-01-01 00:00:00', + 'createdAtTo' => '2021-08-01 00:00:00', + ], + 'test2' => 1, + 'test' => '' + ]; + + self::assertFalse(ComparatorLocator::get(RecursiveLtrArrayComparator::class)->compare($needle, $haystack)); + } + + public function testNotMatchesKeyPositions(): void + { + $needle = [ + 'source' => json_decode('{"medium":"tiktok","source":"Test Ad","campaign":"Test Campaign"}', true) + ]; + $haystack = [ + 'source' => json_decode('{"source":"Test Ad","medium":"tiktok","campaign":"Test Campaign"}', true) + ]; + + self::assertTrue(ComparatorLocator::get(RecursiveLtrArrayComparator::class)->compare($needle, $haystack)); + } }