These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Copyright (C) 2017 Gerrit Addiks. |
||
4 | * This package (including this file) was released under the terms of the GPL-3.0. |
||
5 | * You should have received a copy of the GNU General Public License along with this program. |
||
6 | * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
||
7 | * @license GPL-3.0 |
||
8 | * @author Gerrit Addiks <[email protected]> |
||
9 | */ |
||
10 | |||
11 | namespace Addiks\SymfonyGenerics\Tests\Unit\Controllers\API; |
||
12 | |||
13 | use PHPUnit\Framework\TestCase; |
||
14 | use Addiks\SymfonyGenerics\Controllers\API\GenericEntityInvokeController; |
||
15 | use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
||
16 | use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
||
17 | use Symfony\Component\HttpFoundation\Request; |
||
18 | use ReflectionMethod; |
||
19 | use InvalidArgumentException; |
||
20 | use Symfony\Component\Finder\Exception\AccessDeniedException; |
||
21 | use Addiks\SymfonyGenerics\Events\EntityInteractionEvent; |
||
22 | use Symfony\Component\HttpFoundation\Response; |
||
23 | use Addiks\SymfonyGenerics\Tests\Unit\Controllers\SampleEntity; |
||
24 | |||
25 | final class GenericEntityInvokeControllerTest extends TestCase |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var GenericEntityInvokeController |
||
30 | */ |
||
31 | private $controller; |
||
32 | |||
33 | /** |
||
34 | * @var ControllerHelperInterface |
||
35 | */ |
||
36 | private $controllerHelper; |
||
37 | |||
38 | /** |
||
39 | * @var ArgumentCompilerInterface |
||
40 | */ |
||
41 | private $argumentCompiler; |
||
42 | |||
43 | public function setUp() |
||
44 | { |
||
45 | $this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
||
46 | $this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @test |
||
51 | */ |
||
52 | public function shouldInvodeAnEntityMethod() |
||
53 | { |
||
54 | /** @var Request $request */ |
||
55 | $request = $this->createMock(Request::class); |
||
0 ignored issues
–
show
|
|||
56 | |||
57 | $this->controller = new GenericEntityInvokeController( |
||
58 | $this->controllerHelper, |
||
59 | $this->argumentCompiler, |
||
60 | [ |
||
61 | 'entity-class' => get_class($this->argumentCompiler), |
||
62 | 'method' => 'buildArguments', |
||
63 | 'arguments' => [ |
||
64 | 'argumentsConfiguration' => "Lorem", |
||
65 | ] |
||
66 | ] |
||
67 | ); |
||
68 | |||
69 | $this->controllerHelper->expects($this->once())->method('findEntity')->with( |
||
70 | $this->equalTo(get_class($this->argumentCompiler)), |
||
71 | $this->equalTo("123") |
||
72 | )->willReturn($this->argumentCompiler); |
||
73 | |||
74 | $this->controllerHelper->expects($this->once())->method('flushORM'); |
||
75 | $this->controllerHelper->expects($this->once())->method('dispatchEvent')->with( |
||
76 | $this->equalTo("symfony_generics.entity_interaction"), |
||
77 | $this->equalTo(new EntityInteractionEvent( |
||
78 | get_class($this->argumentCompiler), |
||
79 | "123", |
||
80 | $this->argumentCompiler, |
||
81 | 'buildArguments', |
||
82 | [['foo' => 'bar']] |
||
83 | )) |
||
84 | ); |
||
85 | |||
86 | $this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with( |
||
87 | $this->equalTo(new ReflectionMethod(get_class($this->argumentCompiler), 'buildArguments')), |
||
88 | $this->equalTo([ |
||
89 | 'argumentsConfiguration' => "Lorem", |
||
90 | ]) |
||
91 | )->willReturn([ |
||
92 | ['foo' => 'bar'] |
||
93 | ]); |
||
94 | |||
95 | $this->argumentCompiler->expects($this->once())->method('buildArguments')->with( |
||
96 | $this->equalTo(['foo' => 'bar']) |
||
97 | ); |
||
98 | |||
99 | $this->controller->invokeEntityMethod("123"); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @test |
||
104 | */ |
||
105 | public function shouldRedirectAfterInvokation() |
||
106 | { |
||
107 | /** @var Request $request */ |
||
108 | $request = $this->createMock(Request::class); |
||
0 ignored issues
–
show
$request is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
109 | |||
110 | $this->controller = new GenericEntityInvokeController( |
||
111 | $this->controllerHelper, |
||
112 | $this->argumentCompiler, |
||
113 | [ |
||
114 | 'entity-class' => SampleEntity::class, |
||
115 | 'method' => 'getId', |
||
116 | 'redirect-route' => 'some-redirect-route', |
||
117 | 'redirect-route-parameters' => ['some-redirect-route-parameters'] |
||
118 | ] |
||
119 | ); |
||
120 | |||
121 | /** @var SampleEntity $entity */ |
||
122 | $entity = $this->createMock(SampleEntity::class); |
||
123 | $entity->method('getId')->willReturn("some-result"); |
||
124 | |||
125 | $this->argumentCompiler->expects($this->once())->method('buildArguments')->with( |
||
126 | $this->equalTo(['some-redirect-route-parameters']), |
||
127 | $this->equalTo(['result' => "some-result"]) |
||
128 | )->willReturn(['foo' => 'bar']); |
||
129 | |||
130 | $this->controllerHelper->expects($this->once())->method('findEntity')->with( |
||
131 | $this->equalTo(SampleEntity::class), |
||
132 | $this->equalTo("123") |
||
133 | )->willReturn($entity); |
||
134 | |||
135 | $this->controllerHelper->expects($this->once())->method('redirectToRoute')->with( |
||
136 | $this->equalTo('some-redirect-route'), |
||
137 | $this->equalTo(['foo' => 'bar']) |
||
138 | )->willReturn($this->createMock(Response::class)); |
||
139 | |||
140 | $this->controller->invokeEntityMethod("123"); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @test |
||
145 | */ |
||
146 | public function shouldThrowExceptionWhenEntityNotFound() |
||
147 | { |
||
148 | $this->expectException(InvalidArgumentException::class); |
||
149 | |||
150 | /** @var Request $request */ |
||
151 | $request = $this->createMock(Request::class); |
||
0 ignored issues
–
show
$request is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
152 | |||
153 | $this->controller = new GenericEntityInvokeController( |
||
154 | $this->controllerHelper, |
||
155 | $this->argumentCompiler, |
||
156 | [ |
||
157 | 'entity-class' => get_class($this->argumentCompiler), |
||
158 | 'method' => 'buildArguments', |
||
159 | ] |
||
160 | ); |
||
161 | |||
162 | $this->controllerHelper->expects($this->once())->method('findEntity')->with( |
||
163 | $this->equalTo(get_class($this->argumentCompiler)), |
||
164 | $this->equalTo("123") |
||
165 | )->willReturn(null); |
||
166 | |||
167 | $this->controller->invokeEntityMethod("123"); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @test |
||
172 | */ |
||
173 | public function shouldThrowExceptionWhenAccessNotGranted() |
||
174 | { |
||
175 | $this->expectException(AccessDeniedException::class); |
||
176 | |||
177 | /** @var Request $request */ |
||
178 | $request = $this->createMock(Request::class); |
||
0 ignored issues
–
show
$request is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
179 | |||
180 | $this->controller = new GenericEntityInvokeController( |
||
181 | $this->controllerHelper, |
||
182 | $this->argumentCompiler, |
||
183 | [ |
||
184 | 'entity-class' => get_class($this->argumentCompiler), |
||
185 | 'method' => 'buildArguments', |
||
186 | 'deny-access-attribute' => 'foo', |
||
187 | ] |
||
188 | ); |
||
189 | |||
190 | $this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with( |
||
191 | $this->equalTo('foo'), |
||
192 | $this->equalTo($this->argumentCompiler) |
||
193 | )->will($this->returnCallback( |
||
194 | function () { |
||
195 | throw new AccessDeniedException("Lorem ipsum"); |
||
196 | } |
||
197 | )); |
||
198 | |||
199 | $this->controllerHelper->expects($this->once())->method('findEntity')->with( |
||
200 | $this->equalTo(get_class($this->argumentCompiler)), |
||
201 | $this->equalTo("123") |
||
202 | )->willReturn($this->argumentCompiler); |
||
203 | |||
204 | $this->controller->invokeEntityMethod("123"); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @test |
||
209 | */ |
||
210 | public function shouldRejectConstructorCalledAgain() |
||
211 | { |
||
212 | $this->expectException(InvalidArgumentException::class); |
||
213 | |||
214 | $controller = new GenericEntityInvokeController($this->controllerHelper, $this->argumentCompiler, [ |
||
215 | 'entity-class' => get_class($this->argumentCompiler), |
||
216 | 'method' => 'buildArguments', |
||
217 | ]); |
||
218 | |||
219 | $controller->__construct($this->controllerHelper, $this->argumentCompiler, [ |
||
220 | 'entity-class' => get_class($this->argumentCompiler), |
||
221 | 'method' => 'buildArguments', |
||
222 | ]); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @test |
||
227 | */ |
||
228 | public function shouldRejectMissingEntityClass() |
||
229 | { |
||
230 | $this->expectException(InvalidArgumentException::class); |
||
231 | |||
232 | new GenericEntityInvokeController( |
||
233 | $this->controllerHelper, |
||
234 | $this->argumentCompiler, |
||
235 | [ |
||
236 | 'method' => 'buildArguments', |
||
237 | ] |
||
238 | ); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @test |
||
243 | */ |
||
244 | public function shouldRejectMissingMethod() |
||
245 | { |
||
246 | $this->expectException(InvalidArgumentException::class); |
||
247 | |||
248 | new GenericEntityInvokeController( |
||
249 | $this->controllerHelper, |
||
250 | $this->argumentCompiler, |
||
251 | [ |
||
252 | 'entity-class' => get_class($this->argumentCompiler), |
||
253 | ] |
||
254 | ); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @test |
||
259 | */ |
||
260 | View Code Duplication | public function shouldRejectNonEntityClass() |
|
261 | { |
||
262 | $this->expectException(InvalidArgumentException::class); |
||
263 | $this->expectExceptionMessage('Expected an existing class name. Got: "NonExistingClass"'); |
||
264 | |||
265 | new GenericEntityInvokeController( |
||
266 | $this->controllerHelper, |
||
267 | $this->argumentCompiler, |
||
268 | [ |
||
269 | 'entity-class' => "NonExistingClass", |
||
270 | 'method' => 'buildArguments', |
||
271 | ] |
||
272 | ); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @test |
||
277 | */ |
||
278 | View Code Duplication | public function shouldRejectNonExistingMethod() |
|
279 | { |
||
280 | $this->expectException(InvalidArgumentException::class); |
||
281 | |||
282 | new GenericEntityInvokeController( |
||
283 | $this->controllerHelper, |
||
284 | $this->argumentCompiler, |
||
285 | [ |
||
286 | 'entity-class' => get_class($this->argumentCompiler), |
||
287 | 'method' => 'nonExistingMethod', |
||
288 | ] |
||
289 | ); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @test |
||
294 | */ |
||
295 | View Code Duplication | public function shouldRejectNonArrayArguments() |
|
296 | { |
||
297 | $this->expectException(InvalidArgumentException::class); |
||
298 | |||
299 | new GenericEntityInvokeController( |
||
300 | $this->controllerHelper, |
||
301 | $this->argumentCompiler, |
||
302 | [ |
||
303 | 'entity-class' => get_class($this->argumentCompiler), |
||
304 | 'method' => 'buildArguments', |
||
305 | 'arguments' => "12345", |
||
306 | ] |
||
307 | ); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @test |
||
312 | */ |
||
313 | public function shouldBeCallableByInvokingController() |
||
314 | { |
||
315 | /** @var Request $request */ |
||
316 | $request = $this->createMock(Request::class); |
||
317 | $request->method("get")->willReturn(123); |
||
318 | |||
319 | $controller = new GenericEntityInvokeController( |
||
320 | $this->controllerHelper, |
||
321 | $this->argumentCompiler, |
||
322 | [ |
||
323 | 'entity-class' => get_class($this->argumentCompiler), |
||
324 | 'method' => 'buildArguments', |
||
325 | 'arguments' => [ |
||
326 | 'argumentsConfiguration' => "Lorem", |
||
327 | 'request' => "ipsum" |
||
328 | ] |
||
329 | ] |
||
330 | ); |
||
331 | |||
332 | $this->controllerHelper->expects($this->once())->method('findEntity')->with( |
||
333 | $this->equalTo(get_class($this->argumentCompiler)), |
||
334 | $this->equalTo("123") |
||
335 | )->willReturn($this->argumentCompiler); |
||
336 | |||
337 | $this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with( |
||
338 | $this->equalTo(new ReflectionMethod(get_class($this->argumentCompiler), 'buildArguments')), |
||
339 | $this->equalTo([ |
||
340 | 'argumentsConfiguration' => "Lorem", |
||
341 | 'request' => "ipsum" |
||
342 | ]) |
||
343 | )->willReturn([ |
||
344 | ['foo' => 'bar'] |
||
345 | ]); |
||
346 | |||
347 | $this->argumentCompiler->expects($this->once())->method('buildArguments')->with( |
||
348 | $this->equalTo(['foo' => 'bar']) |
||
349 | ); |
||
350 | |||
351 | $this->controllerHelper->method('getCurrentRequest')->willReturn($request); |
||
352 | |||
353 | $controller(); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @test |
||
358 | */ |
||
359 | public function shouldRejectCallWithoutRequest() |
||
360 | { |
||
361 | $this->expectException(InvalidArgumentException::class); |
||
362 | |||
363 | $controller = new GenericEntityInvokeController( |
||
364 | $this->controllerHelper, |
||
365 | $this->argumentCompiler, |
||
366 | [ |
||
367 | 'entity-class' => get_class($this->argumentCompiler), |
||
368 | 'method' => 'buildArguments', |
||
369 | ] |
||
370 | ); |
||
371 | |||
372 | $this->controllerHelper->method('getCurrentRequest')->willReturn(null); |
||
373 | |||
374 | $controller(); |
||
375 | } |
||
376 | |||
377 | } |
||
378 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.