Passed
Push — master ( 288a96...964b09 )
by Daniel
10:30
created

tests/php/ORM/PaginatedListTest.php (2 issues)

1
<?php
2
3
namespace SilverStripe\ORM\Tests;
4
5
use SilverStripe\ORM\ArrayList;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\PaginatedList;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\ORM\Queries\SQLSelect;
10
use SilverStripe\ORM\Tests\DataObjectTest\Player;
11
use SilverStripe\View\ArrayData;
12
use SilverStripe\Control\HTTPRequest;
13
14
/**
15
 * Tests for the {@link SilverStripe\ORM\PaginatedList} class.
16
 */
17
class PaginatedListTest extends SapphireTest
18
{
19
20
    protected static $fixture_file = 'DataObjectTest.yml';
21
22
    public static function getExtraDataObjects()
23
    {
24
        return array_merge(
25
            DataObjectTest::$extra_data_objects,
26
            ManyManyListTest::$extra_data_objects
27
        );
28
    }
29
30
    public function testPageStart()
31
    {
32
        $list = new PaginatedList(new ArrayList());
33
        $this->assertEquals(0, $list->getPageStart(), 'The start defaults to 0.');
34
35
        $list->setPageStart(10);
36
        $this->assertEquals(10, $list->getPageStart(), 'You can set the page start.');
37
38
        $list = new PaginatedList(new ArrayList(), array('start' => 50));
39
        $this->assertEquals(50, $list->getPageStart(), 'The page start can be read from the request.');
40
    }
41
42
    public function testGetTotalItems()
43
    {
44
        $list = new PaginatedList(new ArrayList());
45
        $this->assertEquals(0, $list->getTotalItems());
46
47
        $list->setTotalItems(10);
48
        $this->assertEquals(10, $list->getTotalItems());
49
50
        $list = new PaginatedList(
51
            new ArrayList(
52
                array(
53
                new ArrayData(array()),
54
                new ArrayData(array())
55
                )
56
            )
57
        );
58
        $this->assertEquals(2, $list->getTotalItems());
59
    }
60
61
    public function testSetPaginationFromQuery()
62
    {
63
        $query = $this->getMockBuilder(SQLSelect::class)->getMock();
64
        $query->expects($this->once())
65
            ->method('getLimit')
66
            ->will($this->returnValue(array('limit' => 15, 'start' => 30)));
67
        $query->expects($this->once())
68
            ->method('unlimitedRowCount')
69
            ->will($this->returnValue(100));
70
71
        $list = new PaginatedList(new ArrayList());
72
        $list->setPaginationFromQuery($query);
73
74
        $this->assertEquals(15, $list->getPageLength());
75
        $this->assertEquals(30, $list->getPageStart());
76
        $this->assertEquals(100, $list->getTotalItems());
77
    }
78
79
    public function testSetCurrentPage()
80
    {
81
        $list = new PaginatedList(new ArrayList());
82
        $list->setPageLength(10);
83
        $list->setCurrentPage(10);
84
85
        $this->assertEquals(10, $list->CurrentPage());
86
        $this->assertEquals(90, $list->getPageStart());
87
88
        // Test disabled paging
89
        $list->setPageLength(0);
90
        $this->assertEquals(1, $list->CurrentPage());
91
    }
92
93
    public function testGetIterator()
94
    {
95
        $list = new PaginatedList(
96
            new ArrayList([
97
                new DataObject(array('Num' => 1)),
98
                new DataObject(array('Num' => 2)),
99
                new DataObject(array('Num' => 3)),
100
                new DataObject(array('Num' => 4)),
101
                new DataObject(array('Num' => 5)),
102
            ])
103
        );
104
        $list->setPageLength(2);
105
106
        $this->assertListEquals(
107
            array(array('Num' => 1), array('Num' => 2)),
108
            ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy())
0 ignored issues
show
The method getArrayCopy() does not exist on Iterator. It seems like you code against a sub-type of Iterator such as ArrayIterator or RecursiveArrayIterator or Behat\Testwork\Specifica...cificationArrayIterator. ( Ignorable by Annotation )

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

108
            ArrayList::create($list->getIterator()->getInnerIterator()->/** @scrutinizer ignore-call */ getArrayCopy())
Loading history...
109
        );
110
111
        $list->setCurrentPage(2);
112
        $this->assertListEquals(
113
            array(array('Num' => 3), array('Num' => 4)),
114
            ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy())
115
        );
