Passed
Pull Request — master (#7)
by Chito
02:51
created

LampagerArrayProcessorTest::processProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 720

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 720
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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('LampagerTestCase', 'Lampager.Test/Case');
8
App::uses('LampagerArrayCursor', 'Lampager.Model');
9
App::uses('LampagerArrayProcessor', 'Lampager.Model');
10
11
use Lampager\PaginationResult;
12
13
class LampagerArrayProcessorTest extends LampagerTestCase
14
{
15
    /** @var Model */
16
    protected $Post;
17
18
    /** @var CakeRequest */
19
    protected $request;
20
21
    /** @var Controller */
22
    protected $Controller;
23
24
    /** @var PaginatorComponent */
25
    protected $Paginator;
26
27
    /** @var string[] */
28
    public $fixtures = [
29
        'plugin.Lampager.Post',
30
    ];
31
32
    public function setUp()
33
    {
34
        parent::setUp();
35
36
        // Prepare for ModelBehavior
37
        $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...
38
        $this->Post->Behaviors->load('Lampager.Lampager');
39
40
        // Prepare for PaginatorComponent
41
        $this->request = new CakeRequest('posts/index');
42
        $this->request->params['pass'] = [];
43
        $this->request->params['named'] = [];
44
45
        $this->Controller = new Controller($this->request);
46
        $this->Controller->Post = $this->Post;
47
48
        $this->Paginator = new PaginatorComponent($this->getMock(ComponentCollection::class), []);
0 ignored issues
show
Deprecated Code introduced by
The function CakeTestCase::getMock() has been deprecated: Use `getMockBuilder()` or `createMock()` in new unit tests. ( Ignorable by Annotation )

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

48
        $this->Paginator = new PaginatorComponent(/** @scrutinizer ignore-deprecated */ $this->getMock(ComponentCollection::class), []);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

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