Passed
Pull Request — master (#48)
by Chito
12:37
created

PaginationResultTest::testIteratorKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 23
rs 9.7333
cc 1
nc 1
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A PaginationResultTest::testPageCount() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lampager\Cake\Test\TestCase;
6
7
use ArrayIterator;
8
use Cake\I18n\DateTime;
0 ignored issues
show
Bug introduced by
The type Cake\I18n\DateTime was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
    public function testCurrentPage(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

56
    public function testPerPage(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

69
    public function testTotalCount(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        $actual = new PaginationResult($records, $meta);
72
        $this->assertNull($actual->totalCount());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $actual->totalCount() targeting Lampager\Cake\PaginationResult::totalCount() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
    public function testPageCount(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    {
84
        $actual = new PaginationResult($records, $meta);
85
        $this->assertNull($actual->pageCount());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $actual->pageCount() targeting Lampager\Cake\PaginationResult::pageCount() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
    public function testHasPrevPage(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

108
    public function testHasNextPage(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

121
    public function testItems(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    {
123
        $actual = new PaginationResult($records, $meta);
124
        $this->assertEquals(iterator_to_array($records), iterator_to_array($actual->items()));
125
    }
126
127
    /**
128
     * @param Entity[]                     $entities
129
     * @param Entity[]|Traversable<Entity> $records
130
     * @param mixed[]                      $meta
131
     * @dataProvider arrayProvider
132
     * @dataProvider iteratorAggregateProvider
133
     */
134
    public function testPagingParam(array $entities, $records, array $meta): void
135
    {
136
        $actual = new PaginationResult($records, $meta);
137
        $this->assertEquals(count($entities), $actual->pagingParam('count'));
138
        $this->assertNull($actual->pagingParam('totalCount'));
139
        $this->assertEquals($meta['limit'], $actual->pagingParam('perPage'));
140
        $this->assertNull($actual->pagingParam('pageCount'));
141
        $this->assertEquals(0, $actual->pagingParam('currentPage'));
142
        $this->assertEquals($meta['hasPrevious'], $actual->pagingParam('hasPrevPage'));
143
        $this->assertEquals($meta['hasNext'], $actual->pagingParam('hasNextPage'));
144
    }
145
146
    /**
147
     * @param Entity[]                     $entities
148
     * @param Entity[]|Traversable<Entity> $records
149
     * @param mixed[]                      $meta
150
     * @dataProvider arrayProvider
151
     * @dataProvider iteratorAggregateProvider
152
     */
153
    public function testJsonSerialize(array $entities, $records, array $meta, string $expected): void
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

153
    public function testJsonSerialize(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta, string $expected): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
    {
155
        $actual = json_encode(new PaginationResult($records, $meta));
156
        $this->assertJsonStringEqualsJsonString($expected, $actual);
157
    }
158
159
    /**
160
     * @param Entity[]                     $entities
161
     * @param Entity[]|Traversable<Entity> $records
162
     * @param mixed[]                      $meta
163
     * @dataProvider arrayProvider
164
     * @dataProvider iteratorAggregateProvider
165
     */
166
    public function testDebugInfo(array $entities, $records, array $meta): void
167
    {
168
        $actual = (new PaginationResult($records, $meta))->__debugInfo();
169
170
        $this->assertEquals([
171
            '(help)' => 'This is a Lampager Pagination Result object.',
172
            'records' => $entities,
173
            'hasPrevious' => $meta['hasPrevious'],
174
            'previousCursor' => $meta['previousCursor'],
175
            'hasNext' => $meta['hasNext'],
176
            'nextCursor' => $meta['nextCursor'],
177
        ], $actual);
178
    }
179
180
    /**
181
     * @param Entity[]                     $entities
182
     * @param Entity[]|Traversable<Entity> $records
183
     * @param mixed[]                      $meta
184
     * @dataProvider arrayProvider
185
     * @dataProvider iteratorAggregateProvider
186
     */
187
    public function testPublicProperties(array $entities, $records, array $meta): void
0 ignored issues
show
Unused Code introduced by
The parameter $entities is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

187
    public function testPublicProperties(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
    {
189
        $paginationResult = new PaginationResult($records, $meta);
190
191
        $this->assertEquals($meta['hasPrevious'], $paginationResult->hasPrevious);
192
        $this->assertEquals($meta['previousCursor'], $paginationResult->previousCursor);
193
        $this->assertEquals($meta['hasNext'], $paginationResult->hasNext);
194
        $this->assertEquals($meta['nextCursor'], $paginationResult->nextCursor);
195
    }
196
197
    public static function arrayProvider(): Generator
198
    {
199
        yield 'Array iteration' => [
200
            [
201
                new Entity([
202
                    'id' => 1,
203
                    'modified' => new DateTime('2017-01-01 10:00:00'),
204
                ]),
205
                new Entity([
206
                    'id' => 3,
207
                    'modified' => new DateTime('2017-01-01 10:00:00'),
208
                ]),
209
                new Entity([
210
                    'id' => 5,
211
                    'modified' => new DateTime('2017-01-01 10:00:00'),
212
                ]),
213
            ],
214
            [
215
                new Entity([
216
                    'id' => 1,
217
                    'modified' => new DateTime('2017-01-01 10:00:00'),
218
                ]),
219
                new Entity([
220
                    'id' => 3,
221
                    'modified' => new DateTime('2017-01-01 10:00:00'),
222
                ]),
223
                new Entity([
224
                    'id' => 5,
225
                    'modified' => new DateTime('2017-01-01 10:00:00'),
226
                ]),
227
            ],
228
            [
229
                'hasPrevious' => null,
230
                'previousCursor' => null,
231
                'hasNext' => true,
232
                'nextCursor' => [
233
                    'Posts.id' => 2,
234
                    'Posts.modified' => new DateTime('2017-01-01 11:00:00'),
235
                ],
236
                'limit' => 3,
237
            ],
238
            '{
239
                "records": [
240
                    {
241
                        "id": 1,
242
                        "modified": "2017-01-01T10:00:00+00:00"
243
                    },
244
                    {
245
                        "id": 3,
246
                        "modified": "2017-01-01T10:00:00+00:00"
247
                    },
248
                    {
249
                        "id": 5,
250
                        "modified": "2017-01-01T10:00:00+00:00"
251
                    }
252
                ],
253
                "hasPrevious": null,
254
                "previousCursor": null,
255
                "hasNext": true,
256
                "nextCursor": {
257
                    "Posts.id": 2,
258
                    "Posts.modified": "2017-01-01T11:00:00+00:00"
259
                }
260
            }',
261
        ];
262
263
        yield 'ArrayIterator iteration' => [
264
            [
265
                new Entity([
266
                    'id' => 1,
267
                    'modified' => new DateTime('2017-01-01 10:00:00'),
268
                ]),
269
                new Entity([
270
                    'id' => 3,
271
                    'modified' => new DateTime('2017-01-01 10:00:00'),
272
                ]),
273
                new Entity([
274
                    'id' => 5,
275
                    'modified' => new DateTime('2017-01-01 10:00:00'),
276
                ]),
277
            ],
278
            new ArrayIterator([
279
                new Entity([
280
                    'id' => 1,
281
                    'modified' => new DateTime('2017-01-01 10:00:00'),
282
                ]),
283
                new Entity([
284
                    'id' => 3,
285
                    'modified' => new DateTime('2017-01-01 10:00:00'),
286
                ]),
287
                new Entity([
288
                    'id' => 5,
289
                    'modified' => new DateTime('2017-01-01 10:00:00'),
290
                ]),
291
            ]),
292
            [
293
                'hasPrevious' => null,
294
                'previousCursor' => null,
295
                'hasNext' => true,
296
                'nextCursor' => [
297
                    'Posts.id' => 2,
298
                    'Posts.modified' => new DateTime('2017-01-01 11:00:00'),
299
                ],
300
                'limit' => 3,
301
            ],
302
            '{
303
                "records": [
304
                    {
305
                        "id": 1,
306
                        "modified": "2017-01-01T10:00:00+00:00"
307
                    },
308
                    {
309
                        "id": 3,
310
                        "modified": "2017-01-01T10:00:00+00:00"
311
                    },
312
                    {
313
                        "id": 5,
314
                        "modified": "2017-01-01T10:00:00+00:00"
315
                    }
316
                ],
317
                "hasPrevious": null,
318
                "previousCursor": null,
319
                "hasNext": true,
320
                "nextCursor": {
321
                    "Posts.id": 2,
322
                    "Posts.modified": "2017-01-01T11:00:00+00:00"
323
                }
324
            }',
325
        ];
326
    }
327
328
    public static function iteratorAggregateProvider(): Generator
329
    {
330
        yield 'IteratorAggregate iteration' => [
331
            [
332
                new Entity([
333
                    'id' => 1,
334
                    'modified' => new DateTime('2017-01-01 10:00:00'),
335
                ]),
336
                new Entity([
337
                    'id' => 3,
338
                    'modified' => new DateTime('2017-01-01 10:00:00'),
339
                ]),
340
                new Entity([
341
                    'id' => 5,
342
                    'modified' => new DateTime('2017-01-01 10:00:00'),
343
                ]),
344
            ],
345
            new class implements IteratorAggregate {
346
                public function getIterator(): Traversable
347
                {
348
                    return new ArrayIterator([
349
                        new Entity([
350
                            'id' => 1,
351
                            'modified' => new DateTime('2017-01-01 10:00:00'),
352
                        ]),
353
                        new Entity([
354
                            'id' => 3,
355
                            'modified' => new DateTime('2017-01-01 10:00:00'),
356
                        ]),
357
                        new Entity([
358
                            'id' => 5,
359
                            'modified' => new DateTime('2017-01-01 10:00:00'),
360
                        ]),
361
                    ]);
362
                }
363
            },
364
            [
365
                'hasPrevious' => null,
366
                'previousCursor' => null,
367
                'hasNext' => true,
368
                'nextCursor' => [
369
                    'Posts.id' => 2,
370
                    'Posts.modified' => new DateTime('2017-01-01 11:00:00'),
371
                ],
372
                'limit' => 3,
373
            ],
374
            '{
375
                "records": [
376
                    {
377
                        "id": 1,
378
                        "modified": "2017-01-01T10:00:00+00:00"
379
                    },
380
                    {
381
                        "id": 3,
382
                        "modified": "2017-01-01T10:00:00+00:00"
383
                    },
384
                    {
385
                        "id": 5,
386
                        "modified": "2017-01-01T10:00:00+00:00"
387
                    }
388
                ],
389
                "hasPrevious": null,
390
                "previousCursor": null,
391
                "hasNext": true,
392
                "nextCursor": {
393
                    "Posts.id": 2,
394
                    "Posts.modified": "2017-01-01T11:00:00+00:00"
395
                }
396
            }',
397
        ];
398
    }
399
}
400