Passed
Push — master ( af41cd...8dcf4e )
by Gerrit
01:54
created

shouldRedirectWhenNeeded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 9.296
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 Psr\Container\ContainerInterface;
15
use Addiks\SymfonyGenerics\Controllers\API\GenericServiceInvokeController;
16
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
17
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
18
use Addiks\SymfonyGenerics\Tests\Unit\Controllers\SampleService;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
22
use InvalidArgumentException;
23
use ReflectionMethod;
24
use ErrorException;
25
26
final class GenericServiceInvokeControllerTest extends TestCase
27
{
28
29
    /**
30
     * @var ControllerHelperInterface
31
     */
32
    private $controllerHelper;
33
34
    /**
35
     * @var ArgumentCompilerInterface
36
     */
37
    private $argumentCompiler;
38
39
    /**
40
     * @var ContainerInterface
41
     */
42
    private $container;
43
44
    public function setUp()
45
    {
46
        $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...
47
        $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...
48
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Psr\C...tainerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Psr\Container\ContainerInterface> of property $container.

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...
49
    }
50
51
    /**
52
     * @test
53
     */
54 View Code Duplication
    public function shouldPreventConstructorCalledAgain()
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...
55
    {
56
        $this->expectException(InvalidArgumentException::class);
57
58
        $controller = new GenericServiceInvokeController(
59
            $this->controllerHelper,
60
            $this->argumentCompiler,
61
            $this->container,
62
            [
63
                'service' => 'some_service',
64
                'method' => 'doFoo'
65
            ]
66
        );
67
68
        $controller->__construct(
69
            $this->controllerHelper,
70
            $this->argumentCompiler,
71
            $this->container,
72
            [
73
                'service' => 'some_service',
74
                'method' => 'doFoo'
75
            ]
76
        );
77
    }
78
79
    /**
80
     * @test
81
     */
82 View Code Duplication
    public function shouldFailIfServiceIsMissing()
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...
83
    {
84
        $this->expectException(InvalidArgumentException::class);
85
86
        $controller = new GenericServiceInvokeController(
0 ignored issues
show
Unused Code introduced by
$controller 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 $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.

Loading history...
87
            $this->controllerHelper,
88
            $this->argumentCompiler,
89
            $this->container,
90
            [
91
                'method' => 'doFoo'
92
            ]
93
        );
94
    }
95
96
    /**
97
     * @test
98
     */
99 View Code Duplication
    public function shouldFailIfMethodIsMissing()
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...
100
    {
101
        $this->expectException(InvalidArgumentException::class);
102
103
        $controller = new GenericServiceInvokeController(
0 ignored issues
show
Unused Code introduced by
$controller 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 $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.

Loading history...
104
            $this->controllerHelper,
105
            $this->argumentCompiler,
106
            $this->container,
107
            [
108
                'service' => 'some_service',
109
            ]
110
        );
111
    }
112
113
    /**
114
     * @test
115
     */
116 View Code Duplication
    public function shouldCallService()
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...
117
    {
118
        /** @var Request $request */
119
        $request = $this->createMock(Request::class);
120
121
        /** @var SampleService $service */
122
        $service = $this->createMock(SampleService::class);
123
124
        $service->expects($this->once())->method('doFoo')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollers\SampleService>.

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...
125
            $this->equalTo('lorem'),
126
            $this->equalTo('ipsum')
127
        );
128
129
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

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...
130
            $this->equalTo('some_service')
131
        )->willReturn($service);
132
133
        $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...
134
135
        $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...
136
            $this->equalTo(new ReflectionMethod($service, 'doFoo')),
137
            $this->equalTo(['lorem' => 'ipsum']),
138
            $this->identicalTo($request)
139
        )->willReturn(['lorem', 'ipsum']);
140
141
        /** @var string $expectedResponseContent */
142
        $expectedResponseContent = "Service call completed";
143
144
        $controller = new GenericServiceInvokeController(
145
            $this->controllerHelper,
146
            $this->argumentCompiler,
147
            $this->container,
148
            [
149
                'service' => 'some_service',
150
                'method' => 'doFoo',
151
                'arguments' => ['lorem' => 'ipsum']
152
            ]
153
        );
154
155
        /** @var Response $actualResponse */
156
        $actualResponse = $controller->callService($request);
157
158
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
159
    }
160
161
    /**
162
     * @test
163
     */
164
    public function shouldRedirectWhenNeeded()
165
    {
166
        /** @var Request $request */
167
        $request = $this->createMock(Request::class);
168
169
        /** @var SampleService $service */
170
        $service = $this->createMock(SampleService::class);
171
172
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

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...
173
            $this->equalTo('some_service')
174
        )->willReturn($service);
