Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/ArrayComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
*/
namespace SebastianBergmann\Comparator;

use function array_is_list;
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;
Expand Down Expand Up @@ -39,8 +41,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;
Expand Down Expand Up @@ -124,4 +126,13 @@ private function indent(string $lines): string
{
return trim(str_replace("\n", "\n ", $lines));
}

private function canonicalize(array &$array): void
{
if (array_is_list($array)) {
sort($array);
} else {
ksort($array);
}
}
}
12 changes: 12 additions & 0 deletions tests/ArrayComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public static function assertEqualsSucceedsProvider()
['true'],
[true],
],
[
['a', 'b' => [1, 2], 'c' => [1, 2, 'd' => [1, 2]]],
['c' => [1, 'd' => [2, 1], 2], 'b' => [2, 1], 'a'],
0,
true,
],
];
}

Expand Down Expand Up @@ -115,6 +121,12 @@ public static function assertEqualsFailsProvider()
['false'],
[false],
],
[
['a', 'b' => [1, 2]],
['b' => [2, 1], 'a', 'c' => 3],
0,
true,
],
];
}

Expand Down