|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Asserts\Content; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
|
8
|
|
|
use VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint; |
|
9
|
|
|
use VGirol\JsonApiAssert\Messages; |
|
10
|
|
|
use VGirol\JsonApiConstant\Members; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This trait adds the ability to test pagination (links and meta). |
|
14
|
|
|
*/ |
|
15
|
|
|
trait AssertPagination |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Gets the list of allowed members for pagination links |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
18 |
|
private static function allowedMembers(): array |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
18 |
|
Members::LINK_PAGINATION_FIRST, |
|
26
|
|
|
Members::LINK_PAGINATION_LAST, |
|
27
|
|
|
Members::LINK_PAGINATION_PREV, |
|
28
|
|
|
Members::LINK_PAGINATION_NEXT |
|
29
|
|
|
]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Asserts that a links object has pagination links. |
|
34
|
|
|
* |
|
35
|
|
|
* @link https://jsonapi.org/format/#document-links |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $links A links object to inspect |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
41
|
|
|
*/ |
|
42
|
6 |
|
public static function assertHasPaginationLinks($links): void |
|
43
|
|
|
{ |
|
44
|
6 |
|
static::assertContainsAtLeastOneMember( |
|
45
|
6 |
|
static::allowedMembers(), |
|
46
|
|
|
$links |
|
47
|
|
|
); |
|
48
|
3 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Asserts that a links object has no pagination links. |
|
52
|
|
|
* |
|
53
|
|
|
* @link https://jsonapi.org/format/#document-links |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $links A links object to inspect |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public static function assertHasNoPaginationLinks($links): void |
|
61
|
|
|
{ |
|
62
|
6 |
|
foreach (static::allowedMembers() as $name) { |
|
63
|
6 |
|
static::assertNotHasMember($name, $links); |
|
64
|
|
|
} |
|
65
|
3 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Asserts that a links object has the expected pagination links. |
|
69
|
|
|
* |
|
70
|
|
|
* @link https://jsonapi.org/format/#document-links |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $expected The expected links object |
|
73
|
|
|
* @param array $json The links object to test |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
77
|
|
|
*/ |
|
78
|
6 |
|
public static function assertPaginationLinksEquals($expected, $json): void |
|
79
|
|
|
{ |
|
80
|
6 |
|
PHPUnit::assertThat( |
|
81
|
6 |
|
$json, |
|
82
|
6 |
|
self::paginationLinksEqualConstraint($expected), |
|
83
|
6 |
|
Messages::PAGINATION_LINKS_NOT_EQUAL |
|
84
|
|
|
); |
|
85
|
3 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Asserts that a meta object has a "pagination" member. |
|
89
|
|
|
* |
|
90
|
|
|
* @link https://jsonapi.org/format/#document-meta |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $meta The meta object to inspect |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
96
|
|
|
*/ |
|
97
|
6 |
|
public static function assertHasPaginationMeta($meta): void |
|
98
|
|
|
{ |
|
99
|
6 |
|
static::assertHasMember(Members::META_PAGINATION, $meta); |
|
100
|
3 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Asserts that a meta object has no "pagination" member. |
|
104
|
|
|
* |
|
105
|
|
|
* @link https://jsonapi.org/format/#document-meta |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $meta The meta object to inspect |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
111
|
|
|
*/ |
|
112
|
6 |
|
public static function assertHasNoPaginationMeta($meta): void |
|
113
|
|
|
{ |
|
114
|
6 |
|
static::assertNotHasMember(Members::META_PAGINATION, $meta); |
|
115
|
3 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Asserts that a meta object has the expected pagination member. |
|
119
|
|
|
* |
|
120
|
|
|
* @link https://jsonapi.org/format/#document-links |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $expected The expected pagination meta object |
|
123
|
|
|
* @param array $json The meta object to test |
|
124
|
|
|
* |
|
125
|
|
|
* @return void |
|
126
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
127
|
|
|
*/ |
|
128
|
6 |
|
public static function assertPaginationMetaEquals($expected, $json): void |
|
129
|
|
|
{ |
|
130
|
6 |
|
PHPUnit::assertEquals($expected, $json, Messages::PAGINATION_META_NOT_EQUAL); |
|
131
|
3 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint class. |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $expected The expected links |
|
137
|
|
|
* |
|
138
|
|
|
* @return \VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint |
|
139
|
|
|
*/ |
|
140
|
6 |
|
private static function paginationLinksEqualConstraint($expected): PaginationLinksEqualConstraint |
|
141
|
|
|
{ |
|
142
|
6 |
|
return new PaginationLinksEqualConstraint($expected, static::allowedMembers()); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|