|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Constraint; |
|
6
|
|
|
|
|
7
|
|
|
use VGirol\JsonApiConstant\Members; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A constraint class to assert that a link object equals an expected value. |
|
11
|
|
|
*/ |
|
12
|
|
|
class LinkEqualsConstraint extends AbstractConstraint |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Undocumented variable |
|
16
|
|
|
* |
|
17
|
|
|
* @var array|string|null |
|
18
|
|
|
*/ |
|
19
|
|
|
private $expected; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array|string|null $expected |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
63 |
|
public function __construct($expected) |
|
29
|
|
|
{ |
|
30
|
63 |
|
$this->expected = $expected; |
|
31
|
63 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Returns a string representation of the constraint. |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
12 |
|
public function toString(): string |
|
39
|
|
|
{ |
|
40
|
12 |
|
return \sprintf( |
|
41
|
12 |
|
'equals %s', |
|
42
|
12 |
|
$this->exporter()->export($this->expected) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Evaluates the constraint for parameter $other. Returns true if the |
|
48
|
|
|
* constraint is met, false otherwise. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array|string|null $other value or object to evaluate |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
60 |
|
protected function matches($other): bool |
|
55
|
|
|
{ |
|
56
|
60 |
|
if ($this->expected === null) { |
|
57
|
9 |
|
return ($other === null); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
51 |
|
if ($other === null) { |
|
61
|
3 |
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @var string $href */ |
|
65
|
48 |
|
$href = \is_array($other) && isset($other[Members::LINK_HREF]) ? $other[Members::LINK_HREF] : $other; |
|
66
|
|
|
|
|
67
|
|
|
/** @var string $expectedHref */ |
|
68
|
48 |
|
$expectedHref = \is_array($this->expected) && isset($this->expected[Members::LINK_HREF]) ? |
|
69
|
48 |
|
$this->expected[Members::LINK_HREF] : $this->expected; |
|
70
|
|
|
|
|
71
|
48 |
|
$linkElms = explode('?', $href); |
|
72
|
48 |
|
$expectedElms = explode('?', $expectedHref); |
|
73
|
|
|
|
|
74
|
48 |
|
if (count($expectedElms) != count($linkElms)) { |
|
75
|
6 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
42 |
|
if ($expectedElms[0] != $linkElms[0]) { |
|
79
|
12 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
30 |
|
if (count($linkElms) == 1) { |
|
83
|
21 |
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
9 |
|
$expectedQuery = explode('&', $expectedElms[1]); |
|
87
|
9 |
|
$linkQuery = explode('&', $linkElms[1]); |
|
88
|
|
|
|
|
89
|
9 |
|
if (count($expectedQuery) != count($linkQuery)) { |
|
90
|
3 |
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
6 |
|
$diff = array_diff($expectedQuery, $linkQuery); |
|
94
|
|
|
|
|
95
|
6 |
|
return count($diff) === 0; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|