phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

assertJsonStringEqualsJsonString considers objects with sequential numeric keys equal to arrays

Open lpd-au opened this issue 5 years ago • 0 comments

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) {
  }
}

lpd-au avatar Jan 14 '21 14:01 lpd-au