Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/PHPUnit/PHPMatcherConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,34 @@ protected function matches($value) : bool
*/
protected function fail($other, $description, ComparisonFailure $comparisonFailure = null) : void
{
if (null === $comparisonFailure
&& \is_string($other)
&& \is_string($this->pattern)
&& \class_exists(Json::class)
) {
[$error] = Json::canonicalize($other);

if ($error) {
parent::fail($other, $description);
}

[$error] = Json::canonicalize($this->pattern);

if ($error) {
parent::fail($other, $description);
}

$comparisonFailure = new ComparisonFailure(
\json_decode($this->pattern),
\json_decode($other),
Json::prettify($this->pattern),
Json::prettify($other),
false,
'Failed asserting that the pattern matches the given value.'
);
parent::fail($other, $description, $comparisonFailure ?? $this->createComparisonFailure($other));
}

private function createComparisonFailure($other) : ?ComparisonFailure
{
if (!\is_string($other) || !\is_string($this->pattern) || !\class_exists(Json::class)) {
return null;
}

[$error, $otherJson] = Json::canonicalize($other);

if ($error) {
return null;
}

[$error, $patternJson] = Json::canonicalize($this->pattern);

if ($error) {
return null;
}

parent::fail($other, $description, $comparisonFailure);
return new ComparisonFailure(
\json_decode($this->pattern),
\json_decode($other),
Json::prettify($patternJson),
Json::prettify($otherJson),
false,
'Failed asserting that the pattern matches the given value.'
);
}
}
45 changes: 45 additions & 0 deletions tests/PHPUnit/PHPMatcherConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Coduo\PHPMatcher\PHPUnit\PHPMatcherConstraint;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;

class PHPMatcherConstraintTest extends TestCase
Expand Down Expand Up @@ -50,6 +51,50 @@ public function test_it_sets_additional_failure_description() : void
$this->assertFalse($constraint->evaluate(42));
}

public function test_expected_as_string_is_sorted() : void
{
$constraint = new PHPMatcherConstraint('{"b": 2, "a": 1}');

try {
$constraint->evaluate('null');

$this->fail();
} catch (ExpectationFailedException $exception) {
$this->assertSame(
<<<'JSON'
{
"a": 1,
"b": 2
}
JSON
,
$exception->getComparisonFailure()->getExpectedAsString()
);
}
}

public function test_actual_as_string_is_sorted() : void
{
$constraint = new PHPMatcherConstraint('{}');

try {
$constraint->evaluate('{"b": 2, "a": 1}');

$this->fail();
} catch (ExpectationFailedException $exception) {
$this->assertSame(
<<<'JSON'
{
"a": 1,
"b": 2
}
JSON
,
$exception->getComparisonFailure()->getActualAsString()
);
}
}

public function test_that_pattern_could_be_an_array() : void
{
$constraint = new PHPMatcherConstraint(['foo' => '@string@']);
Expand Down