phpunit
phpunit copied to clipboard
assertJsonStringEqualsJsonString considers objects with sequential numeric keys equal to arrays
| Q | A |
|---|---|
| PHPUnit version | 9.5.0 |
| PHP version | 7.4.14 |
| Installation Method | Composer |
Summary
When a PHP array with sequentially increasing numeric keys is encoded twice, once with JSON_FORCE_OBJECT and once without JSON_FORCE_OBJECT, assertJsonStringEqualsJsonString considers both outputs to be equal.
How to reproduce
$a = '[{}]';
$b = '{"0": {}}';
$this->assertJsonStringEqualsJsonString($a, $b);
$this->assertEquals(json_decode($a), json_decode($b));
$this->assertJsonStringEqualsJsonString('[]', '{}');
$this->assertJsonStringEqualsJsonString('{}', '[]');
Current behavior
The first test passes and the remaining three fail.
Expected behavior
All tests fail because, when decoded,
array(1) {
[0]=>
object(stdClass)#1 (0) {
}
}
does not equal
object(stdClass)#2 (1) {
["0"]=>
object(stdClass)#1 (0) {
}
}