PagerTest   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 636
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 1
dl 0
loc 636
rs 8.764
c 0
b 0
f 0

34 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testGetMaxPerPage1() 0 14 2
A getGetMaxPerPage1Tests() 0 13 1
A testGetMaxPerPage2() 0 16 1
A testGetMaxPerPage3() 0 15 1
A testGetCurrentMaxLink() 0 15 1
A testGetMaxRecordLimit() 0 7 1
A testGetNbResults() 0 8 1
A testCount() 0 8 1
A testGetQuery() 0 7 1
A testGetCountColumn() 0 7 1
A testParameters() 0 28 1
A testGetMaxPageLinks() 0 7 1
A testIsFirstPage() 0 7 1
A testIsLastPage() 0 18 1
A testGetLinks() 0 30 1
A testHaveToPaginate() 0 10 1
A testIterator() 0 35 2
A testValid() 0 8 1
A testNext() 0 8 1
A testKey() 0 8 1
A testCurrent() 0 10 1
A testGetCursor() 0 18 1
B testGetObjectByCursor() 0 58 4
A testGetFirstPage() 0 4 1
A testGetNextPage() 0 11 1
A testGetPreviousPage() 0 10 1
A testGetFirstIndex() 0 16 1
A testGetLastIndex() 0 21 1
B testGetNext() 0 57 4
B testGetPrevious() 0 57 4
A testSerialize() 0 15 1
A testUnserialize() 0 34 1
A callMethod() 0 8 1

How to fix   Complexity   

Complex Class

Complex classes like PagerTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PagerTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Datagrid;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Datagrid\Pager;
18
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
19
20
/**
21
 * @author Andrej Hudec <[email protected]>
22
 */
