PHPUnit: assert two arrays are equal, but order of elements not important
You can use the assertEqualsCanonicalizing or assertEqualsIgnoringCase methods to compare two arrays and ignore the order of the elements.
You can use the assertEqualsCanonicalizing method to compare two arrays and ignore the order of the elements.
Example of using the assertEqualsCanonicalizing method to compare two arrays and ignore the order of the elements in PHPUnit
$expected = [3, 2, 1];
$actual = [1, 2, 3];
$this->assertEqualsCanonicalizing($expected, $actual);Info
Watch a video course: Learn object oriented PHP
Alternatively, you can sort both arrays before comparing them using the assertEquals method.
Example of sorting arrays before comparing them with assertEquals in PHPUnit
$expected = [3, 2, 1];
sort($expected);
$actual = [1, 2, 3];
sort($actual);
$this->assertEquals($expected, $actual);