|
1
|
|
|
<?php |
|
2
|
|
|
namespace FwlibTest\Html\ListView; |
|
3
|
|
|
|
|
4
|
|
|
use Fwlib\Html\ListView\ListDto; |
|
5
|
|
|
use Fwlib\Html\ListView\Renderer; |
|
6
|
|
|
use Fwlib\Html\ListView\Request; |
|
7
|
|
|
use Fwlib\Html\ListView\RequestInterface; |
|
8
|
|
|
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase; |
|
9
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @SuppressWarnings(PHPMD.TooManyMethods) |
|
13
|
|
|
* |
|
14
|
|
|
* @copyright Copyright 2015 Fwolf |
|
15
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
|
16
|
|
|
*/ |
|
17
|
|
|
class RendererTest extends PHPUnitTestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param string[] $methods |
|
21
|
|
|
* @return MockObject | Renderer |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function buildMock(array $methods = null) |
|
24
|
|
|
{ |
|
25
|
|
|
$mock = $this->getMock( |
|
26
|
|
|
Renderer::class, |
|
27
|
|
|
$methods |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
/** @var Renderer $mock */ |
|
31
|
|
|
$mock->setClass('listTable') |
|
32
|
|
|
->setId(1); |
|
33
|
|
|
|
|
34
|
|
|
return $mock; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
View Code Duplication |
public function testAddOrderByLink() |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$renderer = $this->buildMock(); |
|
41
|
|
|
|
|
42
|
|
|
/** @var MockObject|Request $request */ |
|
43
|
|
|
$request = $this->getMock(Request::class, ['getBaseUrl']); |
|
44
|
|
|
$request->expects($this->any()) |
|
|
|
|
|
|
45
|
|
|
->method('getBaseUrl') |
|
46
|
|
|
->willReturn('http://domain.tld/'); |
|
47
|
|
|
$request->setOrderByParameter('ob') |
|
|
|
|
|
|
48
|
|
|
->setOrderByDirectionParameter('od'); |
|
49
|
|
|
$renderer->setRequest($request); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertEquals( |
|
52
|
|
|
"<a href='http://domain.tld/?ob=foo&od=DESC'>head(asc)</a>", |
|
53
|
|
|
$this->reflectionCall( |
|
54
|
|
|
$renderer, |
|
55
|
|
|
'addOrderByLink', |
|
56
|
|
|
['foo', 'head(asc)', 'ASC'] |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function testAddOrderByText() |
|
63
|
|
|
{ |
|
64
|
|
|
$renderer = $this->buildMock(); |
|
65
|
|
|
$renderer->setConfig('orderByTextAsc', '[Ascending]'); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals( |
|
68
|
|
|
'head[Ascending]', |
|
69
|
|
|
$this->reflectionCall($renderer, 'addOrderByText', ['head', 'ASC']) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
public function testGetHtml() |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var MockObject|Renderer $renderer */ |
|
77
|
|
|
$renderer = $this->getMock( |
|
78
|
|
|
Renderer::class, |
|
79
|
|
|
['getListTable', 'getPager', 'getJs'] |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$renderer->expects($this->any()) |
|
|
|
|
|
|
83
|
|
|
->method('getListTable') |
|
84
|
|
|
->willReturn('<!-- table -->'); |
|
85
|
|
|
|
|
86
|
|
|
$renderer->expects($this->any()) |
|
87
|
|
|
->method('getPager') |
|
88
|
|
|
->willReturnOnConsecutiveCalls( |
|
89
|
|
|
'<!-- top pager -->', |
|
90
|
|
|
'<!-- bottom pager -->' |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$renderer->expects($this->any()) |
|
94
|
|
|
->method('getJs') |
|
95
|
|
|
->willReturn('<!-- js -->'); |
|
96
|
|
|
|
|
97
|
|
|
$renderer->setClass('listTable')->setId(1); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$renderer->setPreContent('<!-- pre content -->'); |
|
|
|
|
|
|
100
|
|
|
$renderer->setPostContent('<!-- post content -->'); |
|
|
|
|
|
|
101
|
|
|
$renderer->setConfig('showTopPager', true); |
|
|
|
|
|
|
102
|
|
|
$renderer->setConfig('showBottomPager', true); |
|
103
|
|
|
|
|
104
|
|
|
$html = "<!-- pre content --> |
|
105
|
|
|
|
|
106
|
|
|
<div class='listTable' id='listTable-1'> |
|
107
|
|
|
|
|
108
|
|
|
<!-- top pager --> |
|
109
|
|
|
|
|
110
|
|
|
<!-- table --> |
|
111
|
|
|
|
|
112
|
|
|
<!-- bottom pager --> |
|
113
|
|
|
|
|
114
|
|
|
</div> |
|
115
|
|
|
|
|
116
|
|
|
<!-- js --> |
|
117
|
|
|
|
|
118
|
|
|
<!-- post content -->"; |
|
119
|
|
|
$this->assertEquals($html, $renderer->getHtml()); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
public function testGetJs() |
|
124
|
|
|
{ |
|
125
|
|
|
$renderer = $this->buildMock(); |
|
126
|
|
|
$renderer->setClass('list-view') |
|
127
|
|
|
->setId(42) |
|
128
|
|
|
->setConfig('pageNumberInputFocusSelect', true); |
|
129
|
|
|
|
|
130
|
|
|
/** @var MockObject|RequestInterface $request */ |
|
131
|
|
|
$request = $this->getMock(Request::class, ['getPageParameter']); |
|
132
|
|
|
$request->expects($this->any()) |
|
|
|
|
|
|
133
|
|
|
->method('getPageParameter') |
|
134
|
|
|
->willReturn('pageNumber'); |
|
135
|
|
|
$renderer->setRequest($request); |
|
136
|
|
|
|
|
137
|
|
|
$html = " |
|
138
|
|
|
<script type='text/javascript'> |
|
139
|
|
|
(function() { |
|
140
|
|
|
|
|
141
|
|
|
$('#list-view-42').find('input[name=pageNumber]').on('mouseover', function() { |
|
142
|
|
|
this.select(); |
|
143
|
|
|
}); |
|
144
|
|
|
|
|
145
|
|
|
}) (); |
|
146
|
|
|
</script> |
|
147
|
|
|
"; |
|
148
|
|
|
$this->assertEquals($html, $this->reflectionCall($renderer, 'getJs')); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
public function testGetListBody() |
|
153
|
|
|
{ |
|
154
|
|
|
$renderer = $this->buildMock(); |
|
155
|
|
|
|
|
156
|
|
|
$renderer->setConfig('trAppend', [1 => "dir='ltr'"]); |
|
157
|
|
|
$renderer->setConfig('tdAppend', ['foo' => 'nowrap']); |
|
158
|
|
|
|
|
159
|
|
|
$listDto = (new ListDto())->setBody([ |
|
|
|
|
|
|
160
|
|
|
[ |
|
161
|
|
|
'dummy' => 'Dummy1', |
|
162
|
|
|
'foo' => 'Foo1', |
|
163
|
|
|
'bar' => 'Bar1', |
|
164
|
|
|
], |
|
165
|
|
|
[ |
|
166
|
|
|
'dummy' => 'Dummy2', |
|
167
|
|
|
'foo' => 'Foo2', |
|
168
|
|
|
'bar' => 'Bar2', |
|
169
|
|
|
], |
|
170
|
|
|
[ |
|
171
|
|
|
'dummy' => 'Dummy3', |
|
172
|
|
|
'foo' => 'Foo3', |
|
173
|
|
|
'bar' => 'Bar3', |
|
174
|
|
|
], |
|
175
|
|
|
]); |
|
176
|
|
|
$renderer->setListDto($listDto); |
|
177
|
|
|
|
|
178
|
|
|
$html = "<tbody> |
|
179
|
|
|
<tr class='listTable__body__tr'> |
|
180
|
|
|
<td class='listTable__td__dummy' id='listTable-1__td__dummy--0'> |
|
181
|
|
|
Dummy1 |
|
182
|
|
|
</td> |
|
183
|
|
|
<td class='listTable__td__foo' id='listTable-1__td__foo--0' nowrap> |
|
184
|
|
|
Foo1 |
|
185
|
|
|
</td> |
|
186
|
|
|
<td class='listTable__td__bar' id='listTable-1__td__bar--0'> |
|
187
|
|
|
Bar1 |
|
188
|
|
|
</td> |
|
189
|
|
|
</tr> |
|
190
|
|
|
<tr class='listTable__body__tr' dir='ltr'> |
|
191
|
|
|
<td class='listTable__td__dummy' id='listTable-1__td__dummy--1'> |
|
192
|
|
|
Dummy2 |
|
193
|
|
|
</td> |
|
194
|
|
|
<td class='listTable__td__foo' id='listTable-1__td__foo--1' nowrap> |
|
195
|
|
|
Foo2 |
|
196
|
|
|
</td> |
|
197
|
|
|
<td class='listTable__td__bar' id='listTable-1__td__bar--1'> |
|
198
|
|
|
Bar2 |
|
199
|
|
|
</td> |
|
200
|
|
|
</tr> |
|
201
|
|
|
<tr class='listTable__body__tr'> |
|
202
|
|
|
<td class='listTable__td__dummy' id='listTable-1__td__dummy--2'> |
|
203
|
|
|
Dummy3 |
|
204
|
|
|
</td> |
|
205
|
|
|
<td class='listTable__td__foo' id='listTable-1__td__foo--2' nowrap> |
|
206
|
|
|
Foo3 |
|
207
|
|
|
</td> |
|
208
|
|
|
<td class='listTable__td__bar' id='listTable-1__td__bar--2'> |
|
209
|
|
|
Bar3 |
|
210
|
|
|
</td> |
|
211
|
|
|
</tr> |
|
212
|
|
|
</tbody>"; |
|
213
|
|
|
$this->assertEquals( |
|
214
|
|
|
$html, |
|
215
|
|
|
$this->reflectionCall($renderer, 'getListBody') |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
public function testGetListHead() |
|
221
|
|
|
{ |
|
222
|
|
|
$renderer = $this->buildMock(); |
|
223
|
|
|
$renderer->setConfig('thAppend', ['foo' => 'nowrap']); |
|
224
|
|
|
|
|
225
|
|
|
/** @var MockObject|Request $request */ |
|
226
|
|
|
$request = $this->getMock(Request::class, []); |
|
227
|
|
|
$renderer->setRequest($request); |
|
228
|
|
|
|
|
229
|
|
|
$listDto = (new ListDto())->setHead([ |
|
230
|
|
|
'dummy' => 'Dummy', |
|
231
|
|
|
'foo' => 'Foo', |
|
232
|
|
|
'bar' => 'Bar', |
|
233
|
|
|
]); |
|
234
|
|
|
$renderer->setListDto($listDto); |
|
235
|
|
|
|
|
236
|
|
|
$html = "<thead> |
|
237
|
|
|
<tr class='listTable__head__tr'> |
|
238
|
|
|
<th id='listTable-1__th__dummy'>Dummy</th> |
|
239
|
|
|
<th id='listTable-1__th__foo' nowrap>Foo</th> |
|
240
|
|
|
<th id='listTable-1__th__bar'>Bar</th> |
|
241
|
|
|
</tr> |
|
242
|
|
|
</thead>"; |
|
243
|
|
|
$this->assertEquals( |
|
244
|
|
|
$html, |
|
245
|
|
|
$this->reflectionCall($renderer, 'getListHead') |
|
246
|
|
|
); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
public function testGetListHeadText() |
|
251
|
|
|
{ |
|
252
|
|
|
$renderer = $this->buildMock(); |
|
253
|
|
|
$renderer->setConfig('orderByTextAsc', '[Asc]'); |
|
254
|
|
|
|
|
255
|
|
|
/** @var MockObject|Request $request */ |
|
256
|
|
|
$request = $this->getMock(Request::class, ['getBaseUrl', 'getOrderBy']); |
|
257
|
|
|
|
|
258
|
|
|
$request->expects($this->any()) |
|
|
|
|
|
|
259
|
|
|
->method('getBaseUrl') |
|
260
|
|
|
->willReturn('http://domain.tld/'); |
|
261
|
|
|
|
|
262
|
|
|
$request->expects($this->any()) |
|
263
|
|
|
->method('getOrderBy') |
|
264
|
|
|
->willReturnOnConsecutiveCalls(null, ['foo' => 'asc']); |
|
265
|
|
|
|
|
266
|
|
|
$request->setOrderByParameter('ob') |
|
|
|
|
|
|
267
|
|
|
->setOrderByDirectionParameter('od'); |
|
268
|
|
|
$renderer->setRequest($request); |
|
269
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
// Empty order by |
|
272
|
|
|
$this->assertEquals( |
|
273
|
|
|
"head", |
|
274
|
|
|
$this->reflectionCall($renderer, 'getListHeadText', ['foo', 'head']) |
|
275
|
|
|
); |
|
276
|
|
|
|
|
277
|
|
|
$this->assertEquals( |
|
278
|
|
|
"<a href='http://domain.tld/?ob=foo&od=DESC'>head[Asc]</a>", |
|
279
|
|
|
$this->reflectionCall($renderer, 'getListHeadText', ['foo', 'head']) |
|
280
|
|
|
); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
|
|
284
|
|
View Code Duplication |
public function testGetListTable() |
|
|
|
|
|
|
285
|
|
|
{ |
|
286
|
|
|
/** @var MockObject|Renderer $renderer */ |
|
287
|
|
|
$renderer = $this->getMock( |
|
288
|
|
|
Renderer::class, |
|
289
|
|
|
['getListHead', 'getListBody'] |
|
290
|
|
|
); |
|
291
|
|
|
|
|
292
|
|
|
$renderer->expects($this->any()) |
|
|
|
|
|
|
293
|
|
|
->method('getListHead') |
|
294
|
|
|
->willReturn("<thead>\n</thead>"); |
|
295
|
|
|
|
|
296
|
|
|
$renderer->expects($this->any()) |
|
297
|
|
|
->method('getListBody') |
|
298
|
|
|
->willReturn("<tbody>\n</tbody>"); |
|
299
|
|
|
|
|
300
|
|
|
$renderer->setClass('listTable')->setId(1); |
|
|
|
|
|
|
301
|
|
|
|
|
302
|
|
|
$html = "<table class='listTable__table' id='listTable-1__table'> |
|
303
|
|
|
<thead> |
|
304
|
|
|
</thead> |
|
305
|
|
|
|
|
306
|
|
|
<tbody> |
|
307
|
|
|
</tbody> |
|
308
|
|
|
</table>"; |
|
309
|
|
|
$this->assertEquals( |
|
310
|
|
|
$html, |
|
311
|
|
|
$this->reflectionCall($renderer, 'getListTable') |
|
312
|
|
|
); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
public function testGetPager() |
|
317
|
|
|
{ |
|
318
|
|
|
/** @var MockObject|Renderer $renderer */ |
|
319
|
|
|
$renderer = $this->getMock( |
|
320
|
|
|
Renderer::class, |
|
321
|
|
|
['getSafePage', 'getSafePageSize'] |
|
322
|
|
|
); |
|
323
|
|
|
$renderer->expects($this->any()) |
|
|
|
|
|
|
324
|
|
|
->method('getSafePage') |
|
325
|
|
|
->willReturn(2); |
|
326
|
|
|
$renderer->expects($this->any()) |
|
327
|
|
|
->method('getSafePageSize') |
|
328
|
|
|
->willReturn(10); |
|
329
|
|
|
|
|
330
|
|
|
$renderer->setClass('listTable')->setId(1); |
|
|
|
|
|
|
331
|
|
|
|
|
332
|
|
|
/** @var MockObject|RequestInterface $request */ |
|
333
|
|
|
$request = $this->getMock(Request::class, ['getBaseUrl']); |
|
334
|
|
|
$request->expects($this->once()) |
|
|
|
|
|
|
335
|
|
|
->method('getBaseUrl') |
|
336
|
|
|
->willReturn('http://domain.tld/?foo=1&Bar=2'); |
|
337
|
|
|
$renderer->setRequest($request); |
|
|
|
|
|
|
338
|
|
|
|
|
339
|
|
|
$listDto = new ListDto; |
|
340
|
|
|
$listDto->setRowCount(42); |
|
341
|
|
|
$renderer->setListDto($listDto); |
|
|
|
|
|
|
342
|
|
|
|
|
343
|
|
|
$html = "<div class='listTable__pager' id='listTable-1__pager--top'> |
|
344
|
|
|
<a href='http://domain.tld/?foo=1&Bar=2&p=1'>首页</a> | " . " |
|
345
|
|
|
<a href='http://domain.tld/?foo=1&Bar=2&p=1'>上一页</a> | " . " |
|
346
|
|
|
<a href='http://domain.tld/?foo=1&Bar=2&p=3'>下一页</a> | " . " |
|
347
|
|
|
<a href='http://domain.tld/?foo=1&Bar=2&p=5'>尾页</a> | " . " |
|
348
|
|
|
共42条信息,每页显示10条,当前为第2/5页 | " . " |
|
349
|
|
|
转到第 |
|
350
|
|
|
<form method='get' action='http://domain.tld/'> |
|
351
|
|
|
<input type='hidden' name='foo' value='1' /> |
|
352
|
|
|
<input type='hidden' name='Bar' value='2' /> |
|
353
|
|
|
<input type='text' name='p' value='2' size='1' /> |
|
354
|
|
|
页 |
|
355
|
|
|
<input type='submit' value='转' /> |
|
356
|
|
|
</form> |
|
357
|
|
|
</div>"; |
|
358
|
|
|
$this->assertEquals( |
|
359
|
|
|
$html, |
|
360
|
|
|
$this->reflectionCall($renderer, 'getPager', ['top']) |
|
361
|
|
|
); |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
|
|
365
|
|
|
public function testGetPagerJumpFormWithEmptyUrl() |
|
366
|
|
|
{ |
|
367
|
|
|
$renderer = $this->buildMock(); |
|
368
|
|
|
|
|
369
|
|
|
$html = "转到第 |
|
370
|
|
|
<form method='get' action=''> |
|
371
|
|
|
<input type='text' name='p' value='1' size='1' /> |
|
372
|
|
|
页 |
|
373
|
|
|
<input type='submit' value='转' /> |
|
374
|
|
|
</form>"; |
|
375
|
|
|
$this->assertEquals( |
|
376
|
|
|
$html, |
|
377
|
|
|
$this->reflectionCall( |
|
378
|
|
|
$renderer, |
|
379
|
|
|
'getPagerJumpForm', |
|
380
|
|
|
['', 1, 5, 'p'] |
|
381
|
|
|
) |
|
382
|
|
|
); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
|
|
386
|
|
View Code Duplication |
public function testGetSafePage() |
|
|
|
|
|
|
387
|
|
|
{ |
|
388
|
|
|
$renderer = $this->buildMock(); |
|
389
|
|
|
|
|
390
|
|
|
/** @var MockObject|RequestInterface $request */ |
|
391
|
|
|
$request = $this->getMock(Request::class, ['getPage']); |
|
392
|
|
|
$request->expects($this->any()) |
|
|
|
|
|
|
393
|
|
|
->method('getPage') |
|
394
|
|
|
->willReturn(3); |
|
395
|
|
|
$renderer->setRequest($request); |
|
396
|
|
|
|
|
397
|
|
|
// In max page |
|
398
|
|
|
$this->assertEquals( |
|
399
|
|
|
3, |
|
400
|
|
|
$this->reflectionCall($renderer, 'getSafePage', ['3']) |
|
401
|
|
|
); |
|
402
|
|
|
|
|
403
|
|
|
// Exceed max page |
|
404
|
|
|
$this->assertEquals( |
|
405
|
|
|
2, |
|
406
|
|
|
$this->reflectionCall($renderer, 'getSafePage', ['2']) |
|
407
|
|
|
); |
|
408
|
|
|
} |
|
409
|
|
|
} |
|
410
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.