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

PaginatedListTest::testIteration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 37
nc 1
nop 0
dl 0
loc 56
rs 9.7251
c 0
b 0
f 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
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 testIteration()
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
            $list
109
        );
110
111
        $list->setCurrentPage(2);
112
        $this->assertListEquals(
113
            array(array('Num' => 3), array('Num' => 4)),
114
            $list
115
        );
116
117
        $list->setCurrentPage(3);
118
        $this->assertListEquals(
119
            array(array('Num' => 5)),
120
            $list
121
        );
122
123
        $list->setCurrentPage(999);
124
        $this->assertDOSEquals(array(), $list);
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Dev\SapphireTest::assertDOSEquals() has been deprecated: 4.0.0:5.0.0 Use assertListEquals() instead ( Ignorable by Annotation )

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

124
        /** @scrutinizer ignore-deprecated */ $this->assertDOSEquals(array(), $list);

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...
125
126
        // Test disabled paging
127
        $list->setPageLength(0);
128
        $list->setCurrentPage(1);
129
        $this->assertListEquals(
130
            [
131
                array('Num' => 1),
132
                array('Num' => 2),
133
                array('Num' => 3),
134
                array('Num' => 4),
135
                array('Num' => 5)
136
            ],
137
            $list
138
        );
139
140
        // Test with dataobjectset
141
        $players = Player::get();
142
        $list = new PaginatedList($players);
143
        $list->setPageLength(1);
144
        $list;
145
        $this->assertEquals(
146
            4,
147
            $list->getTotalItems(),
148
            'Getting an iterator should not trim the list to the page length.'
149
        );
150
    }
151
152
    public function testPages()
153
    {
154
        $list = new PaginatedList(new ArrayList());
155
        $list->setPageLength(10);
156
        $list->setTotalItems(50);
157
158
        $this->assertCount(5, $list->Pages());
159
        $this->assertCount(3, $list->Pages(3));
160
        $this->assertCount(5, $list->Pages(15));
161
162
        $list->setCurrentPage(3);
163
164
        $expectAll = array(
165
            array('PageNum' => 1),
166
            array('PageNum' => 2),
167
            array('PageNum' => 3, 'CurrentBool' => true),
168
            array('PageNum' => 4),
169
            array('PageNum' => 5),
170
        );
171
        $this->assertListEquals($expectAll, $list->Pages());
172
173
        $expectLimited = array(
174
            array('PageNum' => 2),
175
            array('PageNum' => 3, 'CurrentBool' => true),
176
            array('PageNum' => 4),
177
        );
178
        $this->assertListEquals($expectLimited, $list->Pages(3));
179
180
        // Disable paging
181
        $list->setPageLength(0);
182
        $expectAll = array(
183
            array('PageNum' => 1, 'CurrentBool' => true),
184
        );
185
        $this->assertListEquals($expectAll, $list->Pages());
186
    }
187
188
    public function testPaginationSummary()
189
    {
190
        $list = new PaginatedList(new ArrayList());
191
192
        $list->setPageLength(10);
193
        $list->setTotalItems(250);
194
        $list->setCurrentPage(6);
195
196
        $expect = array(
197
            array('PageNum' => 1),
198
            array('PageNum' => null),
199
            array('PageNum' => 4),
200
            array('PageNum' => 5),
201
            array('PageNum' => 6, 'CurrentBool' => true),
202
            array('PageNum' => 7),
203
            array('PageNum' => 8),
204
            array('PageNum' => null),
205
            array('PageNum' => 25),
206
        );
207
        $this->assertListEquals($expect, $list->PaginationSummary(4));
208
209
        // Disable paging
210
        $list->setPageLength(0);
211
        $expect = array(
212
            array('PageNum' => 1, 'CurrentBool' => true)
213
        );
214
        $this->assertListEquals($expect, $list->PaginationSummary(4));
215
    }
216
217
    public function testLimitItems()
218
    {
219
        $list = new ArrayList(range(1, 50));
220
        $list = new PaginatedList($list);
221
222
        $list->setCurrentPage(3);
223
        $this->assertEquals(10, count($list->toArray()));
224
225
        $list->setLimitItems(false);
226
        $this->assertEquals(50, count($list->toArray()));
227
    }
228
229
    public function testCurrentPage()
