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