1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Tests\Controller; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
19
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
22
|
|
|
use Throwable; |
23
|
|
|
use Twig\Environment; |
24
|
|
|
use WBW\Bundle\CoreBundle\Controller\AbstractController; |
25
|
|
|
use WBW\Bundle\CoreBundle\Event\NotificationEvent; |
26
|
|
|
use WBW\Bundle\CoreBundle\Event\ToastEvent; |
27
|
|
|
use WBW\Bundle\CoreBundle\EventListener\KernelEventListener; |
28
|
|
|
use WBW\Bundle\CoreBundle\Exception\BadUserRoleException; |
29
|
|
|
use WBW\Bundle\CoreBundle\Service\SymfonyBCService; |
30
|
|
|
use WBW\Bundle\CoreBundle\Tests\AbstractWebTestCase; |
31
|
|
|
use WBW\Bundle\CoreBundle\Tests\Fixtures\Controller\TestAbstractController; |
32
|
|
|
use WBW\Library\Symfony\Assets\NotificationInterface; |
33
|
|
|
use WBW\Library\Symfony\Assets\ToastInterface; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Abstract controller test. |
37
|
|
|
* |
38
|
|
|
* @author webeweb <https://github.com/webeweb> |
39
|
|
|
* @package WBW\Bundle\CoreBundle\Tests\Controller |
40
|
|
|
*/ |
41
|
|
|
class AbstractControllerTest extends AbstractWebTestCase { |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Controller. |
45
|
|
|
* |
46
|
|
|
* @var AbstractController |
47
|
|
|
*/ |
48
|
|
|
private $controller; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected function setUp(): void { |
54
|
|
|
parent::setUp(); |
55
|
|
|
|
56
|
|
|
// Set a controller mock. |
57
|
|
|
$this->controller = static::$kernel->getContainer()->get(TestAbstractController::class); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Test getContainer() |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function testGetContainer(): void { |
66
|
|
|
|
67
|
|
|
$obj = $this->controller; |
68
|
|
|
|
69
|
|
|
$res = $obj->getContainer(); |
70
|
|
|
$this->assertInstanceOf(ContainerInterface::class, $res); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Test getEntityManager() |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
78
|
|
|
*/ |
79
|
|
|
public function testGetEntityManager(): void { |
80
|
|
|
|
81
|
|
|
$obj = $this->controller; |
82
|
|
|
|
83
|
|
|
$res = $obj->getEntityManager(); |
84
|
|
|
$this->assertInstanceOf(EntityManagerInterface::class, $res); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test getEventDispatcher() |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
92
|
|
|
*/ |
93
|
|
|
public function testGetEventDispatcher(): void { |
94
|
|
|
|
95
|
|
|
$obj = $this->controller; |
96
|
|
|
|
97
|
|
|
$res = $obj->getEventDispatcher(); |
98
|
|
|
$this->assertInstanceOf(EventDispatcherInterface::class, $res); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Test getKernelEventListener() |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
106
|
|
|
*/ |
107
|
|
|
public function testGetKernelEventListener(): void { |
108
|
|
|
|
109
|
|
|
$obj = $this->controller; |
110
|
|
|
|
111
|
|
|
$res = $obj->getKernelEventListener(); |
112
|
|
|
$this->assertInstanceOf(KernelEventListener::class, $res); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Test getLogger() |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
120
|
|
|
*/ |
121
|
|
|
public function testGetLogger(): void { |
122
|
|
|
|
123
|
|
|
$obj = $this->controller; |
124
|
|
|
|
125
|
|
|
$res = $obj->getLogger(); |
126
|
|
|
$this->assertInstanceOf(LoggerInterface::class, $res); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Test getMailer() |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
134
|
|
|
*/ |
135
|
|
|
public function testGetMailer(): void { |
136
|
|
|
|
137
|
|
|
$obj = $this->controller; |
138
|
|
|
|
139
|
|
|
$res = $obj->getMailer(); |
140
|
|
|
$this->assertInstanceOf(MailerInterface::class, $res); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Test getRouter() |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
148
|
|
|
*/ |
149
|
|
|
public function testGetRouter(): void { |
150
|
|
|
|
151
|
|
|
$obj = $this->controller; |
152
|
|
|
|
153
|
|
|
$res = $obj->getRouter(); |
154
|
|
|
$this->assertInstanceOf(RouterInterface::class, $res); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Test getSession() |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function testGetSession(): void { |
163
|
|
|
|
164
|
|
|
$obj = $this->controller; |
165
|
|
|
|
166
|
|
|
try { |
167
|
|
|
|
168
|
|
|
$res = $obj->getSession(); |
169
|
|
|
$this->assertInstanceOf(SessionInterface::class, $res); |
170
|
|
|
} catch (Throwable $ex) { |
171
|
|
|
|
172
|
|
|
$this->assertInstanceOf("Symfony\\Component\\HttpFoundation\\Exception\\SessionNotFoundException", $ex); |
173
|
|
|
$this->assertEquals("There is currently no session available.", $ex->getMessage()); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Test getSubscribedServices() |
179
|
|
|
* |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
public function testGetSubscribedServices(): void { |
183
|
|
|
|
184
|
|
|
$res = AbstractController::getSubscribedServices(); |
185
|
|
|
|
186
|
|
|
$this->assertArrayHasKey("doctrine.orm.entity_manager", $res); |
187
|
|
|
$this->assertArrayHasKey("event_dispatcher", $res); |
188
|
|
|
$this->assertArrayHasKey("logger", $res); |
189
|
|
|
$this->assertArrayHasKey("mailer", $res); |
190
|
|
|
$this->assertArrayHasKey("translator", $res); |
191
|
|
|
$this->assertArrayHasKey(KernelEventListener::SERVICE_NAME, $res); |
192
|
|
|
$this->assertArrayHasKey(SymfonyBCService::SERVICE_NAME, $res); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Test getTranslator() |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
200
|
|
|
*/ |
201
|
|
|
public function testGetTranslator(): void { |
202
|
|
|
|
203
|
|
|
$obj = $this->controller; |
204
|
|
|
|
205
|
|
|
$res = $obj->getTranslator(); |
206
|
|
|
$this->assertInstanceOf(TranslatorInterface::class, $res); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Test getTwig() |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
214
|
|
|
*/ |
215
|
|
|
public function testGetTwig(): void { |
216
|
|
|
|
217
|
|
|
$obj = $this->controller; |
218
|
|
|
|
219
|
|
|
$res = $obj->getTwig(); |
220
|
|
|
$this->assertInstanceOf(Environment::class, $res); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Test hasRoleOrRedirect() |
225
|
|
|
* |
226
|
|
|
* @return void |
227
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
228
|
|
|
*/ |
229
|
|
|
public function testHasRoleOrRedirect(): void { |
230
|
|
|
|
231
|
|
|
$obj = new TestAbstractController(); |
232
|
|
|
$obj->setContainer(static::$kernel->getContainer()); |
233
|
|
|
|
234
|
|
|
try { |
235
|
|
|
|
236
|
|
|
$obj->hasRolesOrRedirect(["ROLE_USER"], false, "redirectUrl"); |
237
|
|
|
} catch (Throwable $ex) { |
238
|
|
|
|
239
|
|
|
$this->assertInstanceOf(BadUserRoleException::class, $ex); |
240
|
|
|
$this->assertEquals('User "anonymous" is not allowed to access to "" with roles [ROLE_USER]', $ex->getMessage()); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Test newDefaultJsonResponseData() |
246
|
|
|
* |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
|
public function testNewDefaultJsonResponseData(): void { |
250
|
|
|
|
251
|
|
|
$obj = new TestAbstractController(); |
252
|
|
|
|
253
|
|
|
$res = $obj->newDefaultJsonResponseData(true, [], "message"); |
254
|
|
|
$this->assertEquals([], $res->getData()); |
255
|
|
|
$this->assertNull($res->getErrors()); |
256
|
|
|
$this->assertEquals("message", $res->getMessage()); |
257
|
|
|
$this->assertTrue($res->getSuccess()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Test notify() |
262
|
|
|
* |
263
|
|
|
* @return void |
264
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
265
|
|
|
*/ |
266
|
|
|
public function testNotify(): void { |
267
|
|
|
|
268
|
|
|
// Set a Notification mock. |
269
|
|
|
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock(); |
270
|
|
|
|
271
|
|
|
$obj = new TestAbstractController(); |
272
|
|
|
$obj->setContainer(static::$kernel->getContainer()); |
273
|
|
|
|
274
|
|
|
$res = $obj->notify("eventName", $notification); |
275
|
|
|
$this->assertNotNull($res); |
276
|
|
|
|
277
|
|
|
$this->assertInstanceOf(NotificationEvent::class, $res); |
278
|
|
|
$this->assertEquals("eventName", $res->getEventName()); |
279
|
|
|
$this->assertSame($notification, $res->getNotification()); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Test toast() |
284
|
|
|
* |
285
|
|
|
* @return void |
286
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
287
|
|
|
*/ |
288
|
|
|
public function testToast(): void { |
289
|
|
|
|
290
|
|
|
// Set a Toast mock. |
291
|
|
|
$toast = $this->getMockBuilder(ToastInterface::class)->getMock(); |
292
|
|
|
|
293
|
|
|
$obj = new TestAbstractController(); |
294
|
|
|
$obj->setContainer(static::$kernel->getContainer()); |
295
|
|
|
|
296
|
|
|
$res = $obj->toast("eventName", $toast); |
297
|
|
|
$this->assertNotNull($res); |
298
|
|
|
|
299
|
|
|
$this->assertInstanceOf(ToastEvent::class, $res); |
300
|
|
|
$this->assertEquals("eventName", $res->getEventName()); |
301
|
|
|
$this->assertSame($toast, $res->getToast()); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Test translate() |
306
|
|
|
* |
307
|
|
|
* @return void |
308
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
309
|
|
|
*/ |
310
|
|
|
public function testTranslate(): void { |
311
|
|
|
|
312
|
|
|
$obj = new TestAbstractController(); |
313
|
|
|
$obj->setContainer(static::$kernel->getContainer()); |
314
|
|
|
|
315
|
|
|
$this->assertEquals("id", $obj->translate("id")); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|