Passed
Push — master ( 9971c3...383493 )
by Chito
01:52
created

PaginationResultTest::testIteratorNext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 21
rs 9.7666
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Lampager\Cake\Test\TestCase;
4
5
use ArrayIterator;
6
use Cake\I18n\Time;
7
use Cake\ORM\Entity;
8
use IteratorAggregate;
9
use Lampager\Cake\PaginationResult;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use Traversable;
12
13
class PaginationResultTest extends TestCase
14
{
15
    /**
16
     * @param        Entity[]                     $entities
17
     * @param        Entity[]|Traversable<Entity> $records
18
     * @param        mixed[]                      $meta
19
     * @dataProvider arrayProvider
20
     * @dataProvider iteratorAggregateProvider
21
     */
22
    public function testIteratorCurrent(array $entities, $records, array $meta)
23
    {
24
        $actual = new PaginationResult($records, $meta);
25
26
        $this->assertEquals($entities[0], $actual->current());
27
28
        $actual->next();
29
        $this->assertTrue($actual->valid());
30
        $this->assertEquals(1, $actual->key());
31
        $this->assertEquals($entities[1], $actual->current());
32
33
        $actual->next();
34
        $this->assertTrue($actual->valid());
35
        $this->assertEquals(2, $actual->key());
36
        $this->assertEquals($entities[2], $actual->current());
37
38
        $actual->next();
39
        $this->assertFalse($actual->valid());
40
41
        $actual->rewind();
42
        $this->assertTrue($actual->valid());
43
        $this->assertEquals(0, $actual->key());
44
        $this->assertEquals($entities[0], $actual->current());
45
    }
46
47
    /**
48
     * @param        Entity[]                     $entities
49
     * @param        Entity[]|Traversable<Entity> $records
50
     * @param        mixed[]                      $meta
51
     * @dataProvider arrayProvider
52
     * @dataProvider iteratorAggregateProvider
53
     */
54
    public function testIteratorKey(array $entities, $records, array $meta)
55
    {
56
        $actual = new PaginationResult($records, $meta);
57
58
        $this->assertEquals(0, $actual->key());
59
60
        $actual->next();
61
        $this->assertTrue($actual->valid());
62
        $this->assertEquals(1, $actual->key());
63
        $this->assertEquals($entities[1], $actual->current());
64
65
        $actual->next();
66
        $this->assertTrue($actual->valid());
67
        $this->assertEquals(2, $actual->key());
68
        $this->assertEquals($entities[2], $actual->current());
69
70
        $actual->next();
71
        $this->assertFalse($actual->valid());
72
73
        $actual->rewind();
74
        $this->assertTrue($actual->valid());
75
        $this->assertEquals(0, $actual->key());
76
        $this->assertEquals($entities[0], $actual->current());
77
    }
78
79
    /**
80
     * @param        Entity[]                     $entities
81
     * @param        Entity[]|Traversable<Entity> $records
82
     * @param        mixed[]                      $meta
83
     * @dataProvider arrayProvider
84
     * @dataProvider iteratorAggregateProvider
85
     */
86
    public function testIteratorNext(array $entities, $records, array $meta)
87
    {
88
        $actual = new PaginationResult($records, $meta);
89
90
        $actual->next();
91
        $this->assertTrue($actual->valid());
92
        $this->assertEquals(1, $actual->key());
93
        $this->assertEquals($entities[1], $actual->current());
94
95
        $actual->next();
96
        $this->assertTrue($actual->valid());
97
        $this->assertEquals(2, $actual->key());
98
        $this->assertEquals($entities[2], $actual->current());
99
100
        $actual->next();
101
        $this->assertFalse($actual->valid());
102
103
        $actual->rewind();
104
        $this->assertTrue($actual->valid());
105
        $this->assertEquals(0, $actual->key());
106
        $this->assertEquals($entities[0], $actual->current());
107
    }
108
109
    /**
110
     * @param        Entity[]                     $entities
111
     * @param        Entity[]|Traversable<Entity> $records
112
     * @param        mixed[]                      $meta
113
     * @dataProvider arrayProvider
114
     * @dataProvider iteratorAggregateProvider
115
     */
116
    public function testIteratorValid(array $entities, $records, array $meta)
117
    {
118
        $actual = new PaginationResult($records, $meta);
119
120
        $this->assertTrue($actual->valid());
121
        $this->assertEquals(0, $actual->key());
122
        $this->assertEquals($entities[0], $actual->current());
123
124
        $actual->next();
125
        $this->assertTrue($actual->valid());
126
        $this->assertEquals(1, $actual->key());
127
        $this->assertEquals($entities[1], $actual->current());
128
129
        $actual->next();
130
        $this->assertTrue($actual->valid());
131
        $this->assertEquals(2, $actual->key());
132
        $this->assertEquals($entities[2], $actual->current());
133
134
        $actual->next();
135
        $this->assertFalse($actual->valid());
136
137
        $actual->rewind();
138
        $this->assertTrue($actual->valid());
139
        $this->assertEquals(0, $actual->key());
140
        $this->assertEquals($entities[0], $actual->current());
141
    }
142
143
    /**
144
     * @param        Entity[]                     $entities
145
     * @param        Entity[]|Traversable<Entity> $records
146
     * @param        mixed[]                      $meta
147
     * @param        string                       $expected
148
     * @dataProvider arrayProvider
149
     * @dataProvider iteratorAggregateProvider
150
     */
151
    public function testJsonSerialize(array $entities, $records, array $meta, $expected)
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

151
    public function testJsonSerialize(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta, $expected)

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...
152
    {
153
        $actual = json_encode(new PaginationResult($records, $meta));
154
        $this->assertJsonStringEqualsJsonString($expected, $actual);
155
    }
156
157
    /**
158
     * @param        Entity[]                     $entities
159
     * @param        Entity[]|Traversable<Entity> $records
160
     * @param        mixed[]                      $meta
161
     * @dataProvider arrayProvider
162
     * @dataProvider iteratorAggregateProvider
163
     */
164
    public function testSerializeAndUnserialize(array $entities, $records, array $meta)
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

164
    public function testSerializeAndUnserialize(/** @scrutinizer ignore-unused */ array $entities, $records, array $meta)

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...
165
    {
166
        $actual = unserialize(serialize(new PaginationResult($records, $meta)));
167
        $expected = new PaginationResult($records, $meta);
168
        $this->assertJsonEquals($expected, $actual);
169
    }
170
171
    /**
172
     * @param        Entity[]                     $entities
173
     * @param        Entity[]|Traversable<Entity> $records
174
     * @param        mixed[]                      $meta
175
     * @dataProvider arrayProvider
176
     * @dataProvider iteratorAggregateProvider
177
     */
178
    public function testDebugInfo(array $entities, $records, array $meta)
179
    {
180
        $actual = (new PaginationResult($records, $meta))->__debugInfo();
181
182
        $this->assertEquals([
183
            '(help)' => 'This is a Lampager Pagination Result object.',
184
            'records' => $entities,
185
            'hasPrevious' => $meta['hasPrevious'],
186
            'previousCursor' => $meta['previousCursor'],
187
            'hasNext' => $meta['hasNext'],
188
            'nextCursor' => $meta['nextCursor'],
189
        ], $actual);
190
    }
191
192
    public function arrayProvider()
193
    {
194
        yield 'Array iteration' => [
195
            [
196
                new Entity([
197
                    'id' => 1,
198
                    'modified' => new Time('2017-01-01 10:00:00'),
199
                ]),
200
                new Entity([
201
                    'id' => 3,
202
                    'modified' => new Time('2017-01-01 10:00:00'),
203
                ]),
204
                new Entity([
205
                    'id' => 5,
206
                    'modified' => new Time('2017-01-01 10:00:00'),
207
                ]),
208
            ],
209
            [
210
                new Entity([
211
                    'id' => 1,
212
                    'modified' => new Time('2017-01-01 10:00:00'),
213
                ]),
214
                new Entity([
215
                    'id' => 3,
216
                    'modified' => new Time('2017-01-01 10:00:00'),
217
                ]),
218
                new Entity([
219
                    'id' => 5,
220
                    'modified' => new Time('2017-01-01 10:00:00'),
221
                ]),
222
            ],
223
            [
224
                'hasPrevious' => null,
225
                'previousCursor' => null,
226
                'hasNext' => true,
227
                'nextCursor' => [
228
                    'Posts.id' => 2,
229
                    'Posts.modified' => new Time('2017-01-01 11:00:00'),
230
                ],
231
            ],
232
            '{
233
                "records": [
234
                    {
235
                        "id": 1,
236
                        "modified": "2017-01-01T10:00:00+00:00"
237
                    },
238
                    {
239
                        "id": 3,
240
                        "modified": "2017-01-01T10:00:00+00:00"
241
                    },
242
                    {
243
                        "id": 5,
244
                        "modified": "2017-01-01T10:00:00+00:00"
245
                    }
246
                ],
247
                "hasPrevious": null,
248
                "previousCursor": null,
249
                "hasNext": true,
250
                "nextCursor": {
251
                    "Posts.id": 2,
252
                    "Posts.modified": "2017-01-01T11:00:00+00:00"
253
                }
254
            }',
255
        ];
256
257
        yield 'ArrayIterator iteration' => [
258
            [
259
                new Entity([
260
                    'id' => 1,
261
                    'modified' => new Time('2017-01-01 10:00:00'),
262
                ]),
263
                new Entity([
264
                    'id' => 3,
265
                    'modified' => new Time('2017-01-01 10:00:00'),
266
                ]),
267
                new Entity([
268
                    'id' => 5,
269
                    'modified' => new Time('2017-01-01 10:00:00'),
270
                ]),
271
            ],
272
            new ArrayIterator([
273
                new Entity([
274
                    'id' => 1,
275
                    'modified' => new Time('2017-01-01 10:00:00'),
276
                ]),
277
                new Entity([
278
                    'id' => 3,
279
                    'modified' => new Time('2017-01-01 10:00:00'),
280
                ]),
281
                new Entity([
282
                    'id' => 5,
283
                    'modified' => new Time('2017-01-01 10:00:00'),
284
                ]),
285
            ]),
286
            [
287
                'hasPrevious' => null,
288
                'previousCursor' => null,
289
                'hasNext' => true,
290
                'nextCursor' => [
291
                    'Posts.id' => 2,
292
                    'Posts.modified' => new Time('2017-01-01 11:00:00'),
293
                ],
294
            ],
295
            '{
296
                "records": [
297
                    {
298
                        "id": 1,
299
                        "modified": "2017-01-01T10:00:00+00:00"
300
                    },
301
                    {
302
                        "id": 3,
303
                        "modified": "2017-01-01T10:00:00+00:00"
304
                    },
305
                    {
306
                        "id": 5,
307
                        "modified": "2017-01-01T10:00:00+00:00"
308
                    }
309
                ],
310
                "hasPrevious": null,
311
                "previousCursor": null,
312
                "hasNext": true,
313
                "nextCursor": {
314
                    "Posts.id": 2,
315
                    "Posts.modified": "2017-01-01T11:00:00+00:00"
316
                }
317
            }',
318
        ];
