HateoasPaginationTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 98
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A itSupportPaginationWhenThereAreNoData() 0 16 1
A itHavePaginationWhenLinkFieldIsNotEmpty() 0 27 1
A itNotHavePaginationWhenLinkFieldIsEmpty() 0 19 1
A itNotSupportPaginationWhenThereAreNotEmbeddedField() 0 15 1
A itNotSupportPaginationWhenThereAreNotLinkField() 0 10 1
A itNotSupportPaginationWhenThereAreNotLinks() 0 12 1
A dataProviderItNotSupportPaginationWhenThereAreNotLinks() 0 14 1
A itNotSupportPaginationWhenResponseIsEmpty() 0 5 1
1
<?php
2
3
namespace ElevenLabs\Api\Service\Pagination\Provider;
4
5
use ElevenLabs\Api\Definition\ResponseDefinition;
6
use ElevenLabs\Api\Service\Pagination\Pagination;
7
use ElevenLabs\Api\Service\Pagination\PaginationLinks;
8
use PHPUnit\Framework\TestCase;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Class HateoasPaginationTest
13
 *
14
 * @package ElevenLabs\Api\Service\Pagination\Provider
15
 */
16
class HateoasPaginationTest extends TestCase
17
{
18
    /**
19
     * @var ResponseInterface
20
     */
21
    private $response;
22
23
    /**
24
     * @var ResponseDefinition
25
     */
26
    private $responseDefinition;
27
28
    /**
29
     * @var array
30
     */
31
    private $fields;
32
33
    public function setUp()
34
    {
35
        $this->response = $this->createMock(ResponseInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...sponseInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Psr\Http\Message\ResponseInterface of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->responseDefinition = $this->createMock(ResponseDefinition::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Eleven...ponseDefinition::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type ElevenLabs\Api\Definition\ResponseDefinition of property $responseDefinition.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->fields = [
38
            HateoasPagination::DEFAULT_PAGINATION_VALUE['page'] => "page",
39
            HateoasPagination::DEFAULT_PAGINATION_VALUE['perPage'] => 'perPage',
40
            HateoasPagination::DEFAULT_PAGINATION_VALUE['totalItems'] => 'totalItems',
41
            HateoasPagination::DEFAULT_PAGINATION_VALUE['totalPages'] => 'totalPages'
42
        ];
43
    }
44
45
    /** @test */
46
    public function itNotHavePaginationWhenLinkFieldIsEmpty()
47
    {
48
        $data = [
49
            'page' => 1,
50
            'perPage' => 10,
51
            'totalItems' => 20,
52
            'totalPages' => 2,
53
            '_links' => [],
54
            '_embedded' => ['item' => []],
55
        ];
56
        $provider = new HateoasPagination($this->fields);
57
        $pagination = $provider->getPagination($data, $this->response, $this->responseDefinition);
58
        $this->assertInstanceOf(Pagination::class, $pagination);
59
        $this->assertEquals(1, $pagination->getPage());
60
        $this->assertEquals(10, $pagination->getPerPage());
61
        $this->assertEquals(20, $pagination->getTotalItems());
62
        $this->assertEquals(2, $pagination->getTotalPages());
63
        $this->assertNull($pagination->getLinks());
64
        $this->assertEquals([], $data);
65
    }
66
67
    /** @test */
68
    public function itHavePaginationWhenLinkFieldIsNotEmpty()
69
    {
70
        $data = [
71
            'page' => 1,
72
            'perPage' => 10,
73
            'totalItems' => 20,
74
            'totalPages' => 2,
75
            '_links' => [
76
                'self' => ['href' => 'http://example.org/self'],
77
                'first' => ['href' => 'http://example.org/first'],
78
                'last' => ['href' => 'http://example.org/last'],
79
            ],
80
            '_embedded' => ['item' => []],
81
        ];
82
        $provider = new HateoasPagination($this->fields);
83
        $pagination = $provider->getPagination($data, $this->response, $this->responseDefinition);
84
        $this->assertInstanceOf(Pagination::class, $pagination);
85
        $this->assertEquals(1, $pagination->getPage());
86
        $this->assertEquals(10, $pagination->getPerPage());
87
        $this->assertEquals(20, $pagination->getTotalItems());
88
        $this->assertEquals(2, $pagination->getTotalPages());
89
        $this->assertInstanceOf(PaginationLinks::class, $pagination->getLinks());
90
        $this->assertEquals('http://example.org/first', $pagination->getLinks()->getFirst());
91
        $this->assertEquals('http://example.org/last', $pagination->getLinks()->getLast());
92
        $this->assertFalse($pagination->getLinks()->hasNext());
93
        $this->assertFalse($pagination->getLinks()->hasPrev());
94
        $this->assertEquals([], $data);
95
    }
96
97
    /** @test */
98
    public function itNotSupportPaginationWhenResponseIsEmpty()
99
    {
100
        $data = [];
101
        $provider = new HateoasPagination($this->fields);
102
        $this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition));
103
    }
104
105
    /** @test */
106
    public function itNotSupportPaginationWhenThereAreNotLinkField()
107
    {
108
        $data = [
109
            'page' => 1,
110
            'perPage' => 10,
111
            'totalItems' => 20,
112
            'totalPages' => 2,
113
        ];
114
        $provider = new HateoasPagination($this->fields);
115
        $this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition));
