From 5b1cbe380c10c4b4f24bb9d326e3a56aa0107c40 Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Mon, 14 Aug 2023 11:30:16 +0200 Subject: [PATCH 1/3] feature/support-multidimensional-and-associative-arrays-when-canonicalize --- src/ArrayComparator.php | 34 ++++++++++++++++++++++++++++++++-- tests/ArrayComparatorTest.php | 12 ++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/ArrayComparator.php b/src/ArrayComparator.php index 07eba9d3..723c7bf8 100644 --- a/src/ArrayComparator.php +++ b/src/ArrayComparator.php @@ -12,6 +12,7 @@ use function array_key_exists; use function assert; use function is_array; +use function ksort; use function sort; use function sprintf; use function str_replace; @@ -39,8 +40,8 @@ public function assertEquals(mixed $expected, mixed $actual, float $delta = 0.0, assert(is_array($actual)); if ($canonicalize) { - sort($expected); - sort($actual); + $this->canonicalize($expected); + $this->canonicalize($actual); } $remaining = $actual; @@ -124,4 +125,33 @@ private function indent(string $lines): string { return trim(str_replace("\n", "\n ", $lines)); } + + private function canonicalize(array &$array): void + { + if ($this->isIndexedArray($array)) { + sort($array); + } else { + ksort($array); + } + + foreach ($array as &$element) { + if (is_array($element)) { + $this->canonicalize($element); + } + } + } + + private function isIndexedArray(array $array): bool + { + $expectedKey = 0; + + foreach ($array as $key => $value) { + if ($key !== $expectedKey) { + return false; + } + $expectedKey++; + } + + return true; + } } diff --git a/tests/ArrayComparatorTest.php b/tests/ArrayComparatorTest.php index e1c2389a..2828e6b5 100644 --- a/tests/ArrayComparatorTest.php +++ b/tests/ArrayComparatorTest.php @@ -70,6 +70,12 @@ public static function assertEqualsSucceedsProvider() ['true'], [true], ], + [ + ['a', 'b' => [1, 2]], + ['b' => [2, 1], 'a'], + 0, + true, + ], ]; } @@ -115,6 +121,12 @@ public static function assertEqualsFailsProvider() ['false'], [false], ], + [ + ['a', 'b' => [1, 2]], + ['b' => [2, 1], 'a', 'c' => 3], + 0, + true, + ], ]; } From 501f6873c6b4d4d131ea020bd2fafa392e78a26e Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Mon, 14 Aug 2023 15:14:43 +0200 Subject: [PATCH 2/3] - refactoring --- src/ArrayComparator.php | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/ArrayComparator.php b/src/ArrayComparator.php index 723c7bf8..33147979 100644 --- a/src/ArrayComparator.php +++ b/src/ArrayComparator.php @@ -9,6 +9,7 @@ */ namespace SebastianBergmann\Comparator; +use function array_is_list; use function array_key_exists; use function assert; use function is_array; @@ -128,7 +129,7 @@ private function indent(string $lines): string private function canonicalize(array &$array): void { - if ($this->isIndexedArray($array)) { + if (array_is_list($array)) { sort($array); } else { ksort($array); @@ -140,18 +141,4 @@ private function canonicalize(array &$array): void } } } - - private function isIndexedArray(array $array): bool - { - $expectedKey = 0; - - foreach ($array as $key => $value) { - if ($key !== $expectedKey) { - return false; - } - $expectedKey++; - } - - return true; - } } From bc39224de5c64afbc08d4725d26f7bf563996281 Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Sun, 15 Oct 2023 14:54:08 +0200 Subject: [PATCH 3/3] - refactoring --- src/ArrayComparator.php | 6 ------ tests/ArrayComparatorTest.php | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/ArrayComparator.php b/src/ArrayComparator.php index 33147979..35a83e07 100644 --- a/src/ArrayComparator.php +++ b/src/ArrayComparator.php @@ -134,11 +134,5 @@ private function canonicalize(array &$array): void } else { ksort($array); } - - foreach ($array as &$element) { - if (is_array($element)) { - $this->canonicalize($element); - } - } } } diff --git a/tests/ArrayComparatorTest.php b/tests/ArrayComparatorTest.php index 2828e6b5..0534ed2c 100644 --- a/tests/ArrayComparatorTest.php +++ b/tests/ArrayComparatorTest.php @@ -71,8 +71,8 @@ public static function assertEqualsSucceedsProvider() [true], ], [ - ['a', 'b' => [1, 2]], - ['b' => [2, 1], 'a'], + ['a', 'b' => [1, 2], 'c' => [1, 2, 'd' => [1, 2]]], + ['c' => [1, 'd' => [2, 1], 2], 'b' => [2, 1], 'a'], 0, true, ],