Completed
Push — master ( 9b2fdc...4fe6b6 )
by Vincent
03:18 queued 22s
created

AssertPagination::paginationLinksEqualConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 24
    private static function allowedMembers(): array
23
    {
24
        return [
25 24
            Members::LINK_PAGINATION_FIRST,
26 24
            Members::LINK_PAGINATION_LAST,
27 24
            Members::LINK_PAGINATION_PREV,
28 24
            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\ExpectationFailedException
41
     */
42 6
    public static function assertHasPaginationLinks($links): void
43
    {
44 6
        static::assertContainsAtLeastOneMember(
45 6
            static::allowedMembers(),
46 2
            $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\ExpectationFailedException
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\ExpectationFailedException
77
     */
78 12
    public static function assertPaginationLinksEquals($expected, $json): void
79
    {
80 12
        PHPUnit::assertThat(
81 12
            $json,
82 12
            self::paginationLinksEqualConstraint($expected),
83 12
            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\ExpectationFailedException
96
     */
97
    public static function assertHasPaginationMeta($meta): void
98
    {
99
        static::assertHasMember(Members::META_PAGINATION, $meta);
100
    }
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\ExpectationFailedException
111
     */
112
    public static function assertHasNoPaginationMeta($meta): void
113
    {
114
        static::assertNotHasMember(Members::META_PAGINATION, $meta);
115
    }
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\ExpectationFailedException
127
     */
128
    public static function assertPaginationMetaEquals($expected, $json): void
129
    {
130
        PHPUnit::assertEquals($expected, $json, Messages::PAGINATION_META_NOT_EQUAL);
131
    }
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 12
    private static function paginationLinksEqualConstraint($expected): PaginationLinksEqualConstraint
141
    {
142 12
        return new PaginationLinksEqualConstraint($expected, static::allowedMembers());
143
    }
144
}
145