1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\ResourceBundle\Tests\Rest\View; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\View\View; |
15
|
|
|
use Hateoas\Configuration\Route; |
16
|
|
|
use Hateoas\Representation\Factory\PagerfantaFactory; |
17
|
|
|
use Lug\Bundle\ResourceBundle\Rest\RestEvents; |
18
|
|
|
use Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber\PagerfantaViewSubscriber; |
19
|
|
|
use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent; |
20
|
|
|
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; |
21
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
22
|
|
|
use Pagerfanta\Pagerfanta; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
24
|
|
|
use Symfony\Component\Form\FormRendererInterface; |
25
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
27
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author GeLo <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class PagerfantaViewSubscriberTest extends \PHPUnit_Framework_TestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var PagerfantaViewSubscriber |
36
|
|
|
*/ |
37
|
|
|
private $subscriber; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
41
|
|
|
*/ |
42
|
|
|
private $parameterResolver; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|FormRendererInterface |
46
|
|
|
*/ |
47
|
|
|
private $pagerfantaFactory; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|RequestStack |
51
|
|
|
*/ |
52
|
|
|
private $requestStack; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function setUp() |
58
|
|
|
{ |
59
|
|
|
$this->parameterResolver = $this->createParameterResolverMock(); |
60
|
|
|
$this->pagerfantaFactory = $this->createPagerfantaFactoryMock(); |
61
|
|
|
$this->requestStack = $this->createRequestStackMock(); |
62
|
|
|
|
63
|
|
|
$this->subscriber = new PagerfantaViewSubscriber( |
64
|
|
|
$this->parameterResolver, |
65
|
|
|
$this->pagerfantaFactory, |
66
|
|
|
$this->requestStack |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testInheritance() |
71
|
|
|
{ |
72
|
|
|
$this->assertInstanceOf(EventSubscriberInterface::class, $this->subscriber); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testSubscribedEvents() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$this->assertSame( |
78
|
|
|
[RestEvents::VIEW => [ |
79
|
|
|
['onApi', -3000], |
80
|
|
|
['onView', -3000], |
81
|
|
|
]], |
82
|
|
|
$this->subscriber->getSubscribedEvents() |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function testApiWithoutApi() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this->parameterResolver |
|
|
|
|
89
|
|
|
->expects($this->once()) |
90
|
|
|
->method('resolveApi') |
91
|
|
|
->will($this->returnValue(false)); |
92
|
|
|
|
93
|
|
|
$event = $this->createViewEventMock(); |
94
|
|
|
$event |
95
|
|
|
->expects($this->never()) |
96
|
|
|
->method('getView'); |
97
|
|
|
|
98
|
|
|
$this->subscriber->onApi($event); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public function testApiWithoutPagerfanta() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$this->parameterResolver |
|
|
|
|
104
|
|
|
->expects($this->once()) |
105
|
|
|
->method('resolveApi') |
106
|
|
|
->will($this->returnValue(true)); |
107
|
|
|
|
108
|
|
|
$event = $this->createViewEventMock(); |
109
|
|
|
$event |
110
|
|
|
->expects($this->once()) |
111
|
|
|
->method('getView') |
112
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
113
|
|
|
|
114
|
|
|
$view |
115
|
|
|
->expects($this->once()) |
116
|
|
|
->method('getData') |
117
|
|
|
->will($this->returnValue('data')); |
118
|
|
|
|
119
|
|
|
$view |
120
|
|
|
->expects($this->never()) |
121
|
|
|
->method('setData'); |
122
|
|
|
|
123
|
|
|
$this->subscriber->onApi($event); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testApiWithPagerfanta() |
127
|
|
|
{ |
128
|
|
|
$this->parameterResolver |
|
|
|
|
129
|
|
|
->expects($this->once()) |
130
|
|
|
->method('resolveApi') |
131
|
|
|
->will($this->returnValue(true)); |
132
|
|
|
|
133
|
|
|
$event = $this->createViewEventMock(); |
134
|
|
|
$event |
135
|
|
|
->expects($this->once()) |
136
|
|
|
->method('getView') |
137
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
138
|
|
|
|
139
|
|
|
$view |
140
|
|
|
->expects($this->once()) |
141
|
|
|
->method('getData') |
142
|
|
|
->will($this->returnValue($pagerfanta = $this->createPagerfantaMock())); |
143
|
|
|
|
144
|
|
|
$this->parameterResolver |
145
|
|
|
->expects($this->once()) |
146
|
|
|
->method('resolveCurrentPage') |
147
|
|
|
->will($this->returnValue($currentPage = 2)); |
148
|
|
|
|
149
|
|
|
$pagerfanta |
150
|
|
|
->expects($this->once()) |
151
|
|
|
->method('setCurrentPage') |
152
|
|
|
->with($this->identicalTo($currentPage)); |
153
|
|
|
|
154
|
|
|
$this->parameterResolver |
155
|
|
|
->expects($this->once()) |
156
|
|
|
->method('resolveMaxPerPage') |
157
|
|
|
->will($this->returnValue($maxPerPage = 20)); |
158
|
|
|
|
159
|
|
|
$pagerfanta |
160
|
|
|
->expects($this->once()) |
161
|
|
|
->method('setMaxPerPage') |
162
|
|
|
->with($this->identicalTo($maxPerPage)); |
163
|
|
|
|
164
|
|
|
$pagerfanta |
165
|
|
|
->expects($this->once()) |
166
|
|
|
->method('getIterator') |
167
|
|
|
->will($this->returnValue(new \ArrayIterator($values = ['value']))); |
168
|
|
|
|
169
|
|
|
$view |
170
|
|
|
->expects($this->once()) |
171
|
|
|
->method('setData') |
172
|
|
|
->with($this->identicalTo($values)); |
173
|
|
|
|
174
|
|
|
$this->subscriber->onApi($event); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testApiWithHateoas() |
178
|
|
|
{ |
179
|
|
|
$this->parameterResolver |
|
|
|
|
180
|
|
|
->expects($this->once()) |
181
|
|
|
->method('resolveApi') |
182
|
|
|
->will($this->returnValue(true)); |
183
|
|
|
|
184
|
|
|
$this->parameterResolver |
185
|
|
|
->expects($this->once()) |
186
|
|
|
->method('resolveHateoas') |
187
|
|
|
->will($this->returnValue(true)); |
188
|
|
|
|
189
|
|
|
$event = $this->createViewEventMock(); |
190
|
|
|
$event |
191
|
|
|
->expects($this->once()) |
192
|
|
|
->method('getView') |
193
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
194
|
|
|
|
195
|
|
|
$view |
196
|
|
|
->expects($this->once()) |
197
|
|
|
->method('getData') |
198
|
|
|
->will($this->returnValue($pagerfanta = $this->createPagerfantaMock())); |
199
|
|
|
|
200
|
|
|
$this->parameterResolver |
201
|
|
|
->expects($this->once()) |
202
|
|
|
->method('resolveCurrentPage') |
203
|
|
|
->will($this->returnValue($currentPage = 2)); |
204
|
|
|
|
205
|
|
|
$pagerfanta |
206
|
|
|
->expects($this->once()) |
207
|
|
|
->method('setCurrentPage') |
208
|
|
|
->with($this->identicalTo($currentPage)); |
209
|
|
|
|
210
|
|
|
$this->parameterResolver |
211
|
|
|
->expects($this->once()) |
212
|
|
|
->method('resolveMaxPerPage') |
213
|
|
|
->will($this->returnValue($maxPerPage = 20)); |
214
|
|
|
|
215
|
|
|
$pagerfanta |
216
|
|
|
->expects($this->once()) |
217
|
|
|
->method('setMaxPerPage') |
218
|
|
|
->with($this->identicalTo($maxPerPage)); |
219
|
|
|
|
220
|
|
|
$this->requestStack |
|
|
|
|
221
|
|
|
->expects($this->once()) |
222
|
|
|
->method('getMasterRequest') |
223
|
|
|
->will($this->returnValue($request = $this->createRequestMock())); |
224
|
|
|
|
225
|
|
|
$request->attributes |
|
|
|
|
226
|
|
|
->expects($this->exactly(2)) |
227
|
|
|
->method('get') |
228
|
|
|
->will($this->returnValueMap([ |
229
|
|
|
['_route', null, false, $route = 'route'], |
230
|
|
|
['_route_params', [], false, $routeParameters = ['foo' => 'bar']], |
231
|
|
|
])); |
232
|
|
|
|
233
|
|
|
$request->query |
|
|
|
|
234
|
|
|
->expects($this->once()) |
235
|
|
|
->method('all') |
236
|
|
|
->will($this->returnValue($queryParameters = ['baz' => 'bat'])); |
237
|
|
|
|
238
|
|
|
$this->pagerfantaFactory |
|
|
|
|
239
|
|
|
->expects($this->once()) |
240
|
|
|
->method('createRepresentation') |
241
|
|
|
->with( |
242
|
|
|
$this->identicalTo($pagerfanta), |
243
|
|
|
$this->callback(function ($config) use ($route, $routeParameters, $queryParameters) { |
244
|
|
|
return $config instanceof Route |
245
|
|
|
&& $config->getName() === $route |
246
|
|
|
&& $config->getParameters() === array_merge($routeParameters, $queryParameters); |
247
|
|
|
}) |
248
|
|
|
) |
249
|
|
|
->will($this->returnValue($representation = 'representation')); |
250
|
|
|
|
251
|
|
|
$view |
252
|
|
|
->expects($this->once()) |
253
|
|
|
->method('setData') |
254
|
|
|
->with($this->identicalTo($representation)); |
255
|
|
|
|
256
|
|
|
$this->subscriber->onApi($event); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @expectedException \Lug\Bundle\ResourceBundle\Exception\RequestNotFoundException |
261
|
|
|
* @expectedExceptionMessage The request could not be found. |
262
|
|
|
*/ |
263
|
|
|
public function testApiWithHateoasButWithoutRequest() |
264
|
|
|
{ |
265
|
|
|
$this->parameterResolver |
|
|
|
|
266
|
|
|
->expects($this->once()) |
267
|
|
|
->method('resolveApi') |
268
|
|
|
->will($this->returnValue(true)); |
269
|
|
|
|
270
|
|
|
$this->parameterResolver |
271
|
|
|
->expects($this->once()) |
272
|
|
|
->method('resolveHateoas') |
273
|
|
|
->will($this->returnValue(true)); |
274
|
|
|
|
275
|
|
|
$event = $this->createViewEventMock(); |
276
|
|
|
$event |
277
|
|
|
->expects($this->once()) |
278
|
|
|
->method('getView') |
279
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
280
|
|
|
|
281
|
|
|
$view |
282
|
|
|
->expects($this->once()) |
283
|
|
|
->method('getData') |
284
|
|
|
->will($this->returnValue($pagerfanta = $this->createPagerfantaMock())); |
285
|
|
|
|
286
|
|
|
$this->requestStack |
|
|
|
|
287
|
|
|
->expects($this->once()) |
288
|
|
|
->method('getMasterRequest') |
289
|
|
|
->will($this->returnValue(null)); |
290
|
|
|
|
291
|
|
|
$this->subscriber->onApi($event); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
View Code Duplication |
public function testViewWithApi() |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
$this->parameterResolver |
|
|
|
|
297
|
|
|
->expects($this->once()) |
298
|
|
|
->method('resolveApi') |
299
|
|
|
->will($this->returnValue(true)); |
300
|
|
|
|
301
|
|
|
$event = $this->createViewEventMock(); |
302
|
|
|
$event |
303
|
|
|
->expects($this->never()) |
304
|
|
|
->method('getView'); |
305
|
|
|
|
306
|
|
|
$this->subscriber->onView($event); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
View Code Duplication |
public function testViewWithoutPagerfanta() |
|
|
|
|
310
|
|
|
{ |
311
|
|
|
$this->parameterResolver |
|
|
|
|
312
|
|
|
->expects($this->once()) |
313
|
|
|
->method('resolveApi') |
314
|
|
|
->will($this->returnValue(false)); |
315
|
|
|
|
316
|
|
|
$event = $this->createViewEventMock(); |
317
|
|
|
$event |
318
|
|
|
->expects($this->once()) |
319
|
|
|
->method('getView') |
320
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
321
|
|
|
|
322
|
|
|
$view |
323
|
|
|
->expects($this->once()) |
324
|
|
|
->method('getData') |
325
|
|
|
->will($this->returnValue('data')); |
326
|
|
|
|
327
|
|
|
$view |
328
|
|
|
->expects($this->never()) |
329
|
|
|
->method('setTemplateVar'); |
330
|
|
|
|
331
|
|
|
$this->subscriber->onView($event); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function testViewWithPagerfanta() |
335
|
|
|
{ |
336
|
|
|
$this->parameterResolver |
|
|
|
|
337
|
|
|
->expects($this->once()) |
338
|
|
|
->method('resolveApi') |
339
|
|
|
->will($this->returnValue(false)); |
340
|
|
|
|
341
|
|
|
$event = $this->createViewEventMock(); |
342
|
|
|
$event |
343
|
|
|
->expects($this->once()) |
344
|
|
|
->method('getView') |
345
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
346
|
|
|
|
347
|
|
|
$view |
348
|
|
|
->expects($this->once()) |
349
|
|
|
->method('getData') |
350
|
|
|
->will($this->returnValue($this->createPagerfantaMock())); |
351
|
|
|
|
352
|
|
|
$event |
353
|
|
|
->expects($this->once()) |
354
|
|
|
->method('getResource') |
355
|
|
|
->will($this->returnValue($resource = $this->createResourceMock())); |
356
|
|
|
|
357
|
|
|
$resource |
358
|
|
|
->expects($this->once()) |
359
|
|
|
->method('getName') |
360
|
|
|
->will($this->returnValue($name = 'name')); |
361
|
|
|
|
362
|
|
|
$view |
363
|
|
|
->expects($this->once()) |
364
|
|
|
->method('setTemplateVar') |
365
|
|
|
->with($this->identicalTo($name.'s')); |
366
|
|
|
|
367
|
|
|
$this->subscriber->onView($event); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
372
|
|
|
*/ |
373
|
|
|
private function createParameterResolverMock() |
374
|
|
|
{ |
375
|
|
|
return $this->createMock(ParameterResolverInterface::class); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|PagerfantaFactory |
380
|
|
|
*/ |
381
|
|
|
private function createPagerfantaFactoryMock() |
382
|
|
|
{ |
383
|
|
|
return $this->createMock(PagerfantaFactory::class); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|RequestStack |
388
|
|
|
*/ |
389
|
|
|
private function createRequestStackMock() |
390
|
|
|
{ |
391
|
|
|
return $this->createMock(RequestStack::class); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ViewEvent |
396
|
|
|
*/ |
397
|
|
|
private function createViewEventMock() |
398
|
|
|
{ |
399
|
|
|
return $this->createMock(ViewEvent::class); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|View |
404
|
|
|
*/ |
405
|
|
|
private function createViewMock() |
406
|
|
|
{ |
407
|
|
|
return $this->createMock(View::class); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Pagerfanta |
412
|
|
|
*/ |
413
|
|
|
private function createPagerfantaMock() |
414
|
|
|
{ |
415
|
|
|
return $this->createMock(Pagerfanta::class); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Request |
420
|
|
|
*/ |
421
|
|
View Code Duplication |
private function createRequestMock() |
|
|
|
|
422
|
|
|
{ |
423
|
|
|
$request = $this->createMock(Request::class); |
424
|
|
|
|
425
|
|
|
$request->attributes = $this->createParameterBagMock(); |
|
|
|
|
426
|
|
|
$request->query = $this->createParameterBagMock(); |
|
|
|
|
427
|
|
|
|
428
|
|
|
return $request; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ParameterBag |
433
|
|
|
*/ |
434
|
|
|
private function createParameterBagMock() |
435
|
|
|
{ |
436
|
|
|
return $this->createMock(ParameterBag::class); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
441
|
|
|
*/ |
442
|
|
|
private function createResourceMock() |
443
|
|
|
{ |
444
|
|
|
return $this->createMock(ResourceInterface::class); |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
|
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.