|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Constraint; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A constraint class to assert that a link object equals an expected value. |
|
9
|
|
|
*/ |
|
10
|
|
|
class PaginationLinksEqualConstraint extends AbstractConstraint |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Undocumented variable |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $expected; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The list of the allowed link names for pagination |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $allowedMembers; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $expected |
|
30
|
|
|
* @param array $allowedMembers |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
21 |
|
public function __construct($expected, $allowedMembers) |
|
35
|
|
|
{ |
|
36
|
21 |
|
$this->expected = $expected; |
|
37
|
21 |
|
$this->allowedMembers = $allowedMembers; |
|
38
|
21 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns a string representation of the constraint. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
6 |
|
public function toString(): string |
|
46
|
|
|
{ |
|
47
|
6 |
|
return \sprintf( |
|
48
|
6 |
|
'equals %s', |
|
49
|
6 |
|
$this->exporter()->export($this->expected) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Evaluates the constraint for parameter $other. Returns true if the |
|
55
|
|
|
* constraint is met, false otherwise. |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $other value or object to evaluate |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
18 |
|
protected function matches($other): bool |
|
62
|
|
|
{ |
|
63
|
|
|
// Add missing members with false value |
|
64
|
18 |
|
$cleanExpected = array_merge( |
|
65
|
18 |
|
array_fill_keys($this->allowedMembers, false), |
|
66
|
18 |
|
$this->expected |
|
67
|
|
|
); |
|
68
|
18 |
|
asort($cleanExpected); |
|
69
|
|
|
|
|
70
|
|
|
// Extract only pagination members from incoming json |
|
71
|
18 |
|
$cleanJson = array_intersect_key($other, array_flip($this->allowedMembers)); |
|
72
|
18 |
|
asort($cleanJson); |
|
73
|
|
|
|
|
74
|
|
|
// Search for unexpected members |
|
75
|
18 |
|
$notExpectedMembers = array_keys( |
|
76
|
6 |
|
array_filter( |
|
77
|
18 |
|
$cleanExpected, |
|
78
|
|
|
/** |
|
79
|
|
|
* @param mixed $value |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
18 |
|
function ($value) { |
|
84
|
18 |
|
return $value === false; |
|
85
|
18 |
|
} |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
18 |
|
if (count(array_intersect_key($cleanJson, array_flip($notExpectedMembers))) !== 0) { |
|
89
|
6 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Extracts expected members |
|
93
|
12 |
|
$expectedMembers = array_filter( |
|
94
|
12 |
|
$cleanExpected, |
|
95
|
|
|
/** |
|
96
|
|
|
* @param mixed $value |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
12 |
|
function ($value) { |
|
101
|
12 |
|
return $value !== false; |
|
102
|
12 |
|
} |
|
103
|
|
|
); |
|
104
|
12 |
|
if (array_keys($expectedMembers) != array_keys($cleanJson)) { |
|
105
|
3 |
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Extracts members whose value have to be tested |
|
109
|
9 |
|
$expectedValues = array_filter( |
|
110
|
9 |
|
$expectedMembers, |
|
111
|
|
|
/** |
|
112
|
|
|
* @param mixed $value |
|
113
|
|
|
* |
|
114
|
|
|
* @return bool |
|
115
|
|
|
*/ |
|
116
|
9 |
|
function ($value) { |
|
117
|
9 |
|
return $value !== true; |
|
118
|
9 |
|
} |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
9 |
|
foreach ($expectedValues as $name => $expectedLink) { |
|
122
|
9 |
|
$constraint = new LinkEqualsConstraint($expectedLink); |
|
123
|
9 |
|
if ($constraint->check($cleanJson[$name]) === false) { |
|
124
|
3 |
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
6 |
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|