diff --git a/.phive/phars.xml b/.phive/phars.xml index 4817da71..1e6273c2 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -1,6 +1,6 @@ - + - + diff --git a/ChangeLog.md b/ChangeLog.md index f0c02d44..6a30ada1 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -14,6 +14,12 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt * Removed support for PHP 8.1 +## [5.0.2] - 2024-08-12 + +### Fixed + +* [#112](https://github.com/sebastianbergmann/comparator/issues/112): Arrays with different keys and the same values are considered equal in canonicalize mode + ## [5.0.1] - 2023-08-14 ### Fixed @@ -158,6 +164,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt [6.0.1]: https://github.com/sebastianbergmann/comparator/compare/6.0.0...6.0.1 [6.0.0]: https://github.com/sebastianbergmann/comparator/compare/5.0...6.0.0 +[5.0.2]: https://github.com/sebastianbergmann/comparator/compare/5.0.1...5.0.2 [5.0.1]: https://github.com/sebastianbergmann/comparator/compare/5.0.0...5.0.1 [5.0.0]: https://github.com/sebastianbergmann/comparator/compare/4.0.8...5.0.0 [4.0.8]: https://github.com/sebastianbergmann/comparator/compare/4.0.7...4.0.8 diff --git a/src/ArrayComparator.php b/src/ArrayComparator.php index 5af5a437..d6f68bba 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; @@ -41,8 +42,13 @@ public function assertEquals(mixed $expected, mixed $actual, float $delta = 0.0, assert(is_array($actual)); if ($canonicalize) { - sort($expected); - sort($actual); + if (array_is_list($expected)) { + sort($expected); + } + + if (array_is_list($actual)) { + sort($actual); + } } $remaining = $actual; diff --git a/tests/unit/ArrayComparatorTest.php b/tests/unit/ArrayComparatorTest.php index 96ceadd9..d16fedfa 100644 --- a/tests/unit/ArrayComparatorTest.php +++ b/tests/unit/ArrayComparatorTest.php @@ -73,6 +73,30 @@ public static function assertEqualsSucceedsProvider(): array ['true'], [true], ], + [ + [1, [1, 2, 3]], + [[3, 1, 2], 1], + 0, + true, + ], + [ + [1, [4, 7], [1, 2, 3], [0, 0, 0, 0]], + [[3, 1, 2], [0, 0, 0, 0], [4, 7], 1], + 0, + true, + ], + [ + [1, [4, [4, 5, 6, 7, 8]], [1, 2, 3]], + [[3, 1, 2], [[4, 8, 6, 7, 5], 4], 1], + 0, + true, + ], + [ + [null, null, 1, 1], + [1, null, 1, null], + 0, + true, + ], ]; } @@ -121,6 +145,24 @@ public static function assertEqualsFailsProvider(): array ['false'], [false], ], + [ + ['a' => '1', 'b' => '2'], + ['c' => '1', 'd' => '2'], + 0, + true, + ], + [ + ['a' => '1', 'b' => '2'], + ['a' => '2', 'b' => '1'], + 0, + true, + ], + [ + ['a' => '1', 'b' => '2'], + ['a' => '1', 'b' => '2', 'c' => '2'], + 0, + true, + ], ]; } diff --git a/tools/php-cs-fixer b/tools/php-cs-fixer index 8b324b44..aa9727fa 100755 Binary files a/tools/php-cs-fixer and b/tools/php-cs-fixer differ diff --git a/tools/phpstan b/tools/phpstan index de555a13..75fa21f0 100755 Binary files a/tools/phpstan and b/tools/phpstan differ