116
117
        $list->setCurrentPage(3);
118
        $this->assertListEquals(
119
            array(array('Num' => 5)),
120
            ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy())
121
        );
122
123
        $list->setCurrentPage(999);
124
        $this->assertListEquals(
125
            array(),
126
            ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy())
127
        );
128
129
        // Test disabled paging
130
        $list->setPageLength(0);
131
        $list->setCurrentPage(1);
132
        $this->assertListEquals(
133
            [
134
                array('Num' => 1),
135
                array('Num' => 2),
136
                array('Num' => 3),
137
                array('Num' => 4),
138
                array('Num' => 5),
139
            ],
140
            ArrayList::create($list->getIterator()->getInnerIterator()->getArrayCopy())
141
        );
142
143
        // Test with dataobjectset
144
        $players = Player::get();
145
        $list = new PaginatedList($players);
146
        $list->setPageLength(1);
147
        $list->getIterator();
148
        $this->assertEquals(
149
            4,
150
            $list->getTotalItems(),
151
            'Getting an iterator should not trim the list to the page length.'
152
        );
153
    }
154
155
    public function testPages()
156
    {
157
        $list = new PaginatedList(new ArrayList());
158
        $list->setPageLength(10);
159
        $list->setTotalItems(50);
160
161
        $this->assertCount(5, $list->Pages());
162
        $this->assertCount(3, $list->Pages(3));
163
        $this->assertCount(5, $list->Pages(15));
164
165
        $list->setCurrentPage(3);
166
167
        $expectAll = array(
168
            array('PageNum' => 1),
169
            array('PageNum' => 2),
170
            array('PageNum' => 3, 'CurrentBool' => true),
171
            array('PageNum' => 4),
172
            array('PageNum' => 5),
173
        );
174
        $this->assertListEquals($expectAll, $list->Pages());
175
176
        $expectLimited = array(
177
            array('PageNum' => 2),
178
            array('PageNum' => 3, 'CurrentBool' => true),
179
            array('PageNum' => 4),
180
        );
181
        $this->assertListEquals($expectLimited, $list->Pages(3));
182
183
        // Disable paging
184
        $list->setPageLength(0);
185
        $expectAll = array(
186
            array('PageNum' => 1, 'CurrentBool' => true),
187
        );
188
        $this->assertListEquals($expectAll, $list->Pages());
189
    }
190
191
    public function testPaginationSummary()
192
    {
193
        $list = new PaginatedList(new ArrayList());
194
195
        $list->setPageLength(10);
196
        $list->setTotalItems(250);
197
        $list->setCurrentPage(6);
198
199
        $expect = array(
200
            array('PageNum' => 1),
201
            array('PageNum' => null),
202
            array('PageNum' => 4),
203
            array('PageNum' => 5),
204
            array('PageNum' => 6, 'CurrentBool' => true),
205
            array('PageNum' => 7),
206
            array('PageNum' => 8),
207
            array('PageNum' => null),
208
            array('PageNum' => 25),
209
        );
210
        $this->assertListEquals($expect, $list->PaginationSummary(4));
211
212
        // Disable paging
213
        $list->setPageLength(0);
214
        $expect = array(
215
            array('PageNum' => 1, 'CurrentBool' => true)
216
        );
217
        $this->assertListEquals($expect, $list->PaginationSummary(4));
218
    }
219
220
    public function testLimitItems()
221
    {
222
        $list = new ArrayList(range(1, 50));
223
        $list = new PaginatedList($list);
224
225
        $list->setCurrentPage(3);
226
        $this->assertCount(10, $list->getIterator()->getInnerIterator());
0 ignored issues
show
$list->getIterator()->getInnerIterator() of type Iterator is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount(). ( Ignorable by Annotation )

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

226
        $this->assertCount(10, /** @scrutinizer ignore-type */ $list->getIterator()->getInnerIterator());
Loading history...
227
228
        $list->setLimitItems(false);
229
        $this->assertCount(50, $list->getIterator()->getInnerIterator());
230
    }
231
232
    public function testCurrentPage()