175
176
        $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...
177
            $this->equalTo(new ReflectionMethod($service, 'doFoo')),
178
            $this->equalTo(['lorem' => 'ipsum']),
179
            $this->identicalTo($request)
180
        )->willReturn(['lorem', 'ipsum']);
181
182
        $controller = new GenericServiceInvokeController(
183
            $this->controllerHelper,
184
            $this->argumentCompiler,
185
            $this->container,
186
            [
187
                'service' => 'some_service',
188
                'method' => 'doFoo',
189
                'arguments' => ['lorem' => 'ipsum'],
190
                'success-redirect' => 'some_redirect_route'
191
            ]
192
        );
193
194
        $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...
195
            $this->equalTo("some_redirect_route"),
196
            $this->equalTo([]),
197
            $this->equalTo(303)
198
        );
199
200
        $controller->callService($request);
201
202
    }
203
204
    /**
205
     * @test
206
     */
207 View Code Duplication
    public function shouldCheckIfAccessIsGranted()
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...
208
    {
209
        $this->expectException(AccessDeniedException::class);
210
211
        /** @var Request $request */
212
        $request = $this->createMock(Request::class);
213
214
        $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...
215
            $this->equalTo('bar'),
216
            $this->identicalTo($request)
217
        )->will($this->returnCallback(
218
            function () {
219
                throw new AccessDeniedException("Lorem ipsum");
220
            }
221
        ));
222
223
        $controller = new GenericServiceInvokeController(
224
            $this->controllerHelper,
225
            $this->argumentCompiler,
226
            $this->container,
227
            [
228
                'service' => 'some_service',
229
                'method' => 'doFoo',
230
                'authorization-attributes' => 'bar'
231
            ]
232
        );
233
234
        $controller->callService($request);
235
    }
236
237
    /**
238
     * @test
239
     */
240
    public function shouldThrowExceptionIfServiceNotFound()
241
    {
242
        $this->expectException(ErrorException::class);
243
244
        /** @var Request $request */
245
        $request = $this->createMock(Request::class);
246
247
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

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...
248
            $this->equalTo('some_service')
249
        )->willReturn(null);
250
251
        $controller = new GenericServiceInvokeController(
252
            $this->controllerHelper,
253
            $this->argumentCompiler,
254
            $this->container,
255
            [
256
                'service' => 'some_service',
257
                'method' => 'doFoo',
258
                'arguments' => ['lorem' => 'ipsum']
259
            ]
260
        );
261
262
        $controller->callService($request);
263
    }
264
265
    /**
266
     * @test
267
     */
268 View Code Duplication
    public function shouldBeCallableByInvokingController()
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...
269
    {
270
        /** @var Request $request */
271
        $request = $this->createMock(Request::class);
272
273
        /** @var SampleService $service */
274
        $service = $this->createMock(SampleService::class);
275
276
        $service->expects($this->once())->method('doFoo')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollers\SampleService>.

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...
277
            $this->equalTo('lorem'),
278
            $this->equalTo('ipsum')
279
        );
280
281
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

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...
282
            $this->equalTo('some_service')
283
        )->willReturn($service);
284
285
        $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...
286
            $this->equalTo(new ReflectionMethod($service, 'doFoo')),
287
            $this->equalTo(['lorem' => 'ipsum']),
288
            $this->identicalTo($request)
289
        )->willReturn(['lorem', 'ipsum']);
290
291
        /** @var string $expectedResponseContent */
292
        $expectedResponseContent = "Service call completed";
293
294
        $controller = new GenericServiceInvokeController(
295
            $this->controllerHelper,
296
            $this->argumentCompiler,
297
            $this->container,
298
            [
299
                'service' => 'some_service',
300
                'method' => 'doFoo',
301
                'arguments' => ['lorem' => 'ipsum']
302
            ]
303
        );
304
305
        $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...
306
307
        /** @var Response $actualResponse */
308
        $actualResponse = $controller();
309
310
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
311
    }
312
313
    /**
314
     * @test
315
     */
316 View Code Duplication
    public function shouldRejectCallWithoutRequest()
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...
317
    {
318
        $this->expectException(InvalidArgumentException::class);
319
320
        $controller = new GenericServiceInvokeController(
321
            $this->controllerHelper,
322
            $this->argumentCompiler,
323
            $this->container,
324
            [
325
                'service' => 'some_service',
326
                'method' => 'doFoo',
327
            ]
328
        );
329
330
        $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...
331
332
        $controller();
333
    }
334
335
}
336