diff output for assertEqualsCanonicalizing() is affected by order
| Q | A |
|---|---|
| PHPUnit version | 11.3.5 |
| PHP version | 8.3.8 |
| Installation Method | Composer |
Summary
The diff output when assertEqualsCanonicalizing() fails is affected by the order of the arrays.
Current behavior
Consider this assertion:
$this->assertEqualsCanonicalizing(
[
"alpha",
"beta",
],
[
"alpha",
"beta",
"gamma",
],
);
the output is:
--- Expected
+++ Actual
@@ @@
Array (
0 => 'alpha'
1 => 'beta'
+ 2 => 'gamma'
)
but with this assertion:
$this->assertEqualsCanonicalizing(
[
"alpha",
"gamma",
],
[
"alpha",
"beta",
"gamma",
],
);
The output is:
--- Expected
+++ Actual
@@ @@
Array (
0 => 'alpha'
- 1 => 'gamma'
+ 1 => 'beta'
+ 2 => 'gamma'
)
In both cases, there is one item extra item the actual value compared to the expected value, but in one case the diff shows that very clearly, and in the other case, the diff has extra noise.
How to reproduce
Expected behavior
Only 'beta' is surplus, so the output should be:
--- Expected
+++ Actual
@@ @@
Array (
+ 1 => 'beta'
)
Interesting that the PHPUnit manual for assertEqualsCanonicalizing() also shows the noise. So this seems to be the intended behavior, even though it is not helpful and not what you would expect given the implication that as the arrays are sorted and the keys should be simply ignored.
Also minor point @joachim-n but I think you have a typo in your 'expected behavior'. You say "only gamma is surplus" but it is actually 'beta' that is surplus, that is, 'beta' is in array 2 but not array 1.
Fixed the typo, thank you!
I came here from https://www.drupal.org/project/gitlab_templates/issues/3475794 which is similar but not the same. Your view on that would be good, particularly if you can link to a gitlab pipeline where you discovered this (if there is one).