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

shouldBeCallableByInvokingController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
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\GenericEntityFetchController;
15
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
16
use Addiks\SymfonyGenerics\Tests\Unit\Controllers\SampleEntity;
17
use Symfony\Component\HttpFoundation\Response;
18
use ErrorException;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Finder\Exception\AccessDeniedException;
21
use InvalidArgumentException;
22
use Symfony\Component\HttpFoundation\Request;
23
24
final class GenericEntityFetchControllerTest extends TestCase
25
{
26
27
    /**
28
     * @var ControllerHelperInterface
29
     */
30
    private $controllerHelper;
31
32
    public function setUp()
33
    {
34
        $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...
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function shouldFetchEntity()
41
    {
42
        $entity = new SampleEntity();
43
44
        $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...
45
            $this->equalTo(SampleEntity::class),
46
            $this->equalTo("some-id")
47
        )->willReturn($entity);
48
49
        /** @var string $expectedResponseContent */
50
        $expectedResponseContent = '{"id":"some_id","fooCalled":false,"constructArgument":"foo"}';
51
52
        $controller = new GenericEntityFetchController($this->controllerHelper, [
53
            'entity-class' => SampleEntity::class
54
        ]);
55
56
        /** @var Response $actualResponse */
57
        $actualResponse = $controller->fetchEntity("some-id");
58
59
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function shouldApplyDataTemplate()
66
    {
67
        $entity = new SampleEntity();
68
69
        $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...
70
            $this->equalTo(SampleEntity::class),
71
            $this->equalTo("some-id")
72
        )->willReturn($entity);
73
74
        /** @var string $expectedResponseContent */
75
        $expectedResponseContent = '{"lorem":false,"ipsum":"foo"}';
76
77
        $controller = new GenericEntityFetchController($this->controllerHelper, [
78
            'entity-class' => SampleEntity::class,
79
            'data-template' => [
80
                'lorem' => 'fooCalled',
81
                'ipsum' => 'constructArgument'
82
            ]
83
        ]);
84
85
        /** @var Response $actualResponse */
86
        $actualResponse = $controller->fetchEntity("some-id");
87
88
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function shouldThrowExceptionOnInvalidDataTemplateEntry()
95
    {
96
        $this->expectException(ErrorException::class);
97
98
        $entity = new SampleEntity();
99
100
        $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...
101
            $this->equalTo(SampleEntity::class),
102
            $this->equalTo("some-id")
103
        )->willReturn($entity);
104
105
        $controller = new GenericEntityFetchController($this->controllerHelper, [
106
            'entity-class' => SampleEntity::class,
107
            'data-template' => [
108
                'lorem' => 'fooCalled',
109
                'ipsum' => 'constructArgument',
110
                'dolor' => false,
111
            ]
112
        ]);
113
114
        $controller->fetchEntity("some-id");
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function shouldThrowExceptionOnInvalidNormalizerResult()
121
    {
122
        $this->expectException(ErrorException::class);
123
124
        $entity = new SampleEntity();
125
126
        $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...
127
            $this->equalTo(SampleEntity::class),
128
            $this->equalTo("some-id")
129
        )->willReturn($entity);
130
131
        /** @var NormalizerInterface $normalizer */
132
        $normalizer = $this->createMock(NormalizerInterface::class);
133
        $normalizer->expects($this->once())->method('normalize')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...er\NormalizerInterface>.

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->identicalTo($entity)
135
        )->willReturn('*non-array*');
136
137
        $controller = new GenericEntityFetchController($this->controllerHelper, [
138
            'entity-class' => SampleEntity::class,
139
            'normalizer' => $normalizer,
140
        ]);
141
142
        $controller->fetchEntity("some-id");
143
    }
144
145
    /**
146
     * @test
147
     */
148 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...
149
    {
150
        $this->expectException(AccessDeniedException::class);
151
152
        /** @var mixed $entity */
153
        $entity = new SampleEntity();
154
155
        $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...
156
            $this->equalTo(SampleEntity::class),
157
            $this->equalTo("some-id")
158
        )->willReturn($entity);
159
160
        $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...
161
            $this->equalTo('some-attribute'),
162
            $this->identicalTo($entity)
