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