233
    {
234
        $list = new PaginatedList(new ArrayList());
235
        $list->setTotalItems(50);
236
237
        $this->assertEquals(1, $list->CurrentPage());
238
        $list->setPageStart(10);
239
        $this->assertEquals(2, $list->CurrentPage());
240
        $list->setPageStart(40);
241
        $this->assertEquals(5, $list->CurrentPage());
242
243
        // Disable paging
244
        $list->setPageLength(0);
245
        $this->assertEquals(1, $list->CurrentPage());
246
    }
247
248
    public function testTotalPages()
249
    {
250
        $list = new PaginatedList(new ArrayList());
251
252
        $list->setPageLength(1);
253
        $this->assertEquals(0, $list->TotalPages());
254
255
        $list->setTotalItems(1);
256
        $this->assertEquals(1, $list->TotalPages());
257
258
        $list->setTotalItems(5);
259
        $this->assertEquals(5, $list->TotalPages());
260
261
        // Disable paging
262
        $list->setPageLength(0);
263
        $this->assertEquals(1, $list->TotalPages());
264
265
        $list->setTotalItems(0);
266
        $this->assertEquals(0, $list->TotalPages());
267
    }
268
269
    public function testMoreThanOnePage()
270
    {
271
        $list = new PaginatedList(new ArrayList());
272
273
        $list->setPageLength(1);
274
        $list->setTotalItems(1);
275
        $this->assertFalse($list->MoreThanOnePage());
276
277
        $list->setTotalItems(2);
278
        $this->assertTrue($list->MoreThanOnePage());
279
280
        // Disable paging
281
        $list->setPageLength(0);
282
        $this->assertFalse($list->MoreThanOnePage());
283
    }
284
285
    public function testNotFirstPage()
286
    {
287
        $list = new PaginatedList(new ArrayList());
288
        $this->assertFalse($list->NotFirstPage());
289
        $list->setCurrentPage(2);
290
        $this->assertTrue($list->NotFirstPage());
291
    }
292
293
    public function testNotLastPage()
294
    {
295
        $list = new PaginatedList(new ArrayList());
296
        $list->setTotalItems(50);
297
298
        $this->assertTrue($list->NotLastPage());
299
        $list->setCurrentPage(5);
300
        $this->assertFalse($list->NotLastPage());
301
    }
302
303
    public function testFirstItem()
304
    {
305
        $list = new PaginatedList(new ArrayList());
306
        $this->assertEquals(1, $list->FirstItem());
307
        $list->setPageStart(10);
308
        $this->assertEquals(11, $list->FirstItem());
309
    }
310
311
    public function testLastItem()
312
    {
313
        $list = new PaginatedList(new ArrayList());
314
        $list->setPageLength(10);
315
        $list->setTotalItems(25);
316
317
        $list->setCurrentPage(1);
318
        $this->assertEquals(10, $list->LastItem());
319
        $list->setCurrentPage(2);
320
        $this->assertEquals(20, $list->LastItem());
321
        $list->setCurrentPage(3);
322
        $this->assertEquals(25, $list->LastItem());
323
324
        // Disable paging
325
        $list->setPageLength(0);
326
        $this->assertEquals(25, $list->LastItem());
327
    }
328
329
    public function testFirstLink()
330
    {
331
        $list = new PaginatedList(new ArrayList());
332
        $this->assertContains('start=0', $list->FirstLink());
333
    }
334
335
    public function testFirstLinkContainsCurrentGetParameters()
336
    {
337
        $request = new HTTPRequest(
338
            'GET',
339
            'http://example.com/my-cool-page',
340
            ['awesomeness' => 'nextLevel', 'start' => 20]
341
        );
342
        $list = new PaginatedList(new ArrayList(), $request);
343
        $list->setTotalItems(50);
344
        $list->setPageLength(10);
345
346
        // check the query string has correct parameters
347
        $queryString = parse_url($list->FirstLink(), PHP_URL_QUERY);
348
        parse_str($queryString, $queryParams);
349
350
        $this->assertArrayHasKey('awesomeness', $queryParams);
351
        $this->assertequals('nextLevel', $queryParams['awesomeness']);
352
        $this->assertArrayHasKey('start', $queryParams);
353
        $this->assertequals(0, $queryParams['start']);
354
    }
355
356
    public function testLastLink()
