Completed
Pull Request — master (#183)
by Konstantin
02:30
created

PagerfantaTest   D

Complexity

Total Complexity 84

Size/Duplication

Total Lines 738
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 84
lcom 1
cbo 4
dl 0
loc 738
rs 4.4444
c 5
b 1
f 1

80 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A setAdapterNbResultsAny() 0 4 1
A setAdapterNbResultsOnce() 0 4 1
A setAdapterNbResults() 0 7 1
A testGetAdapterShouldReturnTheAdapter() 0 4 1
A testGetAllowOutOfRangePagesShouldBeFalseByDefault() 0 4 1
A testSetAllowOutOfRangePagesShouldSetTrue() 0 5 1
A testSetAllowOutOfRangePagesShouldSetFalse() 0 5 1
A testSetAllowOutOfRangePagesShouldReturnThePagerfanta() 0 4 1
A testSetAllowOutOfRangePagesShouldThrowNotBooleanExceptionWhenNotBoolean() 0 4 1
A testGetNormalizeOutOfRangePagesShouldBeFalseByDefault() 0 4 1
A testSetNormalizeOutOfRangePagesShouldSetTrue() 0 5 1
A testSetNormalizeOutOfRangePagesShouldSetFalse() 0 5 1
A testSetNormalizeOutOfRangePagesShouldReturnThePagerfanta() 0 4 1
A testSetNormalizeOutOfRangePagesShouldThrowNotBooleanExceptionWhenNotBoolean() 0 4 1
A notBooleanProvider() 0 8 1
A testSetMaxPerPageShouldSetAnInteger() 0 6 1
A setMaxPerPageShouldSetAnIntegerProvider() 0 8 1
A testSetMaxPerPageShouldConvertStringsToIntegers() 0 5 1
A setMaxPerPageShouldConvertStringsToIntegersProvider() 0 8 1
A testSetMaxPerPageShouldReturnThePagerfanta() 0 4 1
A testSetMaxPerPageShouldThrowExceptionWhenInvalid() 0 4 1
A setMaxPerPageShouldThrowExceptionWhenInvalidProvider() 0 9 1
A testSetMaxPerPageShouldThrowExceptionWhenLessThan1() 0 4 1
A setMaxPerPageShouldThrowExceptionWhenLessThan1Provider() 0 7 1
A testSetMaxPerPageShouldResetCurrentPageResults() 0 8 1
A testSetMaxPerPageShouldResetNbResults() 0 8 1
A testSetMaxPerPageShouldResetNbPages() 0 8 1
A prepareForResetNbResults() 0 13 1
A testGetNbResultsShouldReturnTheNbResultsFromTheAdapter() 0 6 1
A testGetNbResultsShouldCacheTheNbResultsFromTheAdapter() 0 7 1
A testGetNbPagesShouldCalculateTheNumberOfPages() 0 7 1
A testGetNbPagesShouldRoundToUp() 0 7 1
A testGetNbPagesShouldReturn1WhenThereAreNoResults() 0 6 1
A testSetCurrentPageShouldSetAnInteger() 0 8 1
A setCurrentPageShouldSetAnIntegerProvider() 0 8 1
A testSetCurrentPageShouldConvertStringsToIntegers() 0 8 1
A setCurrentPageShouldConvertStringsToIntegersProvider() 0 8 1
A testSetCurrentPageShouldThrowExceptionWhenInvalid() 0 4 1
A setCurrentPageShouldThrowExceptionWhenInvalidProvider() 0 9 1
A testCurrentPagePageShouldThrowExceptionWhenLessThan1() 0 4 1
A setCurrentPageShouldThrowExceptionWhenLessThan1Provider() 0 7 1
A testSetCurrentPageShouldThrowExceptionWhenThePageIsOutOfRange() 0 6 1
A testSetCurrentPageShouldNotThrowExceptionWhenIndicatingAllowOurOfRangePages() 0 9 1
A testSetCurrentPageShouldNotThrowExceptionWhenIndicatingAllowOurOfRangePagesWithOldBooleanArguments() 0 8 1
A testSetCurrentPageShouldReturnThePagerfanta() 0 7 1
A testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePages() 0 10 1
A testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePagesWithDeprecatedBooleansArguments() 0 8 1
A testSetCurrentPageShouldResetCurrentPageResults() 0 8 1
A testGetCurrentPageResultsShouldReturnASliceFromTheAdapterDependingOnTheCurrentPageAndMaxPerPage() 0 16 1
A testGetCurrentPageResultsShouldReturnASliceFromTheAdapterDependingOnTheCurrentPageAndMaxPerPageProvider() 0 9 1
A testGetCurrentPageResultsShouldCacheTheResults() 0 16 1
A testGetCurrentPageOffsetStart() 0 8 1
A testGetCurrentPageOffsetStartWith0NbResults() 0 8 1
A testGetCurrentPageOffsetEnd() 0 8 1
A testGetCurrentPageOffsetEndOnEndPage() 0 8 1
A testHaveToPaginateReturnsTrueWhenTheNumberOfResultsIsGreaterThanTheMaxPerPage() 0 7 1
A testHaveToPaginateReturnsFalseWhenTheNumberOfResultsIsEqualToMaxPerPage() 0 7 1
A testHaveToPaginateReturnsFalseWhenTheNumberOfResultsIsLessThanMaxPerPage() 0 7 1
A testHasPreviousPageShouldReturnTrueWhenTheCurrentPageIsGreaterThan1() 0 9 2
A testHasPreviousPageShouldReturnFalseWhenTheCurrentPageIs1() 0 7 1
A testGetPreviousPageShouldReturnThePreviousPage() 0 10 2
A testGetPreviousPageShouldThrowALogicExceptionIfThereIsNoPreviousPage() 0 8 1
A testHasNextPageShouldReturnTrueIfTheCurrentPageIsNotTheLast() 0 10 2
A testHasNextPageShouldReturnFalseIfTheCurrentPageIsTheLast() 0 8 1
A testGetNextPageShouldReturnTheNextPage() 0 10 2
A testGetNextPageShouldThrowALogicExceptionIfTheCurrentPageIsTheLast() 0 8 1
A testCountShouldReturnNbResults() 0 6 1
A testPagerfantaShouldImplementCountableInterface() 0 4 1
A testGetIteratorShouldReturnCurrentPageResultsIfItIsAnIterator() 0 8 1
A testGetIteratorShouldReturnTheIteratorOfCurrentPageResultsIfItIsAnIteratorAggregate() 0 8 1
A testGetIteratorShouldReturnAnArrayIteratorIfCurrentPageResultsIsAnArray() 0 8 1
A setAdapterGetSlice() 0 7 1
A testPagerfantaShouldImplementIteratorAggregateInterface() 0 4 1
A testLazyAdapterDoNotCallsGetNbResultsOnSetCurrentPage() 0 12 1
A testLazyAdapterCallsGetNbResultsOnGetCurrentPageResults() 0 19 1
A assertResetCurrentPageResults() 0 21 1
A testGetPageNumberForItemShouldReturnTheGoodPage() 0 7 1
A testGetPageNumberForItemShouldThrowANotIntegerItemExceptionIfTheItemIsNotAnInteger() 0 6 1
A testGetPageNumberForItemShouldThrowALogicExceptionIfTheItemIsMoreThanNbPage() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like PagerfantaTest 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 PagerfantaTest, and based on these observations, apply Extract Interface, too.

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);
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
    private function setAdapterGetSlice($currentPageResults)
