|
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\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
|
|
View Code Duplication |
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
|
|
|
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('buildRouteArguments')->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
|
|
|
|
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..