Passed
Push — master ( 527539...f78a4e )
by Gerrit
02:05
created

shouldRejectNonEntityClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...HelperInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...trollerHelperInterface> of property $controllerHelper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        $this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...mpilerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...umentCompilerInterface> of property $argumentCompiler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
    }
48
49
    /**
50
     * @test
51
     */
52
    public function shouldInvodeAnEntityMethod()
53
    {
54
        /** @var Request $request */
55
        $request = $this->createMock(Request::class);
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
                    'request' => "ipsum"
66
                ]
67
            ]
68
        );
69
70
        $this->controllerHelper->expects($this->once())->method('findEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            $this->equalTo(get_class($this->argumentCompiler)),
72
            $this->equalTo("123")
73
        )->willReturn($this->argumentCompiler);
74
75
        $this->controllerHelper->expects($this->once())->method('flushORM');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        $this->controllerHelper->expects($this->once())->method('dispatchEvent')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            $this->equalTo("symfony_generics.entity_interaction"),
78
            $this->equalTo(new EntityInteractionEvent(
79
                get_class($this->argumentCompiler),
80
                "123",
81
                $this->argumentCompiler,
82
                'buildArguments',
83
                [['foo' => 'bar'], $request]
84
            ))
85
        );
86
87
        $this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            $this->equalTo(new ReflectionMethod(get_class($this->argumentCompiler), 'buildArguments')),
89
            $this->equalTo([
90
                'argumentsConfiguration' => "Lorem",
91
                'request' => "ipsum"
92
            ]),
93
            $this->identicalTo($request)
94
        )->willReturn([
95
            ['foo' => 'bar'],
96
            $request
97
        ]);
98
99
        $this->argumentCompiler->expects($this->once())->method('buildArguments')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
            $this->equalTo(['foo' => 'bar']),
101
            $this->identicalTo($request)
102
        );
103
104
        $this->controller->invokeEntityMethod($request, "123");
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function shouldRedirectAfterInvokation()
111
    {
112
        /** @var Request $request */
113
        $request = $this->createMock(Request::class);
114
115
        $this->controller = new GenericEntityInvokeController(
116
            $this->controllerHelper,
117
            $this->argumentCompiler,
118
            [
119
                'entity-class' => SampleEntity::class,
120
                'method' => 'getId',
121
                'redirect-route' => 'some-redirect-route',
122
                'redirect-route-parameters' => ['some-redirect-route-parameters']
123
            ]
124
        );
125
126
        /** @var SampleEntity $entity */
127
        $entity = $this->createMock(SampleEntity::class);
128
        $entity->method('getId')->willReturn("some-result");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...ntrollers\SampleEntity>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
130
        $this->argumentCompiler->expects($this->once())->method('buildArguments')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
            $this->equalTo(['some-redirect-route-parameters']),
132
            $this->identicalTo($request),
133
            $this->equalTo(['result' => "some-result"])
134
        )->willReturn(['foo' => 'bar']);
135
136
        $this->controllerHelper->expects($this->once())->method('findEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
            $this->equalTo(SampleEntity::class),
138
            $this->equalTo("123")
139
        )->willReturn($entity);
140
141
        $this->controllerHelper->expects($this->once())->method('redirectToRoute')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
            $this->equalTo('some-redirect-route'),
143
            $this->equalTo(['foo' => 'bar'])
144
        )->willReturn($this->createMock(Response::class));
145
146
        $this->controller->invokeEntityMethod($request, "123");
147
    }
148
149
    /**
150
     * @test
151
     */
152
    public function shouldThrowExceptionWhenEntityNotFound()
153
    {
154
        $this->expectException(InvalidArgumentException::class);
155
156
        /** @var Request $request */
157
        $request = $this->createMock(Request::class);
158
159
        $this->controller = new GenericEntityInvokeController(
160
            $this->controllerHelper,
161
            $this->argumentCompiler,
162
            [
163
                'entity-class' => get_class($this->argumentCompiler),
164
                'method' => 'buildArguments',
165
            ]
166
        );
167
168
        $this->controllerHelper->expects($this->once())->method('findEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
            $this->equalTo(get_class($this->argumentCompiler)),
170
            $this->equalTo("123")
171
        )->willReturn(null);
172
173
        $this->controller->invokeEntityMethod($request, "123");
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function shouldThrowExceptionWhenAccessNotGranted()
180
    {
181
        $this->expectException(AccessDeniedException::class);
182
183
        /** @var Request $request */
184
        $request = $this->createMock(Request::class);
185
186
        $this->controller = new GenericEntityInvokeController(
187
            $this->controllerHelper,
188
            $this->argumentCompiler,
189
            [
190
                'entity-class' => get_class($this->argumentCompiler),
191
                'method' => 'buildArguments',
192
                'deny-access-attribute' => 'foo',
193
            ]
194
        );
195
196
        $this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
197
            $this->equalTo('foo'),
198
            $this->equalTo($this->argumentCompiler)
199
        )->will($this->returnCallback(
200
            function () {
201
                throw new AccessDeniedException("Lorem ipsum");
202
            }
203
        ));
204
205
        $this->controllerHelper->expects($this->once())->method('findEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
206
            $this->equalTo(get_class($this->argumentCompiler)),
207
            $this->equalTo("123")
208
        )->willReturn($this->argumentCompiler);
209
210
        $this->controller->invokeEntityMethod($request, "123");
211
    }
