| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of PHPUnit.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace PHPUnit\Framework\Constraint;
|
|
|
11 |
|
|
|
12 |
use function json_decode;
|
|
|
13 |
use function sprintf;
|
|
|
14 |
use PHPUnit\Framework\ExpectationFailedException;
|
|
|
15 |
use PHPUnit\Util\Json;
|
|
|
16 |
use SebastianBergmann\Comparator\ComparisonFailure;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
|
|
|
20 |
*/
|
|
|
21 |
final class JsonMatches extends Constraint
|
|
|
22 |
{
|
|
|
23 |
/**
|
|
|
24 |
* @var string
|
|
|
25 |
*/
|
|
|
26 |
private $value;
|
|
|
27 |
|
|
|
28 |
public function __construct(string $value)
|
|
|
29 |
{
|
|
|
30 |
$this->value = $value;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Returns a string representation of the object.
|
|
|
35 |
*/
|
|
|
36 |
public function toString(): string
|
|
|
37 |
{
|
|
|
38 |
return sprintf(
|
|
|
39 |
'matches JSON string "%s"',
|
|
|
40 |
$this->value
|
|
|
41 |
);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Evaluates the constraint for parameter $other. Returns true if the
|
|
|
46 |
* constraint is met, false otherwise.
|
|
|
47 |
*
|
|
|
48 |
* This method can be overridden to implement the evaluation algorithm.
|
|
|
49 |
*
|
|
|
50 |
* @param mixed $other value or object to evaluate
|
|
|
51 |
*/
|
|
|
52 |
protected function matches($other): bool
|
|
|
53 |
{
|
|
|
54 |
[$error, $recodedOther] = Json::canonicalize($other);
|
|
|
55 |
|
|
|
56 |
if ($error) {
|
|
|
57 |
return false;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
[$error, $recodedValue] = Json::canonicalize($this->value);
|
|
|
61 |
|
|
|
62 |
if ($error) {
|
|
|
63 |
return false;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
return $recodedOther == $recodedValue;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Throws an exception for the given compared value and test description.
|
|
|
71 |
*
|
|
|
72 |
* @param mixed $other evaluated value or object
|
|
|
73 |
* @param string $description Additional information about the test
|
|
|
74 |
* @param ComparisonFailure $comparisonFailure
|
|
|
75 |
*
|
|
|
76 |
* @throws \PHPUnit\Framework\Exception
|
|
|
77 |
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
78 |
* @throws ExpectationFailedException
|
|
|
79 |
*
|
|
|
80 |
* @psalm-return never-return
|
|
|
81 |
*/
|
|
|
82 |
protected function fail($other, $description, ComparisonFailure $comparisonFailure = null): void
|
|
|
83 |
{
|
|
|
84 |
if ($comparisonFailure === null) {
|
|
|
85 |
[$error, $recodedOther] = Json::canonicalize($other);
|
|
|
86 |
|
|
|
87 |
if ($error) {
|
|
|
88 |
parent::fail($other, $description);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
[$error, $recodedValue] = Json::canonicalize($this->value);
|
|
|
92 |
|
|
|
93 |
if ($error) {
|
|
|
94 |
parent::fail($other, $description);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
$comparisonFailure = new ComparisonFailure(
|
|
|
98 |
json_decode($this->value),
|
|
|
99 |
json_decode($other),
|
|
|
100 |
Json::prettify($recodedValue),
|
|
|
101 |
Json::prettify($recodedOther),
|
|
|
102 |
false,
|
|
|
103 |
'Failed asserting that two json values are equal.'
|
|
|
104 |
);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
parent::fail($other, $description, $comparisonFailure);
|
|
|
108 |
}
|
|
|
109 |
}
|