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\DefaultControllerHelper; |
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
16
|
|
|
use Twig_Environment; |
17
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
18
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
22
|
|
|
use stdClass; |
23
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
26
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
27
|
|
|
use Symfony\Component\HttpFoundation\Response; |
28
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\EventDispatcher\Event; |
31
|
|
|
|
32
|
|
|
final class DefaultControllerHelperTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var DefaultControllerHelper |
37
|
|
|
*/ |
38
|
|
|
private $controllerHelper; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var EntityManagerInterface |
42
|
|
|
*/ |
43
|
|
|
private $entityManager; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Twig_Environment |
47
|
|
|
*/ |
48
|
|
|
private $twig; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var AuthorizationCheckerInterface |
52
|
|
|
*/ |
53
|
|
|
private $authorization; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var UrlGeneratorInterface |
57
|
|
|
*/ |
58
|
|
|
private $urlGenerator; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var Session |
62
|
|
|
*/ |
63
|
|
|
private $session; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var LoggerInterface |
67
|
|
|
*/ |
68
|
|
|
private $logger; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var EventDispatcherInterface |
72
|
|
|
*/ |
73
|
|
|
private $eventDispatcher; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var RequestStack |
77
|
|
|
*/ |
78
|
|
|
private $requestStack; |
79
|
|
|
|
80
|
|
|
public function setUp() |
81
|
|
|
{ |
82
|
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class); |
|
|
|
|
83
|
|
|
$this->twig = $this->createMock(Twig_Environment::class); |
|
|
|
|
84
|
|
|
$this->authorization = $this->createMock(AuthorizationCheckerInterface::class); |
|
|
|
|
85
|
|
|
$this->urlGenerator = $this->createMock(UrlGeneratorInterface::class); |
|
|
|
|
86
|
|
|
$this->session = $this->createMock(Session::class); |
|
|
|
|
87
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
|
|
|
|
88
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
|
|
|
|
89
|
|
|
$this->requestStack = $this->createMock(RequestStack::class); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->controllerHelper = new DefaultControllerHelper( |
92
|
|
|
$this->entityManager, |
|
|
|
|
93
|
|
|
$this->twig, |
|
|
|
|
94
|
|
|
$this->authorization, |
|
|
|
|
95
|
|
|
$this->urlGenerator, |
|
|
|
|
96
|
|
|
$this->session, |
|
|
|
|
97
|
|
|
$this->logger, |
|
|
|
|
98
|
|
|
$this->eventDispatcher, |
|
|
|
|
99
|
|
|
$this->requestStack |
|
|
|
|
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
*/ |
106
|
|
|
public function shouldRenderTemplate() |
107
|
|
|
{ |
108
|
|
|
$this->twig->expects($this->once())->method('render')->with( |
|
|
|
|
109
|
|
|
$this->equalTo('some-template'), |
110
|
|
|
$this->equalTo(['foo', 'bar']) |
111
|
|
|
)->willReturn('some-response'); |
112
|
|
|
|
113
|
|
|
/** @var Response $actualResult */ |
114
|
|
|
$actualResult = $this->controllerHelper->renderTemplate('some-template', ['foo', 'bar']); |
115
|
|
|
|
116
|
|
|
$this->assertEquals(new Response('some-response'), $actualResult); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @test |
121
|
|
|
*/ |
122
|
|
|
public function shouldFindEntity() |
123
|
|
|
{ |
124
|
|
|
$this->entityManager->expects($this->once())->method('find')->with( |
|
|
|
|
125
|
|
|
$this->equalTo('some-class'), |
126
|
|
|
$this->equalTo('foo') |
127
|
|
|
)->willReturn('some-entity'); |
128
|
|
|
|
129
|
|
|
/** @var mixed $actualResult */ |
130
|
|
|
$actualResult = $this->controllerHelper->findEntity('some-class', 'foo'); |
131
|
|
|
|
132
|
|
|
$this->assertEquals('some-entity', $actualResult); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @test |
137
|
|
|
*/ |
138
|
|
|
public function shouldFindEntities() |
139
|
|
|
{ |
140
|
|
|
/** @var ObjectRepository $repository */ |
141
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
142
|
|
|
$repository->method('findBy')->with( |
143
|
|
|
$this->equalTo(['foo' => 'bar']) |
144
|
|
|
)->willReturn(['some-entity', 'some-other-entity']); |
145
|
|
|
|
146
|
|
|
$this->entityManager->expects($this->once())->method('getRepository')->with( |
|
|
|
|
147
|
|
|
$this->equalTo('some-class') |
148
|
|
|
)->willReturn($repository); |
149
|
|
|
|
150
|
|
|
/** @var mixed $actualResult */ |
151
|
|
|
$actualResult = $this->controllerHelper->findEntities('some-class', ['foo' => 'bar']); |
152
|
|
|
|
153
|
|
|
$this->assertEquals(['some-entity', 'some-other-entity'], $actualResult); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @test |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function shouldPersistEntity() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
/** @var stdClass $entity */ |
162
|
|
|
$entity = $this->createMock(stdClass::class); |
163
|
|
|
|
164
|
|
|
$this->entityManager->expects($this->once())->method('persist')->with($this->identicalTo($entity)); |
|
|
|
|
165
|
|
|
|
166
|
|
|
$this->controllerHelper->persistEntity($entity); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @test |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
public function shouldRemoveEntity() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
/** @var stdClass $entity */ |
175
|
|
|
$entity = $this->createMock(stdClass::class); |
176
|
|
|
|
177
|
|
|
$this->entityManager->expects($this->once())->method('remove')->with($this->identicalTo($entity)); |
|
|
|
|
178
|
|
|
|
179
|
|
|
$this->controllerHelper->removeEntity($entity); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @test |
184
|
|
|
*/ |
185
|
|
|
public function shouldFlushORM() |
186
|
|
|
{ |
187
|
|
|
$this->entityManager->expects($this->once())->method('flush'); |
|
|
|
|
188
|
|
|
$this->controllerHelper->flushORM(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @test |
193
|
|
|
*/ |
194
|
|
|
public function shouldHaveRequestStack() |
195
|
|
|
{ |
196
|
|
|
$this->assertSame($this->requestStack, $this->controllerHelper->getRequestStack()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @test |
201
|
|
|
*/ |
202
|
|
|
public function shouldHaveCurrentRequest() |
203
|
|
|
{ |
204
|
|
|
/** @var Request $request */ |
205
|
|
|
$request = $this->createMock(Request::class); |
206
|
|
|
|
207
|
|
|
$this->requestStack->method("getCurrentRequest")->willReturn($request); |
|
|
|
|
208
|
|
|
|
209
|
|
|
$this->assertSame($request, $this->controllerHelper->getCurrentRequest()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @test |
214
|
|
|
*/ |
215
|
|
|
public function shouldDispatchEvent() |
216
|
|
|
{ |
217
|
|
|
/** @var Event $event */ |
218
|
|
|
$event = $this->createMock(Event::class); |
219
|
|
|
|
220
|
|
|
$this->eventDispatcher->expects($this->once())->method('dispatch')->with( |
|
|
|
|
221
|
|
|
$this->equalTo('foo'), |
222
|
|
|
$this->equalTo($event) |
223
|
|
|
)->willReturn($event); |
224
|
|
|
|
225
|
|
|
$this->assertSame($event, $this->controllerHelper->dispatchEvent("foo", $event)); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @test |
230
|
|
|
*/ |
231
|
|
View Code Duplication |
public function shouldHandleException() |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
/** @var Exception $exception */ |
234
|
|
|
$exception = $this->createMock('Exception'); |
235
|
|
|
$exception->method('__toString')->willReturn("some-string"); |
236
|
|
|
|
237
|
|
|
$this->logger->expects($this->once())->method('log')->with( |
|
|
|
|
238
|
|
|
$this->equalTo('error'), |
239
|
|
|
$this->identicalTo('some-string') |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
$this->controllerHelper->handleException($exception); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @test |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
public function shouldAddFlashMessage() |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
/** @var FlashBagInterface $flashBag */ |
251
|
|
|
$flashBag = $this->createMock(FlashBagInterface::class); |
252
|
|
|
$flashBag->expects($this->once())->method('add')->with( |
|
|
|
|
253
|
|
|
$this->equalTo('some-type'), |
254
|
|
|
$this->equalTo("Lorem ipsum dolor sit amet!") |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
$this->session->method('getFlashBag')->willReturn($flashBag); |
|
|
|
|
258
|
|
|
|
259
|
|
|
$this->controllerHelper->addFlashMessage("Lorem ipsum dolor sit amet!", "some-type"); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @test |
264
|
|
|
*/ |
265
|
|
View Code Duplication |
public function shouldUseDefaultCodeWhenRedirectingToRoute() |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
$this->urlGenerator->expects($this->once())->method('generate')->with( |
|
|
|
|
268
|
|
|
$this->equalTo('some_route'), |
269
|
|
|
$this->equalTo(['foo' => 'bar']), |
270
|
|
|
$this->equalTo(UrlGeneratorInterface::ABSOLUTE_URL) |
271
|
|
|
)->willReturn("*this-is-some-url*"); |
272
|
|
|
|
273
|
|
|
$expectedResponse = new RedirectResponse("*this-is-some-url*", 301); |
274
|
|
|
|
275
|
|
|
/** @var RedirectResponse $actualResponse */ |
276
|
|
|
$actualResponse = $this->controllerHelper->redirectToRoute("some_route", ['foo' => 'bar']); |
277
|
|
|
|
278
|
|
|
$this->assertEquals($expectedResponse, $actualResponse); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @test |
283
|
|
|
*/ |
284
|
|
View Code Duplication |
public function shouldRedirectToRoute() |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
$this->urlGenerator->expects($this->once())->method('generate')->with( |
|
|
|
|
287
|
|
|
$this->equalTo('some_route'), |
288
|
|
|
$this->equalTo(['foo' => 'bar']), |
289
|
|
|
$this->equalTo(UrlGeneratorInterface::ABSOLUTE_URL) |
290
|
|
|
)->willReturn("*this-is-some-url*"); |
291
|
|
|
|
292
|
|
|
$expectedResponse = new RedirectResponse("*this-is-some-url*", 302); |
293
|
|
|
|
294
|
|
|
/** @var RedirectResponse $actualResponse */ |
295
|
|
|
$actualResponse = $this->controllerHelper->redirectToRoute("some_route", ['foo' => 'bar'], 302); |
296
|
|
|
|
297
|
|
|
$this->assertEquals($expectedResponse, $actualResponse); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @test |
302
|
|
|
*/ |
303
|
|
|
public function shouldDenyAccessUnlessGranted() |
304
|
|
|
{ |
305
|
|
|
$this->expectException(AccessDeniedException::class); |
306
|
|
|
|
307
|
|
|
/** @var stdClass $subject */ |
308
|
|
|
$subject = $this->createMock(stdClass::class); |
309
|
|
|
|
310
|
|
|
$this->authorization->expects($this->once())->method('isGranted')->with( |
|
|
|
|
311
|
|
|
$this->equalTo("foo"), |
312
|
|
|
$this->equalTo($subject) |
313
|
|
|
)->willReturn(false); |
314
|
|
|
|
315
|
|
|
try { |
316
|
|
|
$this->controllerHelper->denyAccessUnlessGranted("foo", $subject); |
317
|
|
|
|
318
|
|
|
} catch (AccessDeniedException $exception) { |
319
|
|
|
$this->assertEquals(["foo"], $exception->getAttributes()); |
320
|
|
|
$this->assertSame($subject, $exception->getSubject()); |
321
|
|
|
|
322
|
|
|
throw $exception; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
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..