212
213
    /**
214
     * @test
215
     */
216
    public function shouldRejectConstructorCalledAgain()
217
    {
218
        $this->expectException(InvalidArgumentException::class);
219
220
        $controller = new GenericEntityInvokeController($this->controllerHelper, $this->argumentCompiler, [
221
            'entity-class' => get_class($this->argumentCompiler),
222
            'method' => 'buildArguments',
223
        ]);
224
225
        $controller->__construct($this->controllerHelper, $this->argumentCompiler, [
226
            'entity-class' => get_class($this->argumentCompiler),
227
            'method' => 'buildArguments',
228
        ]);
229
    }
230
231
    /**
232
     * @test
233
     */
234
    public function shouldRejectMissingEntityClass()
235
    {
236
        $this->expectException(InvalidArgumentException::class);
237
238
        new GenericEntityInvokeController(
239
            $this->controllerHelper,
240
            $this->argumentCompiler,
241
            [
242
                'method' => 'buildArguments',
243
            ]
244
        );
245
    }
246
247
    /**
248
     * @test
249
     */
250
    public function shouldRejectMissingMethod()
251
    {
252
        $this->expectException(InvalidArgumentException::class);
253
254
        new GenericEntityInvokeController(
255
            $this->controllerHelper,
256
            $this->argumentCompiler,
257
            [
258
                'entity-class' => get_class($this->argumentCompiler),
259
            ]
260
        );
261
    }
262
263
    /**
264
     * @test
265
     */
266 View Code Duplication
    public function shouldRejectNonEntityClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
267
    {
268
        $this->expectException(InvalidArgumentException::class);
269
        $this->expectExceptionMessage('Expected an existing class name. Got: "NonExistingClass"');
270
271
        new GenericEntityInvokeController(
272
            $this->controllerHelper,
273
            $this->argumentCompiler,
274
            [
275
                'entity-class' => "NonExistingClass",
276
                'method' => 'buildArguments',
277
            ]
278
        );
279
    }
280
281
    /**
282
     * @test
283
     */
284 View Code Duplication
    public function shouldRejectNonExistingMethod()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
285
    {
286
        $this->expectException(InvalidArgumentException::class);
287
288
        new GenericEntityInvokeController(
289
            $this->controllerHelper,
290
            $this->argumentCompiler,
291
            [
292
                'entity-class' => get_class($this->argumentCompiler),
293
                'method' => 'nonExistingMethod',
294
            ]
295
        );
296
    }
297
298
    /**
299
     * @test
300
     */
301 View Code Duplication
    public function shouldRejectNonArrayArguments()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
302
    {
303
        $this->expectException(InvalidArgumentException::class);
304
305
        new GenericEntityInvokeController(
306
            $this->controllerHelper,
307
            $this->argumentCompiler,
308
            [
309
                'entity-class' => get_class($this->argumentCompiler),
310
                'method' => 'buildArguments',
311
                'arguments' => "12345",
312
            ]
313
        );
314
    }
315
316
    /**
317
     * @test
318
     */
319
    public function shouldBeCallableByInvokingController()
320
    {
321
        /** @var Request $request */
322
        $request = $this->createMock(Request::class);
323
        $request->method("get")->willReturn(123);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Symfony\Component\HttpFoundation\Request. Did you maybe mean enableHttpMethodParameterOverride()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
324
325
        $controller = new GenericEntityInvokeController(
326
            $this->controllerHelper,
327
            $this->argumentCompiler,
328
            [
329
                'entity-class' => get_class($this->argumentCompiler),
330
                'method' => 'buildArguments',
331
                'arguments' => [
332
                    'argumentsConfiguration' => "Lorem",
333
                    'request' => "ipsum"
334
                ]
335
            ]
336
        );
337
338
        $this->controllerHelper->expects($this->once())->method('findEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
339
            $this->equalTo(get_class($this->argumentCompiler)),
340
            $this->equalTo("123")
341
        )->willReturn($this->argumentCompiler);
342
343
        $this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
344
            $this->equalTo(new ReflectionMethod(get_class($this->argumentCompiler), 'buildArguments')),
345
            $this->equalTo([
346
                'argumentsConfiguration' => "Lorem",
347
                'request' => "ipsum"
348
            ]),
349
            $this->identicalTo($request)
350
        )->willReturn([
351
            ['foo' => 'bar'],
352
            $request
353
        ]);
354
355
        $this->argumentCompiler->expects($this->once())->method('buildArguments')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
356
            $this->equalTo(['foo' => 'bar']),
357
            $this->identicalTo($request)
358
        );
359
360
        $this->controllerHelper->method('getCurrentRequest')->willReturn($request);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
361
362
        $controller();
363
    }
364
365
    /**
366
     * @test
367
     */
368
    public function shouldRejectCallWithoutRequest()
369
    {
370
        $this->expectException(InvalidArgumentException::class);
371
372
        $controller = new GenericEntityInvokeController(
373
            $this->controllerHelper,
374
            $this->argumentCompiler,
375
            [
376
                'entity-class' => get_class($this->argumentCompiler),
377
                'method' => 'buildArguments',
378
            ]
379
        );
380
381
        $this->controllerHelper->method('getCurrentRequest')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
382
383
        $controller();
384
    }
385
386
}
387