AssertPagination   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 30
c 1
b 0
f 0
dl 0
loc 122
ccs 40
cts 40
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A assertResponseHasPagination() 0 4 1
A assertResponseHasNoPaginationMeta() 0 13 2
A assertResponseHasPaginationLinks() 0 8 1
A assertResponseHasPaginationMeta() 0 15 1
A assertResponseHasNoPaginationLinks() 0 13 2
A assertResponseHasNoPagination() 0 4 1
1
<?php
2
3
namespace VGirol\JsonApiAssert\Laravel\Asserts\Content;
4
5
use Illuminate\Testing\TestResponse;
6
use PHPUnit\Framework\Assert as PHPUnit;
7
use VGirol\JsonApiAssert\Laravel\Messages;
8
use VGirol\JsonApiConstant\Members;
9
10
/**
11
 * This trait adds the ability to test pagination informations (links and meta).
12
 */
13
trait AssertPagination
14
{
15
    /**
16
     * Asserts that a document have no pagination links.
17
     *
18
     * @param TestResponse $response
19
     *
20
     * @return void
21
     * @throws \PHPUnit\Framework\AssertionFailedError
22
     */
23 24
    public static function assertResponseHasNoPaginationLinks(TestResponse $response): void
24
    {
25
        // Decode JSON response
26 24
        $json = $response->json();
27
28 24
        if (!isset($json[Members::LINKS])) {
29 3
            static::assertNotHasMember(Members::LINKS, $json);
30
31 3
            return;
32
        }
33
34 21
        $links = $json[Members::LINKS];
35 21
        static::assertHasNoPaginationLinks($links);
36 15
    }
37
38
    /**
39
     * Asserts that a document have the expected pagination links.
40
     *
41
     * @param TestResponse $response
42
     * @param array        $expected
43
     *
44
     * @return void
45
     * @throws \PHPUnit\Framework\AssertionFailedError
46
     */
47 24
    public static function assertResponseHasPaginationLinks(TestResponse $response, $expected)
48
    {
49
        // Decode JSON response
50 24
        $json = $response->json();
51
52 24
        static::assertHasLinks($json);
53 21
        $links = $json[Members::LINKS];
54 21
        static::assertPaginationLinksEquals($expected, $links);
55 12
    }
56
57
    /**
58
     * Asserts that a document have no pagination meta.
59
     *
60
     * @param TestResponse $response
61
     *
62
     * @return void
63
     * @throws \PHPUnit\Framework\AssertionFailedError
64
     */
65 21
    public static function assertResponseHasNoPaginationMeta(TestResponse $response): void
66
    {
67
        // Decode JSON response
68 21
        $json = $response->json();
69
70 21
        if (!isset($json[Members::META])) {
71 3
            static::assertNotHasMember(Members::META, $json);
72
73 3
            return;
74
        }
75
76 18
        $meta = $json[Members::META];
77 18
        static::assertHasNoPaginationMeta($meta);
78 9
    }
79
80
    /**
81
     * Asserts that a document have pagination meta.
82
     *
83
     * @param TestResponse $response
84
     * @param array        $expected
85
     *
86
     * @return void
87
     * @throws \PHPUnit\Framework\AssertionFailedError
88
     */
89 21
    public static function assertResponseHasPaginationMeta(TestResponse $response, $expected)
90
    {
91
        // Decode JSON response
92 21
        $json = $response->json();
93
94 21
        static::assertHasMeta($json);
95 18
        $meta = $json[Members::META];
96
97 18
        static::assertHasMember(Members::META_PAGINATION, $meta);
98
99 12
        $pagination = $meta[Members::META_PAGINATION];
100 12
        PHPUnit::assertEquals(
101 12
            $expected,
102 12
            $pagination,
103 12
            Messages::PAGINATION_META_IS_NOT_AS_EXPECTED
104
        );
105 9
    }
106
107
    /**
108
     * Asserts that a document have no pagination informations (links and meta).
109
     *
110
     * @param TestResponse $response
111
     *
112
     * @return void
113
     * @throws \PHPUnit\Framework\AssertionFailedError
114
     */
115 15
    public static function assertResponseHasNoPagination(TestResponse $response)
116
    {
117 15
        static::assertResponseHasNoPaginationLinks($response);
118 12
        static::assertResponseHasNoPaginationMeta($response);
119 6
    }
120
121
    /**
122
     * Asserts that a document have pagination informations (links and meta).
123
     *
124
     * @param TestResponse $response
125
     * @param array        $expectedLinks
126
     * @param array        $expectedMeta
127
     *
128
     * @return void
129
     * @throws \PHPUnit\Framework\AssertionFailedError
130
     */
131 15
    public static function assertResponseHasPagination(TestResponse $response, $expectedLinks, $expectedMeta)
132
    {
133 15
        static::assertResponseHasPaginationLinks($response, $expectedLinks);
134 9
        static::assertResponseHasPaginationMeta($response, $expectedMeta);
135 6
    }
136
}
137