Skip to content

Commit 33181bc

Browse files
authoredAug 12, 2020
Fixed JSON handling in PHPMatcherConstraint (#208)
* Fixed JSON handling in PHPMatcherConstraint * Move comma to the next line after nowdoc
1 parent 5091e41 commit 33181bc

File tree

2 files changed

+72
-26
lines changed

2 files changed

+72
-26
lines changed
 

‎src/PHPUnit/PHPMatcherConstraint.php

+27-26
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,34 @@ protected function matches($value) : bool
6161
*/
6262
protected function fail($other, $description, ComparisonFailure $comparisonFailure = null) : void
6363
{
64-
if (null === $comparisonFailure
65-
&& \is_string($other)
66-
&& \is_string($this->pattern)
67-
&& \class_exists(Json::class)
68-
) {
69-
[$error] = Json::canonicalize($other);
70-
71-
if ($error) {
72-
parent::fail($other, $description);
73-
}
74-
75-
[$error] = Json::canonicalize($this->pattern);
76-
77-
if ($error) {
78-
parent::fail($other, $description);
79-
}
80-
81-
$comparisonFailure = new ComparisonFailure(
82-
\json_decode($this->pattern),
83-
\json_decode($other),
84-
Json::prettify($this->pattern),
85-
Json::prettify($other),
86-
false,
87-
'Failed asserting that the pattern matches the given value.'
88-
);
64+
parent::fail($other, $description, $comparisonFailure ?? $this->createComparisonFailure($other));
65+
}
66+
67+
private function createComparisonFailure($other) : ?ComparisonFailure
68+
{
69+
if (!\is_string($other) || !\is_string($this->pattern) || !\class_exists(Json::class)) {
70+
return null;
71+
}
72+
73+
[$error, $otherJson] = Json::canonicalize($other);
74+
75+
if ($error) {
76+
return null;
77+
}
78+
79+
[$error, $patternJson] = Json::canonicalize($this->pattern);
80+
81+
if ($error) {
82+
return null;
8983
}
9084

91-
parent::fail($other, $description, $comparisonFailure);
85+
return new ComparisonFailure(
86+
\json_decode($this->pattern),
87+
\json_decode($other),
88+
Json::prettify($patternJson),
89+
Json::prettify($otherJson),
90+
false,
91+
'Failed asserting that the pattern matches the given value.'
92+
);
9293
}
9394
}

‎tests/PHPUnit/PHPMatcherConstraintTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Coduo\PHPMatcher\PHPUnit\PHPMatcherConstraint;
88
use PHPUnit\Framework\AssertionFailedError;
99
use PHPUnit\Framework\Constraint\Constraint;
10+
use PHPUnit\Framework\ExpectationFailedException;
1011
use PHPUnit\Framework\TestCase;
1112

1213
class PHPMatcherConstraintTest extends TestCase
@@ -50,6 +51,50 @@ public function test_it_sets_additional_failure_description() : void
5051
$this->assertFalse($constraint->evaluate(42));
5152
}
5253

54+
public function test_expected_as_string_is_sorted() : void
55+
{
56+
$constraint = new PHPMatcherConstraint('{"b": 2, "a": 1}');
57+
58+
try {
59+
$constraint->evaluate('null');
60+
61+
$this->fail();
62+
} catch (ExpectationFailedException $exception) {
63+
$this->assertSame(
64+
<<<'JSON'
65+
{
66+
"a": 1,
67+
"b": 2
68+
}
69+
JSON
70+
,
71+
$exception->getComparisonFailure()->getExpectedAsString()
72+
);
73+
}
74+
}
75+
76+
public function test_actual_as_string_is_sorted() : void
77+
{
78+
$constraint = new PHPMatcherConstraint('{}');
79+
80+
try {
81+
$constraint->evaluate('{"b": 2, "a": 1}');
82+
83+
$this->fail();
84+
} catch (ExpectationFailedException $exception) {
85+
$this->assertSame(
86+
<<<'JSON'
87+
{
88+
"a": 1,
89+
"b": 2
90+
}
91+
JSON
92+
,
93+
$exception->getComparisonFailure()->getActualAsString()
94+
);
95+
}
96+
}
97+
5398
public function test_that_pattern_could_be_an_array() : void
5499
{
55100
$constraint = new PHPMatcherConstraint(['foo' => '@string@']);

0 commit comments

Comments
 (0)