Passed
Push — master ( a126da...22dd73 )
by Chito
01:42
created

testProcessByComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
App::uses('CakeRequest', 'Network');
4
App::uses('ComponentCollection', 'Controller');
5
App::uses('Controller', 'Controller');
6
App::uses('PaginatorComponent', 'Controller/Component');
7
App::uses('LampagerPaginator', 'Lampager.Model');
8
App::uses('LampagerTestCase', 'Lampager.Test/Case');
9
10
use Lampager\PaginationResult;
11
12
class LampagerArrayProcessorTest extends LampagerTestCase
13
{
14
    /** @var Model */
15
    protected $Post;
16
17
    /** @var CakeRequest */
18
    protected $request;
19
20
    /** @var Controller */
21
    protected $Controller;
22
23
    /** @var PaginatorComponent */
24
    protected $Paginator;
25
26
    /** @var string[] */
27
    public $fixtures = [
28
        'plugin.Lampager.Post',
29
    ];
30
31
    public function setUp()
32
    {
33
        parent::setUp();
34
35
        /** @var ComponentCollection&PHPUnit_Framework_MockObject_MockObject */
36
        $collection = $this->createMock(ComponentCollection::class);
37
38
        // Prepare for ModelBehavior
39
        $this->Post = ClassRegistry::init('Post');
0 ignored issues
show
Documentation Bug introduced by
It seems like ClassRegistry::init('Post') can also be of type boolean. However, the property $Post is declared as type Model. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
40
        $this->Post->Behaviors->load('Lampager.Lampager');
41
42
        // Prepare for PaginatorComponent
43
        $this->request = new CakeRequest('posts/index');
44
        $this->request->params['pass'] = [];
45
        $this->request->params['named'] = [];
46
47
        $this->Controller = new Controller($this->request);
48
        $this->Controller->Post = $this->Post;
49
50
        $this->Paginator = new PaginatorComponent($collection, []);
51
        $this->Paginator->Controller = $this->Controller;
0 ignored issues
show
Bug Best Practice introduced by
The property Controller does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
    }
53
54
    public function tearDown()
55
    {
56
        // Shutdown for ModelBehavior
57
        $this->Post->Behaviors->unload('Lampager.Lampager');
58
59
        // Shutdown for PaginatorComponent
60
        $this->Controller->Components->unload('Paginator');
61
62
        parent::tearDown();
63
    }
64
65
    /**
66
     * Test LampagerArrayProcessor::process by LampagerPaginator::paginate
67
     *
68
     * @param mixed $expected
69
     * @dataProvider processProvider
70
     */
71
    public function testPaginate(array $query, $expected)
72
    {
73
        $cursor = isset($query['cursor']) ? $query['cursor'] : [];
74
        $actual = LampagerPaginator::create($this->Post, $query)->paginate($cursor);
75
        $this->assertInstanceOf(PaginationResult::class, $actual);
76
        $this->assertSame($expected, (array)$actual);
77
    }
78
79
    /**
80
     * Test LampagerArrayProcessor::process by custom finder
81
     *
82
     * @param mixed $expected
83
     * @dataProvider processProvider
84
     */
85
    public function testProcessByFinder(array $query, $expected)
86
    {
87
        $actual = $this->Post->find('lampager', $query);
88
        $this->assertInstanceOf(PaginationResult::class, $actual);
89
        $this->assertSame($expected, (array)$actual);
90
    }
91
92
    /**
93
     * Test LampagerArrayProcessor::process by PaginatorComponent
94
     *
95
     * @param mixed $expected
96
     * @dataProvider processProvider
97
     */
98
    public function testProcessByComponent(array $query, $expected)
99
    {
100
        $this->Paginator->settings = $query;
101
        $actual = $this->Paginator->paginate('Post');
102
        $this->assertInstanceOf(PaginationResult::class, $actual);
103
        $this->assertSame($expected, (array)$actual);
104
    }
105
106
    public function processProvider()
107
    {
108
        yield 'Ascending forward start inclusive' => [
109
            [
110
                'forward' => true,
111
                'seekable' => true,
112
                'limit' => 3,
113
                'order' => [
114
                    'Post.modified' => 'ASC',
115
                    'Post.id' => 'ASC',
116
                ],
117
            ],
118
            [
119
                'records' => [
120
                    [
121
                        'Post' => [
122
                            'id' => '1',
123
                            'modified' => '2017-01-01 10:00:00',
124
                        ],
125
                    ],
126
                    [
127
                        'Post' => [
128
                            'id' => '3',
129
                            'modified' => '2017-01-01 10:00:00',
130
                        ],
131
                    ],
132
                    [
133
                        'Post' => [
134
                            'id' => '5',
135
                            'modified' => '2017-01-01 10:00:00',
136
                        ],
137
                    ],
138
                ],
139
                'hasPrevious' => null,
140
                'previousCursor' => null,
141
                'hasNext' => true,
142
                'nextCursor' => [
143
                    'Post' => [
144
                        'id' => '2',
145
                        'modified' => '2017-01-01 11:00:00',
146
                    ],
147
                ],
148
            ],
149
        ];
150
151
        yield 'Ascending forward start exclusive' => [
152
            [
153
                'forward' => true,
154
                'seekable' => true,
155
                'exclusive' => true,
156
                'limit' => 3,
157
                'order' => [
158
                    'Post.modified' => 'ASC',
159
                    'Post.id' => 'ASC',
160
                ],
161
            ],
162
            [
163
                'records' => [
164
                    [
165
                        'Post' => [
166
                            'id' => '1',
167
                            'modified' => '2017-01-01 10:00:00',
168
                        ],
169
                    ],
170
                    [
171
                        'Post' => [
172
                            'id' => '3',
173
                            'modified' => '2017-01-01 10:00:00',
174
                        ],
175
                    ],
176
                    [
177
                        'Post' => [
178
                            'id' => '5',
179
                            'modified' => '2017-01-01 10:00:00',
180
                        ],
181
                    ],
182
                ],
183
                'hasPrevious' => null,
184
                'previousCursor' => null,
185
                'hasNext' => true,
186
                'nextCursor' => [
187
                    'Post' => [
188
                        'id' => '5',
189
                        'modified' => '2017-01-01 10:00:00',
190
                    ],
191
                ],
192
            ],
193
        ];
194
195
        yield 'Ascending forward inclusive' => [
196
            [
197
                'forward' => true,
198
                'seekable' => true,
199
                'limit' => 3,
200
                'cursor' => [
201
                    'Post' => [
202
                        'id' => '3',
203
                        'modified' => '2017-01-01 10:00:00',
204
                    ],
205
                ],
206
                'order' => [
207
                    'Post.modified' => 'ASC',
208
                    'Post.id' => 'ASC',
209
                ],
210
            ],
211
            [
212
                'records' => [
213
                    [
214
                        'Post' => [
215
                            'id' => '3',
216
                            'modified' => '2017-01-01 10:00:00',
217
                        ],
218
                    ],
219
                    [
220
                        'Post' => [
221
                            'id' => '5',
222
                            'modified' => '2017-01-01 10:00:00',
223
                        ],
224
                    ],
225
                    [
226
                        'Post' => [
227
                            'id' => '2',
228
                            'modified' => '2017-01-01 11:00:00',
229
                        ],
230
                    ],
231
                ],
232
                'hasPrevious' => true,
233
                'previousCursor' => [
234
                    'Post' => [
235
                        'id' => '1',
236
                        'modified' => '2017-01-01 10:00:00',
237
                    ],
238
                ],
239
                'hasNext' => true,
240
                'nextCursor' => [
241
                    'Post' => [
242
                        'id' => '4',
243
                        'modified' => '2017-01-01 11:00:00',
244
                    ],
245
                ],
246
            ],
247
        ];
248
249
        yield 'Ascending forward exclusive' => [
250
            [
251
                'forward' => true,
252
                'seekable' => true,
253
                'exclusive' => true,
254
                'limit' => 3,
255
                'cursor' => [
256
                    'Post' => [
257
                        'id' => '3',
258
                        'modified' => '2017-01-01 10:00:00',
259
                    ],
260
                ],
261
                'order' => [
262
                    'Post.modified' => 'ASC',
263
                    'Post.id' => 'ASC',
264
                ],
265
            ],
266
            [
267
                'records' => [
268
                    [
269
                        'Post' => [
270
                            'id' => '5',
271
                            'modified' => '2017-01-01 10:00:00',
272
                        ],
273
                    ],
274
                    [
275
                        'Post' => [
276
                            'id' => '2',
277
                            'modified' => '2017-01-01 11:00:00',
278
                        ],
279
                    ],
280
                    [
281
                        'Post' => [
282
                            'id' => '4',
283
                            'modified' => '2017-01-01 11:00:00',
284
                        ],
285
                    ],
286
                ],
287
                'hasPrevious' => true,
288
                'previousCursor' => [
289
                    'Post' => [
290
                        'id' => '5',
291
                        'modified' => '2017-01-01 10:00:00',
292
                    ],
293
                ],
294
                'hasNext' => false,
295
                'nextCursor' => null,
296
            ],
297
        ];
298
299
        yield 'Ascending backward start inclusive' => [
300
            [
301
                'backward' => true,
302
                'seekable' => true,
303
                'limit' => 3,
304
                'order' => [
305
                    'Post.modified' => 'ASC',
306
                    'Post.id' => 'ASC',
307
                ],
308
            ],
309
            [
310
                'records' => [
311
                    [
312
                        'Post' => [
313
                            'id' => '5',
314
                            'modified' => '2017-01-01 10:00:00',
315
                        ],
316
                    ],
317
                    [
318
                        'Post' => [
319
                            'id' => '2',
320
                            'modified' => '2017-01-01 11:00:00',
321
                        ],
322
                    ],
323
                    [
324
                        'Post' => [
325
                            'id' => '4',
326
                            'modified' => '2017-01-01 11:00:00',
327
                        ],
328
                    ],
329
                ],
330
                'hasPrevious' => true,
331
                'previousCursor' => [
332
                    'Post' => [
333
                        'id' => '3',
334
                        'modified' => '2017-01-01 10:00:00',
335
                    ],
336
                ],
337
                'hasNext' => null,
338
                'nextCursor' => null,
339
            ],
340
        ];
341
342
        yield 'Ascending backward start exclusive' => [
343
            [
344
                'backward' => true,
345
                'seekable' => true,
346
                'exclusive' => true,
347
                'limit' => 3,
348
                'order' => [
349
                    'Post.modified' => 'ASC',
350
                    'Post.id' => 'ASC',
351
                ],
352
            ],
353
            [
354
                'records' => [
355
                    [
356
                        'Post' => [
357
                            'id' => '5',
358
                            'modified' => '2017-01-01 10:00:00',
359
                        ],
360
                    ],
361
                    [
362
                        'Post' => [
363
                            'id' => '2',
364
                            'modified' => '2017-01-01 11:00:00',
365
                        ],
366
                    ],
367
                    [
368
                        'Post' => [
369
                            'id' => '4',
370
                            'modified' => '2017-01-01 11:00:00',
371
                        ],
372
                    ],
373
                ],
374
                'hasPrevious' => true,
375
                'previousCursor' => [
376
                    'Post' => [
377
                        'id' => '5',
378
                        'modified' => '2017-01-01 10:00:00',
379
                    ],
380
                ],
381
                'hasNext' => null,
382
                'nextCursor' => null,
383
            ],
384
        ];
385
386
        yield 'Ascending backward inclusive' => [
387
            [
388
                'backward' => true,
389
                'seekable' => true,
390
                'limit' => 3,
391
                'cursor' => [
392
                    'Post' => [
393
                        'id' => '3',
394
                        'modified' => '2017-01-01 10:00:00',
395
                    ],
396
                ],
397
                'order' => [
398
                    'Post.modified' => 'ASC',
399
                    'Post.id' => 'ASC',
400
                ],
401
            ],
402
            [
403
                'records' => [
404
                    [
405
                        'Post' => [
406
                            'id' => '1',
407
                            'modified' => '2017-01-01 10:00:00',
408
                        ],
409
                    ],
410
                    [
411
                        'Post' => [
412
                            'id' => '3',
413
                            'modified' => '2017-01-01 10:00:00',
414
                        ],
415
                    ],
416
                ],
417
                'hasPrevious' => false,
418
                'previousCursor' => null,
419
                'hasNext' => true,
420
                'nextCursor' => [
421
                    'Post' => [
422
                        'id' => '5',
423
                        'modified' => '2017-01-01 10:00:00',
424
                    ],
425
                ],
426
            ],
427
        ];
428
429
        yield 'Ascending backward exclusive' => [
430
            [
431
                'backward' => true,
432
                'seekable' => true,
433
                'exclusive' => true,
434
                'limit' => 3,
435
                'cursor' => [
436
                    'Post' => [
437
                        'id' => '3',
438
                        'modified' => '2017-01-01 10:00:00',
439
                    ],
440
                ],
441
                'order' => [
442
                    'Post.modified' => 'ASC',
443
                    'Post.id' => 'ASC',
444
                ],
445
            ],
446
            [
447
                'records' => [
448
                    [
449
                        'Post' => [
450
                            'id' => '1',
451
                            'modified' => '2017-01-01 10:00:00',
452
                        ],
453
                    ],
454
                ],
455
                'hasPrevious' => false,
456
                'previousCursor' => null,
457
                'hasNext' => true,
458
                'nextCursor' => [
459
                    'Post' => [
460
                        'id' => '1',
461
                        'modified' => '2017-01-01 10:00:00',
462
                    ],
463
                ],
464
            ],
465
        ];
466
467
        yield 'Descending forward start inclusive' => [
468
            [
469
                'forward' => true,
470
                'seekable' => true,
471
                'limit' => 3,
472
                'order' => [
473
                    'Post.modified' => 'DESC',
474
                    'Post.id' => 'DESC',
475
                ],
476
            ],
477
            [
478
                'records' => [
479
                    [
480
                        'Post' => [
481
                            'id' => '4',
482
                            'modified' => '2017-01-01 11:00:00',
483
                        ],
484
                    ],
485
                    [
486
                        'Post' => [
487
                            'id' => '2',
488
                            'modified' => '2017-01-01 11:00:00',
489
                        ],
490
                    ],
491
                    [
492
                        'Post' => [
493
                            'id' => '5',
494
                            'modified' => '2017-01-01 10:00:00',
495
                        ],
496
                    ],
497
                ],
498
                'hasPrevious' => null,
499
                'previousCursor' => null,
500
                'hasNext' => true,
501
                'nextCursor' => [
502
                    'Post' => [
503
                        'id' => '3',
504
                        'modified' => '2017-01-01 10:00:00',
505
                    ],
506
                ],
507
            ],
508
        ];
509
510
        yield 'Descending forward start exclusive' => [
511
            [
512
                'forward' => true,
513
                'seekable' => true,
514
                'exclusive' => true,
515
                'limit' => 3,
516
                'order' => [
517
                    'Post.modified' => 'DESC',
518
                    'Post.id' => 'DESC',
519
                ],
520
            ],
521
            [
522
                'records' => [
523
                    [
524
                        'Post' => [
525
                            'id' => '4',
526
                            'modified' => '2017-01-01 11:00:00',
527
                        ],
528
                    ],
529
                    [
530
                        'Post' => [
531
                            'id' => '2',
532
                            'modified' => '2017-01-01 11:00:00',
533
                        ],
534
                    ],
535
                    [
536
                        'Post' => [
537
                            'id' => '5',
538
                            'modified' => '2017-01-01 10:00:00',
539
                        ],
540
                    ],
541
                ],
542
                'hasPrevious' => null,
543
                'previousCursor' => null,
544
                'hasNext' => true,
545
                'nextCursor' => [
546
                    'Post' => [
547
                        'id' => '5',
548
                        'modified' => '2017-01-01 10:00:00',
549
                    ],
550
                ],
551
            ],
552
        ];
553
554
        yield 'Descending forward inclusive' => [
555
            [
556
                'forward' => true,
557
                'seekable' => true,
558
                'limit' => 3,
559
                'cursor' => [
560
                    'Post' => [
561
                        'id' => '3',
562
                        'modified' => '2017-01-01 10:00:00',
563
                    ],
564
                ],
565
                'order' => [
566
                    'Post.modified' => 'DESC',
567
                    'Post.id' => 'DESC',
568
                ],
569
            ],
570
            [
571
                'records' => [
572
                    [
573
                        'Post' => [
574
                            'id' => '3',
575
                            'modified' => '2017-01-01 10:00:00',
576
                        ],
577
                    ],
578
                    [
579
                        'Post' => [
580
                            'id' => '1',
581
                            'modified' => '2017-01-01 10:00:00',
582
                        ],
583
                    ],
584
                ],
585
                'hasPrevious' => true,
586
                'previousCursor' => [
587
                    'Post' => [
588
                        'id' => '5',
589
                        'modified' => '2017-01-01 10:00:00',
590
                    ],
591
                ],
592
                'hasNext' => false,
593
                'nextCursor' => null,
594
            ],
595
        ];
596
597
        yield 'Descending forward exclusive' => [
598
            [
599
                'forward' => true,
600
                'seekable' => true,
601
                'exclusive' => true,
602
                'limit' => 3,
603
                'cursor' => [
604
                    'Post' => [
605
                        'id' => '3',
606
                        'modified' => '2017-01-01 10:00:00',
607
                    ],
608
                ],
609
                'order' => [
610
                    'Post.modified' => 'DESC',
611
                    'Post.id' => 'DESC',
612
                ],
613
            ],
614
            [
615
                'records' => [
616
                    [
617
                        'Post' => [
618
                            'id' => '1',
619
                            'modified' => '2017-01-01 10:00:00',
620
                        ],
621
                    ],
622
                ],
623
                'hasPrevious' => true,
624
                'previousCursor' => [
625
                    'Post' => [
626
                        'id' => '1',
627
                        'modified' => '2017-01-01 10:00:00',
628
                    ],
629
                ],
630
                'hasNext' => false,
631
                'nextCursor' => null,
632
            ],
633
        ];
634
635
        yield 'Descending backward start inclusive' => [
636
            [
637
                'backward' => true,
638
                'seekable' => true,
639
                'limit' => 3,
640
                'order' => [
641
                    'Post.modified' => 'DESC',
642
                    'Post.id' => 'DESC',
643
                ],
644
            ],
645
            [
646
                'records' => [
647
                    [
648
                        'Post' => [
649
                            'id' => '5',
650
                            'modified' => '2017-01-01 10:00:00',
651
                        ],
652
                    ],
653
                    [
654
                        'Post' => [
655
                            'id' => '3',
656
                            'modified' => '2017-01-01 10:00:00',
657
                        ],
658
                    ],
659
                    [
660
                        'Post' => [
661
                            'id' => '1',
662
                            'modified' => '2017-01-01 10:00:00',
663
                        ],
664
                    ],
665
                ],
666
                'hasPrevious' => true,
667
                'previousCursor' => [
668
                    'Post' => [
669
                        'id' => '2',
670
                        'modified' => '2017-01-01 11:00:00',
671
                    ],
672
                ],
673
                'hasNext' => null,
674
                'nextCursor' => null,
675
            ],
676
        ];
677
678
        yield 'Descending backward start exclusive' => [
679
            [
680
                'backward' => true,
681
                'seekable' => true,
682
                'exclusive' => true,
683
                'limit' => 3,
684
                'order' => [
685
                    'Post.modified' => 'DESC',
686
                    'Post.id' => 'DESC',
687
                ],
688
            ],
689
            [
690
                'records' => [
691
                    [
692
                        'Post' => [
693
                            'id' => '5',
694
                            'modified' => '2017-01-01 10:00:00',
695
                        ],
696
                    ],
697
                    [
698
                        'Post' => [
699
                            'id' => '3',
700
                            'modified' => '2017-01-01 10:00:00',
701
                        ],
702
                    ],
703
                    [
704
                        'Post' => [
705
                            'id' => '1',
706
                            'modified' => '2017-01-01 10:00:00',
707
                        ],
708
                    ],
709
                ],
710
                'hasPrevious' => true,
711
                'previousCursor' => [
712
                    'Post' => [
713
                        'id' => '5',
714
                        'modified' => '2017-01-01 10:00:00',
715
                    ],
716
                ],
717
                'hasNext' => null,
718
                'nextCursor' => null,
719
            ],
720
        ];
721
722
        yield 'Descending backward inclusive' => [
723
            [
724
                'backward' => true,
725
                'seekable' => true,
726
                'limit' => 3,
727
                'cursor' => [
728
                    'Post' => [
729
                        'id' => '3',
730
                        'modified' => '2017-01-01 10:00:00',
731
                    ],
732
                ],
733
                'order' => [
734
                    'Post.modified' => 'desc',
735
                    'Post.id' => 'desc',
736
                ],
737
            ],
738
            [
739
                'records' => [
740
                    [
741
                        'Post' => [
742
                            'id' => '2',
743
                            'modified' => '2017-01-01 11:00:00',
744
                        ],
745
                    ],
746
                    [
747
                        'Post' => [
748
                            'id' => '5',
749
                            'modified' => '2017-01-01 10:00:00',
750
                        ],
751
                    ],
752
                    [
753
                        'Post' => [
754
                            'id' => '3',
755
                            'modified' => '2017-01-01 10:00:00',
756
                        ],
757
                    ],
758
                ],
759
                'hasPrevious' => true,
760
                'previousCursor' => [
761
                    'Post' => [
762
                        'id' => '4',
763
                        'modified' => '2017-01-01 11:00:00',
764
                    ],
765
                ],
766
                'hasNext' => true,
767
                'nextCursor' => [
768
                    'Post' => [
769
                        'id' => '1',
770
                        'modified' => '2017-01-01 10:00:00',
771
                    ],
772
                ],
773
            ],
774
        ];
775
776
        yield 'Descending backward exclusive' => [
777
            [
778
                'backward' => true,
779
                'seekable' => true,
780
                'exclusive' => true,
781
                'limit' => 3,
782
                'cursor' => [
783
                    'Post' => [
784
                        'id' => '3',
785
                        'modified' => '2017-01-01 10:00:00',
786
                    ],
787
                ],
788
                'order' => [
789
                    'Post.modified' => 'desc',
790
                    'Post.id' => 'desc',
791
                ],
792
            ],
793
            [
794
                'records' => [
795
                    [
796
                        'Post' => [
797
                            'id' => '4',
798
                            'modified' => '2017-01-01 11:00:00',
799
                        ],
800
                    ],
801
                    [
802
                        'Post' => [
803
                            'id' => '2',
804
                            'modified' => '2017-01-01 11:00:00',
805
                        ],
806
                    ],
807
                    [
808
                        'Post' => [
809
                            'id' => '5',
810
                            'modified' => '2017-01-01 10:00:00',
811
                        ],
812
                    ],
813
                ],
814
                'hasPrevious' => false,
815
                'previousCursor' => null,
816
                'hasNext' => true,
817
                'nextCursor' => [
818
                    'Post' => [
819
                        'id' => '5',
820
                        'modified' => '2017-01-01 10:00:00',
821
                    ],
822
                ],
823
            ],
824
        ];
825
    }
826
}
827