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
|
|
|
|
26
|
|
|
final class DefaultControllerHelperTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DefaultControllerHelper |
31
|
|
|
*/ |
32
|
|
|
private $controllerHelper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var EntityManagerInterface |
36
|
|
|
*/ |
37
|
|
|
private $entityManager; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Twig_Environment |
41
|
|
|
*/ |
42
|
|
|
private $twig; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var AuthorizationCheckerInterface |
46
|
|
|
*/ |
47
|
|
|
private $authorization; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var UrlGeneratorInterface |
51
|
|
|
*/ |
52
|
|
|
private $urlGenerator; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Session |
56
|
|
|
*/ |
57
|
|
|
private $session; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var LoggerInterface |
61
|
|
|
*/ |
62
|
|
|
private $logger; |
63
|
|
|
|
64
|
|
|
public function setUp() |
65
|
|
|
{ |
66
|
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class); |
|
|
|
|
67
|
|
|
$this->twig = $this->createMock(Twig_Environment::class); |
|
|
|
|
68
|
|
|
$this->authorization = $this->createMock(AuthorizationCheckerInterface::class); |
|
|
|
|
69
|
|
|
$this->urlGenerator = $this->createMock(UrlGeneratorInterface::class); |
|
|
|
|
70
|
|
|
$this->session = $this->createMock(Session::class); |
|
|
|
|
71
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$this->controllerHelper = new DefaultControllerHelper( |
74
|
|
|
$this->entityManager, |
|
|
|
|
75
|
|
|
$this->twig, |
|
|
|
|
76
|
|
|
$this->authorization, |
|
|
|
|
77
|
|
|
$this->urlGenerator, |
|
|
|
|
78
|
|
|
$this->session, |
|
|
|
|
79
|
|
|
$this->logger |
|
|
|
|
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function shouldPersistEntity() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
/** @var stdClass $entity */ |
89
|
|
|
$entity = $this->createMock(stdClass::class); |
90
|
|
|
|
91
|
|
|
$this->entityManager->expects($this->once())->method('persist')->with($this->identicalTo($entity)); |
|
|
|
|
92
|
|
|
|
93
|
|
|
$this->controllerHelper->persistEntity($entity); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function shouldRemoveEntity() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
/** @var stdClass $entity */ |
102
|
|
|
$entity = $this->createMock(stdClass::class); |
103
|
|
|
|
104
|
|
|
$this->entityManager->expects($this->once())->method('remove')->with($this->identicalTo($entity)); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$this->controllerHelper->removeEntity($entity); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @test |
111
|
|
|
*/ |
112
|
|
|
public function shouldFlushORM() |
113
|
|
|
{ |
114
|
|
|
$this->entityManager->expects($this->once())->method('flush'); |
|
|
|
|
115
|
|
|
$this->controllerHelper->flushORM(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @test |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function shouldHandleException() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
/** @var Exception $exception */ |
124
|
|
|
$exception = $this->createMock('Exception'); |
125
|
|
|
$exception->method('__toString')->willReturn("some-string"); |
126
|
|
|
|
127
|
|
|
$this->logger->expects($this->once())->method('log')->with( |
|
|
|
|
128
|
|
|
$this->equalTo('error'), |
129
|
|
|
$this->identicalTo('some-string') |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->controllerHelper->handleException($exception); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @test |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function shouldAddFlashMessage() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
/** @var FlashBagInterface $flashBag */ |
141
|
|
|
$flashBag = $this->createMock(FlashBagInterface::class); |
142
|
|
|
$flashBag->expects($this->once())->method('add')->with( |
|
|
|
|
143
|
|
|
$this->equalTo('some-type'), |
144
|
|
|
$this->equalTo("Lorem ipsum dolor sit amet!") |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$this->session->method('getFlashBag')->willReturn($flashBag); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$this->controllerHelper->addFlashMessage("Lorem ipsum dolor sit amet!", "some-type"); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @test |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function shouldUseDefaultCodeWhenRedirectingToRoute() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$this->urlGenerator->expects($this->once())->method('generate')->with( |
|
|
|
|
158
|
|
|
$this->equalTo('some_route'), |
159
|
|
|
$this->equalTo(['foo' => 'bar']), |
160
|
|
|
$this->equalTo(UrlGeneratorInterface::ABSOLUTE_URL) |
161
|
|
|
)->willReturn("*this-is-some-url*"); |
162
|
|
|
|
163
|
|
|
$expectedResponse = new RedirectResponse("*this-is-some-url*", 301); |
164
|
|
|
|
165
|
|
|
/** @var RedirectResponse $actualResponse */ |
166
|
|
|
$actualResponse = $this->controllerHelper->redirectToRoute("some_route", ['foo' => 'bar']); |
167
|
|
|
|
168
|
|
|
$this->assertEquals($expectedResponse, $actualResponse); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
public function shouldRedirectToRoute() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$this->urlGenerator->expects($this->once())->method('generate')->with( |
|
|
|
|
177
|
|
|
$this->equalTo('some_route'), |
178
|
|
|
$this->equalTo(['foo' => 'bar']), |
179
|
|
|
$this->equalTo(UrlGeneratorInterface::ABSOLUTE_URL) |
180
|
|
|
)->willReturn("*this-is-some-url*"); |
181
|
|
|
|
182
|
|
|
$expectedResponse = new RedirectResponse("*this-is-some-url*", 302); |
183
|
|
|
|
184
|
|
|
/** @var RedirectResponse $actualResponse */ |
185
|
|
|
$actualResponse = $this->controllerHelper->redirectToRoute("some_route", ['foo' => 'bar'], 302); |
186
|
|
|
|
187
|
|
|
$this->assertEquals($expectedResponse, $actualResponse); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @test |
192
|
|
|
*/ |
193
|
|
|
public function shouldDenyAccessUnlessGranted() |
194
|
|
|
{ |
195
|
|
|
$this->expectException(AccessDeniedException::class); |
196
|
|
|
|
197
|
|
|
/** @var stdClass $subject */ |
198
|
|
|
$subject = $this->createMock(stdClass::class); |
199
|
|
|
|
200
|
|
|
$this->authorization->expects($this->once())->method('isGranted')->with( |
|
|
|
|
201
|
|
|
$this->equalTo("foo"), |
202
|
|
|
$this->equalTo($subject) |
203
|
|
|
)->willReturn(false); |
204
|
|
|
|
205
|
|
|
try { |
206
|
|
|
$this->controllerHelper->denyAccessUnlessGranted("foo", $subject); |
207
|
|
|
|
208
|
|
|
} catch (AccessDeniedException $exception) { |
209
|
|
|
$this->assertEquals(["foo"], $exception->getAttributes()); |
210
|
|
|
$this->assertSame($subject, $exception->getSubject()); |
211
|
|
|
|
212
|
|
|
throw $exception; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
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..