Skip to content

Commit

Permalink
Fix for issue #112 - ArrayComparator in canonicalize mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mgleska authored and sebastianbergmann committed Aug 12, 2024
1 parent e12faed commit b021fdb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ArrayComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\Comparator;

use function array_is_list;
use function array_key_exists;
use function assert;
use function is_array;
Expand Down Expand Up @@ -39,8 +40,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;
Expand Down
42 changes: 42 additions & 0 deletions tests/ArrayComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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,
],
];
}

Expand Down Expand Up @@ -112,6 +136,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,
],
];
}

Expand Down

0 comments on commit b021fdb

Please sign in to comment.