663
    {
664
        $this->adapter
665
            ->expects($this->any())
666
            ->method('getSlice')
667
            ->will($this->returnValue($currentPageResults));
668
    }
669
670
    public function testPagerfantaShouldImplementIteratorAggregateInterface()
671
    {
672
        $this->assertInstanceOf('IteratorAggregate', $this->pagerfanta);
673
    }
674
675
    public function testLazyAdapterDoNotCallsGetNbResultsOnSetCurrentPage()
676
    {
677
        $adapter = $this->getMock('Pagerfanta\Adapter\LazyAdapterInterface');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

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...
678
679
        $adapter
680
            ->expects($this->never())
681
            ->method('getNbResults');
682
683
        $pagerfanta = new Pagerfanta($adapter);
684
        $pagerfanta->setMaxPerPage(10);
685
        $pagerfanta->setCurrentPage(3);
686
    }
687
688
    /**
689
     * @expectedException Pagerfanta\Exception\OutOfRangeCurrentPageException
690
     */
691
    public function testLazyAdapterCallsGetNbResultsOnGetCurrentPageResults()
692
    {
693
        $adapter = $this->getMock('Pagerfanta\Adapter\LazyAdapterInterface');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

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...
694
695
        $adapter
696
            ->expects($this->any())
697
            ->method('getSlice')
698
            ->will($this->returnValue(range(0, 9)));
699
700
        $adapter
701
            ->expects($this->any())
702
            ->method('getNbResults')
703
            ->will($this->returnValue(20));
704
705
        $pagerfanta = new Pagerfanta($adapter);
706
        $pagerfanta->setMaxPerPage(10);
707
        $pagerfanta->setCurrentPage(3);
708
        $pagerfanta->getCurrentPageResults();
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