forked from martin-helmich/phpunit-json-assert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonValueMatchesMany.php
More file actions
88 lines (78 loc) · 2.29 KB
/
JsonValueMatchesMany.php
File metadata and controls
88 lines (78 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace Sid\JsonAssert\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
/**
* Constraint that asserts that a JSON document matches an entire set of JSON
* value constraints.
*
* @package Sid\JsonAssert
* @subpackage Constraint
*/
class JsonValueMatchesMany extends Constraint
{
/** @var JsonValueMatches[] */
private $constraints = array();
/**
* JsonValueMatchesMany constructor.
*
* @param array $constraints A set of constraints. This is a key-value map
* where each key is a JSON path expression,
* associated with a constraint that all values
* matched by that expression must fulfill.
*/
public function __construct(array $constraints)
{
foreach ($constraints as $key => $constraint) {
if (!$constraint instanceof Constraint) {
$constraint = new IsEqual($constraint);
}
$this->constraints[] = new JsonValueMatches($key, $constraint);
}
}
/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString(): string
{
return implode(
' and ',
array_map(
function (Constraint $constraint) {
return $constraint->toString();
},
$this->constraints
)
);
}
/**
* @inheritdoc
*/
protected function matches($other): bool
{
foreach ($this->constraints as $constraint) {
if (!$constraint->evaluate($other, '', true)) {
return false;
}
}
return true;
}
/**
* Returns a string representation of matches that evaluate to false.
*
* @return string
*/
protected function additionalFailureDescription($other): string
{
/** @var string[] */
$failedConstraints = array();
foreach ($this->constraints as $constraint) {
if (!$constraint->evaluate($other, '', true)) {
$failedConstraints[] = $constraint->toString();
}
}
return "\n" . implode("\n", $failedConstraints);
}
}