163
        )->will($this->returnCallback(
164
            function () {
165
                throw new AccessDeniedException('Lorem ipsum!');
166
            }
167
        ));
168
169
        $controller = new GenericEntityFetchController($this->controllerHelper, [
170
            'entity-class' => SampleEntity::class,
171
            'authorization-attribute' => 'some-attribute',
172
        ]);
173
174
        $controller->fetchEntity("some-id");
175
    }
176
177
    /**
178
     * @test
179
     */
180
    public function shouldThrowExceptionWhenEntityNotFound()
181
    {
182
        $this->expectException(InvalidArgumentException::class);
183
184
        $controller = new GenericEntityFetchController($this->controllerHelper, [
185
            'entity-class' => SampleEntity::class,
186
        ]);
187
188
        $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...
189
            $this->equalTo(SampleEntity::class),
190
            $this->equalTo("some-id")
191
        )->willReturn(null);
192
193
        $controller->fetchEntity("some-id");
194
    }
195
196
    /**
197
     * @test
198
     */
199
    public function shouldThrowExceptionWhenEntityClassDoesNotExist()
200
    {
201
        $this->expectException(InvalidArgumentException::class);
202
203
        $controller = new GenericEntityFetchController($this->controllerHelper, [
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...
204
            'entity-class' => "NotExisting",
205
        ]);
206
    }
207
208
    /**
209
     * @test
210
     */
211
    public function shouldThrowExceptionWhenMissingEntityClass()
212
    {
213
        $this->expectException(InvalidArgumentException::class);
214
215
        $controller = new GenericEntityFetchController($this->controllerHelper, [
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...
216
        ]);
217
    }
218
219
    /**
220
     * @test
221
     */
222 View Code Duplication
    public function shouldThrowExceptionOnInvalidNormalizer()
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...
223
    {
224
        $this->expectException(InvalidArgumentException::class);
225
226
        $controller = new GenericEntityFetchController($this->controllerHelper, [
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...
227
            'entity-class' => SampleEntity::class,
228
            'normalizer' => false
229
        ]);
230
    }
231
232
    /**
233
     * @test
234
     */
235 View Code Duplication
    public function shouldThrowExceptionOnInvalidEncoder()
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...
236
    {
237
        $this->expectException(InvalidArgumentException::class);
238
239
        $controller = new GenericEntityFetchController($this->controllerHelper, [
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...
240
            'entity-class' => SampleEntity::class,
241
            'encoder' => false
242
        ]);
243
    }
244
245
    /**
246
     * @test
247
     */
248 View Code Duplication
    public function shouldThrowExceptionWhenCallingConstructorAgain()
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...
249
    {
250
        $this->expectException(InvalidArgumentException::class);
251
252
        $controller = new GenericEntityFetchController($this->controllerHelper, [
253
            'entity-class' => SampleEntity::class,
254
        ]);
255
256
        $controller->__construct($this->controllerHelper, [
257
            'entity-class' => SampleEntity::class,
258
        ]);
259
    }
260
261
    /**
262
     * @test
263
     */
264
    public function shouldBeCallableByInvokingController()
265
    {
266
        $entity = new SampleEntity();
267
268
        $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...
269
            $this->equalTo(SampleEntity::class),
270
            $this->equalTo("some-id")
271
        )->willReturn($entity);
272
273
        /** @var string $expectedResponseContent */
274
        $expectedResponseContent = '{"id":"some_id","fooCalled":false,"constructArgument":"foo"}';
275
276
        $controller = new GenericEntityFetchController($this->controllerHelper, [
277
            'entity-class' => SampleEntity::class,
278
        ]);
279
280
        /** @var Request $request */
281
        $request = $this->createMock(Request::class);
282
        $request->method("get")->willReturn('some-id');
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...
283
284
        $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...
285
286
        /** @var Response $actualResponse */
287
        $actualResponse = $controller();
288
289
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
290
    }
291
292
    /**
293
     * @test
294
     */
295 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...
296
    {
297
        $this->expectException(InvalidArgumentException::class);
298
299
        $controller = new GenericEntityFetchController($this->controllerHelper, [
300
            'entity-class' => SampleEntity::class,
301
        ]);
302
303
        $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...
304
305
        $controller();
306
    }
307
308
}
309