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); |
|
|
|
|
46
|
|
|
$this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class); |
|
|
|
|
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, [ |
|
|
|
|
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function shouldThrowExceptionWhenEntityClassIsInvalid() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$this->expectException(InvalidArgumentException::class); |
82
|
|
|
|
83
|
|
|
$controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [ |
|
|
|
|
84
|
|
|
'entity-class' => "DoesNotExist" |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @test |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function shouldThrowExceptionOnInvalidNormalizer() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$this->expectException(InvalidArgumentException::class); |
94
|
|
|
|
95
|
|
|
$controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [ |
|
|
|
|
96
|
|
|
'entity-class' => SampleEntity::class, |
97
|
|
|
'normalizer' => false |
98
|
|
|
]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @test |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function shouldThrowExceptionOnInvalidEncoder() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->expectException(InvalidArgumentException::class); |
107
|
|
|
|
108
|
|
|
$controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [ |
|
|
|
|
109
|
|
|
'entity-class' => SampleEntity::class, |
110
|
|
|
'encoder' => false |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @test |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function shouldListEntities() |
|
|
|
|
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( |
|
|
|
|
132
|
|
|
$this->equalTo(SampleEntity::class), |
133
|
|
|
$this->equalTo(['blah' => 'blubb']) |
134
|
|
|
)->willReturn([$entityA, $entityB]); |
135
|
|
|
|
136
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildArguments')->with( |
|
|
|
|
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( |
|
|
|
|
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( |
|
|
|
|
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() |
|
|
|
|
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( |
|
|
|
|
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() |
|
|
|
|
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( |
|
|
|
|
235
|
|
|
$this->equalTo(SampleEntity::class), |
236
|
|
|
$this->equalTo(['blah' => 'blubb']) |
237
|
|
|
)->willReturn([$entityA, $entityB]); |
238
|
|
|
|
239
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildArguments')->with( |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
275
|
|
|
|
276
|
|
|
$controller(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
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..