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\Routing; |
13
|
|
|
|
14
|
|
|
use Lug\Bundle\ResourceBundle\Routing\CachedParameterResolver; |
15
|
|
|
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; |
16
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class CachedParameterResolverTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var CachedParameterResolver |
25
|
|
|
*/ |
26
|
|
|
private $cachedParameterResolver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
30
|
|
|
*/ |
31
|
|
|
private $parameterResolver; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->parameterResolver = $this->createParameterResolverMock(); |
|
|
|
|
39
|
|
|
$this->cachedParameterResolver = new CachedParameterResolver($this->parameterResolver); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testInheritance() |
43
|
|
|
{ |
44
|
|
|
$this->assertInstanceOf(ParameterResolverInterface::class, $this->cachedParameterResolver); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testResolveApi() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->parameterResolver |
50
|
|
|
->expects($this->once()) |
51
|
|
|
->method('resolveApi') |
52
|
|
|
->will($this->returnValue(true)); |
53
|
|
|
|
54
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveApi()); |
55
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveApi()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function testResolveCriteria() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->parameterResolver |
61
|
|
|
->expects($this->once()) |
62
|
|
|
->method('resolveCriteria') |
63
|
|
|
->with($this->identicalTo($mandatory = false)) |
64
|
|
|
->will($this->returnValue($criteria = ['criteria'])); |
65
|
|
|
|
66
|
|
|
$this->assertSame($criteria, $this->cachedParameterResolver->resolveCriteria($mandatory)); |
67
|
|
|
$this->assertSame($criteria, $this->cachedParameterResolver->resolveCriteria($mandatory)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testResolveCriteriaMissing() |
71
|
|
|
{ |
72
|
|
|
$this->parameterResolver |
73
|
|
|
->expects($this->at(0)) |
74
|
|
|
->method('resolveCriteria') |
75
|
|
|
->with($this->identicalTo($firstMandatory = false)) |
76
|
|
|
->will($this->returnValue($criteria = [])); |
77
|
|
|
|
78
|
|
|
$this->parameterResolver |
79
|
|
|
->expects($this->at(1)) |
80
|
|
|
->method('resolveCriteria') |
81
|
|
|
->with($this->identicalTo($secondMandatory = true)) |
82
|
|
|
->will($this->throwException($exception = new \Exception())); |
83
|
|
|
|
84
|
|
|
$this->assertSame($criteria, $this->cachedParameterResolver->resolveCriteria($firstMandatory)); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
$this->cachedParameterResolver->resolveCriteria($secondMandatory); |
88
|
|
|
} catch (\Exception $e) { |
89
|
|
|
$this->assertSame($exception, $e); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
public function testResolveCurrentPage() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->parameterResolver |
96
|
|
|
->expects($this->once()) |
97
|
|
|
->method('resolveCurrentPage') |
98
|
|
|
->will($this->returnValue($currentPage = 2)); |
99
|
|
|
|
100
|
|
|
$this->assertSame($currentPage, $this->cachedParameterResolver->resolveCurrentPage()); |
101
|
|
|
$this->assertSame($currentPage, $this->cachedParameterResolver->resolveCurrentPage()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testResolveForm() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->parameterResolver |
107
|
|
|
->expects($this->once()) |
108
|
|
|
->method('resolveForm') |
109
|
|
|
->with($this->identicalTo($resource = $this->createResourceMock())) |
110
|
|
|
->will($this->returnValue($form = 'form')); |
111
|
|
|
|
112
|
|
|
$this->assertSame($form, $this->cachedParameterResolver->resolveForm($resource)); |
|
|
|
|
113
|
|
|
$this->assertSame($form, $this->cachedParameterResolver->resolveForm($resource)); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testResolveGrid() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this->parameterResolver |
119
|
|
|
->expects($this->once()) |
120
|
|
|
->method('resolveGrid') |
121
|
|
|
->with($this->identicalTo($resource = $this->createResourceMock())) |
122
|
|
|
->will($this->returnValue($grid = 'grid')); |
123
|
|
|
|
124
|
|
|
$this->assertSame($grid, $this->cachedParameterResolver->resolveGrid($resource)); |
|
|
|
|
125
|
|
|
$this->assertSame($grid, $this->cachedParameterResolver->resolveGrid($resource)); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
public function testResolveHateoas() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$this->parameterResolver |
131
|
|
|
->expects($this->once()) |
132
|
|
|
->method('resolveHateoas') |
133
|
|
|
->will($this->returnValue(true)); |
134
|
|
|
|
135
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveHateoas()); |
136
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveHateoas()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function testResolveLocationRoute() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$this->parameterResolver |
142
|
|
|
->expects($this->once()) |
143
|
|
|
->method('resolveLocationRoute') |
144
|
|
|
->will($this->returnValue($locationRoute = 'location_route')); |
145
|
|
|
|
146
|
|
|
$this->assertSame($locationRoute, $this->cachedParameterResolver->resolveLocationRoute()); |
147
|
|
|
$this->assertSame($locationRoute, $this->cachedParameterResolver->resolveLocationRoute()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
public function testResolveLocationRouteParameters() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$this->parameterResolver |
153
|
|
|
->expects($this->once()) |
154
|
|
|
->method('resolveLocationRouteParameters') |
155
|
|
|
->with($this->identicalTo($object = new \stdClass())) |
156
|
|
|
->will($this->returnValue($locationRouteParameters = ['location_route_param'])); |
157
|
|
|
|
158
|
|
|
$this->assertSame( |
159
|
|
|
$locationRouteParameters, |
160
|
|
|
$this->cachedParameterResolver->resolveLocationRouteParameters($object) |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$this->assertSame( |
164
|
|
|
$locationRouteParameters, |
165
|
|
|
$this->cachedParameterResolver->resolveLocationRouteParameters($object) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
public function testResolveMaxPerPage() |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$this->parameterResolver |
172
|
|
|
->expects($this->once()) |
173
|
|
|
->method('resolveMaxPerPage') |
174
|
|
|
->will($this->returnValue($maxPerPage = 5)); |
175
|
|
|
|
176
|
|
|
$this->assertSame($maxPerPage, $this->cachedParameterResolver->resolveMaxPerPage()); |
177
|
|
|
$this->assertSame($maxPerPage, $this->cachedParameterResolver->resolveMaxPerPage()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
View Code Duplication |
public function testResolveRedirectRoute() |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$this->parameterResolver |
183
|
|
|
->expects($this->once()) |
184
|
|
|
->method('resolveRedirectRoute') |
185
|
|
|
->will($this->returnValue($redirectRoute = 'redirect_route')); |
186
|
|
|
|
187
|
|
|
$this->assertSame($redirectRoute, $this->cachedParameterResolver->resolveRedirectRoute()); |
188
|
|
|
$this->assertSame($redirectRoute, $this->cachedParameterResolver->resolveRedirectRoute()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testResolveRedirectRouteParameters() |
192
|
|
|
{ |
193
|
|
|
$this->parameterResolver |
194
|
|
|
->expects($this->once()) |
195
|
|
|
->method('resolveRedirectRouteParameters') |
196
|
|
|
->with( |
197
|
|
|
$this->identicalTo($object = new \stdClass()), |
198
|
|
|
$this->identicalTo($forwardParameters = true) |
199
|
|
|
) |
200
|
|
|
->will($this->returnValue($redirectRouteParameters = ['redirect_route_param'])); |
201
|
|
|
|
202
|
|
|
$this->assertSame( |
203
|
|
|
$redirectRouteParameters, |
204
|
|
|
$this->cachedParameterResolver->resolveRedirectRouteParameters($object, $forwardParameters) |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
$this->assertSame( |
208
|
|
|
$redirectRouteParameters, |
209
|
|
|
$this->cachedParameterResolver->resolveRedirectRouteParameters($object, $forwardParameters) |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
View Code Duplication |
public function testResolveRedirectRouteParametersForward() |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$this->parameterResolver |
216
|
|
|
->expects($this->once()) |
217
|
|
|
->method('resolveRedirectRouteParametersForward') |
218
|
|
|
->will($this->returnValue(true)); |
219
|
|
|
|
220
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveRedirectRouteParametersForward()); |
221
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveRedirectRouteParametersForward()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
View Code Duplication |
public function testResolveRepositoryMethod() |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
$this->parameterResolver |
227
|
|
|
->expects($this->once()) |
228
|
|
|
->method('resolveRepositoryMethod') |
229
|
|
|
->with($this->identicalTo($action = 'action')) |
230
|
|
|
->will($this->returnValue($repositoryMethod = 'repository_method')); |
231
|
|
|
|
232
|
|
|
$this->assertSame($repositoryMethod, $this->cachedParameterResolver->resolveRepositoryMethod($action)); |
233
|
|
|
$this->assertSame($repositoryMethod, $this->cachedParameterResolver->resolveRepositoryMethod($action)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
View Code Duplication |
public function testResolveSerializerGroups() |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
$this->parameterResolver |
239
|
|
|
->expects($this->once()) |
240
|
|
|
->method('resolveSerializerGroups') |
241
|
|
|
->will($this->returnValue($groups = ['group'])); |
242
|
|
|
|
243
|
|
|
$this->assertSame($groups, $this->cachedParameterResolver->resolveSerializerGroups()); |
244
|
|
|
$this->assertSame($groups, $this->cachedParameterResolver->resolveSerializerGroups()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
View Code Duplication |
public function testResolveSerializerNull() |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
$this->parameterResolver |
250
|
|
|
->expects($this->once()) |
251
|
|
|
->method('resolveSerializerNull') |
252
|
|
|
->will($this->returnValue(true)); |
253
|
|
|
|
254
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveSerializerNull()); |
255
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveSerializerNull()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
View Code Duplication |
public function testResolveSorting() |
|
|
|
|
259
|
|
|
{ |
260
|
|
|
$this->parameterResolver |
261
|
|
|
->expects($this->once()) |
262
|
|
|
->method('resolveSorting') |
263
|
|
|
->will($this->returnValue($sorting = ['sorting'])); |
264
|
|
|
|
265
|
|
|
$this->assertSame($sorting, $this->cachedParameterResolver->resolveSorting()); |
266
|
|
|
$this->assertSame($sorting, $this->cachedParameterResolver->resolveSorting()); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
View Code Duplication |
public function testResolveTemplate() |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$this->parameterResolver |
272
|
|
|
->expects($this->once()) |
273
|
|
|
->method('resolveTemplate') |
274
|
|
|
->will($this->returnValue($template = 'template')); |
275
|
|
|
|
276
|
|
|
$this->assertSame($template, $this->cachedParameterResolver->resolveTemplate()); |
277
|
|
|
$this->assertSame($template, $this->cachedParameterResolver->resolveTemplate()); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
View Code Duplication |
public function testResolveThemes() |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
$this->parameterResolver |
283
|
|
|
->expects($this->once()) |
284
|
|
|
->method('resolveThemes') |
285
|
|
|
->will($this->returnValue($themes = ['theme'])); |
286
|
|
|
|
287
|
|
|
$this->assertSame($themes, $this->cachedParameterResolver->resolveThemes()); |
288
|
|
|
$this->assertSame($themes, $this->cachedParameterResolver->resolveThemes()); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
View Code Duplication |
public function testResolveTranslationDomain() |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
$this->parameterResolver |
294
|
|
|
->expects($this->once()) |
295
|
|
|
->method('resolveTranslationDomain') |
296
|
|
|
->will($this->returnValue($translationDomain = 'translation_domain')); |
297
|
|
|
|
298
|
|
|
$this->assertSame($translationDomain, $this->cachedParameterResolver->resolveTranslationDomain()); |
299
|
|
|
$this->assertSame($translationDomain, $this->cachedParameterResolver->resolveTranslationDomain()); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
View Code Duplication |
public function testResolveValidationGroups() |
|
|
|
|
303
|
|
|
{ |
304
|
|
|
$this->parameterResolver |
305
|
|
|
->expects($this->once()) |
306
|
|
|
->method('resolveValidationGroups') |
307
|
|
|
->will($this->returnValue($groups = ['group'])); |
308
|
|
|
|
309
|
|
|
$this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups()); |
310
|
|
|
$this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups()); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
View Code Duplication |
public function testResolveVoter() |
|
|
|
|
314
|
|
|
{ |
315
|
|
|
$this->parameterResolver |
316
|
|
|
->expects($this->once()) |
317
|
|
|
->method('resolveVoter') |
318
|
|
|
->will($this->returnValue(true)); |
319
|
|
|
|
320
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveVoter()); |
321
|
|
|
$this->assertTrue($this->cachedParameterResolver->resolveVoter()); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
326
|
|
|
*/ |
327
|
|
|
private function createParameterResolverMock() |
328
|
|
|
{ |
329
|
|
|
return $this->getMock(ParameterResolverInterface::class); |
|
|
|
|
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
334
|
|
|
*/ |
335
|
|
|
private function createResourceMock() |
336
|
|
|
{ |
337
|
|
|
return $this->getMock(ResourceInterface::class); |
|
|
|
|
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
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.