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); |
|
|
|
|
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( |
|
|
|
|
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( |
|
|
|
|
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( |
|
|
|
|
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( |
|
|
|
|
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( |
|
|
|
|
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() |
|
|
|
|
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( |
|
|
|
|
156
|
|
|
$this->equalTo(SampleEntity::class), |
157
|
|
|
$this->equalTo("some-id") |
158
|
|
|
)->willReturn($entity); |
159
|
|
|
|
160
|
|
|
$this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with( |
|
|
|
|
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( |
|
|
|
|
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, [ |
|
|
|
|
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, [ |
|
|
|
|
216
|
|
|
]); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @test |
221
|
|
|
*/ |
222
|
|
View Code Duplication |
public function shouldThrowExceptionOnInvalidNormalizer() |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
$this->expectException(InvalidArgumentException::class); |
225
|
|
|
|
226
|
|
|
$controller = new GenericEntityFetchController($this->controllerHelper, [ |
|
|
|
|
227
|
|
|
'entity-class' => SampleEntity::class, |
228
|
|
|
'normalizer' => false |
229
|
|
|
]); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @test |
234
|
|
|
*/ |
235
|
|
View Code Duplication |
public function shouldThrowExceptionOnInvalidEncoder() |
|
|
|
|
236
|
|
|
{ |
237
|
|
|
$this->expectException(InvalidArgumentException::class); |
238
|
|
|
|
239
|
|
|
$controller = new GenericEntityFetchController($this->controllerHelper, [ |
|
|
|
|
240
|
|
|
'entity-class' => SampleEntity::class, |
241
|
|
|
'encoder' => false |
242
|
|
|
]); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @test |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
public function shouldThrowExceptionWhenCallingConstructorAgain() |
|
|
|
|
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( |
|
|
|
|
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'); |
|
|
|
|
283
|
|
|
|
284
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
304
|
|
|
|
305
|
|
|
$controller(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
} |
309
|
|
|
|
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..