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\Context\Context; |
15
|
|
|
use FOS\RestBundle\View\View; |
16
|
|
|
use JMS\Serializer\Exclusion\GroupsExclusionStrategy; |
17
|
|
|
use Lug\Bundle\ResourceBundle\Rest\RestEvents; |
18
|
|
|
use Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber\ResourceViewSubscriber; |
19
|
|
|
use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent; |
20
|
|
|
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; |
21
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ResourceViewSubscriberTest extends \PHPUnit_Framework_TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ResourceViewSubscriber |
31
|
|
|
*/ |
32
|
|
|
private $subscriber; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
36
|
|
|
*/ |
37
|
|
|
private $parameterResolver; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function setUp() |
43
|
|
|
{ |
44
|
|
|
$this->parameterResolver = $this->createParameterResolverMock(); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->subscriber = new ResourceViewSubscriber($this->parameterResolver); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testInheritance() |
50
|
|
|
{ |
51
|
|
|
$this->assertInstanceOf(EventSubscriberInterface::class, $this->subscriber); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testSubscribedEvents() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->assertSame( |
57
|
|
|
[RestEvents::VIEW => [ |
58
|
|
|
['onApi', -4000], |
59
|
|
|
['onView', -4000], |
60
|
|
|
]], |
61
|
|
|
$this->subscriber->getSubscribedEvents() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testApiWithoutApi() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->parameterResolver |
68
|
|
|
->expects($this->once()) |
69
|
|
|
->method('resolveApi') |
70
|
|
|
->will($this->returnValue(false)); |
71
|
|
|
|
72
|
|
|
$event = $this->createViewEventMock(); |
73
|
|
|
$event |
|
|
|
|
74
|
|
|
->expects($this->never()) |
75
|
|
|
->method('getView'); |
76
|
|
|
|
77
|
|
|
$this->subscriber->onApi($event); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testApiWithSerializerGroups() |
81
|
|
|
{ |
82
|
|
|
$this->parameterResolver |
83
|
|
|
->expects($this->once()) |
84
|
|
|
->method('resolveApi') |
85
|
|
|
->will($this->returnValue(true)); |
86
|
|
|
|
87
|
|
|
$this->parameterResolver |
88
|
|
|
->expects($this->once()) |
89
|
|
|
->method('resolveSerializerGroups') |
90
|
|
|
->will($this->returnValue($groups = ['group'])); |
91
|
|
|
|
92
|
|
|
$this->parameterResolver |
93
|
|
|
->expects($this->once()) |
94
|
|
|
->method('resolveSerializerNull') |
95
|
|
|
->will($this->returnValue($null = true)); |
96
|
|
|
|
97
|
|
|
$event = $this->createViewEventMock(); |
98
|
|
|
$event |
|
|
|
|
99
|
|
|
->expects($this->once()) |
100
|
|
|
->method('getView') |
101
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
102
|
|
|
|
103
|
|
|
$view |
|
|
|
|
104
|
|
|
->expects($this->exactly(2)) |
105
|
|
|
->method('getContext') |
106
|
|
|
->will($this->returnValue($context = new Context())); |
107
|
|
|
|
108
|
|
|
$this->subscriber->onApi($event); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$this->assertSame($groups, $context->getGroups()); |
111
|
|
|
$this->assertSame($null, $context->getSerializeNull()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testApiWithoutSerializerGroups() |
115
|
|
|
{ |
116
|
|
|
$this->parameterResolver |
117
|
|
|
->expects($this->once()) |
118
|
|
|
->method('resolveApi') |
119
|
|
|
->will($this->returnValue(true)); |
120
|
|
|
|
121
|
|
|
$this->parameterResolver |
122
|
|
|
->expects($this->once()) |
123
|
|
|
->method('resolveSerializerGroups') |
124
|
|
|
->will($this->returnValue([])); |
125
|
|
|
|
126
|
|
|
$this->parameterResolver |
127
|
|
|
->expects($this->once()) |
128
|
|
|
->method('resolveSerializerNull') |
129
|
|
|
->will($this->returnValue($null = true)); |
130
|
|
|
|
131
|
|
|
$event = $this->createViewEventMock(); |
132
|
|
|
$event |
|
|
|
|
133
|
|
|
->expects($this->once()) |
134
|
|
|
->method('getView') |
135
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
136
|
|
|
|
137
|
|
|
$event |
138
|
|
|
->expects($this->once()) |
139
|
|
|
->method('getResource') |
140
|
|
|
->will($this->returnValue($resource = $this->createResourceMock())); |
141
|
|
|
|
142
|
|
|
$resource |
|
|
|
|
143
|
|
|
->expects($this->once()) |
144
|
|
|
->method('getName') |
145
|
|
|
->will($this->returnValue($name = 'name')); |
146
|
|
|
|
147
|
|
|
$view |
|
|
|
|
148
|
|
|
->expects($this->exactly(2)) |
149
|
|
|
->method('getContext') |
150
|
|
|
->will($this->returnValue($context = new Context())); |
151
|
|
|
|
152
|
|
|
$this->subscriber->onApi($event); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$this->assertSame([GroupsExclusionStrategy::DEFAULT_GROUP, 'lug.'.$name], $context->getGroups()); |
155
|
|
|
$this->assertSame($null, $context->getSerializeNull()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
View Code Duplication |
public function testViewWithApi() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$this->parameterResolver |
161
|
|
|
->expects($this->once()) |
162
|
|
|
->method('resolveApi') |
163
|
|
|
->will($this->returnValue(true)); |
164
|
|
|
|
165
|
|
|
$event = $this->createViewEventMock(); |
166
|
|
|
$event |
|
|
|
|
167
|
|
|
->expects($this->never()) |
168
|
|
|
->method('getView'); |
169
|
|
|
|
170
|
|
|
$this->subscriber->onView($event); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
View Code Duplication |
public function testViewWithoutTemplateVar() |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$this->parameterResolver |
176
|
|
|
->expects($this->once()) |
177
|
|
|
->method('resolveApi') |
178
|
|
|
->will($this->returnValue(false)); |
179
|
|
|
|
180
|
|
|
$event = $this->createViewEventMock(); |
181
|
|
|
$event |
|
|
|
|
182
|
|
|
->expects($this->once()) |
183
|
|
|
->method('getView') |
184
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
185
|
|
|
|
186
|
|
|
$view |
|
|
|
|
187
|
|
|
->expects($this->once()) |
188
|
|
|
->method('getData') |
189
|
|
|
->will($this->returnValue($data = new \stdClass())); |
190
|
|
|
|
191
|
|
|
$this->parameterResolver |
192
|
|
|
->expects($this->once()) |
193
|
|
|
->method('resolveTemplate') |
194
|
|
|
->will($this->returnValue($template = 'template')); |
195
|
|
|
|
196
|
|
|
$view |
197
|
|
|
->expects($this->once()) |
198
|
|
|
->method('setTemplate') |
199
|
|
|
->with($this->identicalTo($template)) |
200
|
|
|
->will($this->returnSelf()); |
201
|
|
|
|
202
|
|
|
$event |
203
|
|
|
->expects($this->once()) |
204
|
|
|
->method('getResource') |
205
|
|
|
->will($this->returnValue($resource = $this->createResourceMock())); |
206
|
|
|
|
207
|
|
|
$view |
208
|
|
|
->expects($this->once()) |
209
|
|
|
->method('getTemplateVar') |
210
|
|
|
->will($this->returnValue(null)); |
211
|
|
|
|
212
|
|
|
$view |
213
|
|
|
->expects($this->once()) |
214
|
|
|
->method('setData') |
215
|
|
|
->with($this->identicalTo(['data' => $data, 'resource' => $resource])); |
216
|
|
|
|
217
|
|
|
$this->subscriber->onView($event); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
View Code Duplication |
public function testViewWithTemplateVar() |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
$this->parameterResolver |
223
|
|
|
->expects($this->once()) |
224
|
|
|
->method('resolveApi') |
225
|
|
|
->will($this->returnValue(false)); |
226
|
|
|
|
227
|
|
|
$event = $this->createViewEventMock(); |
228
|
|
|
$event |
|
|
|
|
229
|
|
|
->expects($this->once()) |
230
|
|
|
->method('getView') |
231
|
|
|
->will($this->returnValue($view = $this->createViewMock())); |
232
|
|
|
|
233
|
|
|
$view |
|
|
|
|
234
|
|
|
->expects($this->once()) |
235
|
|
|
->method('getData') |
236
|
|
|
->will($this->returnValue($data = new \stdClass())); |
237
|
|
|
|
238
|
|
|
$this->parameterResolver |
239
|
|
|
->expects($this->once()) |
240
|
|
|
->method('resolveTemplate') |
241
|
|
|
->will($this->returnValue($template = 'template')); |
242
|
|
|
|
243
|
|
|
$view |
244
|
|
|
->expects($this->once()) |
245
|
|
|
->method('setTemplate') |
246
|
|
|
->with($this->identicalTo($template)) |
247
|
|
|
->will($this->returnSelf()); |
248
|
|
|
|
249
|
|
|
$event |
250
|
|
|
->expects($this->once()) |
251
|
|
|
->method('getResource') |
252
|
|
|
->will($this->returnValue($resource = $this->createResourceMock())); |
253
|
|
|
|
254
|
|
|
$view |
255
|
|
|
->expects($this->once()) |
256
|
|
|
->method('getTemplateVar') |
257
|
|
|
->will($this->returnValue($templateVar = 'template_var')); |
258
|
|
|
|
259
|
|
|
$view |
260
|
|
|
->expects($this->once()) |
261
|
|
|
->method('setData') |
262
|
|
|
->with($this->identicalTo([$templateVar => $data, 'resource' => $resource])); |
263
|
|
|
|
264
|
|
|
$this->subscriber->onView($event); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
269
|
|
|
*/ |
270
|
|
|
private function createParameterResolverMock() |
271
|
|
|
{ |
272
|
|
|
return $this->getMock(ParameterResolverInterface::class); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ViewEvent |
277
|
|
|
*/ |
278
|
|
|
private function createViewEventMock() |
279
|
|
|
{ |
280
|
|
|
return $this->getMockBuilder(ViewEvent::class) |
281
|
|
|
->disableOriginalConstructor() |
282
|
|
|
->getMock(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|View |
287
|
|
|
*/ |
288
|
|
|
private function createViewMock() |
289
|
|
|
{ |
290
|
|
|
return $this->getMock(View::class); |
|
|
|
|
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
295
|
|
|
*/ |
296
|
|
|
private function createResourceMock() |
297
|
|
|
{ |
298
|
|
|
return $this->getMock(ResourceInterface::class); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
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.