1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pablodip\ModuleTestBundle\Tests\Module; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
6
|
|
|
|
7
|
|
|
class PagerfantaControllerTest extends WebTestCase |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public function testDefaultView() |
11
|
|
|
{ |
12
|
|
|
$this->assertView('default-view', <<<EOF |
13
|
|
|
<nav> |
14
|
|
|
<span class="disabled">Previous</span> |
15
|
|
|
<span class="current">1</span> |
16
|
|
|
<a href="/pagerfanta/default-view?page=2">2</a> |
17
|
|
|
<a href="/pagerfanta/default-view?page=3">3</a> |
18
|
|
|
<a href="/pagerfanta/default-view?page=4">4</a> |
19
|
|
|
<a href="/pagerfanta/default-view?page=5">5</a> |
20
|
|
|
<span class="dots">...</span> |
21
|
|
|
<a href="/pagerfanta/default-view?page=10">10</a> |
22
|
|
|
<a href="/pagerfanta/default-view?page=2" rel="next">Next</a> |
23
|
|
|
</nav> |
24
|
|
|
EOF |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testTwitterBootstrapView() |
29
|
|
|
{ |
30
|
|
|
$this->assertView('twitter-bootstrap-view', <<<EOF |
31
|
|
|
<div class="pagination"> |
32
|
|
|
<ul> |
33
|
|
|
<li class="prev disabled"><span>← Previous</span></li> |
34
|
|
|
<li class="active"><span>1</span></li> |
35
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-view?page=2">2</a></li> |
36
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-view?page=3">3</a></li> |
37
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-view?page=4">4</a></li> |
38
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-view?page=5">5</a></li> |
39
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-view?page=6">6</a></li> |
40
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-view?page=7">7</a></li> |
41
|
|
|
<li class="disabled"><span>…</span></li> |
42
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-view?page=10">10</a></li> |
43
|
|
|
<li class="next"><a href="/pagerfanta/twitter-bootstrap-view?page=2" rel="next">Next →</a></li> |
44
|
|
|
</ul> |
45
|
|
|
</div> |
46
|
|
|
EOF |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testTwitterBootstrap3View() |
51
|
|
|
{ |
52
|
|
|
$this->assertView('twitter-bootstrap3-view', <<<EOF |
53
|
|
|
<ul class="pagination"> |
54
|
|
|
<li class="prev disabled"><span>← Previous</span></li> |
55
|
|
|
<li class="active"><span>1 <span class="sr-only">(current)</span></span></li> |
56
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-view?page=2">2</a></li> |
57
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-view?page=3">3</a></li> |
58
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-view?page=4">4</a></li> |
59
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-view?page=5">5</a></li> |
60
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-view?page=6">6</a></li> |
61
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-view?page=7">7</a></li> |
62
|
|
|
<li class="disabled"><span>…</span></li> |
63
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-view?page=10">10</a></li> |
64
|
|
|
<li class="next"><a href="/pagerfanta/twitter-bootstrap3-view?page=2" rel="next">Next →</a></li> |
65
|
|
|
</ul> |
66
|
|
|
EOF |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testViewWithOptions() |
71
|
|
|
{ |
72
|
|
|
$this->assertView('view-with-options', <<<EOF |
73
|
|
|
<nav> |
74
|
|
|
<span class="disabled">Previous</span> |
75
|
|
|
<span class="current">1</span> |
76
|
|
|
<a href="/pagerfanta/view-with-options?page=2">2</a> |
77
|
|
|
<a href="/pagerfanta/view-with-options?page=3">3</a> |
78
|
|
|
<span class="dots">...</span> |
79
|
|
|
<a href="/pagerfanta/view-with-options?page=10">10</a> |
80
|
|
|
<a href="/pagerfanta/view-with-options?page=2" rel="next">Next</a> |
81
|
|
|
</nav> |
82
|
|
|
EOF |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testDefaultTranslatedView() |
87
|
|
|
{ |
88
|
|
|
$this->assertView('default-translated-view', <<<EOF |
89
|
|
|
<nav> |
90
|
|
|
<span class="disabled">« Anterior</span> |
91
|
|
|
<span class="current">1</span> |
92
|
|
|
<a href="/pagerfanta/default-translated-view?page=2">2</a> |
93
|
|
|
<a href="/pagerfanta/default-translated-view?page=3">3</a> |
94
|
|
|
<a href="/pagerfanta/default-translated-view?page=4">4</a> |
95
|
|
|
<a href="/pagerfanta/default-translated-view?page=5">5</a> |
96
|
|
|
<span class="dots">...</span> |
97
|
|
|
<a href="/pagerfanta/default-translated-view?page=10">10</a> |
98
|
|
|
<a href="/pagerfanta/default-translated-view?page=2" rel="next">Siguiente »</a> |
99
|
|
|
</nav> |
100
|
|
|
EOF |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testTwitterBootstrapTranslatedView() |
105
|
|
|
{ |
106
|
|
|
$this->assertView('twitter-bootstrap-translated-view', <<<EOF |
107
|
|
|
<div class="pagination"> |
108
|
|
|
<ul> |
109
|
|
|
<li class="prev disabled"><span>← Anterior</span></li> |
110
|
|
|
<li class="active"><span>1</span></li> |
111
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-translated-view?page=2">2</a></li> |
112
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-translated-view?page=3">3</a></li> |
113
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-translated-view?page=4">4</a></li> |
114
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-translated-view?page=5">5</a></li> |
115
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-translated-view?page=6">6</a></li> |
116
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-translated-view?page=7">7</a></li> |
117
|
|
|
<li class="disabled"><span>…</span></li> |
118
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap-translated-view?page=10">10</a></li> |
119
|
|
|
<li class="next"><a href="/pagerfanta/twitter-bootstrap-translated-view?page=2" rel="next">Siguiente →</a></li> |
120
|
|
|
</ul> |
121
|
|
|
</div> |
122
|
|
|
EOF |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testTwitterBootstrap3TranslatedView() |
127
|
|
|
{ |
128
|
|
|
$this->assertView('twitter-bootstrap3-translated-view', <<<EOF |
129
|
|
|
<ul class="pagination"> |
130
|
|
|
<li class="prev disabled"><span>← Anterior</span></li> |
131
|
|
|
<li class="active"><span>1 <span class="sr-only">(current)</span></span></li> |
132
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-translated-view?page=2">2</a></li> |
133
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-translated-view?page=3">3</a></li> |
134
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-translated-view?page=4">4</a></li> |
135
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-translated-view?page=5">5</a></li> |
136
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-translated-view?page=6">6</a></li> |
137
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-translated-view?page=7">7</a></li> |
138
|
|
|
<li class="disabled"><span>…</span></li> |
139
|
|
|
<li><a href="/pagerfanta/twitter-bootstrap3-translated-view?page=10">10</a></li> |
140
|
|
|
<li class="next"><a href="/pagerfanta/twitter-bootstrap3-translated-view?page=2" rel="next">Siguiente →</a></li> |
141
|
|
|
</ul> |
142
|
|
|
EOF |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testMyView1() |
147
|
|
|
{ |
148
|
|
|
$this->assertView('my-view-1', <<<EOF |
149
|
|
|
<nav> |
150
|
|
|
<span class="disabled">Anterior</span> |
151
|
|
|
<span class="current">1</span> |
152
|
|
|
<a href="/pagerfanta/my-view-1?page=2">2</a> |
153
|
|
|
<a href="/pagerfanta/my-view-1?page=3">3</a> |
154
|
|
|
<a href="/pagerfanta/my-view-1?page=4">4</a> |
155
|
|
|
<a href="/pagerfanta/my-view-1?page=5">5</a> |
156
|
|
|
<span class="dots">...</span> |
157
|
|
|
<a href="/pagerfanta/my-view-1?page=10">10</a> |
158
|
|
|
<a href="/pagerfanta/my-view-1?page=2" rel="next">Siguiente</a> |
159
|
|
|
</nav> |
160
|
|
|
EOF |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testViewWithRouteParams() |
165
|
|
|
{ |
166
|
|
|
$this->assertView('view-with-route-params', <<<EOF |
167
|
|
|
<nav> |
168
|
|
|
<span class="disabled">Previous</span> |
169
|
|
|
<span class="current">1</span> |
170
|
|
|
<a href="/pagerfanta/view-with-route-params?test=im-a-test&page=2">2</a> |
171
|
|
|
<a href="/pagerfanta/view-with-route-params?test=im-a-test&page=3">3</a> |
172
|
|
|
<a href="/pagerfanta/view-with-route-params?test=im-a-test&page=4">4</a> |
173
|
|
|
<a href="/pagerfanta/view-with-route-params?test=im-a-test&page=5">5</a> |
174
|
|
|
<span class="dots">...</span><a href="/pagerfanta/view-with-route-params?test=im-a-test&page=10">10</a> |
175
|
|
|
<a href="/pagerfanta/view-with-route-params?test=im-a-test&page=2" rel="next">Next</a> |
176
|
|
|
</nav> |
177
|
|
|
EOF |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testDefaultShortView() |
183
|
|
|
{ |
184
|
|
|
$this->assertView('default-short-view', <<<EOF |
185
|
|
|
<nav> |
186
|
|
|
<span class="disabled">Previous</span> |
187
|
|
|
<span class="current">1</span> |
188
|
|
|
<a href="/pagerfanta/default-short-view?test=im-a-short&page=2">2</a> |
189
|
|
|
<a href="/pagerfanta/default-short-view?test=im-a-short&page=3">3</a> |
190
|
|
|
<a href="/pagerfanta/default-short-view?test=im-a-short&page=4">4</a> |
191
|
|
|
<a href="/pagerfanta/default-short-view?test=im-a-short&page=5">5</a> |
192
|
|
|
<span class="dots">...</span><a href="/pagerfanta/default-short-view?test=im-a-short&page=10">10</a> |
193
|
|
|
<a href="/pagerfanta/default-short-view?test=im-a-short&page=2" rel="next">Next</a> |
194
|
|
|
</nav> |
195
|
|
|
|
196
|
|
|
EOF |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @test |
202
|
|
|
*/ |
203
|
|
|
public function testOutOfRangeExceptionWithNoneStrategy() |
204
|
|
|
{ |
205
|
|
|
$client = static::createClient(); |
206
|
|
|
$client->request('GET', $this->buildViewUrl('custom-page?currentPage=51')); |
207
|
|
|
|
208
|
|
|
$response = $client->getResponse(); |
209
|
|
|
$this->assertSame(404, $response->getStatusCode()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @test |
214
|
|
|
*/ |
215
|
|
|
public function testFirstPageLinkDoesHaveParam() |
216
|
|
|
{ |
217
|
|
|
$this->assertView('custom-page?currentPage=2', <<<EOF |
218
|
|
|
<nav> |
219
|
|
|
<a href="/pagerfanta/custom-page?currentPage=2&page=1" rel="prev">Previous</a> |
220
|
|
|
<a href="/pagerfanta/custom-page?currentPage=2&page=1">1</a> |
221
|
|
|
<span class="current">2</span> |
222
|
|
|
<a href="/pagerfanta/custom-page?currentPage=2&page=3">3</a> |
223
|
|
|
<a href="/pagerfanta/custom-page?currentPage=2&page=4">4</a> |
224
|
|
|
<a href="/pagerfanta/custom-page?currentPage=2&page=5">5</a> |
225
|
|
|
<span class="dots">...</span> |
226
|
|
|
<a href="/pagerfanta/custom-page?currentPage=2&page=10">10</a> |
227
|
|
|
<a href="/pagerfanta/custom-page?currentPage=2&page=3" rel="next">Next</a> |
228
|
|
|
</nav> |
229
|
|
|
EOF |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @test |
235
|
|
|
*/ |
236
|
|
|
public function testWithoutFirstPageParam() |
237
|
|
|
{ |
238
|
|
|
$this->assertView('view-without-first-page-param?currentPage=2', <<<EOF |
239
|
|
|
<nav> |
240
|
|
|
<a href="/pagerfanta/view-without-first-page-param?currentPage=2" rel="prev">Previous</a> |
241
|
|
|
<a href="/pagerfanta/view-without-first-page-param?currentPage=2">1</a> |
242
|
|
|
<span class="current">2</span> |
243
|
|
|
<a href="/pagerfanta/view-without-first-page-param?currentPage=2&page=3">3</a> |
244
|
|
|
<a href="/pagerfanta/view-without-first-page-param?currentPage=2&page=4">4</a> |
245
|
|
|
<a href="/pagerfanta/view-without-first-page-param?currentPage=2&page=5">5</a> |
246
|
|
|
<span class="dots">...</span> |
247
|
|
|
<a href="/pagerfanta/view-without-first-page-param?currentPage=2&page=10">10</a> |
248
|
|
|
<a href="/pagerfanta/view-without-first-page-param?currentPage=2&page=3" rel="next">Next</a> |
249
|
|
|
</nav> |
250
|
|
|
EOF |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @test |
256
|
|
|
*/ |
257
|
|
|
public function testWrongMaxPerPageExceptionWithNoneStrategy() |
258
|
|
|
{ |
259
|
|
|
$client = static::createClient(); |
260
|
|
|
$client->request('GET', $this->buildViewUrl('custom-page?maxPerPage=invalid')); |
261
|
|
|
|
262
|
|
|
$response = $client->getResponse(); |
263
|
|
|
$this->assertSame(404, $response->getStatusCode()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @test |
268
|
|
|
*/ |
269
|
|
|
public function testOutOfRangeExceptionWithToHttpNotFoundStrategy() |
270
|
|
|
{ |
271
|
|
|
$client = static::createClient(array('environment' => 'test_convert_exceptions')); |
272
|
|
|
|
273
|
|
|
$client->request('GET', $this->buildViewUrl('custom-page?currentPage=51')); |
274
|
|
|
|
275
|
|
|
$response = $client->getResponse(); |
276
|
|
|
$this->assertSame(404, $response->getStatusCode()); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @test |
281
|
|
|
*/ |
282
|
|
|
public function testWrongMaxPerPageExceptionWithToHttpNotFoundStrategy() |
283
|
|
|
{ |
284
|
|
|
$client = static::createClient(array('environment' => 'test_convert_exceptions')); |
285
|
|
|
$client->request('GET', $this->buildViewUrl('custom-page?maxPerPage=invalid')); |
286
|
|
|
|
287
|
|
|
$response = $client->getResponse(); |
288
|
|
|
$this->assertSame(404, $response->getStatusCode()); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @test |
293
|
|
|
*/ |
294
|
|
|
public function testCorrectMaxPerPageAndCurrentPageWithNoneStrategy() |
295
|
|
|
{ |
296
|
|
|
$client = static::createClient(); |
297
|
|
|
$client->request('GET', $this->buildViewUrl('custom-page?maxPerPage=10¤tPage=1')); |
298
|
|
|
|
299
|
|
|
$response = $client->getResponse(); |
300
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
private function assertView($view, $html) |
304
|
|
|
{ |
305
|
|
|
$client = static::createClient(); |
306
|
|
|
|
307
|
|
|
$crawler = $client->request('GET', $this->buildViewUrl($view)); |
|
|
|
|
308
|
|
|
|
309
|
|
|
$response = $client->getResponse(); |
310
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
311
|
|
|
$this->assertSame($this->removeWhitespacesBetweenTags($html), $response->getContent()); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
private function buildViewUrl($view) |
315
|
|
|
{ |
316
|
|
|
return sprintf('/pagerfanta/%s', $view); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
private function removeWhitespacesBetweenTags($string) |
320
|
|
|
{ |
321
|
|
|
return preg_replace('/>\s+</', '><', $string); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.