Skip to content

assertEqualsCanonicalizing() produces confusing message when elements cannot be converted to string #4923

Open
@kiler129

Description

Q A
PHPUnit version 9.5.18
PHP version 8.1.2
Installation Method PHAR

Summary

When assertEqualsCanonicalizing() is utilized the assertion result display will crash when one of the array elements, which is presented in the diff, is not castable to string.

Current behavior

When test is run the testing framework crashes with, at-first confusing, error message:

1) FooTest::testFoo
Object of class stdClass could not be converted to int

How to reproduce

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class FooTest extends TestCase
{
    public function testFoo(): void
    {
       $data = [
            'one' => 123,
            'two' => new \stdClass(),
        ];

        $copy = $data;
        $copy['one'] = 1234;

        $this->assertEqualsCanonicalizing($data, $copy);
    }
}

Expected behavior

The diff should handle such cases properly and display e.g. object ID, similarly to how e.g. assertSame() does it:

1) FooTest::testFoo
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
 Array &0 (
-    'one' => 123
+    'one' => 1234
     'two' => stdClass Object &000000000000000d0000000000000000 ()
 )

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugSomething is broken

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions