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

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
);
}
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).
The problem seems to be caused by Json.php:26 (the
trueparameter). 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: