Passed
Push — master ( 381e35...37729d )
by Chito
01:51
created

LampagerArrayProcessorTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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', 'Test/Case');
8
App::uses('LampagerArrayCursor', 'Model');
9
App::uses('LampagerArrayProcessor', 'Model');
10
11
use Lampager\Query\Order;
12
13
class LampagerArrayProcessorTest extends LampagerTestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
        'app.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 object<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');
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 method CakeTestCase::getMock() has been deprecated with message: Use `getMockBuilder()` or `createMock()` in new unit tests.

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

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

Loading history...
49
        $this->Paginator->Controller = $this->Controller;
0 ignored issues
show
Bug introduced by
The property Controller does not seem to exist in PaginatorComponent.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

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