23
class PagerTest extends TestCase
24
{
25
    /**
26
     * @var Pager
27
     */
28
    private $pager;
29
30
    protected function setUp(): void
31
    {
32
        $this->pager = $this->getMockForAbstractClass(Pager::class);
33
    }
34
35
    /**
36
     * @dataProvider getGetMaxPerPage1Tests
37
     */
38
    public function testGetMaxPerPage1(int $expectedMaxPerPage, int $expectedPage, int $maxPerPage, ?int $page): void
39
    {
40
        $this->assertSame(10, $this->pager->getMaxPerPage());
41
        $this->assertSame(1, $this->pager->getPage());
42
43
        if (null !== $page) {
44
            $this->pager->setPage($page);
45
        }
46
47
        $this->pager->setMaxPerPage($maxPerPage);
48
49
        $this->assertSame($expectedPage, $this->pager->getPage());
50
        $this->assertSame($expectedMaxPerPage, $this->pager->getMaxPerPage());
51
    }
52
53
    public function getGetMaxPerPage1Tests(): array
54
    {
55
        return [
56
            [123, 1, 123, 1],
57
            [123, 321, 123, 321],
58
            [1, 1, 1, 0],
59
            [0, 0, 0, 0],
60
            [1, 1, -1, 1],
61
            [1, 1, -1, 0],
62
            [1, 1, -1, -1],
63
            [0, 0, 0, null],
64
        ];
65
    }
66
67
    public function testGetMaxPerPage2(): void
68
    {
69
        $this->assertSame(10, $this->pager->getMaxPerPage());
70
        $this->assertSame(1, $this->pager->getPage());
71
72
        $this->pager->setMaxPerPage(0);
73
        $this->pager->setPage(0);
74
75
        $this->assertSame(0, $this->pager->getMaxPerPage());
76
        $this->assertSame(0, $this->pager->getPage());
77
78
        $this->pager->setMaxPerPage(12);
79
80
        $this->assertSame(12, $this->pager->getMaxPerPage());
81
        $this->assertSame(1, $this->pager->getPage());
82
    }
83
84
    public function testGetMaxPerPage3(): void
85
    {
86
        $this->assertSame(10, $this->pager->getMaxPerPage());
87
        $this->assertSame(1, $this->pager->getPage());
88
89
        $this->pager->setMaxPerPage(0);
90
91
        $this->assertSame(0, $this->pager->getMaxPerPage());
92
        $this->assertSame(0, $this->pager->getPage());
93
94
        $this->pager->setMaxPerPage(-1);
95
96
        $this->assertSame(1, $this->pager->getMaxPerPage());
97
        $this->assertSame(1, $this->pager->getPage());
98
    }
99
100
    public function testGetCurrentMaxLink(): void
101
    {
102
        $this->assertSame(1, $this->pager->getCurrentMaxLink());
103
104
        $this->pager->getLinks();
105
        $this->assertSame(1, $this->pager->getCurrentMaxLink());
106
107
        $this->callMethod($this->pager, 'setLastPage', [20]);
108
        $this->pager->getLinks(10);
109
        $this->assertSame(10, $this->pager->getCurrentMaxLink());
110
111
        $this->pager->setMaxPageLinks(5);
112
        $this->pager->setPage(2);
113
        $this->assertSame(10, $this->pager->getCurrentMaxLink());
114
    }
115
116
    public function testGetMaxRecordLimit(): void
117
    {
118
        $this->assertFalse($this->pager->getMaxRecordLimit());
119
120
        $this->pager->setMaxRecordLimit(99);
121
        $this->assertSame(99, $this->pager->getMaxRecordLimit());
122
    }
123
124
    public function testGetNbResults(): void
125
    {
126
        $this->assertSame(0, $this->pager->getNbResults());
127
128
        $this->callMethod($this->pager, 'setNbResults', [100]);
129
130
        $this->assertSame(100, $this->pager->getNbResults());
131
    }
132
133
    public function testCount(): void
134
    {
135
        $this->assertSame(0, $this->pager->count());
136
137
        $this->callMethod($this->pager, 'setNbResults', [100]);
138
139
        $this->assertSame(100, $this->pager->count());
140
    }
141
142
    public function testGetQuery(): void
143
    {
144
        $query = $this->createMock(ProxyQueryInterface::class);
145
146
        $this->pager->setQuery($query);
147
        $this->assertSame($query, $this->pager->getQuery());
148
    }
149
150
    public function testGetCountColumn(): void
151
    {
152
        $this->assertSame(['id'], $this->pager->getCountColumn());
153
154
        $this->pager->setCountColumn(['foo']);
155
        $this->assertSame(['foo'], $this->pager->getCountColumn());
156
    }
157
158
    public function testParameters(): void
159
    {
160
        $this->assertNull($this->pager->getParameter('foo', null));
161
        $this->assertSame('bar', $this->pager->getParameter('foo', 'bar'));
162
        $this->assertFalse($this->pager->hasParameter('foo'));
163
        $this->assertSame([], $this->pager->getParameters());
164
165
        $this->pager->setParameter('foo', 'foo_value');
166
167
        $this->assertTrue($this->pager->hasParameter('foo'));
168
        $this->assertSame('foo_value', $this->pager->getParameter('foo', null));
169
        $this->assertSame('foo_value', $this->pager->getParameter('foo', 'bar'));
170
        $this->assertSame(['foo' => 'foo_value'], $this->pager->getParameters());
171
172
        $this->pager->setParameter('foo', 'baz');
173
174
        $this->assertTrue($this->pager->hasParameter('foo'));
175
        $this->assertSame('baz', $this->pager->getParameter('foo', null));
176
        $this->assertSame('baz', $this->pager->getParameter('foo', 'bar'));
177
        $this->assertSame(['foo' => 'baz'], $this->pager->getParameters());
178
179
        $this->pager->setParameter('foo2', 'foo2_value');
180
181
        $this->assertTrue($this->pager->hasParameter('foo2'));
182
        $this->assertSame('foo2_value', $this->pager->getParameter('foo2', null));
183
        $this->assertSame('foo2_value', $this->pager->getParameter('foo2', 'bar'));
184
        $this->assertSame(['foo' => 'baz', 'foo2' => 'foo2_value'], $this->pager->getParameters());
185
    }
186
187
    public function testGetMaxPageLinks(): void
188
    {
189
        $this->assertSame(0, $this->pager->getMaxPageLinks());
190
191
        $this->pager->setMaxPageLinks(123);
192
        $this->assertSame(123, $this->pager->getMaxPageLinks());
193
    }
194
195
    public function testIsFirstPage(): void
196
    {
197
        $this->assertTrue($this->pager->isFirstPage());
198
199
        $this->pager->setPage(123);
200
        $this->assertFalse($this->pager->isFirstPage());
201
    }
202
203
    public function testIsLastPage(): void
204
    {
205
        $this->assertTrue($this->pager->isLastPage());
206
        $this->assertSame(1, $this->pager->getLastPage());
207
208
        $this->pager->setPage(10);
209
        $this->callMethod($this->pager, 'setLastPage', [50]);
210
        $this->assertSame(50, $this->pager->getLastPage());
211
        $this->assertFalse($this->pager->isLastPage());
212
213
        $this->pager->setPage(50);
214
        $this->assertTrue($this->pager->isLastPage());
215
216
        $this->callMethod($this->pager, 'setLastPage', [20]);
217
        $this->assertSame(20, $this->pager->getPage());
218
        $this->assertSame(20, $this->pager->getLastPage());
219
        $this->assertTrue($this->pager->isLastPage());
220
    }
221
222
    public function testGetLinks(): void
223
    {
224
        $this->assertSame([], $this->pager->getLinks());
225
226
        $this->pager->setPage(1);
227
        $this->pager->setMaxPageLinks(1);
228
        $this->assertSame([1], $this->pager->getLinks());
229
        $this->assertSame([1], $this->pager->getLinks(10));
230
231
        $this->pager->setPage(1);
232
        $this->pager->setMaxPageLinks(7);
233
        $this->callMethod($this->pager, 'setLastPage', [50]);
234
        $this->assertCount(7, $this->pager->getLinks());
235
        $this->assertSame([1, 2, 3, 4, 5, 6, 7], $this->pager->getLinks());
236
237
        $this->pager->setPage(10);
238
        $this->pager->setMaxPageLinks(12);
239
        $this->assertCount(5, $this->pager->getLinks(5));
240
        $this->assertSame([8, 9, 10, 11, 12], $this->pager->getLinks(5));
241
242
        $this->pager->setPage(10);
243
        $this->pager->setMaxPageLinks(6);
244
        $this->assertCount(6, $this->pager->getLinks());
245
        $this->assertSame([7, 8, 9, 10, 11, 12], $this->pager->getLinks());
246
247
        $this->pager->setPage(50);
248
        $this->pager->setMaxPageLinks(6);
249
        $this->assertCount(6, $this->pager->getLinks());
250
        $this->assertSame([45, 46, 47, 48, 49, 50], $this->pager->getLinks());
251
    }
252
253
    public function testHaveToPaginate(): void
254
    {
255
        $this->assertFalse($this->pager->haveToPaginate());
256
257
        $this->pager->setMaxPerPage(10);
258
        $this->assertFalse($this->pager->haveToPaginate());
259
260
        $this->callMethod($this->pager, 'setNbResults', [100]);
261
        $this->assertTrue($this->pager->haveToPaginate());
262
    }
263
264
    public function testIterator(): void
265
    {
266
        $this->assertInstanceOf(\Iterator::class, $this->pager);
267
268
        $object1 = new \stdClass();
269
        $object1->foo = 'bar1';
270
271
        $object2 = new \stdClass();
272
        $object2->foo = 'bar2';
273
274
        $object3 = new \stdClass();
275
        $object3->foo = 'bar3';
276
277
        $expectedObjects = [$object1, $object2, $object3];
278
279
        $this->pager
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Datagrid\Pager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
280
            ->method('getResults')
281
            ->willReturn($expectedObjects);
282
283
        $counter = 0;
284
        $values = [];
285
        foreach ($this->pager as $key => $value) {
286
            $values[$key] = $value;
287
            ++$counter;
288
        }
289
290
        $this->assertSame(3, $counter);
291
        $this->assertSame($object3, $value);
0 ignored issues
show
Bug introduced by
The variable $value seems to be defined by a foreach iteration on line 285. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
292
        $this->assertSame($expectedObjects, $values);
293
294
        $this->assertFalse($this->pager->valid());
295
296
        $this->callMethod($this->pager, 'resetIterator');
297
        $this->assertTrue($this->pager->valid());
298
    }
299
300
    public function testValid(): void
301
    {
302
        $this->pager
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Datagrid\Pager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
303
            ->method('getResults')
304
            ->willReturn([]);
305
306
        $this->assertFalse($this->pager->valid());
307
    }
308
309
    public function testNext(): void
310
    {
311
        $this->pager
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Datagrid\Pager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
312
            ->method('getResults')
313
            ->willReturn([]);
314
315
        $this->assertFalse($this->pager->next());
316
    }
317
318
    public function testKey(): void
319
    {
320
        $this->pager
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Datagrid\Pager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
321
            ->method('getResults')
322
            ->willReturn([123 => new \stdClass()]);
323
324
        $this->assertSame(123, $this->pager->key());
325
    }
326
327
    public function testCurrent(): void
328
    {
329
        $object = new \stdClass();
330
331
        $this->pager
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Datagrid\Pager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
332
            ->method('getResults')
333
            ->willReturn([$object]);
334
335
        $this->assertSame($object, $this->pager->current());
336
    }
337
338
    public function testGetCursor(): void
339
    {
340
        $this->assertSame(1, $this->pager->getCursor());
341
342
        $this->pager->setCursor(0);
343
        $this->assertSame(1, $this->pager->getCursor());
344
345
        $this->pager->setCursor(300);
346
        $this->assertSame(0, $this->pager->getCursor());
347
348
        $this->callMethod($this->pager, 'setNbResults', [100]);
349
350
        $this->pager->setCursor(5);
351
        $this->assertSame(5, $this->pager->getCursor());
352
353
        $this->pager->setCursor(300);
354
        $this->assertSame(100, $this->pager->getCursor());
355
    }
356
357
    public function testGetObjectByCursor(): void
358
    {
359
        $object1 = new \stdClass();
360
        $object1->foo = 'bar1';
361
362
        $object2 = new \stdClass();
363
        $object2->foo = 'bar2';
364
365
        $object3 = new \stdClass();
366
        $object3->foo = 'bar3';
367
368
        $this->callMethod($this->pager, 'setNbResults', [3]);
369
370
        $query = $this->createMock(ProxyQueryInterface::class);
371
372
        $query
373
            ->method('setFirstResult')
374
            ->willReturn($query);
375
376
        $query
377
            ->method('setMaxResults')
378
            ->willReturn($query);
379
380
        $id = 0;
381
        $query
382
            ->method('execute')
383
            ->willReturnCallback(static function () use (&$id, $object1, $object2, $object3): ?array {
384
                switch ($id) {
385
                    case 0:
386
                        return [$object1];
387
388
                    case 1:
389
                        return [$object2];
390
391
                    case 2:
392
                        return [$object3];
393
                }
394
395
                return null;
396
            });
397
398
        $this->pager->setQuery($query);
399
400
        $this->assertSame($object1, $this->pager->getObjectByCursor(1));
401
        $this->assertSame(1, $this->pager->getCursor());
402
403
        $id = 1;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
404
        $this->assertSame($object2, $this->pager->getObjectByCursor(2));
405
        $this->assertSame(2, $this->pager->getCursor());
406
407
        $id = 2;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
408
        $this->assertSame($object3, $this->pager->getObjectByCursor(3));
409
        $this->assertSame(3, $this->pager->getCursor());
410
411
        $id = 3;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
412
        $this->assertNull($this->pager->getObjectByCursor(4));
413
        $this->assertSame(3, $this->pager->getCursor());
414
    }
415
416
    public function testGetFirstPage(): void
417
    {
418
        $this->assertSame(1, $this->pager->getFirstPage());
419
    }
420
421
    public function testGetNextPage(): void
422
    {
423
        $this->assertSame(1, $this->pager->getNextPage());
424
425
        $this->pager->setPage(3);
426
        $this->callMethod($this->pager, 'setLastPage', [20]);
427
        $this->assertSame(4, $this->pager->getNextPage());
428
429
        $this->pager->setPage(21);
430
        $this->assertSame(20, $this->pager->getNextPage());
431
    }
432
433
    public function testGetPreviousPage(): void
434
    {
435
        $this->assertSame(1, $this->pager->getPreviousPage());
436
437
        $this->pager->setPage(3);
438
        $this->assertSame(2, $this->pager->getPreviousPage());
439
440
        $this->pager->setPage(21);
441
        $this->assertSame(20, $this->pager->getPreviousPage());
442
    }
443
444
    public function testGetFirstIndex(): void
445
    {
446
        $this->assertSame(1, $this->pager->getFirstIndex());
447
448
        $this->pager->setMaxPerPage(0);
449
        $this->pager->setPage(0);
450
        $this->assertSame(1, $this->pager->getFirstIndex());
451
452
        $this->pager->setPage(2);
453
        $this->pager->setMaxPerPage(10);
454
        $this->assertSame(11, $this->pager->getFirstIndex());
455
456
        $this->pager->setPage(4);
457
        $this->pager->setMaxPerPage(7);
458
        $this->assertSame(22, $this->pager->getFirstIndex());
459
    }
460
461
    public function testGetLastIndex(): void
462
    {
463
        $this->assertSame(0, $this->pager->getLastIndex());
464
465
        $this->pager->setMaxPerPage(0);
466
        $this->pager->setPage(0);
467
        $this->assertSame(0, $this->pager->getLastIndex());
468
469
        $this->callMethod($this->pager, 'setNbResults', [100]);
470
471
        $this->assertSame(100, $this->pager->getLastIndex());
472
473
        $this->pager->setPage(2);
474
        $this->assertSame(0, $this->pager->getLastIndex());
475
476
        $this->pager->setMaxPerPage(10);
477
        $this->assertSame(20, $this->pager->getLastIndex());
478
479
        $this->pager->setPage(11);
480
        $this->assertSame(100, $this->pager->getLastIndex());
481
    }
482
483
    public function testGetNext(): void
484
    {
485
        $this->assertNull($this->pager->getNext());
486
487
        $object1 = new \stdClass();
488
        $object1->foo = 'bar1';
489
490
        $object2 = new \stdClass();
491
        $object2->foo = 'bar2';
492
493
        $object3 = new \stdClass();
494
        $object3->foo = 'bar3';
495
496
        $this->callMethod($this->pager, 'setNbResults', [3]);
497
498
        $query = $this->createMock(ProxyQueryInterface::class);
499
500
        $query
501
            ->method('setFirstResult')
502
            ->willReturn($query);
503
504
        $query
505
            ->method('setMaxResults')
506
            ->willReturn($query);
507
508
        $id = 0;
509
        $query
510
            ->method('execute')
511
            ->willReturnCallback(static function () use (&$id, $object1, $object2, $object3): ?array {
512
                switch ($id) {
513
                    case 0:
514
                        return [$object1];
515
516
                    case 1:
517
                        return [$object2];
518
519
                    case 2:
520
                        return [$object3];
521
                }
522
523
                return null;
524
            });
525
526
        $this->pager->setQuery($query);
527
528
        $this->pager->setCursor(1);
529
        $this->assertSame($object1, $this->pager->getCurrent());
530
531
        ++$id;
532
        $this->assertSame($object2, $this->pager->getNext());
533
534
        ++$id;
535
        $this->assertSame($object3, $this->pager->getNext());
536
537
        ++$id;
538
        $this->assertNull($this->pager->getNext());
539
    }
540
541
    public function testGetPrevious(): void
542
    {
543
        $this->assertNull($this->pager->getPrevious());
544
545
        $object1 = new \stdClass();
546
        $object1->foo = 'bar1';
547
548
        $object2 = new \stdClass();
549
        $object2->foo = 'bar2';
550
551
        $object3 = new \stdClass();
552
        $object3->foo = 'bar3';
553
554
        $this->callMethod($this->pager, 'setNbResults', [3]);
555
556
        $query = $this->createMock(ProxyQueryInterface::class);
557
558
        $query
559
            ->method('setFirstResult')
560
            ->willReturn($query);
561
562
        $query
563
            ->method('setMaxResults')
564
            ->willReturn($query);
565
566
        $id = 2;
567
        $query
568
            ->method('execute')
569
            ->willReturnCallback(static function () use (&$id, $object1, $object2, $object3): ?array {
570
                switch ($id) {
571
                    case 0:
572
                        return [$object1];
573
574
                    case 1:
575
                        return [$object2];
576
577
                    case 2:
578
                        return [$object3];
579
                }
580
581
                return null;
582
            });
583
584
        $this->pager->setQuery($query);
585
586
        $this->pager->setCursor(2);
587
        $this->assertSame($object3, $this->pager->getCurrent());
588
589
        --$id;
590
        $this->assertSame($object2, $this->pager->getPrevious());
591
592
        --$id;
593
        $this->assertSame($object1, $this->pager->getPrevious());
594
595
        --$id;
596
        $this->assertNull($this->pager->getPrevious());
597
    }
598
599
    public function testSerialize(): void
600
    {
601
        $pagerClone = clone $this->pager;
602
        $data = $this->pager->serialize();
603
        $this->assertNotEmpty($data);
604
605
        $this->pager->setPage(12);
606
        $this->pager->setMaxPerPage(4);
607
        $this->pager->setMaxPageLinks(6);
608
609
        $this->pager->unserialize($data);
610
        $this->assertSame($pagerClone->getPage(), $this->pager->getPage());
611
        $this->assertSame($pagerClone->getMaxPerPage(), $this->pager->getMaxPerPage());
612
        $this->assertSame($pagerClone->getMaxPageLinks(), $this->pager->getMaxPageLinks());
613
    }
614
615
    public function testUnserialize(): void
616
    {
617
        $serialized = [
618
            'page' => 6,
619
            'maxPerPage' => 7,
620
            'maxPageLinks' => 5,
621
            'lastPage' => 4,
622
            'nbResults' => 30,
623
            'cursor' => 3,
624
            'parameters' => ['foo' => 'bar'],
625
            'currentMaxLink' => 2,
626
            'maxRecordLimit' => 22,
627
            'countColumn' => ['idx'],
628
        ];
629
630
        $this->pager
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Datagrid\Pager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
631
            ->method('getResults')
632
            ->willReturn([]);
633
        $this->pager->current();
634
635
        $this->pager->unserialize(serialize($serialized));
636
637
        $this->assertSame(7, $this->pager->getMaxPerPage());
638
        $this->assertSame(6, $this->pager->getPage());
639
        $this->assertSame(5, $this->pager->getMaxPageLinks());
640
        $this->assertSame(4, $this->pager->getLastPage());
641
        $this->assertSame(['idx'], $this->pager->getCountColumn());
642
        $this->assertSame(30, $this->pager->getNbResults());
643
        $this->assertSame(3, $this->pager->getCursor());
644
        $this->assertSame(['foo' => 'bar'], $this->pager->getParameters());
645
        $this->assertSame(2, $this->pager->getCurrentMaxLink());
646
        $this->assertSame(22, $this->pager->getMaxRecordLimit());
647
        $this->assertNull($this->pager->getQuery());
648
    }
649
650
    protected function callMethod($obj, string $name, array $args = [])
651
    {
652
        $class = new \ReflectionClass($obj);
653
        $method = $class->getMethod($name);
654
        $method->setAccessible(true);
655
656
        return $method->invokeArgs($obj, $args);
657
    }
658
}
659