Completed
Push — master ( d7fcd7...2c5f85 )
by
unknown
02:50
created

testJsonSerializeShouldReturnAnArrayOfCurrentPageResultsIfItIsAnIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Pagerfanta\Tests;
4
5
use Pagerfanta\Pagerfanta;
6
use PHPUnit\Framework\TestCase;
7
8
class IteratorAggregate implements \IteratorAggregate
9
{
10
    private $iterator;
11
12
    public function __construct()
13
    {
14
        $this->iterator = new \ArrayIterator(array('ups'));
15
    }
16
17
    public function getIterator()
18
    {
19
        return $this->iterator;
20
    }
21
}
22
23
class PagerfantaTest extends TestCase
24
{
25
    private $adapter;
26
    /**
27
     * @var Pagerfanta
28
     */
29
    private $pagerfanta;
30
31
    protected function setUp()
32
    {
33
        $this->adapter = $this->getMockBuilder('Pagerfanta\Adapter\AdapterInterface')->getMock();
34
        $this->pagerfanta = new Pagerfanta($this->adapter);
0 ignored issues
show
Documentation introduced by
$this->adapter is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Pagerfanta\Adapter\AdapterInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
    }
36
37
    private function setAdapterNbResultsAny($nbResults)
38
    {
39
        $this->setAdapterNbResults($this->any(), $nbResults);
40
    }
41
42
    private function setAdapterNbResultsOnce($nbResults)
43
    {
44
        $this->setAdapterNbResults($this->once(), $nbResults);
45
    }
46
47
    private function setAdapterNbResults($expects, $nbResults)
48
    {
49
        $this->adapter
50
            ->expects($expects)
51
            ->method('getNbResults')
52
            ->will($this->returnValue($nbResults));
53
    }
54
55
    public function testGetAdapterShouldReturnTheAdapter()
56
    {
57
        $this->assertSame($this->adapter, $this->pagerfanta->getAdapter());
58
    }
59
60
    public function testGetAllowOutOfRangePagesShouldBeFalseByDefault()
61
    {
62
        $this->assertFalse($this->pagerfanta->getAllowOutOfRangePages());
63
    }
64
65
    public function testSetAllowOutOfRangePagesShouldSetTrue()
66
    {
67
        $this->pagerfanta->setAllowOutOfRangePages(true);
68
        $this->assertTrue($this->pagerfanta->getAllowOutOfRangePages());
69
    }
70
71
    public function testSetAllowOutOfRangePagesShouldSetFalse()
72
    {
73
        $this->pagerfanta->setAllowOutOfRangePages(false);
74
        $this->assertFalse($this->pagerfanta->getAllowOutOfRangePages());
75
    }
76
77
    public function testSetAllowOutOfRangePagesShouldReturnThePagerfanta()
78
    {
79
        $this->assertSame($this->pagerfanta, $this->pagerfanta->setAllowOutOfRangePages(true));
80
    }
81
82
    /**
83
     * @expectedException \Pagerfanta\Exception\NotBooleanException
84
     * @dataProvider notBooleanProvider
85
     */
86
    public function testSetAllowOutOfRangePagesShouldThrowNotBooleanExceptionWhenNotBoolean($value)
87
    {
88
        $this->pagerfanta->setAllowOutOfRangePages($value);
89
    }
90
91
    public function testGetNormalizeOutOfRangePagesShouldBeFalseByDefault()
92
    {
93
        $this->assertFalse($this->pagerfanta->getNormalizeOutOfRangePages());
94
    }
95
96
    public function testSetNormalizeOutOfRangePagesShouldSetTrue()
97
    {
98
        $this->pagerfanta->setNormalizeOutOfRangePages(true);
99
        $this->assertTrue($this->pagerfanta->getNormalizeOutOfRangePages());
100
    }
101
102
    public function testSetNormalizeOutOfRangePagesShouldSetFalse()
103
    {
104
        $this->pagerfanta->setNormalizeOutOfRangePages(false);
105
        $this->assertFalse($this->pagerfanta->getNormalizeOutOfRangePages());
106
    }
107
108
    public function testSetNormalizeOutOfRangePagesShouldReturnThePagerfanta()
109
    {
110
        $this->assertSame($this->pagerfanta, $this->pagerfanta->setNormalizeOutOfRangePages(true));
111
    }
112
113
    /**
114
     * @expectedException \Pagerfanta\Exception\NotBooleanException
115
     * @dataProvider notBooleanProvider
116
     */
117
    public function testSetNormalizeOutOfRangePagesShouldThrowNotBooleanExceptionWhenNotBoolean($value)
118
    {
119
        $this->pagerfanta->setNormalizeOutOfRangePages($value);
120
    }
121
122
    public function notBooleanProvider()
123
    {
124
        return array(
125
            array(1),
126
            array('1'),
127
            array(1.1),
128
        );
129
    }
130
131
    /**
132
     * @dataProvider setMaxPerPageShouldSetAnIntegerProvider
133
     */
134
    public function testSetMaxPerPageShouldSetAnInteger($maxPerPage)
135
    {
136
        $this->pagerfanta->setMaxPerPage($maxPerPage);
137
138
        $this->assertSame($maxPerPage, $this->pagerfanta->getMaxPerPage());
139
    }
140
141
    public function setMaxPerPageShouldSetAnIntegerProvider()
142
    {
143
        return array(
144
            array(1),
145
            array(10),
146
            array(25),
147
        );
148
    }
149
150
    /**
151
     * @dataProvider setMaxPerPageShouldConvertStringsToIntegersProvider
152
     */
153
    public function testSetMaxPerPageShouldConvertStringsToIntegers($maxPerPage)
154
    {
155
        $this->pagerfanta->setMaxPerPage($maxPerPage);
156
        $this->assertSame((int) $maxPerPage, $this->pagerfanta->getMaxPerPage());
157
    }
158
159
    public function setMaxPerPageShouldConvertStringsToIntegersProvider()
160
    {
161
        return array(
162
            array('1'),
163
            array('10'),
164
            array('25'),
165
        );
166
    }
167
168
    public function testSetMaxPerPageShouldReturnThePagerfanta()
169
    {
170
        $this->assertSame($this->pagerfanta, $this->pagerfanta->setMaxPerPage(10));
171
    }
172
173
    /**
174
     * @expectedException \Pagerfanta\Exception\NotIntegerMaxPerPageException
175
     * @dataProvider      setMaxPerPageShouldThrowExceptionWhenInvalidProvider
176
     */
177
    public function testSetMaxPerPageShouldThrowExceptionWhenInvalid($maxPerPage)
178
    {
179
        $this->pagerfanta->setMaxPerPage($maxPerPage);
180
    }
181
182
    public function setMaxPerPageShouldThrowExceptionWhenInvalidProvider()
183
    {
184
        return array(
185
            array(1.1),
186
            array('1.1'),
187
            array(true),
188
            array(array(1)),
189
        );
190
    }
191
192
    /**
193
     * @expectedException \Pagerfanta\Exception\LessThan1MaxPerPageException
194
     * @dataProvider      setMaxPerPageShouldThrowExceptionWhenLessThan1Provider
195
     */
196
    public function testSetMaxPerPageShouldThrowExceptionWhenLessThan1($maxPerPage)
197
    {
198
        $this->pagerfanta->setMaxPerPage($maxPerPage);
199
    }
200
201
    public function setMaxPerPageShouldThrowExceptionWhenLessThan1Provider()
202
    {
203
        return array(
204
            array(0),
205
            array(-1),
206
        );
207
    }
208
209
    public function testSetMaxPerPageShouldResetCurrentPageResults()
210
    {
211
        $pagerfanta = $this->pagerfanta;
212
213
        $this->assertResetCurrentPageResults(function () use ($pagerfanta) {
214
            $pagerfanta->setMaxPerPage(10);
215
        });
216
    }
217
218
    public function testSetMaxPerPageShouldResetNbResults()
219
    {
220
        $this->prepareForResetNbResults();
221
222
        $this->assertSame(100, $this->pagerfanta->getNbResults());
223
        $this->pagerfanta->setMaxPerPage(10);
224
        $this->assertSame(50, $this->pagerfanta->getNbResults());
225
    }
226
227
    public function testSetMaxPerPageShouldResetNbPages()
228
    {
229
        $this->prepareForResetNbResults();
230
231
        $this->assertSame(10, $this->pagerfanta->getNbPages());
232
        $this->pagerfanta->setMaxPerPage(10);
233
        $this->assertSame(5, $this->pagerfanta->getNbPages());
234
    }
235
236
    private function prepareForResetNbResults()
237
    {
238
        $this->pagerfanta->setMaxPerPage(10);
239
240
        $this->adapter
241
            ->expects($this->at(0))
242
            ->method('getNbResults')
243
            ->will($this->returnValue(100));
244
        $this->adapter
245
            ->expects($this->at(1))
246
            ->method('getNbResults')
247
            ->will($this->returnValue(50));
248
    }
249
250
    public function testGetNbResultsShouldReturnTheNbResultsFromTheAdapter()
251
    {
252
        $this->setAdapterNbResultsAny(20);
253
254
        $this->assertSame(20, $this->pagerfanta->getNbResults());
255
    }
256
257
    public function testGetNbResultsShouldCacheTheNbResultsFromTheAdapter()
258
    {
259
        $this->setAdapterNbResultsOnce(20);
260
261
        $this->pagerfanta->getNbResults();
262
        $this->pagerfanta->getNbResults();
263
    }
264
265
    public function testGetNbPagesShouldCalculateTheNumberOfPages()
266
    {
267
        $this->setAdapterNbResultsAny(100);
268
        $this->pagerfanta->setMaxPerPage(20);
269
270
        $this->assertSame(5, $this->pagerfanta->getNbPages());
271
    }
272
273
    public function testGetNbPagesShouldRoundToUp()
274
    {
275
        $this->setAdapterNbResultsAny(100);
276
        $this->pagerfanta->setMaxPerPage(15);
277
278
        $this->assertSame(7, $this->pagerfanta->getNbPages());
279
    }
280
281
    public function testGetNbPagesShouldReturn1WhenThereAreNoResults()
282
    {
283
        $this->setAdapterNbResultsAny(0);
284
285
        $this->assertSame(1, $this->pagerfanta->getNbPages());
286
    }
287
288
    /**
289
     * @dataProvider setCurrentPageShouldSetAnIntegerProvider
290
     */
291
    public function testSetCurrentPageShouldSetAnInteger($currentPage)
292
    {
293
        $this->setAdapterNbResultsAny(100);
294
        $this->pagerfanta->setMaxPerPage(2);
295
        $this->pagerfanta->setCurrentPage($currentPage);
296
297
        $this->assertSame($currentPage, $this->pagerfanta->getCurrentPage());
298
    }
299
300
    public function setCurrentPageShouldSetAnIntegerProvider()
301
    {
302
        return array(
303
            array(1),
304
            array(10),
305
            array(25),
306
        );
307
    }
308
309
    /**
310
     * @dataProvider setCurrentPageShouldConvertStringsToIntegersProvider
311
     */
312
    public function testSetCurrentPageShouldConvertStringsToIntegers($currentPage)
313
    {
314
        $this->setAdapterNbResultsAny(100);
315
        $this->pagerfanta->setMaxPerPage(2);
316
        $this->pagerfanta->setCurrentPage($currentPage);
317
318
        $this->assertSame((int) $currentPage, $this->pagerfanta->getCurrentPage());
319
    }
320
321
    public function setCurrentPageShouldConvertStringsToIntegersProvider()
322
    {
323
        return array(
324
            array('1'),
325
            array('10'),
326
            array('25'),
327
        );
328
    }
329
330
    /**
331
     * @expectedException Pagerfanta\Exception\NotIntegerCurrentPageException
332
     * @dataProvider      setCurrentPageShouldThrowExceptionWhenInvalidProvider
333
     */
334
    public function testSetCurrentPageShouldThrowExceptionWhenInvalid($currentPage)
335
    {
336
        $this->pagerfanta->setCurrentPage($currentPage);
337
    }
338
339
    public function setCurrentPageShouldThrowExceptionWhenInvalidProvider()
340
    {
341
        return array(
342
            array(1.1),
343
            array('1.1'),
344
            array(true),
345
            array(array(1)),
346
        );
347
    }
348
349
    /**
350
     * @expectedException Pagerfanta\Exception\LessThan1CurrentPageException
351
     * @dataProvider      setCurrentPageShouldThrowExceptionWhenLessThan1Provider
352
     */
353
    public function testCurrentPagePageShouldThrowExceptionWhenLessThan1($currentPage)
354
    {
355
        $this->pagerfanta->setCurrentPage($currentPage);
356
    }
357
358
    public function setCurrentPageShouldThrowExceptionWhenLessThan1Provider()
359
    {
360
        return array(
361
            array(0),
362
            array(-1),
363
        );
364
    }
365
366
    /**
367
     * @expectedException Pagerfanta\Exception\OutOfRangeCurrentPageException
368
     */
369
    public function testSetCurrentPageShouldThrowExceptionWhenThePageIsOutOfRange()
370
    {
371
        $this->setAdapterNbResultsAny(100);
372
        $this->pagerfanta->setMaxPerPage(10);
373
        $this->pagerfanta->setCurrentPage(11);
374
    }
375
376
    public function testSetCurrentPageShouldNotThrowExceptionWhenIndicatingAllowOurOfRangePages()
377
    {
378
        $this->setAdapterNbResultsAny(100);
379
        $this->pagerfanta->setMaxPerPage(10);
380
        $this->pagerfanta->setAllowOutOfRangePages(true);
381
        $this->pagerfanta->setCurrentPage(11);
382
383
        $this->assertSame(11, $this->pagerfanta->getCurrentPage());
384
    }
385
386
    public function testSetCurrentPageShouldNotThrowExceptionWhenIndicatingAllowOurOfRangePagesWithOldBooleanArguments()
387
    {
388
        $this->setAdapterNbResultsAny(100);
389
        $this->pagerfanta->setMaxPerPage(10);
390
        $this->pagerfanta->setCurrentPage(11, true);
391
392
        $this->assertSame(11, $this->pagerfanta->getCurrentPage());
393
    }
394
395
    public function testSetCurrentPageShouldReturnThePagerfanta()
396
    {
397
        $this->setAdapterNbResultsAny(100);
398
        $this->pagerfanta->setMaxPerPage(10);
399
400
        $this->assertSame($this->pagerfanta, $this->pagerfanta->setCurrentPage(1));
401
    }
402
403
    public function testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePages()
404
    {
405
        $this->setAdapterNbResultsAny(100);
406
        $this->pagerfanta->setMaxPerPage(10);
407
        $this->pagerfanta->setAllowOutOfRangePages(false);
408
        $this->pagerfanta->setNormalizeOutOfRangePages(true);
409
        $this->pagerfanta->setCurrentPage(11);
410
411
        $this->assertSame(10, $this->pagerfanta->getCurrentPage());
412
    }
413
414
    public function testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePagesWithDeprecatedBooleansArguments()
415
    {
416
        $this->setAdapterNbResultsAny(100);
417
        $this->pagerfanta->setMaxPerPage(10);
418
        $this->pagerfanta->setCurrentPage(11, false, true);
419
420
        $this->assertSame(10, $this->pagerfanta->getCurrentPage());
421
    }
422
423
    public function testSetCurrentPageShouldResetCurrentPageResults()
424
    {
425
        $pagerfanta = $this->pagerfanta;
426
427
        $this->assertResetCurrentPageResults(function () use ($pagerfanta) {
428
            $pagerfanta->setCurrentPage(1);
429
        });
430
    }
431
432
    /**
433
     * @dataProvider testGetCurrentPageResultsShouldReturnASliceFromTheAdapterDependingOnTheCurrentPageAndMaxPerPageProvider
434
     */
435
    public function testGetCurrentPageResultsShouldReturnASliceFromTheAdapterDependingOnTheCurrentPageAndMaxPerPage($maxPerPage, $currentPage, $offset)
436
    {
437
        $this->setAdapterNbResultsAny(100);
438
        $this->pagerfanta->setMaxPerPage($maxPerPage);
439
        $this->pagerfanta->setCurrentPage($currentPage);
440
441
        $currentPageResults = new \ArrayObject();
442
443
        $this->adapter
444
            ->expects($this->any())
445
            ->method('getSlice')
446
            ->with($this->equalTo($offset), $this->equalTo($maxPerPage))
447
            ->will($this->returnValue($currentPageResults));
448
449
        $this->assertSame($currentPageResults, $this->pagerfanta->getCurrentPageResults());
450
    }
451
452
    public function testGetCurrentPageResultsShouldReturnASliceFromTheAdapterDependingOnTheCurrentPageAndMaxPerPageProvider()
453
    {
454
        // max per page, current page, offset
455
        return array(
456
            array(10, 1, 0),
457
            array(10, 2, 10),
458
            array(20, 3, 40),
459
        );
460
    }
461
462
    public function testGetCurrentPageResultsShouldCacheTheResults()
463
    {
464
        $this->setAdapterNbResultsAny(100);
465
        $this->pagerfanta->setMaxPerPage(10);
466
        $this->pagerfanta->setCurrentPage(1);
467
468
        $currentPageResults = new \ArrayObject();
469
470
        $this->adapter
471
            ->expects($this->once())
472
            ->method('getSlice')
473
            ->will($this->returnValue($currentPageResults));
474
475
        $this->pagerfanta->getCurrentPageResults();
476
        $this->assertSame($currentPageResults, $this->pagerfanta->getCurrentPageResults());
477
    }
478
479
    public function testGetCurrentPageOffsetStart()
480
    {
481
        $this->setAdapterNbResultsAny(100);
482
        $this->pagerfanta->setMaxPerPage(10);
483
        $this->pagerfanta->setCurrentPage(2);
484
485
        $this->assertSame(11, $this->pagerfanta->getCurrentPageOffsetStart());
486
    }
487
488
    public function testGetCurrentPageOffsetStartWith0NbResults()
489
    {
490
        $this->setAdapterNbResultsAny(0);
491
        $this->pagerfanta->setMaxPerPage(10);
492
        $this->pagerfanta->setCurrentPage(1);
493
494
        $this->assertSame(0, $this->pagerfanta->getCurrentPageOffsetStart());
495
    }
496
497
    public function testGetCurrentPageOffsetEnd()
498
    {
499
        $this->setAdapterNbResultsAny(100);
500
        $this->pagerfanta->setMaxPerPage(10);
501
        $this->pagerfanta->setCurrentPage(2);
502
503
        $this->assertSame(20, $this->pagerfanta->getCurrentPageOffsetEnd());
504
    }
505
506
    public function testGetCurrentPageOffsetEndOnEndPage()
507
    {
508
        $this->setAdapterNbResultsAny(90);
509
        $this->pagerfanta->setMaxPerPage(20);
510
        $this->pagerfanta->setCurrentPage(5);
511
512
        $this->assertSame(90, $this->pagerfanta->getCurrentPageOffsetEnd());
513
    }
514
515
    public function testHaveToPaginateReturnsTrueWhenTheNumberOfResultsIsGreaterThanTheMaxPerPage()
516
    {
517
        $this->setAdapterNbResultsAny(100);
518
        $this->pagerfanta->setMaxPerPage(99);
519
520
        $this->assertTrue($this->pagerfanta->haveToPaginate());
521
    }
522
523
    public function testHaveToPaginateReturnsFalseWhenTheNumberOfResultsIsEqualToMaxPerPage()
524
    {
525
        $this->setAdapterNbResultsAny(100);
526
        $this->pagerfanta->setMaxPerPage(100);
527
528
        $this->assertFalse($this->pagerfanta->haveToPaginate());
529
    }
530
531
    public function testHaveToPaginateReturnsFalseWhenTheNumberOfResultsIsLessThanMaxPerPage()
532
    {
533
        $this->setAdapterNbResultsAny(100);
534
        $this->pagerfanta->setMaxPerPage(101);
535
536
        $this->assertFalse($this->pagerfanta->haveToPaginate());
537
    }
538
539
    public function testHasPreviousPageShouldReturnTrueWhenTheCurrentPageIsGreaterThan1()
540
    {
541
        $this->setAdapterNbResultsAny(100);
542
543
        foreach (array(2, 3) as $page) {
544
            $this->pagerfanta->setCurrentPage($page);
545
            $this->assertTrue($this->pagerfanta->hasPreviousPage());
546
        }
547
    }
548
549
    public function testHasPreviousPageShouldReturnFalseWhenTheCurrentPageIs1()
550
    {
551
        $this->setAdapterNbResultsAny(100);
552
        $this->pagerfanta->setCurrentPage(1);
553
554
        $this->assertFalse($this->pagerfanta->hasPreviousPage());
555
    }
556
557
    public function testGetPreviousPageShouldReturnThePreviousPage()
558
    {
559
        $this->setAdapterNbResultsAny(100);
560
        $this->pagerfanta->setMaxPerPage(10);
561
562
        foreach (array(2 => 1, 3 => 2) as $currentPage => $previousPage) {
563
            $this->pagerfanta->setCurrentPage($currentPage);
564
            $this->assertSame($previousPage, $this->pagerfanta->getPreviousPage());
565
        }
566
    }
567
568
    /**
569
     * @expectedException Pagerfanta\Exception\LogicException
570
     */
571
    public function testGetPreviousPageShouldThrowALogicExceptionIfThereIsNoPreviousPage()
572
    {
573
        $this->setAdapterNbResultsAny(100);
574
        $this->pagerfanta->setMaxPerPage(10);
575
        $this->pagerfanta->setCurrentPage(1);
576
577
        $this->pagerfanta->getPreviousPage();
578
    }
579
580
    public function testHasNextPageShouldReturnTrueIfTheCurrentPageIsNotTheLast()
581
    {
582
        $this->setAdapterNbResultsAny(100);
583
        $this->pagerfanta->setMaxPerPage(10);
584
585
        foreach (array(1, 2) as $page) {
586
            $this->pagerfanta->setCurrentPage($page);
587
            $this->assertTrue($this->pagerfanta->hasNextPage());
588
        }
589
    }
590
591
    public function testHasNextPageShouldReturnFalseIfTheCurrentPageIsTheLast()
592
    {
593
        $this->setAdapterNbResultsAny(100);
594
        $this->pagerfanta->setMaxPerPage(10);
595
        $this->pagerfanta->setCurrentPage(10);
596
597
        $this->assertFalse($this->pagerfanta->hasNextPage());
598
    }
599
600
    public function testGetNextPageShouldReturnTheNextPage()
601
    {
602
        $this->setAdapterNbResultsAny(100);
603
        $this->pagerfanta->setMaxPerPage(10);
604
605
        foreach (array(2 => 3, 3 => 4) as $currentPage => $nextPage) {
606
            $this->pagerfanta->setCurrentPage($currentPage);
607
            $this->assertSame($nextPage, $this->pagerfanta->getNextPage());
608
        }
609
    }
610
611
    /**
612
     * @expectedException Pagerfanta\Exception\LogicException
613
     */
614
    public function testGetNextPageShouldThrowALogicExceptionIfTheCurrentPageIsTheLast()
615
    {
616
        $this->setAdapterNbResultsAny(100);
617
        $this->pagerfanta->setMaxPerPage(10);
618
        $this->pagerfanta->setCurrentPage(10);
619
620
        $this->pagerfanta->getNextPage();
621
    }
622
623
    public function testCountShouldReturnNbResults()
624
    {
625
        $this->setAdapterNbResultsAny(30);
626
627
        $this->assertSame(30, $this->pagerfanta->count());
628
    }
629
630
    public function testPagerfantaShouldImplementCountableInterface()
631
    {
632
        $this->assertInstanceOf('Countable', $this->pagerfanta);
633
    }
634
635
    public function testGetIteratorShouldReturnCurrentPageResultsIfItIsAnIterator()
636
    {
637
        $currentPageResults = new \ArrayIterator(array('foo'));
638
        $this->setAdapterGetSlice($currentPageResults);
639
640
        $expected = $currentPageResults;
641
        $this->assertSame($expected, $this->pagerfanta->getIterator());
642
    }
643
644
    public function testGetIteratorShouldReturnTheIteratorOfCurrentPageResultsIfItIsAnIteratorAggregate()
645
    {
646
        $currentPageResults = new IteratorAggregate();
647
        $this->setAdapterGetSlice($currentPageResults);
648
649
        $expected = $currentPageResults->getIterator();
650
        $this->assertSame($expected, $this->pagerfanta->getIterator());
651
    }
652
653
    public function testGetIteratorShouldReturnAnArrayIteratorIfCurrentPageResultsIsAnArray()
654
    {
655
        $currentPageResults = array('foo', 'bar');
656
        $this->setAdapterGetSlice($currentPageResults);
657
658
        $expected = new \ArrayIterator($currentPageResults);
659
        $this->assertEquals($expected, $this->pagerfanta->getIterator());
660
    }
661
662
    public function testJsonSerializeShouldReturnAnArrayOfCurrentPageResultsIfItIsAnIterator()
663
    {
664
        $currentPageResults = new \ArrayIterator(array('foo'));
665
        $this->setAdapterGetSlice($currentPageResults);
666
667
        $expected = array('foo');
668
        $this->assertSame($expected, $this->pagerfanta->jsonSerialize());
669
    }
670
671
    public function testJsonSerializeShouldReturnAnArrayOfCurrentPageResultsIfItIsAnIteratorAggregate()
672
    {
673
        $currentPageResults = new IteratorAggregate();
674
        $this->setAdapterGetSlice($currentPageResults);
675
676
        $expected = iterator_to_array($currentPageResults);
677
        $this->assertSame($expected, $this->pagerfanta->jsonSerialize());
678
    }
679
680
    public function testJsonSerializeShouldReturnAnArrayOfCurrentPageResultsIfCurrentPageResultsIsAnArray()
681
    {
682
        $currentPageResults = array('foo', 'bar');
683
        $this->setAdapterGetSlice($currentPageResults);
684
685
        $expected = $currentPageResults;
686
        $this->assertSame($expected, $this->pagerfanta->jsonSerialize());
687
    }
688
689
    public function testJsonSerializeIsUsedOnJsonEncode()
690
    {
691
        $currentPageResults = array('foo', 'bar');
692
        $this->setAdapterGetSlice($currentPageResults);
693
694
        $expected = json_encode($currentPageResults);
695
        $this->assertSame($expected, json_encode($this->pagerfanta));
696
    }
697
698
    private function setAdapterGetSlice($currentPageResults)
699
    {
700
        $this->adapter
701
            ->expects($this->any())
702
            ->method('getSlice')
703
            ->will($this->returnValue($currentPageResults));
704
    }
705
706
    public function testPagerfantaShouldImplementIteratorAggregateInterface()
707
    {
708
        $this->assertInstanceOf('IteratorAggregate', $this->pagerfanta);
709
    }
710
711
    private function assertResetCurrentPageResults($callback)
712
    {
713
        $this->setAdapterNbResultsAny(100);
714
        $this->pagerfanta->setMaxPerPage(10);
715
716
        $currentPageResults0 = new \ArrayObject();
717
        $currentPageResults1 = new \ArrayObject();
718
719
        $this->adapter
720
            ->expects($this->at(0))
721
            ->method('getSlice')
722
            ->will($this->returnValue($currentPageResults0));
723
        $this->adapter
724
            ->expects($this->at(1))
725
            ->method('getSlice')
726
            ->will($this->returnValue($currentPageResults1));
727
728
        $this->assertSame($currentPageResults0, $this->pagerfanta->getCurrentPageResults());
729
        $callback();
730
        $this->assertSame($currentPageResults1, $this->pagerfanta->getCurrentPageResults());
731
    }
732
733
    public function testGetPageNumberForItemShouldReturnTheGoodPage()
734
    {
735
        $this->setAdapterNbResultsAny(100);
736
        $this->pagerfanta->setMaxPerPage(10);
737
738
        $this->assertEquals(4, $this->pagerfanta->getPageNumberForItemAtPosition(35));
739
    }
740
741
    /**
742
     * @expectedException Pagerfanta\Exception\NotIntegerException
743
     */
744
    public function testGetPageNumberForItemShouldThrowANotIntegerItemExceptionIfTheItemIsNotAnInteger()
745
    {
746
        $this->setAdapterNbResultsAny(100);
747
748
        $this->pagerfanta->getPageNumberForItemAtPosition('foo');
749
    }
750
751
    /**
752
     * @expectedException \OutOfBoundsException
753
     */
754
    public function testGetPageNumberForItemShouldThrowALogicExceptionIfTheItemIsMoreThanNbPage()
755
    {
756
        $this->setAdapterNbResultsAny(100);
757
758
        $this->pagerfanta->getPageNumberForItemAtPosition(101);
759
    }
760
}
761