116
    }
117
118
    /**
119
     * @param array $links
120
     *
121
     * @dataProvider dataProviderItNotSupportPaginationWhenThereAreNotLinks
122
     * @test
123
     */
124
    public function itNotSupportPaginationWhenThereAreNotLinks(array $links)
125
    {
126
        $data = [
127
            'page' => 1,
128
            'perPage' => 10,
129
            'totalItems' => 20,
130
            'totalPages' => 2,
131
            '_links' => $links,
132
            '_embedded' => ['item' => []],
133
        ];
134
        $provider = new HateoasPagination($this->fields);
135
        $this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition));
136
    }
137
138
    /** @test */
139
    public function itNotSupportPaginationWhenThereAreNotEmbeddedField()
140
    {
141
        $data = [
142
            'page' => 1,
143
            'perPage' => 10,
144
            'totalItems' => 20,
145
            'totalPages' => 2,
146
            '_links' => [
147
                'self' => ['href' => 'http://example.org'],
148
                'first' => ['href' => 'http://example.org'],
149
                'last' => ['href' => 'http://example.org'],
150
            ],
151
        ];
152
        $provider = new HateoasPagination($this->fields);
153
        $this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition));
154
    }
155
156
    /** @test */
157
    public function itSupportPaginationWhenThereAreNoData()
158
    {
159
        $data = [
160
            'page' => 1,
161
            'perPage' => 10,
162
            'totalItems' => 20,
163
            'totalPages' => 2,
164
            '_links' => [
165
                'self' => ['href' => 'http://example.org'],
166
                'first' => ['href' => 'http://example.org'],
167
                'last' => ['href' => 'http://example.org'],
168
            ],
169
            '_embedded' => ['item' => []],
170
        ];
171
        $provider = new HateoasPagination($this->fields);
172
        $this->assertTrue($provider->supportPagination($data, $this->response, $this->responseDefinition));
173
    }
174
175
    public function dataProviderItNotSupportPaginationWhenThereAreNotLinks()
176
    {
177
        return [
178
            [
179
                [
180
                    'first' => ['href' => 'http://example.org'],
181
                    'last' => ['href' => 'http://example.org'],
182
                ],
183
                [
184
                    'self' => ['href' => 'http://example.org'],
185
                    'last' => ['href' => 'http://example.org'],
186
                ],
187
                [
188
                    'last' => ['href' => 'http://example.org'],
189
                ]
190
            ]
191
        ];
192
    }
193
}
194