Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

diff output for assertEqualsCanonicalizing() is affected by order #5955

Open
joachim-n opened this issue Sep 18, 2024 · 3 comments
Open

diff output for assertEqualsCanonicalizing() is affected by order #5955

joachim-n opened this issue Sep 18, 2024 · 3 comments
Labels
type/bug Something is broken

Comments

@joachim-n
Copy link

joachim-n commented Sep 18, 2024

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'
 )
@joachim-n joachim-n added the type/bug Something is broken label Sep 18, 2024
@jonathan1055
Copy link

jonathan1055 commented Sep 20, 2024

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.

@joachim-n
Copy link
Author

Fixed the typo, thank you!

@jonathan1055
Copy link

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).

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

2 participants