Skip to content

Empty objects are converted to empty arrays in JSON comparison failure diff #4144

@uuf6429

Description

@uuf6429
Q A
PHPUnit version 8.5.2
PHP version 7.4.3
Installation Method Composer

Summary

If asserting equality between 2 json strings fails, any empty objects ({}) are converted to empty arrays ([]) causing a different diff than expected.

At first, I saw this in PHPStorm and thought it's a PHPStorm issue, but apparently it's not (link to PHPStorm report).

image

The problem seems to be caused by Json.php:26 (the true parameter). I think all PHPUnit versions are affected.

Current behavior

Failed asserting that '{"key": {}}' matches JSON string "{"key": {"sub": 1}}".
--- Expected
+++ Actual
@@ @@
 {
-    "key": {
-        "sub": 1
-    }
+    "key": []
 }

Expected behavior

Failed asserting that '{"key": {}}' matches JSON string "{"key": {"sub": 1}}".
--- Expected
+++ Actual
@@ @@
 {
-    "key": {
-        "sub": 1
-    }
+    "key": {}
 }

How to reproduce

Look at the results of the test failure:

    public function testPoC(): void
    {
        $this->assertJsonStringEqualsJsonString(
            '{"key": {"sub": 1}}',
            '{"key": {}}'              // <- notice that it's an empty object, not array
        );
    }

Metadata

Metadata

Assignees

Labels

feature/assertionIssues related to assertions and expectationstype/bugSomething is broken

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions