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 Lug\Bundle\GridBundle\Form\Type\Batch\GridBatchType; |
16
|
|
|
use Lug\Bundle\GridBundle\View\GridViewInterface; |
17
|
|
|
use Lug\Bundle\ResourceBundle\Form\FormFactoryInterface; |
18
|
|
|
use Lug\Bundle\ResourceBundle\Rest\RestEvents; |
19
|
|
|
use Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber\GridViewSubscriber; |
20
|
|
|
use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent; |
21
|
|
|
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; |
22
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
24
|
|
|
use Symfony\Component\Form\FormInterface; |
25
|
|
|
use Symfony\Component\Form\FormRendererInterface; |
26
|
|
|
use Symfony\Component\Form\FormView; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author GeLo <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class GridViewSubscriberTest extends \PHPUnit_Framework_TestCase |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var GridViewSubscriber |
35
|
|
|
*/ |
36
|
|
|
private $subscriber; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
40
|
|
|
*/ |
41
|
|
|
private $parameterResolver; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|FormFactoryInterface |
45
|
|
|
*/ |
46
|
|
|
private $formFactory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|FormRendererInterface |
50
|
|
|
*/ |
51
|
|
|
private $formRenderer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function setUp() |
57
|
|
|
{ |
58
|
|
|
$this->parameterResolver = $this->createParameterResolverMock(); |
|
|
|
|
59
|
|
|
$this->formFactory = $this->createFormFactoryMock(); |
|
|
|
|
60
|
|
|
$this->formRenderer = $this->createFormRendererMock(); |
61
|
|
|
|
62
|
|
|
$this->subscriber = new GridViewSubscriber($this->parameterResolver, $this->formFactory, $this->formRenderer); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testInheritance() |
66
|
|
|
{ |
67
|
|
|
$this->assertInstanceOf(EventSubscriberInterface::class, $this->subscriber); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
public function testSubscribedEvents() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$this->assertSame( |
73
|
|
|
[RestEvents::VIEW => [ |
74
|
|
|
['onApi', -2000], |
75
|
|
|
['onView', -1000], |
76
|
|
|
]], |
77
|
|
|
$this->subscriber->getSubscribedEvents() |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function testApiWithoutApi() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->parameterResolver |
84
|
|
|
->expects($this->once()) |
85
|
|
|
->method('resolveApi') |
86
|
|
|
->will($this->returnValue(false)); |
87
|
|
|
|
88
|
|
|
$event = $this->createViewEventMock(); |
89
|
|
|
$event |
|
|
|
|
90
|
|
|
->expects($this->never()) |
91
|
|
|
->method('getView'); |
92
|
|
|
|
93
|
|
|
$this->subscriber->onApi($event); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testApiWithoutGrid() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$this->parameterResolver |
99
|
|
|
->expects($this->once()) |
100
|
|
|
->method('resolveApi') |
101
|
|
|
->will($this->returnValue(true)); |
102
|
|
|
|
103
|
|
|
$event = $this->createViewEventMock(); |
104
|
|
|
$event |
|
|
|
|
105
|
|
|
->expects($this->once()) |
106
|
|
|
->method('getView') |
107
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
108
|
|
|
|
109
|
|
|
$view |
|
|
|
|
110
|
|
|
->expects($this->once()) |
111
|
|
|
->method('getData') |
112
|
|
|
->will($this->returnValue('data')); |
113
|
|
|
|
114
|
|
|
$view |
115
|
|
|
->expects($this->never()) |
116
|
|
|
->method('setData'); |
117
|
|
|
|
118
|
|
|
$this->subscriber->onApi($event); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
public function testApiWithGrid() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$this->parameterResolver |
124
|
|
|
->expects($this->once()) |
125
|
|
|
->method('resolveApi') |
126
|
|
|
->will($this->returnValue(true)); |
127
|
|
|
|
128
|
|
|
$event = $this->createViewEventMock(); |
129
|
|
|
$event |
|
|
|
|
130
|
|
|
->expects($this->once()) |
131
|
|
|
->method('getView') |
132
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
133
|
|
|
|
134
|
|
|
$view |
|
|
|
|
135
|
|
|
->expects($this->once()) |
136
|
|
|
->method('getData') |
137
|
|
|
->will($this->returnValue(['grid' => $gridView = $this->createGridViewMock()])); |
138
|
|
|
|
139
|
|
|
$gridView |
|
|
|
|
140
|
|
|
->expects($this->once()) |
141
|
|
|
->method('getDataSource') |
142
|
|
|
->will($this->returnValue($dataSource = 'data_source')); |
143
|
|
|
|
144
|
|
|
$view |
145
|
|
|
->expects($this->once()) |
146
|
|
|
->method('setData') |
147
|
|
|
->with($this->identicalTo($dataSource)); |
148
|
|
|
|
149
|
|
|
$this->subscriber->onApi($event); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
public function testViewWithApi() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$this->parameterResolver |
155
|
|
|
->expects($this->once()) |
156
|
|
|
->method('resolveApi') |
157
|
|
|
->will($this->returnValue(true)); |
158
|
|
|
|
159
|
|
|
$event = $this->createViewEventMock(); |
160
|
|
|
$event |
|
|
|
|
161
|
|
|
->expects($this->never()) |
162
|
|
|
->method('getView'); |
163
|
|
|
|
164
|
|
|
$this->subscriber->onView($event); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
View Code Duplication |
public function testViewWithoutGrid() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$this->parameterResolver |
170
|
|
|
->expects($this->once()) |
171
|
|
|
->method('resolveApi') |
172
|
|
|
->will($this->returnValue(false)); |
173
|
|
|
|
174
|
|
|
$event = $this->createViewEventMock(); |
175
|
|
|
$event |
|
|
|
|
176
|
|
|
->expects($this->once()) |
177
|
|
|
->method('getView') |
178
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
179
|
|
|
|
180
|
|
|
$view |
|
|
|
|
181
|
|
|
->expects($this->once()) |
182
|
|
|
->method('getData') |
183
|
|
|
->will($this->returnValue('data')); |
184
|
|
|
|
185
|
|
|
$view |
186
|
|
|
->expects($this->never()) |
187
|
|
|
->method('setData'); |
188
|
|
|
|
189
|
|
|
$this->subscriber->onView($event); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testViewWithoutBatchForm() |
193
|
|
|
{ |
194
|
|
|
$this->parameterResolver |
195
|
|
|
->expects($this->once()) |
196
|
|
|
->method('resolveApi') |
197
|
|
|
->will($this->returnValue(false)); |
198
|
|
|
|
199
|
|
|
$event = $this->createViewEventMock(); |
200
|
|
|
$event |
|
|
|
|
201
|
|
|
->expects($this->once()) |
202
|
|
|
->method('getView') |
203
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
204
|
|
|
|
205
|
|
|
$event |
206
|
|
|
->expects($this->once()) |
207
|
|
|
->method('getResource') |
208
|
|
|
->will($this->returnValue($resource = $this->createResourceMock())); |
209
|
|
|
|
210
|
|
|
$view |
|
|
|
|
211
|
|
|
->expects($this->once()) |
212
|
|
|
->method('getData') |
213
|
|
|
->will($this->returnValue(['grid' => $gridView = $this->createGridViewMock()])); |
214
|
|
|
|
215
|
|
|
$this->formFactory |
216
|
|
|
->expects($this->once()) |
217
|
|
|
->method('create') |
218
|
|
|
->with( |
219
|
|
|
$this->identicalTo($resource), |
220
|
|
|
$this->identicalTo(GridBatchType::class), |
221
|
|
|
$this->isNull(), |
222
|
|
|
$this->identicalTo(['grid' => $gridView]) |
223
|
|
|
) |
224
|
|
|
->will($this->returnValue($batchForm = $this->createFormMock())); |
225
|
|
|
|
226
|
|
|
$batchForm |
|
|
|
|
227
|
|
|
->expects($this->once()) |
228
|
|
|
->method('createView') |
229
|
|
|
->will($this->returnValue($batchFormView = $this->createFormViewMock())); |
230
|
|
|
|
231
|
|
|
$gridView |
|
|
|
|
232
|
|
|
->expects($this->once()) |
233
|
|
|
->method('setBatchForm') |
234
|
|
|
->with($this->identicalTo($batchFormView)); |
235
|
|
|
|
236
|
|
|
$view |
237
|
|
|
->expects($this->once()) |
238
|
|
|
->method('setTemplateVar') |
239
|
|
|
->with($this->identicalTo('grid')) |
240
|
|
|
->will($this->returnSelf()); |
241
|
|
|
|
242
|
|
|
$view |
243
|
|
|
->expects($this->once()) |
244
|
|
|
->method('setData') |
245
|
|
|
->with($this->identicalTo($gridView)); |
246
|
|
|
|
247
|
|
|
$this->subscriber->onView($event); |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testViewWithBatchForm() |
251
|
|
|
{ |
252
|
|
|
$this->parameterResolver |
253
|
|
|
->expects($this->once()) |
254
|
|
|
->method('resolveApi') |
255
|
|
|
->will($this->returnValue(false)); |
256
|
|
|
|
257
|
|
|
$event = $this->createViewEventMock(); |
258
|
|
|
$event |
|
|
|
|
259
|
|
|
->expects($this->once()) |
260
|
|
|
->method('getView') |
261
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
262
|
|
|
|
263
|
|
|
$view |
|
|
|
|
264
|
|
|
->expects($this->once()) |
265
|
|
|
->method('getData') |
266
|
|
|
->will($this->returnValue([ |
267
|
|
|
'grid' => $gridView = $this->createGridViewMock(), |
268
|
|
|
'batch_form' => $batchForm = $this->createFormMock(), |
269
|
|
|
])); |
270
|
|
|
|
271
|
|
|
$batchForm |
|
|
|
|
272
|
|
|
->expects($this->once()) |
273
|
|
|
->method('createView') |
274
|
|
|
->will($this->returnValue($batchFormView = $this->createFormViewMock())); |
275
|
|
|
|
276
|
|
|
$gridView |
|
|
|
|
277
|
|
|
->expects($this->once()) |
278
|
|
|
->method('setBatchForm') |
279
|
|
|
->with($this->identicalTo($batchFormView)); |
280
|
|
|
|
281
|
|
|
$view |
282
|
|
|
->expects($this->once()) |
283
|
|
|
->method('setTemplateVar') |
284
|
|
|
->with($this->identicalTo('grid')) |
285
|
|
|
->will($this->returnSelf()); |
286
|
|
|
|
287
|
|
|
$view |
288
|
|
|
->expects($this->once()) |
289
|
|
|
->method('setData') |
290
|
|
|
->with($this->identicalTo($gridView)); |
291
|
|
|
|
292
|
|
|
$this->subscriber->onView($event); |
|
|
|
|
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function testViewFormThemes() |
296
|
|
|
{ |
297
|
|
|
$this->parameterResolver |
298
|
|
|
->expects($this->once()) |
299
|
|
|
->method('resolveApi') |
300
|
|
|
->will($this->returnValue(false)); |
301
|
|
|
|
302
|
|
|
$this->parameterResolver |
303
|
|
|
->expects($this->once()) |
304
|
|
|
->method('resolveThemes') |
305
|
|
|
->will($this->returnValue($themes = ['theme'])); |
306
|
|
|
|
307
|
|
|
$event = $this->createViewEventMock(); |
308
|
|
|
$event |
|
|
|
|
309
|
|
|
->expects($this->once()) |
310
|
|
|
->method('getView') |
311
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
312
|
|
|
|
313
|
|
|
$view |
|
|
|
|
314
|
|
|
->expects($this->once()) |
315
|
|
|
->method('getData') |
316
|
|
|
->will($this->returnValue([ |
317
|
|
|
'grid' => $gridView = $this->createGridViewMock(), |
318
|
|
|
'batch_form' => $batchForm = $this->createFormMock(), |
319
|
|
|
])); |
320
|
|
|
|
321
|
|
|
$batchForm |
|
|
|
|
322
|
|
|
->expects($this->once()) |
323
|
|
|
->method('createView') |
324
|
|
|
->will($this->returnValue($batchFormView = $this->createFormViewMock())); |
325
|
|
|
|
326
|
|
|
$gridView |
|
|
|
|
327
|
|
|
->expects($this->once()) |
328
|
|
|
->method('setBatchForm') |
329
|
|
|
->with($this->identicalTo($batchFormView)); |
330
|
|
|
|
331
|
|
|
$gridView |
332
|
|
|
->expects($this->once()) |
333
|
|
|
->method('getForm') |
334
|
|
|
->will($this->returnValue($formView = $this->createFormViewMock())); |
335
|
|
|
|
336
|
|
|
$gridView |
337
|
|
|
->expects($this->exactly(2)) |
338
|
|
|
->method('getBatchForm') |
339
|
|
|
->willReturnOnConsecutiveCalls(null, $batchFormView); |
340
|
|
|
|
341
|
|
|
$this->formRenderer |
|
|
|
|
342
|
|
|
->expects($this->exactly(2)) |
343
|
|
|
->method('setTheme') |
344
|
|
|
->withConsecutive( |
345
|
|
|
[$formView, $themes], |
346
|
|
|
[$batchFormView, $themes] |
347
|
|
|
); |
348
|
|
|
|
349
|
|
|
$view |
350
|
|
|
->expects($this->once()) |
351
|
|
|
->method('setTemplateVar') |
352
|
|
|
->with($this->identicalTo('grid')) |
353
|
|
|
->will($this->returnSelf()); |
354
|
|
|
|
355
|
|
|
$view |
356
|
|
|
->expects($this->once()) |
357
|
|
|
->method('setData') |
358
|
|
|
->with($this->identicalTo($gridView)); |
359
|
|
|
|
360
|
|
|
$this->subscriber->onView($event); |
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
365
|
|
|
*/ |
366
|
|
|
private function createParameterResolverMock() |
367
|
|
|
{ |
368
|
|
|
return $this->getMock(ParameterResolverInterface::class); |
|
|
|
|
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|FormFactoryInterface |
373
|
|
|
*/ |
374
|
|
|
private function createFormFactoryMock() |
375
|
|
|
{ |
376
|
|
|
return $this->getMock(FormFactoryInterface::class); |
|
|
|
|
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|FormRendererInterface |
381
|
|
|
*/ |
382
|
|
|
private function createFormRendererMock() |
383
|
|
|
{ |
384
|
|
|
return $this->getMock(FormRendererInterface::class); |
|
|
|
|
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ViewEvent |
389
|
|
|
*/ |
390
|
|
|
private function createViewEventMock() |
391
|
|
|
{ |
392
|
|
|
return $this->getMockBuilder(ViewEvent::class) |
393
|
|
|
->disableOriginalConstructor() |
394
|
|
|
->getMock(); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
399
|
|
|
*/ |
400
|
|
|
private function createResourceMock() |
401
|
|
|
{ |
402
|
|
|
return $this->getMock(ResourceInterface::class); |
|
|
|
|
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|View |
407
|
|
|
*/ |
408
|
|
|
private function createViewMock() |
409
|
|
|
{ |
410
|
|
|
return $this->getMock(View::class); |
|
|
|
|
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|FormInterface |
415
|
|
|
*/ |
416
|
|
|
private function createFormMock() |
417
|
|
|
{ |
418
|
|
|
return $this->getMock(FormInterface::class); |
|
|
|
|
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|FormView |
423
|
|
|
*/ |
424
|
|
|
private function createFormViewMock() |
425
|
|
|
{ |
426
|
|
|
return $this->getMock(FormView::class); |
|
|
|
|
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|GridViewInterface |
431
|
|
|
*/ |
432
|
|
|
private function createGridViewMock() |
433
|
|
|
{ |
434
|
|
|
return $this->getMock(GridViewInterface::class); |
|
|
|
|
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.