Completed
Pull Request — master (#183)
by Konstantin
04:23
created

PagerfantaTest   D

Complexity

Total Complexity 81

Size/Duplication

Total Lines 710
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 6
Bugs 1 Features 2
Metric Value
dl 0
loc 710
c 6
b 1
f 2
wmc 81
lcom 1
cbo 4
rs 4.4444

77 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetAllowOutOfRangePagesShouldBeFalseByDefault() 0 4 1
A testGetNormalizeOutOfRangePagesShouldBeFalseByDefault() 0 4 1
A testGetNbPagesShouldReturn1WhenThereAreNoResults() 0 6 1
A testGetCurrentPageOffsetStartWith0NbResults() 0 8 1
A setUp() 0 5 1
A testGetAdapterShouldReturnTheAdapter() 0 4 1
A testGetNbResultsShouldReturnTheNbResultsFromTheAdapter() 0 6 1
A testSetCurrentPageShouldThrowExceptionWhenInvalid() 0 4 1
A setAdapterNbResultsAny() 0 4 1
A setAdapterNbResultsOnce() 0 4 1
A setAdapterNbResults() 0 7 1
A testSetAllowOutOfRangePagesShouldSetTrue() 0 5 1
A testSetAllowOutOfRangePagesShouldSetFalse() 0 5 1
A testSetAllowOutOfRangePagesShouldReturnThePagerfanta() 0 4 1
A testSetAllowOutOfRangePagesShouldThrowNotBooleanExceptionWhenNotBoolean() 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 testGetNbResultsShouldCacheTheNbResultsFromTheAdapter() 0 7 1
A testGetNbPagesShouldCalculateTheNumberOfPages() 0 7 1
A testGetNbPagesShouldRoundToUp() 0 7 1
A testSetCurrentPageShouldSetAnInteger() 0 8 1
A setCurrentPageShouldSetAnIntegerProvider() 0 8 1
A testSetCurrentPageShouldConvertStringsToIntegers() 0 8 1
A setCurrentPageShouldConvertStringsToIntegersProvider() 0 8 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 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

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