diff --git a/src/Framework/Constraint/JsonMatches.php b/src/Framework/Constraint/JsonMatches.php index c70edf2c30e..6e36cf5ee90 100644 --- a/src/Framework/Constraint/JsonMatches.php +++ b/src/Framework/Constraint/JsonMatches.php @@ -78,13 +78,13 @@ protected function matches($other): bool protected function fail($other, $description, ComparisonFailure $comparisonFailure = null): void { if ($comparisonFailure === null) { - [$error] = Json::canonicalize($other); + [$error, $recodedOther] = Json::canonicalize($other); if ($error) { parent::fail($other, $description); } - [$error] = Json::canonicalize($this->value); + [$error, $recodedValue] = Json::canonicalize($this->value); if ($error) { parent::fail($other, $description); @@ -93,8 +93,8 @@ protected function fail($other, $description, ComparisonFailure $comparisonFailu $comparisonFailure = new ComparisonFailure( \json_decode($this->value), \json_decode($other), - Json::prettify($this->value), - Json::prettify($other), + Json::prettify($recodedValue), + Json::prettify($recodedOther), false, 'Failed asserting that two json values are equal.' ); diff --git a/src/Util/Json.php b/src/Util/Json.php index b73e5bb63cc..8e7c82c0e0a 100644 --- a/src/Util/Json.php +++ b/src/Util/Json.php @@ -23,7 +23,7 @@ final class Json */ public static function prettify(string $json): string { - $decodedJson = \json_decode($json, true); + $decodedJson = \json_decode($json, false); if (\json_last_error()) { throw new Exception( diff --git a/tests/unit/Framework/Constraint/JsonMatchesTest.php b/tests/unit/Framework/Constraint/JsonMatchesTest.php index d515323002f..464a642bb67 100644 --- a/tests/unit/Framework/Constraint/JsonMatchesTest.php +++ b/tests/unit/Framework/Constraint/JsonMatchesTest.php @@ -46,7 +46,7 @@ public static function evaluateThrowsExpectationFailedExceptionWhenJsonIsValidBu 'string type not equals number' => ['{"age": "5"}', '{"age": 5}'], 'string type not equals boolean' => ['{"age": "true"}', '{"age": true}'], 'string type not equals null' => ['{"age": "null"}', '{"age": null}'], - 'null field different from missing field' => ['{"present": true, "missing": null}', '{"present": true}'], + 'null field different from missing field' => ['{"missing": null, "present": true}', '{"present": true}'], 'array elements are ordered' => ['["first", "second"]', '["second", "first"]'], ]; } @@ -127,6 +127,59 @@ public function testFailErrorWithValidValueAndInvalidOther(): void <<evaluate('{"obj": {}, "val": 2}', '', false); + } catch (ExpectationFailedException $e) { + $this->assertEquals( + <<evaluate('{"obj": {"y": 2, "x": 1}, "val": 2}', '', false); + } catch (ExpectationFailedException $e) { + $this->assertEquals( + <<