comparator icon indicating copy to clipboard operation
comparator copied to clipboard

ArrayComparator#assertEquals(), canonicalizing stops it from comparing array keys

Open ghost opened this issue 6 years ago • 2 comments

When using the canonicalize option both expected and actual arrays get sorted, and keys are removed, which causes comparison to be ok even if keys were not equal.

If canonicalize is set to false, comparison fails if keys are not equal.

image

ghost avatar Dec 23 '19 17:12 ghost

Arrays are equal if they contain the same key-value pairs.

https://github.com/sebastianbergmann/comparator/blob/c57ba51c28b64ef8a4b14b40bceb2c5ca19e9406/src/ArrayComparator.php#L15

So it seems like sort must not be used to canonicalize arrays.

teohhanhui avatar Dec 26 '19 14:12 teohhanhui

Maybe using ksort to sort by key? Problem is there's only 1 test that checks canonicalized arrays, and it fails if using ksort.

image

ghost avatar Dec 26 '19 16:12 ghost