230
    {
231
        $list = new PaginatedList(new ArrayList());
232
        $list->setTotalItems(50);
233
234
        $this->assertEquals(1, $list->CurrentPage());
235
        $list->setPageStart(10);
236
        $this->assertEquals(2, $list->CurrentPage());
237
        $list->setPageStart(40);
238
        $this->assertEquals(5, $list->CurrentPage());
239
240
        // Disable paging
241
        $list->setPageLength(0);
242
        $this->assertEquals(1, $list->CurrentPage());
243
    }
244
245
    public function testTotalPages()
246
    {
247
        $list = new PaginatedList(new ArrayList());
248
249
        $list->setPageLength(1);
250
        $this->assertEquals(0, $list->TotalPages());
251
252
        $list->setTotalItems(1);
253
        $this->assertEquals(1, $list->TotalPages());
254
255
        $list->setTotalItems(5);
256
        $this->assertEquals(5, $list->TotalPages());
257
258
        // Disable paging
259
        $list->setPageLength(0);
260
        $this->assertEquals(1, $list->TotalPages());
261
262
        $list->setTotalItems(0);
263
        $this->assertEquals(0, $list->TotalPages());
264
    }
265
266
    public function testMoreThanOnePage()
267
    {
268
        $list = new PaginatedList(new ArrayList());
269
270
        $list->setPageLength(1);
271
        $list->setTotalItems(1);
272
        $this->assertFalse($list->MoreThanOnePage());
273
274
        $list->setTotalItems(2);
275
        $this->assertTrue($list->MoreThanOnePage());
276
277
        // Disable paging
278
        $list->setPageLength(0);
279
        $this->assertFalse($list->MoreThanOnePage());
280
    }
281
282
    public function testNotFirstPage()
283
    {
284
        $list = new PaginatedList(new ArrayList());
285
        $this->assertFalse($list->NotFirstPage());
286
        $list->setCurrentPage(2);
287
        $this->assertTrue($list->NotFirstPage());
288
    }
289
290
    public function testNotLastPage()
291
    {
292
        $list = new PaginatedList(new ArrayList());
293
        $list->setTotalItems(50);
294
295
        $this->assertTrue($list->NotLastPage());
296
        $list->setCurrentPage(5);
297
        $this->assertFalse($list->NotLastPage());
298
    }
299
300
    public function testFirstItem()
301
    {
302
        $list = new PaginatedList(new ArrayList());
303
        $this->assertEquals(1, $list->FirstItem());
304
        $list->setPageStart(10);
305
        $this->assertEquals(11, $list->FirstItem());
306
    }
307
308
    public function testLastItem()
309
    {
310
        $list = new PaginatedList(new ArrayList());
311
        $list->setPageLength(10);
312
        $list->setTotalItems(25);
313
314
        $list->setCurrentPage(1);
315
        $this->assertEquals(10, $list->LastItem());
316
        $list->setCurrentPage(2);
317
        $this->assertEquals(20, $list->LastItem());
318
        $list->setCurrentPage(3);
319
        $this->assertEquals(25, $list->LastItem());
320
321
        // Disable paging
322
        $list->setPageLength(0);
323
        $this->assertEquals(25, $list->LastItem());
324
    }
325
326
    public function testFirstLink()
327
    {
328
        $list = new PaginatedList(new ArrayList());
329
        $this->assertContains('start=0', $list->FirstLink());
330
    }
331
332
    public function testFirstLinkContainsCurrentGetParameters()
333
    {
334
        $request = new HTTPRequest(
335
            'GET',
336
            'http://example.com/my-cool-page',
337
            ['awesomeness' => 'nextLevel', 'start' => 20]
338
        );
339
        $list = new PaginatedList(new ArrayList(), $request);
340
        $list->setTotalItems(50);
341
        $list->setPageLength(10);
342
343
        // check the query string has correct parameters
344
        $queryString = parse_url($list->FirstLink(), PHP_URL_QUERY);
345
        parse_str($queryString, $queryParams);
346
347
        $this->assertArrayHasKey('awesomeness', $queryParams);
348
        $this->assertequals('nextLevel', $queryParams['awesomeness']);
349
        $this->assertArrayHasKey('start', $queryParams);
350
        $this->assertequals(0, $queryParams['start']);
351
    }
352
353
    public function testLastLink()
