Skip to content

assertJsonStringEqualsJsonString() considers objects with sequential numeric keys equal to be arrays #4584

@lpd-au

Description

@lpd-au
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) {
  }
}

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