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

shouldBeCallableByInvokingController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 42
Ratio 100 %

Importance

Changes 0
Metric Value
dl 42
loc 42
rs 9.248
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\GenericEntityListingController;
15
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
16
use InvalidArgumentException;
17
use Addiks\SymfonyGenerics\Tests\Unit\Controllers\SampleEntity;
18
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
22
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23
use ErrorException;
24
25
final class GenericEntityListingControllerTest extends TestCase
26
{
27
28
    /**
29
     * @var GenericEntityListingController
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 shouldPreventControllerBeingCalledAgain()
53
    {
54
        $this->expectException(InvalidArgumentException::class);
55
56
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
57
            'entity-class' => SampleEntity::class
58
        ]);
59
60
        $controller->__construct($this->controllerHelper, $this->argumentCompiler, [
61
            'entity-class' => SampleEntity::class
62
        ]);
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function shouldThrowExceptionWhenEntityClassIsMissing()
69
    {
70
        $this->expectException(InvalidArgumentException::class);
71
72
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
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...
73
        ]);
74
    }
75
76
    /**
77
     * @test
78
     */
79 View Code Duplication
    public function shouldThrowExceptionWhenEntityClassIsInvalid()
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...
80
    {
81
        $this->expectException(InvalidArgumentException::class);
82
83
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
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...
84
            'entity-class' => "DoesNotExist"
85
        ]);
86
    }
87
88
    /**
89
     * @test
90
     */
91 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...
92
    {
93
        $this->expectException(InvalidArgumentException::class);
94
95
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
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...
96
            'entity-class' => SampleEntity::class,
97
            'normalizer' => false
98
        ]);
99
    }
100
101
    /**
102
     * @test
103
     */
104 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...
105
    {
106
        $this->expectException(InvalidArgumentException::class);
107
108
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
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...
109
            'entity-class' => SampleEntity::class,
110
            'encoder' => false
111
        ]);
112
    }
113
114
    /**
115
     * @test
116
     */
117 View Code Duplication
    public function shouldListEntities()
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...
118
    {
119
        /** @var Request $request */
120
        $request = $this->createMock(Request::class);
121
122
        /** @var string $expectedResponseContent */
123
        $expectedResponseContent = '[{"lorem":false,"ipsum":"foo"},{"lorem":false,"ipsum":"foo"}]';
124
125
        /** @var SampleEntity $entityA */
126
        $entityA = $this->createMock(SampleEntity::class);
127
128
        /** @var SampleEntity $entityB */
129
        $entityB = $this->createMock(SampleEntity::class);
130
131
        $this->controllerHelper->expects($this->once())->method('findEntities')->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...
132
            $this->equalTo(SampleEntity::class),
133
            $this->equalTo(['blah' => 'blubb'])
134
        )->willReturn([$entityA, $entityB]);
135
136
        $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...
137
            $this->equalTo(['foo' => 'bar']),
138
            $this->identicalTo($request)
139
        )->willReturn(['blah' => 'blubb']);
140
141
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
142
            'entity-class' => SampleEntity::class,
143
            'data-template' => [
144
                'lorem' => 'fooCalled',
145
                'ipsum' => 'constructArgument'
146
            ],
147
            'criteria' => [
148
                'foo' => 'bar'
149
            ]
150
        ]);
151
152
        /** @var Response $actualResponse */
153
        $actualResponse = $controller->listEntities($request);
154
155
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
156
    }
157
158
    /**
159
     * @test
160
     */
161
    public function shouldThrowExceptionIfNormalizerReturnsNonArray()
162
    {
163
        $this->expectException(ErrorException::class);
164
165
        /** @var Request $request */
166
        $request = $this->createMock(Request::class);
167
168
        /** @var SampleEntity $entity */
169
        $entity = $this->createMock(SampleEntity::class);
170
171
        $this->controllerHelper->expects($this->once())->method('findEntities')->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...
172
            $this->equalTo(SampleEntity::class),
173
            $this->equalTo([])
174
        )->willReturn([$entity]);
175
176
        /** @var NormalizerInterface $normalizer */
177
        $normalizer = $this->createMock(NormalizerInterface::class);
178
        $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...
179
            $this->identicalTo($entity)
180
        )->willReturn("*non-array*");
181
182
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
183
            'entity-class' => SampleEntity::class,
184
            'normalizer' => $normalizer,
185
        ]);
186
187
        $controller->listEntities($request);
188
    }
189
190
    /**
191
     * @test
192
     */
193 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...
194
    {
195
        /** @var Request $request */
196
        $request = $this->createMock(Request::class);
197
198
        $this->expectException(AccessDeniedException::class);
199
200
        $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...
201
            $this->equalTo('some-attribute'),
202
            $this->identicalTo($request)
203
        )->will($this->returnCallback(
204
            function () {
205
                throw new AccessDeniedException('Lorem ipsum!');
206
            }
207
        ));
208
209
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
210
            'entity-class' => SampleEntity::class,
211
            'authorization-attribute' => 'some-attribute',
212
        ]);
213
214
        $controller->listEntities($request);
215
    }
216
217
    /**
218
     * @test
219
     */
220 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...
221
    {
222
        /** @var Request $request */
223
        $request = $this->createMock(Request::class);
224
225
        /** @var string $expectedResponseContent */
226
        $expectedResponseContent = '[{"lorem":false,"ipsum":"foo"},{"lorem":false,"ipsum":"foo"}]';
227
228
        /** @var SampleEntity $entityA */
229
        $entityA = $this->createMock(SampleEntity::class);
230
231
        /** @var SampleEntity $entityB */
232
        $entityB = $this->createMock(SampleEntity::class);
233
234
        $this->controllerHelper->expects($this->once())->method('findEntities')->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...
235
            $this->equalTo(SampleEntity::class),
236
            $this->equalTo(['blah' => 'blubb'])
237
        )->willReturn([$entityA, $entityB]);
238
239
        $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...
240
            $this->equalTo(['foo' => 'bar']),
241
            $this->identicalTo($request)
242
        )->willReturn(['blah' => 'blubb']);
243
244
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
245
            'entity-class' => SampleEntity::class,
246
            'data-template' => [
247
                'lorem' => 'fooCalled',
248
                'ipsum' => 'constructArgument'
249
            ],
250
            'criteria' => [
251
                'foo' => 'bar'
252
            ]
253
        ]);
254
255
        $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...
256
257
        /** @var Response $actualResponse */
258
        $actualResponse = $controller();
259
260
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
261
    }
262
263
    /**
264
     * @test
265
     */
266
    public function shouldRejectCallWithoutRequest()
267
    {
268
        $this->expectException(InvalidArgumentException::class);
269
270
        $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [
271
            'entity-class' => SampleEntity::class,
272
        ]);
273
274
        $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...
275
276
        $controller();
277
    }
278
279
}
280