319
    }
320
321
    public function iteratorAggregateProvider()
322
    {
323
        /** @var IteratorAggregate&MockObject $iteratorAggregate */
324
        $iteratorAggregate = $this->getMockForAbstractClass(IteratorAggregate::class);
325
        $iteratorAggregate->method('getIterator')->willReturn(new ArrayIterator([
326
            new Entity([
327
                'id' => 1,
328
                'modified' => new Time('2017-01-01 10:00:00'),
329
            ]),
330
            new Entity([
331
                'id' => 3,
332
                'modified' => new Time('2017-01-01 10:00:00'),
333
            ]),
334
            new Entity([
335
                'id' => 5,
336
                'modified' => new Time('2017-01-01 10:00:00'),
337
            ]),
338
        ]));
339
340
        yield 'IteratorAggregate iteration' => [
341
            [
342
                new Entity([
343
                    'id' => 1,
344
                    'modified' => new Time('2017-01-01 10:00:00'),
345
                ]),
346
                new Entity([
347
                    'id' => 3,
348
                    'modified' => new Time('2017-01-01 10:00:00'),
349
                ]),
350
                new Entity([
351
                    'id' => 5,
352
                    'modified' => new Time('2017-01-01 10:00:00'),
353
                ]),
354
            ],
355
            $iteratorAggregate,
356
            [
357
                'hasPrevious' => null,
358
                'previousCursor' => null,
359
                'hasNext' => true,
360
                'nextCursor' => [
361
                    'Posts.id' => 2,
362
                    'Posts.modified' => new Time('2017-01-01 11:00:00'),
363
                ],
364
            ],
365
            '{
366
                "records": [
367
                    {
368
                        "id": 1,
369
                        "modified": "2017-01-01T10:00:00+00:00"
370
                    },
371
                    {
372
                        "id": 3,
373
                        "modified": "2017-01-01T10:00:00+00:00"
374
                    },
375
                    {
376
                        "id": 5,
377
                        "modified": "2017-01-01T10:00:00+00:00"
378
                    }
379
                ],
380
                "hasPrevious": null,
381
                "previousCursor": null,
382
                "hasNext": true,
383
                "nextCursor": {
384
                    "Posts.id": 2,
385
                    "Posts.modified": "2017-01-01T11:00:00+00:00"
386
                }
387
            }',
388
        ];
389
    }
390
}
391