1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ElevenLabs\Api\Service\Tests\Pagination\Provider; |
6
|
|
|
|
7
|
|
|
use ElevenLabs\Api\Definition\ResponseDefinition; |
8
|
|
|
use ElevenLabs\Api\Service\Pagination\Pagination; |
9
|
|
|
use ElevenLabs\Api\Service\Pagination\PaginationLinks; |
10
|
|
|
use ElevenLabs\Api\Service\Pagination\Provider\HateoasPagination; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class HateoasPaginationTest. |
16
|
|
|
*/ |
17
|
|
|
class HateoasPaginationTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ResponseInterface |
21
|
|
|
*/ |
22
|
|
|
private $response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ResponseDefinition |
26
|
|
|
*/ |
27
|
|
|
private $responseDefinition; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $fields; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->response = $this->createMock(ResponseInterface::class); |
|
|
|
|
37
|
|
|
$this->responseDefinition = $this->createMock(ResponseDefinition::class); |
|
|
|
|
38
|
|
|
$this->fields = [ |
39
|
|
|
HateoasPagination::DEFAULT_PAGINATION_VALUE['page'] => "page", |
40
|
|
|
HateoasPagination::DEFAULT_PAGINATION_VALUE['perPage'] => 'perPage', |
41
|
|
|
HateoasPagination::DEFAULT_PAGINATION_VALUE['totalItems'] => 'totalItems', |
42
|
|
|
HateoasPagination::DEFAULT_PAGINATION_VALUE['totalPages'] => 'totalPages', |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
* |
49
|
|
|
* @dataProvider dataProviderItNotHavePaginationWhenLinkFieldIsEmpty |
50
|
|
|
* |
51
|
|
|
* @param array $data |
52
|
|
|
*/ |
53
|
|
|
public function itNotHavePaginationWhenLinkFieldIsEmpty(array $data) |
54
|
|
|
{ |
55
|
|
|
$provider = new HateoasPagination($this->fields); |
56
|
|
|
$pagination = $provider->getPagination($data, $this->response, $this->responseDefinition); |
57
|
|
|
|
58
|
|
|
$this->assertInstanceOf(Pagination::class, $pagination); |
59
|
|
|
$this->assertSame(1, $pagination->getPage()); |
60
|
|
|
$this->assertSame(10, $pagination->getPerPage()); |
61
|
|
|
$this->assertSame(20, $pagination->getTotalItems()); |
62
|
|
|
$this->assertSame(2, $pagination->getTotalPages()); |
63
|
|
|
$this->assertNull($pagination->getLinks()); |
64
|
|
|
$this->assertEquals([], $data); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function dataProviderItNotHavePaginationWhenLinkFieldIsEmpty(): array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
[ |
74
|
|
|
[ |
75
|
|
|
'page' => 1, |
76
|
|
|
'perPage' => 10, |
77
|
|
|
'totalItems' => 20, |
78
|
|
|
'totalPages' => 2, |
79
|
|
|
'_links' => [], |
80
|
|
|
'_embedded' => ['item' => []], |
81
|
|
|
], |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
'page' => '1', |
86
|
|
|
'perPage' => '10', |
87
|
|
|
'totalItems' => '20', |
88
|
|
|
'totalPages' => '2', |
89
|
|
|
'_links' => [], |
90
|
|
|
'_embedded' => ['item' => []], |
91
|
|
|
], |
92
|
|
|
], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
* |
99
|
|
|
* @dataProvider dataProviderItHavePaginationWhenLinkFieldIsNotEmpty |
100
|
|
|
* |
101
|
|
|
* @param array $data |
102
|
|
|
*/ |
103
|
|
|
public function itHavePaginationWhenLinkFieldIsNotEmpty(array $data) |
104
|
|
|
{ |
105
|
|
|
$provider = new HateoasPagination($this->fields); |
106
|
|
|
$pagination = $provider->getPagination($data, $this->response, $this->responseDefinition); |
107
|
|
|
$this->assertInstanceOf(Pagination::class, $pagination); |
108
|
|
|
$this->assertSame(1, $pagination->getPage()); |
109
|
|
|
$this->assertSame(10, $pagination->getPerPage()); |
110
|
|
|
$this->assertSame(20, $pagination->getTotalItems()); |
111
|
|
|
$this->assertSame(2, $pagination->getTotalPages()); |
112
|
|
|
$this->assertInstanceOf(PaginationLinks::class, $pagination->getLinks()); |
113
|
|
|
$this->assertSame('http://example.org/first', $pagination->getLinks()->getFirst()); |
114
|
|
|
$this->assertSame('http://example.org/last', $pagination->getLinks()->getLast()); |
115
|
|
|
$this->assertFalse($pagination->getLinks()->hasNext()); |
116
|
|
|
$this->assertFalse($pagination->getLinks()->hasPrev()); |
117
|
|
|
$this->assertSame([], $data); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function dataProviderItHavePaginationWhenLinkFieldIsNotEmpty(): array |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
[ |
124
|
|
|
[ |
125
|
|
|
'page' => 1, |
126
|
|
|
'perPage' => 10, |
127
|
|
|
'totalItems' => 20, |
128
|
|
|
'totalPages' => 2, |
129
|
|
|
'_links' => [ |
130
|
|
|
'self' => ['href' => 'http://example.org/self'], |
131
|
|
|
'first' => ['href' => 'http://example.org/first'], |
132
|
|
|
'last' => ['href' => 'http://example.org/last'], |
133
|
|
|
], |
134
|
|
|
'_embedded' => ['item' => []], |
135
|
|
|
], |
136
|
|
|
], |
137
|
|
|
[ |
138
|
|
|
[ |
139
|
|
|
'page' => '1', |
140
|
|
|
'perPage' => '10', |
141
|
|
|
'totalItems' => '20', |
142
|
|
|
'totalPages' => '2', |
143
|
|
|
'_links' => [ |
144
|
|
|
'self' => ['href' => 'http://example.org/self'], |
145
|
|
|
'first' => ['href' => 'http://example.org/first'], |
146
|
|
|
'last' => ['href' => 'http://example.org/last'], |
147
|
|
|
], |
148
|
|
|
'_embedded' => ['item' => []], |
149
|
|
|
], |
150
|
|
|
], |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** @test */ |
155
|
|
|
public function itNotSupportPaginationWhenResponseIsEmpty() |
156
|
|
|
{ |
157
|
|
|
$data = []; |
158
|
|
|
$provider = new HateoasPagination($this->fields); |
159
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** @test */ |
163
|
|
|
public function itNotSupportPaginationWhenThereAreNotLinkField() |
164
|
|
|
{ |
165
|
|
|
$data = [ |
166
|
|
|
'page' => 1, |
167
|
|
|
'perPage' => 10, |
168
|
|
|
'totalItems' => 20, |
169
|
|
|
'totalPages' => 2, |
170
|
|
|
]; |
171
|
|
|
$provider = new HateoasPagination($this->fields); |
172
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** @test */ |
176
|
|
|
public function itNotSupportPaginationWhenThereAreNotPageField() |
177
|
|
|
{ |
178
|
|
|
$data = [ |
179
|
|
|
'perPage' => 10, |
180
|
|
|
'totalItems' => 20, |
181
|
|
|
'totalPages' => 2, |
182
|
|
|
'_links' => [ |
183
|
|
|
'self' => ['href' => 'http://example.org/self'], |
184
|
|
|
'first' => ['href' => 'http://example.org/first'], |
185
|
|
|
'last' => ['href' => 'http://example.org/last'], |
186
|
|
|
], |
187
|
|
|
'_embedded' => ['item' => []], |
188
|
|
|
]; |
189
|
|
|
$provider = new HateoasPagination($this->fields); |
190
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @test |
195
|
|
|
* |
196
|
|
|
* @param array $links |
197
|
|
|
* |
198
|
|
|
* @dataProvider dataProviderItNotSupportPaginationWhenThereAreNotLinks |
199
|
|
|
*/ |
200
|
|
|
public function itNotSupportPaginationWhenThereAreNotLinks(array $links) |
201
|
|
|
{ |
202
|
|
|
$data = [ |
203
|
|
|
'page' => 1, |
204
|
|
|
'perPage' => 10, |
205
|
|
|
'totalItems' => 20, |
206
|
|
|
'totalPages' => 2, |
207
|
|
|
'_links' => $links, |
208
|
|
|
'_embedded' => ['item' => []], |
209
|
|
|
]; |
210
|
|
|
$provider = new HateoasPagination($this->fields); |
211
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** @test */ |
215
|
|
|
public function itNotSupportPaginationWhenThereAreNotEmbeddedField() |
216
|
|
|
{ |
217
|
|
|
$data = [ |
218
|
|
|
'page' => 1, |
219
|
|
|
'perPage' => 10, |
220
|
|
|
'totalItems' => 20, |
221
|
|
|
'totalPages' => 2, |
222
|
|
|
'_links' => [ |
223
|
|
|
'self' => ['href' => 'http://example.org'], |
224
|
|
|
'first' => ['href' => 'http://example.org'], |
225
|
|
|
'last' => ['href' => 'http://example.org'], |
226
|
|
|
], |
227
|
|
|
]; |
228
|
|
|
$provider = new HateoasPagination($this->fields); |
229
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** @test */ |
233
|
|
|
public function itSupportPaginationWhenThereAreNoData() |
234
|
|
|
{ |
235
|
|
|
$data = [ |
236
|
|
|
'page' => 1, |
237
|
|
|
'perPage' => 10, |
238
|
|
|
'totalItems' => 20, |
239
|
|
|
'totalPages' => 2, |
240
|
|
|
'_links' => [ |
241
|
|
|
'self' => ['href' => 'http://example.org'], |
242
|
|
|
'first' => ['href' => 'http://example.org'], |
243
|
|
|
'last' => ['href' => 'http://example.org'], |
244
|
|
|
], |
245
|
|
|
'_embedded' => ['item' => []], |
246
|
|
|
]; |
247
|
|
|
$provider = new HateoasPagination($this->fields); |
248
|
|
|
$this->assertTrue($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function dataProviderItNotSupportPaginationWhenThereAreNotLinks(): array |
252
|
|
|
{ |
253
|
|
|
return [ |
254
|
|
|
[ |
255
|
|
|
[ |
256
|
|
|
'first' => ['href' => 'http://example.org'], |
257
|
|
|
'last' => ['href' => 'http://example.org'], |
258
|
|
|
], |
259
|
|
|
[ |
260
|
|
|
'self' => ['href' => 'http://example.org'], |
261
|
|
|
'last' => ['href' => 'http://example.org'], |
262
|
|
|
], |
263
|
|
|
[ |
264
|
|
|
'last' => ['href' => 'http://example.org'], |
265
|
|
|
], |
266
|
|
|
], |
267
|
|
|
]; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
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..