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\Members; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This trait adds the ability to test pagination (links and meta). |
13
|
|
|
*/ |
14
|
|
|
trait AssertPagination |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Gets the list of allowed members for pagination links |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
4 |
|
private static function allowedMembers(): array |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
4 |
|
Members::LINK_PAGINATION_FIRST, |
25
|
4 |
|
Members::LINK_PAGINATION_LAST, |
26
|
4 |
|
Members::LINK_PAGINATION_PREV, |
27
|
4 |
|
Members::LINK_PAGINATION_NEXT |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Asserts that a links object has pagination links. |
33
|
|
|
* |
34
|
|
|
* @link https://jsonapi.org/format/#document-links |
35
|
|
|
* |
36
|
|
|
* @param array $links A links object to inspect |
37
|
|
|
* |
38
|
2 |
|
* @return void |
39
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
40
|
2 |
|
*/ |
41
|
2 |
|
public static function assertHasPaginationLinks($links): void |
42
|
2 |
|
{ |
43
|
|
|
static::assertContainsAtLeastOneMember( |
44
|
1 |
|
static::allowedMembers(), |
45
|
|
|
$links |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Asserts that a links object has no pagination links. |
51
|
|
|
* |
52
|
|
|
* @link https://jsonapi.org/format/#document-links |
53
|
2 |
|
* |
54
|
|
|
* @param array $links A links object to inspect |
55
|
2 |
|
* |
56
|
2 |
|
* @return void |
57
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
58
|
1 |
|
*/ |
59
|
|
|
public static function assertHasNoPaginationLinks($links): void |
60
|
|
|
{ |
61
|
|
|
foreach (static::allowedMembers() as $name) { |
62
|
|
|
static::assertNotHasMember($name, $links); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Asserts that a links object has the expected pagination links. |
68
|
4 |
|
* |
69
|
|
|
* @link https://jsonapi.org/format/#document-links |
70
|
4 |
|
* |
71
|
1 |
|
* @param array $expected The expected links object |
72
|
|
|
* @param array $json The links object to test |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
76
|
|
|
*/ |
77
|
|
|
public static function assertPaginationLinksEquals($expected, $json): void |
78
|
|
|
{ |
79
|
|
|
PHPUnit::assertThat($json, self::paginationLinksEqualConstraint($expected)); |
80
|
4 |
|
} |
81
|
|
|
|
82
|
4 |
|
/** |
83
|
|
|
* Asserts that a meta object has no "pagination" member. |
84
|
|
|
* |
85
|
|
|
* @link https://jsonapi.org/format/#document-meta |
86
|
|
|
* |
87
|
|
|
* @param array $meta The meta object to inspect |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
91
|
|
|
*/ |
92
|
|
|
public static function assertHasNoPaginationMeta($meta): void |
93
|
|
|
{ |
94
|
|
|
static::assertNotHasMember(Members::META_PAGINATION, $meta); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint class. |
99
|
|
|
* |
100
|
|
|
* @param array $expected The expected links |
101
|
|
|
* |
102
|
|
|
* @return \VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint |
103
|
|
|
*/ |
104
|
|
|
private static function paginationLinksEqualConstraint($expected): PaginationLinksEqualConstraint |
105
|
|
|
{ |
106
|
|
|
return new PaginationLinksEqualConstraint($expected, static::allowedMembers()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|