357
    {
358
        $list = new PaginatedList(new ArrayList());
359
        $list->setPageLength(10);
360
        $list->setTotalItems(100);
361
        $this->assertContains('start=90', $list->LastLink());
362
363
        // Disable paging
364
        $list->setPageLength(0);
365
        $this->assertContains('start=0', $list->LastLink());
366
    }
367
368
    public function testLastLinkContainsCurrentGetParameters()
369
    {
370
        $request = new HTTPRequest(
371
            'GET',
372
            'http://example.com/my-cool-page',
373
            ['awesomeness' => 'nextLevel']
374
        );
375
        $list = new PaginatedList(new ArrayList(), $request);
376
        $list->setTotalItems(50);
377
        $list->setPageLength(10);
378
379
        // check the query string has correct parameters
380
        $queryString = parse_url($list->LastLink(), PHP_URL_QUERY);
381
        parse_str($queryString, $queryParams);
382
383
        $this->assertArrayHasKey('awesomeness', $queryParams);
384
        $this->assertequals('nextLevel', $queryParams['awesomeness']);
385
        $this->assertArrayHasKey('start', $queryParams);
386
        $this->assertequals(40, $queryParams['start']);
387
    }
388
389
    public function testNextLink()
390
    {
391
        $list = new PaginatedList(new ArrayList());
392
        $list->setTotalItems(50);
393
394
        $this->assertContains('start=10', $list->NextLink());
395
        $list->setCurrentPage(2);
396
        $this->assertContains('start=20', $list->NextLink());
397
        $list->setCurrentPage(3);
398
        $this->assertContains('start=30', $list->NextLink());
399
        $list->setCurrentPage(4);
400
        $this->assertContains('start=40', $list->NextLink());
401
        $list->setCurrentPage(5);
402
        $this->assertNull($list->NextLink());
403
404
        // Disable paging
405
        $list->setCurrentPage(1);
406
        $list->setPageLength(0);
407
        $this->assertNull($list->NextLink());
408
    }
409
410
    public function testNextLinkContainsCurrentGetParameters()
411
    {
412
        $request = new HTTPRequest(
413
            'GET',
414
            'http://example.com/my-cool-page',
415
            ['awesomeness' => 'nextLevel']
416
        );
417
        $list = new PaginatedList(new ArrayList(), $request);
418
        $list->setTotalItems(50);
419
        $list->setPageLength(10);
420
421
        // check the query string has correct parameters
422
        $queryString = parse_url($list->NextLink(), PHP_URL_QUERY);
423
        parse_str($queryString, $queryParams);
424
425
        $this->assertArrayHasKey('awesomeness', $queryParams);
426
        $this->assertequals('nextLevel', $queryParams['awesomeness']);
427
        $this->assertArrayHasKey('start', $queryParams);
428
        $this->assertequals(10, $queryParams['start']);
429
    }
430
431
    public function testPrevLink()
432
    {
433
        $list = new PaginatedList(new ArrayList());
434
        $list->setTotalItems(50);
435
436
        $this->assertNull($list->PrevLink());
437
        $list->setCurrentPage(2);
438
        $this->assertContains('start=0', $list->PrevLink());
439
        $list->setCurrentPage(3);
440
        $this->assertContains('start=10', $list->PrevLink());
441
        $list->setCurrentPage(5);
442
        $this->assertContains('start=30', $list->PrevLink());
443
444
        // Disable paging
445
        $list->setPageLength(0);
446
        $this->assertNull($list->PrevLink());
447
    }
448
449
    public function testPrevLinkContainsCurrentGetParameters()
450
    {
451
        $request = new HTTPRequest(
452
            'GET',
453
            'http://example.com/my-cool-page',
454
            ['awesomeness' => 'nextLevel', 'start' => '30']
455
        );
456
        $list = new PaginatedList(new ArrayList(), $request);
457
        $list->setTotalItems(50);
458
        $list->setPageLength(10);
459
460
        // check the query string has correct parameters
461
        $queryString = parse_url($list->PrevLink(), PHP_URL_QUERY);
462
        parse_str($queryString, $queryParams);
463
464
        $this->assertArrayHasKey('awesomeness', $queryParams);
465
        $this->assertequals('nextLevel', $queryParams['awesomeness']);
466
        $this->assertArrayHasKey('start', $queryParams);
467
        $this->assertequals(20, $queryParams['start']);
468
    }
469
}
470