1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lampager\Cake\Test\TestCase; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use Cake\I18n\DateTime; |
|
|
|
|
9
|
|
|
use Cake\ORM\Entity; |
10
|
|
|
use Generator; |
11
|
|
|
use IteratorAggregate; |
12
|
|
|
use Lampager\Cake\PaginationResult; |
13
|
|
|
use Traversable; |
14
|
|
|
|
15
|
|
|
class PaginationResultTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function setUp(): void |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
set_error_handler( |
22
|
|
|
static function ($errno, $errstr, $errfile, $errline) { |
23
|
|
|
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); |
24
|
|
|
}, |
25
|
|
|
E_ALL |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function tearDown(): void |
30
|
|
|
{ |
31
|
|
|
restore_error_handler(); |
32
|
|
|
|
33
|
|
|
parent::tearDown(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Entity[] $entities |
38
|
|
|
* @param Entity[]|Traversable<Entity> $records |
39
|
|
|
* @param mixed[] $meta |
40
|
|
|
* @dataProvider arrayProvider |
41
|
|
|
* @dataProvider iteratorAggregateProvider |
42
|
|
|
*/ |
43
|
|
|
public function testCurrentPage(array $entities, $records, array $meta): void |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$actual = new PaginationResult($records, $meta); |
46
|
|
|
$this->assertEquals(0, $actual->currentPage()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Entity[] $entities |
51
|
|
|
* @param Entity[]|Traversable<Entity> $records |
52
|
|
|
* @param mixed[] $meta |
53
|
|
|
* @dataProvider arrayProvider |
54
|
|
|
* @dataProvider iteratorAggregateProvider |
55
|
|
|
*/ |
56
|
|
|
public function testPerPage(array $entities, $records, array $meta): void |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$actual = new PaginationResult($records, $meta); |
59
|
|
|
$this->assertEquals(3, $actual->perPage()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Entity[] $entities |
64
|
|
|
* @param Entity[]|Traversable<Entity> $records |
65
|
|
|
* @param mixed[] $meta |
66
|
|
|
* @dataProvider arrayProvider |
67
|
|
|
* @dataProvider iteratorAggregateProvider |
68
|
|
|
*/ |
69
|
|
|
public function testTotalCount(array $entities, $records, array $meta): void |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$actual = new PaginationResult($records, $meta); |
72
|
|
|
$this->assertNull($actual->totalCount()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Entity[] $entities |
77
|
|
|
* @param Entity[]|Traversable<Entity> $records |
78
|
|
|
* @param mixed[] $meta |
79
|
|
|
* @dataProvider arrayProvider |
80
|
|
|
* @dataProvider iteratorAggregateProvider |
81
|
|
|
*/ |
82
|
|
|
public function testPageCount(array $entities, $records, array $meta): void |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$actual = new PaginationResult($records, $meta); |
85
|
|
|
$this->assertNull($actual->pageCount()); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Entity[] $entities |
90
|
|
|
* @param Entity[]|Traversable<Entity> $records |
91
|
|
|
* @param mixed[] $meta |
92
|
|
|
* @dataProvider arrayProvider |
93
|
|
|
* @dataProvider iteratorAggregateProvider |
94
|
|
|
*/ |
95
|
|
|
public function testHasPrevPage(array $entities, $records, array $meta): void |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$actual = new PaginationResult($records, $meta); |
98
|
|
|
$this->assertEquals((bool)$meta['hasPrevious'], $actual->hasPrevPage()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Entity[] $entities |
103
|
|
|
* @param Entity[]|Traversable<Entity> $records |
104
|
|
|
* @param mixed[] $meta |
105
|
|
|
* @dataProvider arrayProvider |
106
|
|
|
* @dataProvider iteratorAggregateProvider |
107
|
|
|
*/ |
108
|
|
|
public function testHasNextPage(array $entities, $records, array $meta): void |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$actual = new PaginationResult($records, $meta); |
111
|
|
|
$this->assertEquals((bool)$meta['hasNext'], $actual->hasNextPage()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Entity[] $entities |
116
|
|
|
* @param Entity[]|Traversable<Entity> $records |
117
|
|
|
* @param mixed[] $meta |
118
|
|
|
* @dataProvider arrayProvider |
119
|
|
|
* @dataProvider iteratorAggregateProvider |
120
|
|
|
*/ |
121
|
|
|
public function testItems(array $entities, $records, array $meta): void |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$paginationResult = new PaginationResult($records, $meta); |
124
|
|
|
$expected = is_array($records) ? $records : iterator_to_array($records); |
125
|
|
|
$actual = is_array($paginationResult->items()) ? $paginationResult->items() : iterator_to_array($paginationResult->items()); |
126
|
|
|
$this->assertEquals($expected, $actual); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Entity[] $entities |
131
|
|
|
* @param Entity[]|Traversable<Entity> $records |
132
|
|
|
* @param mixed[] $meta |
133
|
|
|
* @dataProvider arrayProvider |
134
|
|
|
* @dataProvider iteratorAggregateProvider |
135
|
|
|
*/ |
136
|
|
|
public function testPagingParam(array $entities, $records, array $meta): void |
137
|
|
|
{ |
138
|
|
|
$actual = new PaginationResult($records, $meta); |
139
|
|
|
$this->assertEquals(count($entities), $actual->pagingParam('count')); |
140
|
|
|
$this->assertNull($actual->pagingParam('totalCount')); |
141
|
|
|
$this->assertEquals($meta['limit'], $actual->pagingParam('perPage')); |
142
|
|
|
$this->assertNull($actual->pagingParam('pageCount')); |
143
|
|
|
$this->assertEquals(0, $actual->pagingParam('currentPage')); |
144
|
|
|
$this->assertEquals($meta['hasPrevious'], $actual->pagingParam('hasPrevPage')); |
145
|
|
|
$this->assertEquals($meta['hasNext'], $actual->pagingParam('hasNextPage')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param Entity[] $entities |
150
|
|
|
* @param Entity[]|Traversable<Entity> $records |
151
|
|
|
* @param mixed[] $meta |
152
|
|
|
* @dataProvider arrayProvider |
153
|
|
|
* @dataProvider iteratorAggregateProvider |
154
|
|
|
*/ |
155
|
|
|
public function testJsonSerialize(array $entities, $records, array $meta, string $expected): void |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$actual = json_encode(new PaginationResult($records, $meta)); |
158
|
|
|
$this->assertJsonStringEqualsJsonString($expected, $actual); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param Entity[] $entities |
163
|
|
|
* @param Entity[]|Traversable<Entity> $records |
164
|
|
|
* @param mixed[] $meta |
165
|
|
|
* @dataProvider arrayProvider |
166
|
|
|
* @dataProvider iteratorAggregateProvider |
167
|
|
|
*/ |
168
|
|
|
public function testDebugInfo(array $entities, $records, array $meta): void |
169
|
|
|
{ |
170
|
|
|
$actual = (new PaginationResult($records, $meta))->__debugInfo(); |
171
|
|
|
|
172
|
|
|
$this->assertEquals([ |
173
|
|
|
'(help)' => 'This is a Lampager Pagination Result object.', |
174
|
|
|
'records' => $entities, |
175
|
|
|
'hasPrevious' => $meta['hasPrevious'], |
176
|
|
|
'previousCursor' => $meta['previousCursor'], |
177
|
|
|
'hasNext' => $meta['hasNext'], |
178
|
|
|
'nextCursor' => $meta['nextCursor'], |
179
|
|
|
], $actual); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param Entity[] $entities |
184
|
|
|
* @param Entity[]|Traversable<Entity> $records |
185
|
|
|
* @param mixed[] $meta |
186
|
|
|
* @dataProvider arrayProvider |
187
|
|
|
* @dataProvider iteratorAggregateProvider |
188
|
|
|
*/ |
189
|
|
|
public function testPublicProperties(array $entities, $records, array $meta): void |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$paginationResult = new PaginationResult($records, $meta); |
192
|
|
|
|
193
|
|
|
$this->assertEquals($meta['hasPrevious'], $paginationResult->hasPrevious); |
194
|
|
|
$this->assertEquals($meta['previousCursor'], $paginationResult->previousCursor); |
195
|
|
|
$this->assertEquals($meta['hasNext'], $paginationResult->hasNext); |
196
|
|
|
$this->assertEquals($meta['nextCursor'], $paginationResult->nextCursor); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public static function arrayProvider(): Generator |
200
|
|
|
{ |
201
|
|
|
yield 'Array iteration' => [ |
202
|
|
|
[ |
203
|
|
|
new Entity([ |
204
|
|
|
'id' => 1, |
205
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
206
|
|
|
]), |
207
|
|
|
new Entity([ |
208
|
|
|
'id' => 3, |
209
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
210
|
|
|
]), |
211
|
|
|
new Entity([ |
212
|
|
|
'id' => 5, |
213
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
214
|
|
|
]), |
215
|
|
|
], |
216
|
|
|
[ |
217
|
|
|
new Entity([ |
218
|
|
|
'id' => 1, |
219
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
220
|
|
|
]), |
221
|
|
|
new Entity([ |
222
|
|
|
'id' => 3, |
223
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
224
|
|
|
]), |
225
|
|
|
new Entity([ |
226
|
|
|
'id' => 5, |
227
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
228
|
|
|
]), |
229
|
|
|
], |
230
|
|
|
[ |
231
|
|
|
'hasPrevious' => null, |
232
|
|
|
'previousCursor' => null, |
233
|
|
|
'hasNext' => true, |
234
|
|
|
'nextCursor' => [ |
235
|
|
|
'Posts.id' => 2, |
236
|
|
|
'Posts.modified' => new DateTime('2017-01-01 11:00:00'), |
237
|
|
|
], |
238
|
|
|
'limit' => 3, |
239
|
|
|
], |
240
|
|
|
'{ |
241
|
|
|
"records": [ |
242
|
|
|
{ |
243
|
|
|
"id": 1, |
244
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
245
|
|
|
}, |
246
|
|
|
{ |
247
|
|
|
"id": 3, |
248
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
249
|
|
|
}, |
250
|
|
|
{ |
251
|
|
|
"id": 5, |
252
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
253
|
|
|
} |
254
|
|
|
], |
255
|
|
|
"hasPrevious": null, |
256
|
|
|
"previousCursor": null, |
257
|
|
|
"hasNext": true, |
258
|
|
|
"nextCursor": { |
259
|
|
|
"Posts.id": 2, |
260
|
|
|
"Posts.modified": "2017-01-01T11:00:00+00:00" |
261
|
|
|
} |
262
|
|
|
}', |
263
|
|
|
]; |
264
|
|
|
|
265
|
|
|
yield 'ArrayIterator iteration' => [ |
266
|
|
|
[ |
267
|
|
|
new Entity([ |
268
|
|
|
'id' => 1, |
269
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
270
|
|
|
]), |
271
|
|
|
new Entity([ |
272
|
|
|
'id' => 3, |
273
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
274
|
|
|
]), |
275
|
|
|
new Entity([ |
276
|
|
|
'id' => 5, |
277
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
278
|
|
|
]), |
279
|
|
|
], |
280
|
|
|
new ArrayIterator([ |
281
|
|
|
new Entity([ |
282
|
|
|
'id' => 1, |
283
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
284
|
|
|
]), |
285
|
|
|
new Entity([ |
286
|
|
|
'id' => 3, |
287
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
288
|
|
|
]), |
289
|
|
|
new Entity([ |
290
|
|
|
'id' => 5, |
291
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
292
|
|
|
]), |
293
|
|
|
]), |
294
|
|
|
[ |
295
|
|
|
'hasPrevious' => null, |
296
|
|
|
'previousCursor' => null, |
297
|
|
|
'hasNext' => true, |
298
|
|
|
'nextCursor' => [ |
299
|
|
|
'Posts.id' => 2, |
300
|
|
|
'Posts.modified' => new DateTime('2017-01-01 11:00:00'), |
301
|
|
|
], |
302
|
|
|
'limit' => 3, |
303
|
|
|
], |
304
|
|
|
'{ |
305
|
|
|
"records": [ |
306
|
|
|
{ |
307
|
|
|
"id": 1, |
308
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
309
|
|
|
}, |
310
|
|
|
{ |
311
|
|
|
"id": 3, |
312
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
313
|
|
|
}, |
314
|
|
|
{ |
315
|
|
|
"id": 5, |
316
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
317
|
|
|
} |
318
|
|
|
], |
319
|
|
|
"hasPrevious": null, |
320
|
|
|
"previousCursor": null, |
321
|
|
|
"hasNext": true, |
322
|
|
|
"nextCursor": { |
323
|
|
|
"Posts.id": 2, |
324
|
|
|
"Posts.modified": "2017-01-01T11:00:00+00:00" |
325
|
|
|
} |
326
|
|
|
}', |
327
|
|
|
]; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public static function iteratorAggregateProvider(): Generator |
331
|
|
|
{ |
332
|
|
|
yield 'IteratorAggregate iteration' => [ |
333
|
|
|
[ |
334
|
|
|
new Entity([ |
335
|
|
|
'id' => 1, |
336
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
337
|
|
|
]), |
338
|
|
|
new Entity([ |
339
|
|
|
'id' => 3, |
340
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
341
|
|
|
]), |
342
|
|
|
new Entity([ |
343
|
|
|
'id' => 5, |
344
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
345
|
|
|
]), |
346
|
|
|
], |
347
|
|
|
new class implements IteratorAggregate { |
348
|
|
|
public function getIterator(): Traversable |
349
|
|
|
{ |
350
|
|
|
return new ArrayIterator([ |
351
|
|
|
new Entity([ |
352
|
|
|
'id' => 1, |
353
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
354
|
|
|
]), |
355
|
|
|
new Entity([ |
356
|
|
|
'id' => 3, |
357
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
358
|
|
|
]), |
359
|
|
|
new Entity([ |
360
|
|
|
'id' => 5, |
361
|
|
|
'modified' => new DateTime('2017-01-01 10:00:00'), |
362
|
|
|
]), |
363
|
|
|
]); |
364
|
|
|
} |
365
|
|
|
}, |
366
|
|
|
[ |
367
|
|
|
'hasPrevious' => null, |
368
|
|
|
'previousCursor' => null, |
369
|
|
|
'hasNext' => true, |
370
|
|
|
'nextCursor' => [ |
371
|
|
|
'Posts.id' => 2, |
372
|
|
|
'Posts.modified' => new DateTime('2017-01-01 11:00:00'), |
373
|
|
|
], |
374
|
|
|
'limit' => 3, |
375
|
|
|
], |
376
|
|
|
'{ |
377
|
|
|
"records": [ |
378
|
|
|
{ |
379
|
|
|
"id": 1, |
380
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
381
|
|
|
}, |
382
|
|
|
{ |
383
|
|
|
"id": 3, |
384
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
385
|
|
|
}, |
386
|
|
|
{ |
387
|
|
|
"id": 5, |
388
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
389
|
|
|
} |
390
|
|
|
], |
391
|
|
|
"hasPrevious": null, |
392
|
|
|
"previousCursor": null, |
393
|
|
|
"hasNext": true, |
394
|
|
|
"nextCursor": { |
395
|
|
|
"Posts.id": 2, |
396
|
|
|
"Posts.modified": "2017-01-01T11:00:00+00:00" |
397
|
|
|
} |
398
|
|
|
}', |
399
|
|
|
]; |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths