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

shouldBeCallableByInvokingController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 9.2
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 stdClass;
19
use ReflectionMethod;
20
use InvalidArgumentException;
21
use Symfony\Component\Finder\Exception\AccessDeniedException;
22
23
final class GenericEntityInvokeControllerTest extends TestCase
24
{
25
26
    /**
27
     * @var GenericEntityInvokeController
28
     */
29
    private $controller;
30
31
    /**
32
     * @var ControllerHelperInterface
33
     */
34
    private $controllerHelper;
35
36
    /**
37
     * @var ArgumentCompilerInterface
38
     */
39
    private $argumentCompiler;
40
41
    public function setUp()
42
    {
43
        $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...
44
        $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...
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function shouldInvodeAnEntityMethod()
51
    {
52
        /** @var Request $request */
53
        $request = $this->createMock(Request::class);
54
55
        $this->controller = new GenericEntityInvokeController(
56
            $this->controllerHelper,
57
            $this->argumentCompiler,
58
            [
59
                'entity-class' => get_class($this->argumentCompiler),
60
                'method' => 'buildArguments',
61
                'arguments' => [
62
                    'argumentsConfiguration' => "Lorem",
63
                    'request' => "ipsum"
64
                ]
65
            ]
66
        );
67
68
        $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...
69
            $this->equalTo(get_class($this->argumentCompiler)),
70
            $this->equalTo("123")
71
        )->willReturn($this->argumentCompiler);
72
73
        $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...
74
            $this->equalTo(new ReflectionMethod(get_class($this->argumentCompiler), 'buildArguments')),
75
            $this->equalTo([
76
                'argumentsConfiguration' => "Lorem",
77
                'request' => "ipsum"
78
            ]),
79
            $this->identicalTo($request)
80
        )->willReturn([
81
            ['foo' => 'bar'],
82
            $request
83
        ]);
84
85
        $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...
86
            $this->equalTo(['foo' => 'bar']),
87
            $this->identicalTo($request)
88
        );
89
90
        $this->controller->invokeEntityMethod($request, "123");
91
    }
92
93
    /**
94
     * @test
95
     */
96
    public function shouldThrowExceptionWhenEntityNotFound()
97
    {
98
        $this->expectException(InvalidArgumentException::class);
99
100
        /** @var Request $request */
101
        $request = $this->createMock(Request::class);
102
103
        $this->controller = new GenericEntityInvokeController(
104
            $this->controllerHelper,
105
            $this->argumentCompiler,
106
            [
107
                'entity-class' => get_class($this->argumentCompiler),
108
                'method' => 'buildArguments',
109
            ]
110
        );
111
112
        $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...
113
            $this->equalTo(get_class($this->argumentCompiler)),
114
            $this->equalTo("123")
115
        )->willReturn(null);
116
117
        $this->controller->invokeEntityMethod($request, "123");
118
    }
119
120
    /**
121
     * @test
122
     */
123
    public function shouldThrowExceptionWhenAccessNotGranted()
124
    {
125
        $this->expectException(AccessDeniedException::class);
126
127
        /** @var Request $request */
128
        $request = $this->createMock(Request::class);
129
130
        $this->controller = new GenericEntityInvokeController(
131
            $this->controllerHelper,
132
            $this->argumentCompiler,
133
            [
134
                'entity-class' => get_class($this->argumentCompiler),
135
                'method' => 'buildArguments',
136
                'deny-access-attribute' => 'foo',
137
            ]
138
        );
139
140
        $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...
141
            $this->equalTo('foo'),
142
            $this->equalTo($this->argumentCompiler)
143
        )->will($this->returnCallback(
144
            function () {
145
                throw new AccessDeniedException("Lorem ipsum");
146
            }
147
        ));
148
149
        $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...
150
            $this->equalTo(get_class($this->argumentCompiler)),
151
            $this->equalTo("123")
152
        )->willReturn($this->argumentCompiler);
153
154
        $this->controller->invokeEntityMethod($request, "123");
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function shouldRejectConstructorCalledAgain()
161
    {
162
        $this->expectException(InvalidArgumentException::class);
163
164
        $controller = new GenericEntityInvokeController($this->controllerHelper, $this->argumentCompiler, [
165
            'entity-class' => get_class($this->argumentCompiler),
166
            'method' => 'buildArguments',
167
        ]);
168
169
        $controller->__construct($this->controllerHelper, $this->argumentCompiler, [
170
            'entity-class' => get_class($this->argumentCompiler),
171
            'method' => 'buildArguments',
172
        ]);
173
    }
174
175
    /**
176
     * @test
177
     */
178
    public function shouldRejectMissingEntityClass()
179
    {
180
        $this->expectException(InvalidArgumentException::class);
181
182
        new GenericEntityInvokeController(
183
            $this->controllerHelper,
184
            $this->argumentCompiler,
185
            [
186
                'method' => 'buildArguments',
187
            ]
188
        );
189
    }
190
191
    /**
192
     * @test
193
     */
194
    public function shouldRejectMissingMethod()
195
    {
196
        $this->expectException(InvalidArgumentException::class);
197
198
        new GenericEntityInvokeController(
199
            $this->controllerHelper,
200
            $this->argumentCompiler,
201
            [
202
                'entity-class' => get_class($this->argumentCompiler),
203
            ]
204
        );
205
    }
206
207
    /**
208
     * @test
209
     */
210 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...
211
    {
212
        $this->expectException(InvalidArgumentException::class);
213
        $this->expectExceptionMessage('Expected an existing class name. Got: "NonExistingClass"');
214
215
        new GenericEntityInvokeController(
216
            $this->controllerHelper,
217
            $this->argumentCompiler,
218
            [
219
                'entity-class' => "NonExistingClass",
220
                'method' => 'buildArguments',
221
            ]
222
        );
223
    }
224
225
    /**
226
     * @test
227
     */
228 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...
229
    {
230
        $this->expectException(InvalidArgumentException::class);
231
232
        new GenericEntityInvokeController(
233
            $this->controllerHelper,
234
            $this->argumentCompiler,
235
            [
236
                'entity-class' => get_class($this->argumentCompiler),
237
                'method' => 'nonExistingMethod',
238
            ]
239
        );
240
    }
241
242
    /**
243
     * @test
244
     */
245 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...
246
    {
247
        $this->expectException(InvalidArgumentException::class);
248
249
        new GenericEntityInvokeController(
250
            $this->controllerHelper,
251
            $this->argumentCompiler,
252
            [
253
                'entity-class' => get_class($this->argumentCompiler),
254
                'method' => 'buildArguments',
255
                'arguments' => "12345",
256
            ]
257
        );
258
    }
259
260
    /**
261
     * @test
262
     */
263
    public function shouldBeCallableByInvokingController()
264
    {
265
        /** @var Request $request */
266
        $request = $this->createMock(Request::class);
267
        $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...
268
269
        $controller = new GenericEntityInvokeController(
270
            $this->controllerHelper,
271
            $this->argumentCompiler,
272
            [
273
                'entity-class' => get_class($this->argumentCompiler),
274
                'method' => 'buildArguments',
275
                'arguments' => [
276
                    'argumentsConfiguration' => "Lorem",
277
                    'request' => "ipsum"
278
                ]
279
            ]
280
        );
281
282
        $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...
283
            $this->equalTo(get_class($this->argumentCompiler)),
284
            $this->equalTo("123")
285
        )->willReturn($this->argumentCompiler);
286
287
        $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...
288
            $this->equalTo(new ReflectionMethod(get_class($this->argumentCompiler), 'buildArguments')),
289
            $this->equalTo([
290
                'argumentsConfiguration' => "Lorem",
291
                'request' => "ipsum"
292
            ]),
293
            $this->identicalTo($request)
294
        )->willReturn([
295
            ['foo' => 'bar'],
296
            $request
297
        ]);
298
299
        $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...
300
            $this->equalTo(['foo' => 'bar']),
301
            $this->identicalTo($request)
302
        );
303
304
        $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...
305
306
        $controller();
307
    }
308
309
    /**
310
     * @test
311
     */
312
    public function shouldRejectCallWithoutRequest()
313
    {
314
        $this->expectException(InvalidArgumentException::class);
315
316
        $controller = new GenericEntityInvokeController(
317
            $this->controllerHelper,
318
            $this->argumentCompiler,
319
            [
320
                'entity-class' => get_class($this->argumentCompiler),
321
                'method' => 'buildArguments',
322
            ]
323
        );
324
325
        $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...
326
327
        $controller();
328
    }
329
330
}
331