|
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\HalProvider; |
|
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class HalProviderTest. |
|
17
|
|
|
*/ |
|
18
|
|
|
class HalProviderTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ResponseInterface|MockObject |
|
22
|
|
|
*/ |
|
23
|
|
|
private $response; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ResponseDefinition|MockObject |
|
27
|
|
|
*/ |
|
28
|
|
|
private $responseDefinition; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $config; |
|
34
|
|
|
|
|
35
|
|
|
public function setUp() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->response = $this->createMock(ResponseInterface::class); |
|
38
|
|
|
$this->responseDefinition = $this->createMock(ResponseDefinition::class); |
|
39
|
|
|
$this->config = [ |
|
40
|
|
|
'page' => '_links.self.href.page', |
|
41
|
|
|
'perPage' => 'itemsPerPage', |
|
42
|
|
|
'totalItems' => 'totalItems', |
|
43
|
|
|
'totalPages' => '_links.last.href.page', |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** @test */ |
|
48
|
|
|
public function itNotSupportPaginationWhenResponseIsEmpty() |
|
49
|
|
|
{ |
|
50
|
|
|
$data = []; |
|
51
|
|
|
$provider = new HalProvider($this->config); |
|
52
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @test */ |
|
56
|
|
|
public function itNotSupportPaginationWhenThereAreNotLinkField() |
|
57
|
|
|
{ |
|
58
|
|
|
$data = [ |
|
59
|
|
|
[ |
|
60
|
|
|
'_links' => [ |
|
61
|
|
|
'self' => ['href' => '/bar?page=1'], |
|
62
|
|
|
'first' => ['href' => '/bar?page=1'], |
|
63
|
|
|
'last' => ['href' => '/bar?page=2'], |
|
64
|
|
|
'next' => ['href' => '/bar?page=2'], |
|
65
|
|
|
], |
|
66
|
|
|
'itemsPerPage' => 10, |
|
67
|
|
|
'totalItems' => 20, |
|
68
|
|
|
'_embedded' => [ |
|
69
|
|
|
'item' => [ |
|
70
|
|
|
[ |
|
71
|
|
|
'_links' => ['self' => ['href' => '/bar/emilie-stroman']], |
|
72
|
|
|
'_embedded' => [ |
|
73
|
|
|
'category' => [ |
|
74
|
|
|
'_links' => ['self' => ['href' => '/foo/foo-1']], |
|
75
|
|
|
'title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', |
|
76
|
|
|
'slug' => 'lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit', |
|
77
|
|
|
'dateCreated' => "2019-03-29T21:58:14+00:00", |
|
78
|
|
|
'dateModified' => "2019-03-29T21:58:14+00:00", |
|
79
|
|
|
], |
|
80
|
|
|
], |
|
81
|
|
|
'nickName' => 'Emilie STROMAN', |
|
82
|
|
|
'slug' => 'emilie-stroman', |
|
83
|
|
|
'score' => 0, |
|
84
|
|
|
'dateCreated' => '2019-03-29T21:58:16+00:00', |
|
85
|
|
|
'dateModified' => '2019-03-29T22:40:56+00:00', |
|
86
|
|
|
], |
|
87
|
|
|
], |
|
88
|
|
|
], |
|
89
|
|
|
], |
|
90
|
|
|
]; |
|
91
|
|
|
$provider = new HalProvider($this->config); |
|
92
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @test |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $links |
|
99
|
|
|
* |
|
100
|
|
|
* @dataProvider dataProviderItNotSupportPaginationWhenThereAreNotLinks |
|
101
|
|
|
*/ |
|
102
|
|
|
public function itNotSupportPaginationWhenThereAreNotLinks(array $links) |
|
103
|
|
|
{ |
|
104
|
|
|
$data = [ |
|
105
|
|
|
'_links' => $links, |
|
106
|
|
|
'itemsPerPage' => 10, |
|
107
|
|
|
'totalItems' => 20, |
|
108
|
|
|
'_embedded' => ['item' => []], |
|
109
|
|
|
]; |
|
110
|
|
|
$provider = new HalProvider($this->config); |
|
111
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
public function dataProviderItNotSupportPaginationWhenThereAreNotLinks(): array |
|
118
|
|
|
{ |
|
119
|
|
|
return [ |
|
120
|
|
|
[ |
|
121
|
|
|
[ |
|
122
|
|
|
'first' => ['href' => 'http://example.org?page=1'], |
|
123
|
|
|
'last' => ['href' => 'http://example.org?page=2'], |
|
124
|
|
|
], |
|
125
|
|
|
], |
|
126
|
|
|
[ |
|
127
|
|
|
[ |
|
128
|
|
|
'self' => ['href' => 'http://example.org?page=1'], |
|
129
|
|
|
'first' => ['href' => 'http://example.org?page=1'], |
|
130
|
|
|
], |
|
131
|
|
|
], |
|
132
|
|
|
[ |
|
133
|
|
|
[ |
|
134
|
|
|
'last' => ['href' => 'http://example.org?page=2'], |
|
135
|
|
|
], |
|
136
|
|
|
], |
|
137
|
|
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** @test */ |
|
141
|
|
|
public function itNotSupportPaginationWhenThereAreNotEmbeddedField() |
|
142
|
|
|
{ |
|
143
|
|
|
$data = [ |
|
144
|
|
|
'_links' => [ |
|
145
|
|
|
'self' => ['href' => 'http://example.org?page=1'], |
|
146
|
|
|
'first' => ['href' => 'http://example.org?page=1'], |
|
147
|
|
|
'last' => ['href' => 'http://example.org?page=2'], |
|
148
|
|
|
], |
|
149
|
|
|
'itemsPerPage' => 10, |
|
150
|
|
|
'totalItems' => 20, |
|
151
|
|
|
]; |
|
152
|
|
|
|
|
153
|
|
|
$provider = new HalProvider($this->config); |
|
154
|
|
|
$this->assertFalse($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** @test */ |
|
158
|
|
|
public function itSupportPaginationWhenThereAreNoData() |
|
159
|
|
|
{ |
|
160
|
|
|
$data = [ |
|
161
|
|
|
'itemsPerPage' => 10, |
|
162
|
|
|
'totalItems' => 20, |
|
163
|
|
|
'_links' => [ |
|
164
|
|
|
'self' => ['href' => 'http://example.org?page=1'], |
|
165
|
|
|
'first' => ['href' => 'http://example.org?page=1'], |
|
166
|
|
|
'last' => ['href' => 'http://example.org?page=2'], |
|
167
|
|
|
], |
|
168
|
|
|
'_embedded' => ['item' => []], |
|
169
|
|
|
]; |
|
170
|
|
|
|
|
171
|
|
|
$provider = new HalProvider($this->config); |
|
172
|
|
|
$this->assertTrue($provider->supportPagination($data, $this->response, $this->responseDefinition)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @test |
|
177
|
|
|
* |
|
178
|
|
|
* @dataProvider dataProviderItHavePagination |
|
179
|
|
|
* |
|
180
|
|
|
* @param array $data |
|
181
|
|
|
* @param array $expected |
|
182
|
|
|
*/ |
|
183
|
|
|
public function itHavePagination(array $data, array $expected) |
|
184
|
|
|
{ |
|
185
|
|
|
$provider = new HalProvider($this->config); |
|
186
|
|
|
$pagination = $provider->getPagination($data, $this->response, $this->responseDefinition); |
|
187
|
|
|
|
|
188
|
|
|
$this->assertInstanceOf(Pagination::class, $pagination); |
|
189
|
|
|
$this->assertSame($expected['page'], $pagination->getPage()); |
|
190
|
|
|
$this->assertSame($expected['perPage'], $pagination->getPerPage()); |
|
191
|
|
|
$this->assertSame($expected['totalItems'], $pagination->getTotalItems()); |
|
192
|
|
|
$this->assertSame($expected['totalPages'], $pagination->getTotalPages()); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertInstanceOf(PaginationLinks::class, $pagination->getLinks()); |
|
195
|
|
|
|
|
196
|
|
|
$links = $pagination->getLinks(); |
|
197
|
|
|
$this->assertSame($expected['first'], $links->getFirst()); |
|
198
|
|
|
$this->assertSame($expected['last'], $links->getLast()); |
|
199
|
|
|
$this->assertSame($expected['next'], $links->getNext()); |
|
200
|
|
|
$this->assertSame($expected['prev'], $links->getPrev()); |
|
201
|
|
|
|
|
202
|
|
|
$this->assertEquals( |
|
203
|
|
|
[ |
|
204
|
|
|
'category', |
|
205
|
|
|
'nickName', |
|
206
|
|
|
'slug', |
|
207
|
|
|
'score', |
|
208
|
|
|
'dateCreated', |
|
209
|
|
|
'dateModified', |
|
210
|
|
|
], |
|
211
|
|
|
array_keys($data[0]) |
|
212
|
|
|
); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return array |
|
217
|
|
|
*/ |
|
218
|
|
|
public function dataProviderItHavePagination(): array |
|
219
|
|
|
{ |
|
220
|
|
|
return [ |
|
221
|
|
|
[ |
|
222
|
|
|
json_decode(file_get_contents(realpath(sprintf('%s/../../fixtures/pagination/pagination_with_embed_self_page.json', __DIR__))), true), |
|
223
|
|
|
[ |
|
224
|
|
|
'page' => 1, |
|
225
|
|
|
'perPage' => 2, |
|
226
|
|
|
'totalItems' => 2, |
|
227
|
|
|
'totalPages' => 1, |
|
228
|
|
|
'first' => '/videos', |
|
229
|
|
|
'last' => '/videos', |
|
230
|
|
|
'next' => null, |
|
231
|
|
|
'prev' => null, |
|
232
|
|
|
], |
|
233
|
|
|
], |
|
234
|
|
|
[ |
|
235
|
|
|
json_decode(file_get_contents(realpath(sprintf('%s/../../fixtures/pagination/pagination_with_embed_first_page.json', __DIR__))), true), |
|
236
|
|
|
[ |
|
237
|
|
|
'page' => 1, |
|
238
|
|
|
'perPage' => 2, |
|
239
|
|
|
'totalItems' => 4, |
|
240
|
|
|
'totalPages' => 2, |
|
241
|
|
|
'first' => '/videos?page=1', |
|
242
|
|
|
'last' => '/videos?page=2', |
|
243
|
|
|
'next' => '/videos?page=2', |
|
244
|
|
|
'prev' => null, |
|
245
|
|
|
], |
|
246
|
|
|
], |
|
247
|
|
|
[ |
|
248
|
|
|
json_decode(file_get_contents(realpath(sprintf('%s/../../fixtures/pagination/pagination_with_embed_second_page.json', __DIR__))), true), |
|
249
|
|
|
[ |
|
250
|
|
|
'page' => 2, |
|
251
|
|
|
'perPage' => 2, |
|
252
|
|
|
'totalItems' => 4, |
|
253
|
|
|
'totalPages' => 2, |
|
254
|
|
|
'first' => '/videos?page=1', |
|
255
|
|
|
'last' => '/videos?page=2', |
|
256
|
|
|
'next' => null, |
|
257
|
|
|
'prev' => '/videos?page=1', |
|
258
|
|
|
], |
|
259
|
|
|
], |
|
260
|
|
|
[ |
|
261
|
|
|
json_decode(file_get_contents(realpath(sprintf('%s/../../fixtures/pagination/pagination_with_embed_last_page.json', __DIR__))), true), |
|
262
|
|
|
[ |
|
263
|
|
|
'page' => 2, |
|
264
|
|
|
'perPage' => 3, |
|
265
|
|
|
'totalItems' => 10, |
|
266
|
|
|
'totalPages' => 4, |
|
267
|
|
|
'first' => '/videos?page=1', |
|
268
|
|
|
'last' => '/videos?page=4', |
|
269
|
|
|
'next' => '/videos?page=3', |
|
270
|
|
|
'prev' => '/videos?page=1', |
|
271
|
|
|
], |
|
272
|
|
|
], |
|
273
|
|
|
]; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|