Completed
Push — master ( c6433c...a8c688 )
by Gerrit
38:24
created

GenericEntityFetchControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
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;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Controllers\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
23
final class GenericEntityFetchControllerTest extends TestCase
24
{
25
26
    /**
27
     * @var ControllerHelperInterface
28
     */
29
    private $controllerHelper;
30
31
    public function setUp()
32
    {
33
        $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...
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function shouldFetchEntity()
40
    {
41
        $entity = new SampleEntity();
42
43
        $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...
44
            $this->equalTo(SampleEntity::class),
45
            $this->equalTo("some-id")
46
        )->willReturn($entity);
47
48
        /** @var string $expectedResponseContent */
49
        $expectedResponseContent = '{"fooCalled":false,"constructArgument":"foo"}';
50
51
        $controller = new GenericEntityFetchController($this->controllerHelper, [
52
            'entity-class' => SampleEntity::class
53
        ]);
54
55
        /** @var Response $actualResponse */
56
        $actualResponse = $controller->fetchEntity("some-id");
57
58
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function shouldApplyDataTemplate()
65
    {
66
        $entity = new SampleEntity();
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(SampleEntity::class),
70
            $this->equalTo("some-id")
71
        )->willReturn($entity);
72
73
        /** @var string $expectedResponseContent */
74
        $expectedResponseContent = '{"lorem":false,"ipsum":"foo"}';
75
76
        $controller = new GenericEntityFetchController($this->controllerHelper, [
77
            'entity-class' => SampleEntity::class,
78
            'data-template' => [
79
                'lorem' => 'fooCalled',
80
                'ipsum' => 'constructArgument'
81
            ]
82
        ]);
83
84
        /** @var Response $actualResponse */
85
        $actualResponse = $controller->fetchEntity("some-id");
86
87
        $this->assertEquals($expectedResponseContent, $actualResponse->getContent());
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function shouldThrowExceptionOnInvalidDataTemplateEntry()
94
    {
95
        $this->expectException(ErrorException::class);
96
97
        $entity = new SampleEntity();
98
99
        $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...
100
            $this->equalTo(SampleEntity::class),
101
            $this->equalTo("some-id")
102
        )->willReturn($entity);
103
104
        $controller = new GenericEntityFetchController($this->controllerHelper, [
105
            'entity-class' => SampleEntity::class,
106
            'data-template' => [
107
                'lorem' => 'fooCalled',
108
                'ipsum' => 'constructArgument',
109
                'dolor' => false,
110
            ]
111
        ]);
112
113
        $controller->fetchEntity("some-id");
114
    }
115
116
    /**
117
     * @test
118
     */
119
    public function shouldThrowExceptionOnInvalidNormalizerResult()
120
    {
121
        $this->expectException(ErrorException::class);
122
123
        $entity = new SampleEntity();
124
125
        $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...
126
            $this->equalTo(SampleEntity::class),
127
            $this->equalTo("some-id")
128
        )->willReturn($entity);
129
130
        /** @var NormalizerInterface $normalizer */
131
        $normalizer = $this->createMock(NormalizerInterface::class);
132
        $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...
133
            $this->identicalTo($entity)
134
        )->willReturn('*non-array*');
135
136
        $controller = new GenericEntityFetchController($this->controllerHelper, [
137
            'entity-class' => SampleEntity::class,
138
            'normalizer' => $normalizer,
139
        ]);
140
141
        $controller->fetchEntity("some-id");
142
    }
143
144
    /**
145
     * @test
146
     */
147 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...
148
    {
149
        $this->expectException(AccessDeniedException::class);
150
151
        /** @var mixed $entity */
152
        $entity = new SampleEntity();
153
154
        $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...
155
            $this->equalTo(SampleEntity::class),
156
            $this->equalTo("some-id")
157
        )->willReturn($entity);
158
159
        $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...
160
            $this->equalTo('some-attribute'),
161
            $this->identicalTo($entity)
162
        )->will($this->returnCallback(
163
            function () {
164
                throw new AccessDeniedException('Lorem ipsum!');
165
            }
166
        ));
167
168
        $controller = new GenericEntityFetchController($this->controllerHelper, [
169
            'entity-class' => SampleEntity::class,
170
            'authorization-attribute' => 'some-attribute',
171
        ]);
172
173
        $controller->fetchEntity("some-id");
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function shouldThrowExceptionWhenEntityNotFound()
180
    {
181
        $this->expectException(InvalidArgumentException::class);
182
183
        $controller = new GenericEntityFetchController($this->controllerHelper, [
184
            'entity-class' => SampleEntity::class,
185
        ]);
186
187
        $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...
188
            $this->equalTo(SampleEntity::class),
189
            $this->equalTo("some-id")
190
        )->willReturn(null);
191
192
        $controller->fetchEntity("some-id");
193
    }
194
195
    /**
196
     * @test
197
     */
198
    public function shouldThrowExceptionWhenEntityClassDoesNotExist()
199
    {
200
        $this->expectException(InvalidArgumentException::class);
201
202
        $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...
203
            'entity-class' => "NotExisting",
204
        ]);
205
    }
206
207
    /**
208
     * @test
209
     */
210
    public function shouldThrowExceptionWhenMissingEntityClass()
211
    {
212
        $this->expectException(InvalidArgumentException::class);
213
214
        $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...
215
        ]);
216
    }
217
218
    /**
219
     * @test
220
     */
221 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...
222
    {
223
        $this->expectException(InvalidArgumentException::class);
224
225
        $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...
226
            'entity-class' => SampleEntity::class,
227
            'normalizer' => false
228
        ]);
229
    }
230
231
    /**
232
     * @test
233
     */
234 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...
235
    {
236
        $this->expectException(InvalidArgumentException::class);
237
238
        $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...
239
            'entity-class' => SampleEntity::class,
240
            'encoder' => false
241
        ]);
242
    }
243
244
    /**
245
     * @test
246
     */
247 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...
248
    {
249
        $this->expectException(InvalidArgumentException::class);
250
251
        $controller = new GenericEntityFetchController($this->controllerHelper, [
252
            'entity-class' => SampleEntity::class,
253
        ]);
254
255
        $controller->__construct($this->controllerHelper, [
256
            'entity-class' => SampleEntity::class,
257
        ]);
258
    }
259
260
261
}
262