Completed
Push — master ( 27d676...dee7dc )
by Gerrit
03:35
created

shouldBeCallableByInvokingController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 44
Ratio 100 %

Importance

Changes 0
Metric Value
dl 44
loc 44
rs 9.216
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->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...
134
            $this->equalTo(new ReflectionMethod($service, 'doFoo')),
135
            $this->equalTo(['lorem' => 'ipsum']),
136
            $this->identicalTo($request)
137
        )->willReturn(['lorem', 'ipsum']);
138
139
        /** @var string $expectedResponseContent */
140
        $expectedResponseContent = "Service call completed";
141
142
        $controller = new GenericServiceInvokeController(
143
            $this->controllerHelper,
144
            $this->argumentCompiler,
145
            $this->container,
146
            [
147
                'service' => 'some_service',
148
                'method' => 'doFoo',
149
                'arguments' => ['lorem' => 'ipsum']
150
            ]
151
        );
152
153
        /** @var Response $actualResponse */
154
        $actualResponse = $controller->callService($request);
155
156
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
157
    }
158
159
    /**
160
     * @test
161
     */
162 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...
163
    {
164
        $this->expectException(AccessDeniedException::class);
165
166
        /** @var Request $request */
167
        $request = $this->createMock(Request::class);
168
169
        $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...
170
            $this->equalTo('bar'),
171
            $this->identicalTo($request)
172
        )->will($this->returnCallback(
173
            function () {
174
                throw new AccessDeniedException("Lorem ipsum");
175
            }
176
        ));
177
178
        $controller = new GenericServiceInvokeController(
179
            $this->controllerHelper,
180
            $this->argumentCompiler,
181
            $this->container,
182
            [
183
                'service' => 'some_service',
184
                'method' => 'doFoo',
185
                'authorization-attributes' => 'bar'
186
            ]
187
        );
188
189
        $controller->callService($request);
190
    }
191
192
    /**
193
     * @test
194
     */
195
    public function shouldThrowExceptionIfServiceNotFound()
196
    {
197
        $this->expectException(ErrorException::class);
198
199
        /** @var Request $request */
200
        $request = $this->createMock(Request::class);
201
202
        $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...
203
            $this->equalTo('some_service')
204
        )->willReturn(null);
205
206
        $controller = new GenericServiceInvokeController(
207
            $this->controllerHelper,
208
            $this->argumentCompiler,
209
            $this->container,
210
            [
211
                'service' => 'some_service',
212
                'method' => 'doFoo',
213
                'arguments' => ['lorem' => 'ipsum']
214
            ]
215
        );
216
217
        $controller->callService($request);
218
    }
219
220
    /**
221
     * @test
222
     */
223 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...
224
    {
225
        /** @var Request $request */
226
        $request = $this->createMock(Request::class);
227
228
        /** @var SampleService $service */
229
        $service = $this->createMock(SampleService::class);
230
231
        $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...
232
            $this->equalTo('lorem'),
233
            $this->equalTo('ipsum')
234
        );
235
236
        $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...
237
            $this->equalTo('some_service')
238
        )->willReturn($service);
239
240
        $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...
241
            $this->equalTo(new ReflectionMethod($service, 'doFoo')),
242
            $this->equalTo(['lorem' => 'ipsum']),
243
            $this->identicalTo($request)
244
        )->willReturn(['lorem', 'ipsum']);
245
246
        /** @var string $expectedResponseContent */
247
        $expectedResponseContent = "Service call completed";
248
249
        $controller = new GenericServiceInvokeController(
250
            $this->controllerHelper,
251
            $this->argumentCompiler,
252
            $this->container,
253
            [
254
                'service' => 'some_service',
255
                'method' => 'doFoo',
256
                'arguments' => ['lorem' => 'ipsum']
257
            ]
258
        );
259
260
        $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...
261
262
        /** @var Response $actualResponse */
263
        $actualResponse = $controller();
264
265
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
266
    }
267
268
    /**
269
     * @test
270
     */
271 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...
272
    {
273
        $this->expectException(InvalidArgumentException::class);
274
275
        $controller = new GenericServiceInvokeController(
276
            $this->controllerHelper,
277
            $this->argumentCompiler,
278
            $this->container,
279
            [
280
                'service' => 'some_service',
281
                'method' => 'doFoo',
282
            ]
283
        );
284
285
        $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...
286
287
        $controller();
288
    }
289
290
}
291