|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KochTest\Pagination; |
|
4
|
|
|
|
|
5
|
|
|
use Koch\Pagination\Pagination; |
|
6
|
|
|
|
|
7
|
|
|
class PaginationTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Pagination |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $object; |
|
13
|
|
|
protected $adapter; |
|
14
|
|
|
|
|
15
|
|
|
protected function setUp() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->adapter = $this->getMock('Koch\Pagination\AdapterInterface'); |
|
18
|
|
|
|
|
19
|
|
|
// constructor injection |
|
20
|
|
|
$this->object = new Pagination($this->adapter); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function tearDown() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->object; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @covers Koch\Pagination\Pagination::setAdapter |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testSetAdapter() |
|
32
|
|
|
{ |
|
33
|
|
|
// setter injection |
|
34
|
|
|
$r = $this->object->setAdapter($this->adapter); |
|
35
|
|
|
|
|
36
|
|
|
// fluent |
|
37
|
|
|
$this->assertInstanceOf('Koch\Pagination\Pagination', $r); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @covers Koch\Pagination\Pagination::getAdapter |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testGetAdapter() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->assertSame($this->adapter, $this->object->getAdapter()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
|
50
|
|
|
* @covers Koch\Pagination\Pagination::getMaxResultsPerPage |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testSetMaxResultsPerPage() |
|
53
|
|
|
{ |
|
54
|
|
|
$maxResultsPerPage = '15'; |
|
55
|
|
|
$r = $this->object->setMaxResultsPerPage($maxResultsPerPage); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertEquals($this->object->getMaxResultsPerPage(), $maxResultsPerPage); |
|
58
|
|
|
|
|
59
|
|
|
// fluent |
|
60
|
|
|
$this->assertInstanceOf('Koch\Pagination\Pagination', $r); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @expectedException InvalidArgumentException |
|
65
|
|
|
* @expectedExceptionMessage There must be more than 1 MaxResultsPerPage. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testSetMaxResultsPerPage_throwsException() |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$this->object->setMaxResultsPerPage(-10); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @covers Koch\Pagination\Pagination::setCurrentPage |
|
74
|
|
|
* @covers Koch\Pagination\Pagination::getCurrentPage |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testSetCurrentPage() |
|
77
|
|
|
{ |
|
78
|
|
|
// string |
|
79
|
|
|
$currentPage = '15'; |
|
80
|
|
|
$this->object->setCurrentPage($currentPage); |
|
81
|
|
|
$this->assertEquals($this->object->getCurrentPage(), $currentPage); |
|
82
|
|
|
|
|
83
|
|
|
// int |
|
84
|
|
|
$currentPage = 15; |
|
85
|
|
|
$r = $this->object->setCurrentPage($currentPage); |
|
86
|
|
|
$this->assertEquals($this->object->getCurrentPage(), $currentPage); |
|
87
|
|
|
|
|
88
|
|
|
// fluent |
|
89
|
|
|
$this->assertInstanceOf('Koch\Pagination\Pagination', $r); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @covers Koch\Pagination\Pagination::getTotalNumberOfResults |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getTotalNumberOfResults() |
|
96
|
|
|
{ |
|
97
|
|
|
// hmm, phpunit bug? calls to the mock are not covered |
|
98
|
|
|
unset($this->object->totalNumberOfResults); |
|
99
|
|
|
$this->assertNull($this->object->totalNumberOfResults); |
|
100
|
|
|
|
|
101
|
|
|
$this->adapter->expects($this->any()) |
|
102
|
|
|
->method('getTotalNumberOfResults')->will($this->returnValue(666)); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertSame(666, $this->object->getTotalNumberOfResults()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
|
109
|
|
|
* @covers Koch\Pagination\Pagination::haveToPaginate |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testHaveToPaginate() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->adapter->expects($this->any()) |
|
114
|
|
|
->method('getTotalNumberOfResults')->will($this->returnValue(15)); |
|
115
|
|
|
|
|
116
|
|
|
$this->object->setMaxResultsPerPage(16); |
|
117
|
|
|
$this->assertFalse($this->object->haveToPaginate()); |
|
118
|
|
|
$this->object->setMaxResultsPerPage(15); |
|
119
|
|
|
$this->assertFalse($this->object->haveToPaginate()); |
|
120
|
|
|
$this->object->setMaxResultsPerPage(14); |
|
121
|
|
|
$this->assertTrue($this->object->haveToPaginate()); |
|
122
|
|
|
$this->object->setMaxResultsPerPage(1); |
|
123
|
|
|
$this->assertTrue($this->object->haveToPaginate()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @covers Koch\Pagination\Pagination::getTotalNumberOfResults |
|
128
|
|
|
* @covers Koch\Pagination\Pagination::setTotalNumberOfResults |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testSetTotalNumberOfResults() |
|
131
|
|
|
{ |
|
132
|
|
|
$this->adapter->expects($this->never())->method('getTotalNumberOfResults'); |
|
133
|
|
|
|
|
134
|
|
|
$this->object->setTotalNumberOfResults(2); |
|
135
|
|
|
$this->assertEquals(2, $this->object->getTotalNumberOfResults()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @covers Koch\Pagination\Pagination::getNumberOfPages |
|
140
|
|
|
* @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
|
141
|
|
|
* @covers Koch\Pagination\Pagination::getLastPage |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testGetNumberOfPages() |
|
144
|
|
|
{ |
|
145
|
|
|
$this->adapter->expects($this->any()) |
|
146
|
|
|
->method('getTotalNumberOfResults')->will($this->returnValue(100)); |
|
147
|
|
|
|
|
148
|
|
|
$this->object->setMaxResultsPerPage(10); |
|
149
|
|
|
$this->assertSame(10, $this->object->getNumberOfPages()); |
|
150
|
|
|
$this->assertSame(10, $this->object->getLastPage()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
|
155
|
|
|
* @covers Koch\Pagination\Pagination::getCurrentPageResults |
|
156
|
|
|
* @covers Koch\Pagination\Pagination::getCurrentPage |
|
157
|
|
|
*/ |
|
158
|
|
|
public function testGetCurrentPageResults() |
|
159
|
|
|
{ |
|
160
|
|
|
$returnValues = [ |
|
161
|
|
|
['foo' => 'bar', 'bar' => 'foo'], |
|
162
|
|
|
['fanta', 'reiner', 'kristall', 'weizen'], |
|
163
|
|
|
]; |
|
164
|
|
|
|
|
165
|
|
|
$this->adapter->expects($this->once())->method('getSlice') |
|
166
|
|
|
->with($this->equalTo(20), $this->equalTo(10)) |
|
167
|
|
|
->will($this->returnValue($returnValues[0])); |
|
168
|
|
|
|
|
169
|
|
|
$this->object->setMaxResultsPerPage(10); |
|
170
|
|
|
$this->object->setCurrentPage(3, true); |
|
|
|
|
|
|
171
|
|
|
$this->assertSame($returnValues[0], $this->object->getCurrentPageResults()); |
|
172
|
|
|
|
|
173
|
|
|
// cached |
|
174
|
|
|
$this->assertSame($returnValues[0], $this->object->getCurrentPageResults()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
|
179
|
|
|
* @covers Koch\Pagination\Pagination::getCurrentPage |
|
180
|
|
|
* @covers Koch\Pagination\Pagination::hasPreviousPage |
|
181
|
|
|
* @covers Koch\Pagination\Pagination::getPreviousPage |
|
182
|
|
|
* @covers Koch\Pagination\Pagination::hasNextPage |
|
183
|
|
|
* @covers Koch\Pagination\Pagination::getNextPage |
|
184
|
|
|
*/ |
|
185
|
|
|
public function testGetPreviousPage() |
|
186
|
|
|
{ |
|
187
|
|
|
$this->adapter->expects($this->any()) |
|
188
|
|
|
->method('getTotalNumberOfResults')->will($this->returnValue(25)); |
|
189
|
|
|
|
|
190
|
|
|
$this->object->setMaxResultsPerPage(5); |
|
191
|
|
|
$this->object->setCurrentPage(1); |
|
192
|
|
|
|
|
193
|
|
|
// first page does not have a previous page |
|
194
|
|
|
$this->assertFalse($this->object->hasPreviousPage()); |
|
195
|
|
|
try { |
|
196
|
|
|
$this->object->getPreviousPage(); |
|
197
|
|
|
$this->fail(); |
|
198
|
|
|
} catch (\Exception $e) { |
|
199
|
|
|
$this->assertInstanceOf('\LogicException', $e); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// first page has a next page |
|
203
|
|
|
$this->assertTrue($this->object->hasNextPage()); |
|
204
|
|
|
$this->assertSame(2, $this->object->getNextPage()); |
|
205
|
|
|
|
|
206
|
|
|
// check in between |
|
207
|
|
|
$this->object->setCurrentPage(5); |
|
208
|
|
|
$this->assertTrue($this->object->hasPreviousPage()); |
|
209
|
|
|
$this->assertSame(4, $this->object->getPreviousPage()); |
|
210
|
|
|
|
|
211
|
|
|
// last page |
|
212
|
|
|
$this->object->setCurrentPage(25); |
|
213
|
|
|
$this->assertTrue($this->object->hasPreviousPage()); |
|
214
|
|
|
$this->assertSame(24, $this->object->getPreviousPage()); |
|
215
|
|
|
$this->assertFalse($this->object->hasNextPage()); |
|
216
|
|
|
|
|
217
|
|
|
// last page does not have a next page |
|
218
|
|
|
try { |
|
219
|
|
|
$this->object->getNextPage(); |
|
220
|
|
|
$this->fail(); |
|
221
|
|
|
} catch (\Exception $e) { |
|
222
|
|
|
$this->assertInstanceOf('\LogicException', $e); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @covers Koch\Pagination\Pagination::render |
|
228
|
|
|
* @covers Koch\Pagination\Pagination::setAdapter |
|
229
|
|
|
* @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
|
230
|
|
|
*/ |
|
231
|
|
|
public function testRender() |
|
232
|
|
|
{ |
|
233
|
|
|
// dataset |
|
234
|
|
|
$this->array = []; |
|
|
|
|
|
|
235
|
|
View Code Duplication |
for ($i = 0; $i < 10; ++$i) { |
|
|
|
|
|
|
236
|
|
|
$this->array[] = rand(1, 999); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
// dataset adapter |
|
240
|
|
|
$adapter = new \Koch\Pagination\Adapter\NativeArray($this->array); |
|
241
|
|
|
$this->object->setAdapter($adapter); |
|
242
|
|
|
|
|
243
|
|
|
// settings |
|
244
|
|
|
$this->object->setMaxResultsPerPage(10); |
|
245
|
|
|
|
|
246
|
|
|
// expected pagination |
|
247
|
|
|
$expected = '<nav class="pagination">'; |
|
248
|
|
|
$expected .= '<a href="URL">‹ First</a>'; |
|
249
|
|
|
$expected .= '<a href="URL">1</a>'; |
|
250
|
|
|
$expected .= '<a href="URL">></a>'; |
|
251
|
|
|
$expected .= '<a href="URL"> Last›</a>'; |
|
252
|
|
|
$expected .= '</nav>'; |
|
253
|
|
|
|
|
254
|
|
|
$this->assertEquals($expected, $this->object->render()); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.