Skip to content

Commit

Permalink
Fix json diff failure issues
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 authored and sebastianbergmann committed Mar 29, 2020
1 parent 3c03f88 commit 40ff696
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Framework/Constraint/JsonMatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.'
);
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
55 changes: 54 additions & 1 deletion tests/unit/Framework/Constraint/JsonMatchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]'],
];
}
Expand Down Expand Up @@ -127,6 +127,59 @@ public function testFailErrorWithValidValueAndInvalidOther(): void
<<<EOF
Failed asserting that '{"Mascott":"Tux"}' matches JSON string "{"Mascott"::}".
EOF
,
TestFailure::exceptionToString($e)
);
}
}

public function testEmptyObjectNotConvertedToArrayInDiff(): void
{
$constraint = new JsonMatches('{"obj": {}, "val": 1}');

try {
$constraint->evaluate('{"obj": {}, "val": 2}', '', false);
} catch (ExpectationFailedException $e) {
$this->assertEquals(
<<<EOF
Failed asserting that '{"obj": {}, "val": 2}' matches JSON string "{"obj": {}, "val": 1}".
--- Expected
+++ Actual
@@ @@
{
"obj": {},
- "val": 1
+ "val": 2
}
EOF
,
TestFailure::exceptionToString($e)
);
}
}

public function testObjectAreCanonicalizedInDiff(): void
{
$constraint = new JsonMatches('{"obj": {"x": 1, "y": 2}, "val": 1}');

try {
$constraint->evaluate('{"obj": {"y": 2, "x": 1}, "val": 2}', '', false);
} catch (ExpectationFailedException $e) {
$this->assertEquals(
<<<EOF
Failed asserting that '{"obj": {"y": 2, "x": 1}, "val": 2}' matches JSON string "{"obj": {"x": 1, "y": 2}, "val": 1}".
--- Expected
+++ Actual
@@ @@
"x": 1,
"y": 2
},
- "val": 1
+ "val": 2
}
EOF
,
TestFailure::exceptionToString($e)
Expand Down

0 comments on commit 40ff696

Please sign in to comment.