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