354
    {
355
        $list = new PaginatedList(new ArrayList());
356
        $list->setPageLength(10);
357
        $list->setTotalItems(100);
358
        $this->assertContains('start=90', $list->LastLink());
359
360
        // Disable paging
361
        $list->setPageLength(0);
362
        $this->assertContains('start=0', $list->LastLink());
363
    }
364
365
    public function testLastLinkContainsCurrentGetParameters()
366
    {
367
        $request = new HTTPRequest(
368
            'GET',
369
            'http://example.com/my-cool-page',
370
            ['awesomeness' => 'nextLevel']
371
        );
372
        $list = new PaginatedList(new ArrayList(), $request);
373
        $list->setTotalItems(50);
374
        $list->setPageLength(10);
375
376
        // check the query string has correct parameters
377
        $queryString = parse_url($list->LastLink(), PHP_URL_QUERY);
378
        parse_str($queryString, $queryParams);
379
380
        $this->assertArrayHasKey('awesomeness', $queryParams);
381
        $this->assertequals('nextLevel', $queryParams['awesomeness']);
382
        $this->assertArrayHasKey('start', $queryParams);
383
        $this->assertequals(40, $queryParams['start']);
384
    }
385
386
    public function testNextLink()
387
    {
388
        $list = new PaginatedList(new ArrayList());
389
        $list->setTotalItems(50);
390
391
        $this->assertContains('start=10', $list->NextLink());
392
        $list->setCurrentPage(2);
393
        $this->assertContains('start=20', $list->NextLink());
394
        $list->setCurrentPage(3);
395
        $this->assertContains('start=30', $list->NextLink());
396
        $list->setCurrentPage(4);
397
        $this->assertContains('start=40', $list->NextLink());
398
        $list->setCurrentPage(5);
399
        $this->assertNull($list->NextLink());
400
401
        // Disable paging
402
        $list->setCurrentPage(1);
403
        $list->setPageLength(0);
404
        $this->assertNull($list->NextLink());
405
    }
406
407
    public function testNextLinkContainsCurrentGetParameters()
408
    {
409
        $request = new HTTPRequest(
410
            'GET',
411
            'http://example.com/my-cool-page',
412
            ['awesomeness' => 'nextLevel']
413
        );
414
        $list = new PaginatedList(new ArrayList(), $request);
415
        $list->setTotalItems(50);
416
        $list->setPageLength(10);
417
418
        // check the query string has correct parameters
419
        $queryString = parse_url($list->NextLink(), PHP_URL_QUERY);
420
        parse_str($queryString, $queryParams);
421
422
        $this->assertArrayHasKey('awesomeness', $queryParams);
423
        $this->assertequals('nextLevel', $queryParams['awesomeness']);
424
        $this->assertArrayHasKey('start', $queryParams);
425
        $this->assertequals(10, $queryParams['start']);
426
    }
427
428
    public function testPrevLink()
429
    {
430
        $list = new PaginatedList(new ArrayList());
431
        $list->setTotalItems(50);
432
433
        $this->assertNull($list->PrevLink());
434
        $list->setCurrentPage(2);
435
        $this->assertContains('start=0', $list->PrevLink());
436
        $list->setCurrentPage(3);
437
        $this->assertContains('start=10', $list->PrevLink());
438
        $list->setCurrentPage(5);
439
        $this->assertContains('start=30', $list->PrevLink());
440
441
        // Disable paging
442
        $list->setPageLength(0);
443
        $this->assertNull($list->PrevLink());
444
    }
445
446
    public function testPrevLinkContainsCurrentGetParameters()
447
    {
448
        $request = new HTTPRequest(
449
            'GET',
450
            'http://example.com/my-cool-page',
451
            ['awesomeness' => 'nextLevel', 'start' => '30']
452
        );
453
        $list = new PaginatedList(new ArrayList(), $request);
454
        $list->setTotalItems(50);
455
        $list->setPageLength(10);
456
457
        // check the query string has correct parameters
458
        $queryString = parse_url($list->PrevLink(), PHP_URL_QUERY);
459
        parse_str($queryString, $queryParams);
460
461
        $this->assertArrayHasKey('awesomeness', $queryParams);
462
        $this->assertequals('nextLevel', $queryParams['awesomeness']);
463
        $this->assertArrayHasKey('start', $queryParams);
464
        $this->assertequals(20, $queryParams['start']);
465
    }
466
}
467