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