Completed
Push — master ( 0926c2...16eb90 )
by WEBEWEB
01:52
created

AbstractControllerTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 331
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 0
loc 331
rs 10
c 0
b 0
f 0
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 Exception;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\DependencyInjection\Container;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
use Symfony\Component\HttpFoundation\Session\SessionInterface;
19
use Symfony\Component\Routing\RouterInterface;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
use WBW\Bundle\CoreBundle\Component\Translation\BaseTranslatorInterface;
22
use WBW\Bundle\CoreBundle\Event\NotificationEvent;
23
use WBW\Bundle\CoreBundle\Event\ToastEvent;
24
use WBW\Bundle\CoreBundle\EventListener\KernelEventListener;
25
use WBW\Bundle\CoreBundle\Exception\BadUserRoleException;
26
use WBW\Bundle\CoreBundle\Helper\FormHelper;
27
use WBW\Bundle\CoreBundle\Notification\NotificationInterface;
28
use WBW\Bundle\CoreBundle\Repository\RepositoryHelper;
29
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase;
30
use WBW\Bundle\CoreBundle\Tests\Fixtures\Controller\TestAbstractController;
31
use WBW\Bundle\CoreBundle\Toast\ToastInterface;
32
33
/**
34
 * Abstract controller test.
35
 *
36
 * @author webeweb <https://github.com/webeweb/>
37
 * @package WBW\Bundle\CoreBundle\Tests\Controller
38
 */
39
class AbstractControllerTest extends AbstractTestCase {
40
41
    /**
42
     * Form helper.
43
     *
44
     * @var FormHelper
45
     */
46
    private $formHelper;
47
48
    /**
49
     * Kernel event listener.
50
     *
51
     * @var KernelEventListener
52
     */
53
    private $kernelEventListener;
54
55
    /**
56
     * Repository helper.
57
     *
58
     * @var RepositoryHelper
59
     */
60
    private $repositoryHelper;
61
62
    /**
63
     * Repository helper.
64
     *
65
     * @var RepositoryReportHelper
66
     */
67
    private $repositoryReportHelper;
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    protected function setUp(): void {
73
        parent::setUp();
74
75
        // Set a Form helper mock.
76
        $this->formHelper = $this->getMockBuilder(FormHelper::class)->disableOriginalConstructor()->getMock();
77
78
        // Set a Kernel event listener mock.
79
        $this->kernelEventListener = $this->getMockBuilder(KernelEventListener::class)->disableOriginalConstructor()->getMock();
80
81
        // Set a Repository helper mock.
82
        $this->repositoryHelper = $this->getMockBuilder(RepositoryHelper::class)->disableOriginalConstructor()->getMock();
83
84
        // Set the Container builder mock.
85
        $this->containerBuilder->set(FormHelper::SERVICE_NAME, $this->formHelper);
86
        $this->containerBuilder->set(KernelEventListener::SERVICE_NAME, $this->kernelEventListener);
87
        $this->containerBuilder->set(RepositoryHelper::SERVICE_NAME, $this->repositoryHelper);
88
    }
89
90
    /**
91
     * Tests the getContainer() method.
92
     *
93
     * @return void
94
     */
95
    public function testGetContainer(): void {
96
97
        $obj = new TestAbstractController();
98
        $obj->setContainer($this->containerBuilder);
99
100
        $res = $obj->getContainer();
101
        $this->assertInstanceOf(Container::class, $res);
102
        $this->assertSame($this->containerBuilder, $res);
103
    }
104
105
    /**
106
     * Tests the getEventDispatcher() method.
107
     *
108
     * @return void
109
     */
110
    public function testGetEventDispatcher(): void {
111
112
        $obj = new TestAbstractController();
113
        $obj->setContainer($this->containerBuilder);
114
115
        $res = $obj->getEventDispatcher();
116
        $this->assertInstanceOf(EventDispatcherInterface::class, $res);
117
        $this->assertSame($this->eventDispatcher, $res);
118
    }
119
120
    /**
121
     * Tests the getFormHelper() method.
122
     *
123
     * @return void
124
     */
125
    public function testGetFormHelper(): void {
126
127
        $obj = new TestAbstractController();
128
        $obj->setContainer($this->containerBuilder);
129
130
        $res = $obj->getFormHelper();
131
        $this->assertInstanceOf(FormHelper::class, $res);
132
        $this->assertSame($this->formHelper, $res);
133
    }
134
135
    /**
136
     * Tests the getKernelEventListener() method.
137
     *
138
     * @return void
139
     */
140
    public function testGetKernelEventListener(): void {
141
142
        $obj = new TestAbstractController();
143
        $obj->setContainer($this->containerBuilder);
144
145
        $res = $obj->getKernelEventListener();
146
        $this->assertInstanceOf(KernelEventListener::class, $res);
147
        $this->assertSame($this->kernelEventListener, $res);
148
    }
149
150
    /**
151
     * Tests the getLogger() method.
152
     *
153
     * @return void
154
     */
155
    public function testGetLogger(): void {
156
157
        $obj = new TestAbstractController();
158
        $obj->setContainer($this->containerBuilder);
159
160
        $res = $obj->getLogger();
161
        $this->assertInstanceOf(LoggerInterface::class, $res);
162
        $this->assertSame($this->logger, $res);
163
    }
164
165
    /**
166
     * Tests the getRepositoryHelper() method.
167
     *
168
     * @return void
169
     */
170
    public function testGetRepositoryHelper(): void {
171
172
        $obj = new TestAbstractController();
173
        $obj->setContainer($this->containerBuilder);
174
175
        $res = $obj->getRepositoryHelper();
176
        $this->assertInstanceOf(RepositoryHelper::class, $res);
177
        $this->assertSame($this->repositoryHelper, $res);
178
    }
179
180
    /**
181
     * Tests the getRouter() method.
182
     *
183
     * @return void
184
     */
185
    public function testGetRouter(): void {
186
187
        $obj = new TestAbstractController();
188
        $obj->setContainer($this->containerBuilder);
189
190
        $res = $obj->getRouter();
191
        $this->assertInstanceOf(RouterInterface::class, $res);
192
        $this->assertSame($this->router, $res);
193
    }
194
195
    /**
196
     * Tests the getSession() method.
197
     *
198
     * @return void
199
     */
200
    public function testGetSession(): void {
201
202
        $obj = new TestAbstractController();
203
        $obj->setContainer($this->containerBuilder);
204
205
        $res = $obj->getSession();
206
        $this->assertInstanceOf(SessionInterface::class, $res);
207
        $this->assertSame($this->session, $res);
208
    }
209
210
    /**
211
     * Tests the getTranslator() method.
212
     *
213
     * @return void
214
     */
215
    public function testGetTranslator(): void {
216
217
        $obj = new TestAbstractController();
218
        $obj->setContainer($this->containerBuilder);
219
220
        $res = $obj->getTranslator();
221
        $this->assertInstanceOf(BaseTranslatorInterface::class, $res);
222
        $this->assertSame($this->translator, $res);
223
    }
224
225
    /**
226
     * Tests the hasRoleOrRedirect() method.
227
     *
228
     * @return void
229
     * @throws Exception Throws an exception if an error occurs.
230
     */
231
    public function testHasRoleOrRedirect(): void {
232
233
        // Set the User mock.
234
        $this->user = $this->getMockBuilder(UserInterface::class)->getMock();
235
        $this->user->expects($this->any())->method("getRoles")->willReturn(["ROLE_GITHUB"]);
236
237
        // Set the Kernel event listener mock.
238
        $this->kernelEventListener->expects($this->any())->method("getUser")->willReturn($this->user);
239
240
        $obj = new TestAbstractController();
241
        $obj->setContainer($this->containerBuilder);
242
243
        $res = $obj->hasRolesOrRedirect(["ROLE_GITHUB"], false, "redirect");
244
        $this->assertTrue($res);
245
    }
246
247
    /**
248
     * Tests the hasRoleOrRedirect() method.
249
     *
250
     * @return void
251
     */
252
    public function testHasRoleOrRedirectWithBadUserRoleException(): void {
253
254
        $obj = new TestAbstractController();
255
        $obj->setContainer($this->containerBuilder);
256
257
        try {
258
259
            $obj->hasRolesOrRedirect(["ROLE_GITHUB"], false, "redirectUrl");
260
        } catch (Exception $ex) {
261
262
            $this->assertInstanceOf(BadUserRoleException::class, $ex);
263
            $this->assertEquals('User "anonymous" is not allowed to access to "" with roles [ROLE_GITHUB]', $ex->getMessage());
264
        }
265
    }
266
267
    /**
268
     * Tests the notify() method.
269
     *
270
     * @return void
271
     */
272
    public function testNotify(): void {
273
274
        // Set a Notification mock.
275
        $notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
276
277
        // Set the Event dispatcher mock.
278
        $this->eventDispatcher->expects($this->any())->method("hasListeners")->willReturn(true);
279
        $this->eventDispatcher->expects($this->any())->method("dispatch")->willReturnCallback(AbstractTestCase::getEventDispatcherDispatchFunction());
280
281
        $obj = new TestAbstractController();
282
        $obj->setContainer($this->containerBuilder);
283
284
        $res = $obj->notify("eventName", $notification);
285
        $this->assertNotNull($res);
286
287
        $this->assertInstanceOf(NotificationEvent::class, $res);
288
        $this->assertEquals("eventName", $res->getEventName());
289
        $this->assertSame($notification, $res->getNotification());
290
    }
291
292
    /**
293
     * Tests the notify() method.
294
     *
295
     * @return void
296
     */
297
    public function testNotifyWithoutListener(): void {
298
299
        // Set a Notification mock.
300
        $notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
301
302
        // Set the Event dispatcher mock.
303
        $this->eventDispatcher->expects($this->any())->method("hasListeners")->willReturn(false);
304
305
        $obj = new TestAbstractController();
306
        $obj->setContainer($this->containerBuilder);
307
308
        $res = $obj->notify("eventName", $notification);
309
        $this->assertNull($res);
310
    }
311
312
    /**
313
     * Tests the toast() method.
314
     *
315
     * @return void
316
     */
317
    public function testToast(): void {
318
319
        // Set a Toast mock.
320
        $toast = $this->getMockBuilder(ToastInterface::class)->getMock();
321
322
        // Set the Event dispatcher mock.
323
        $this->eventDispatcher->expects($this->any())->method("hasListeners")->willReturn(true);
324
        $this->eventDispatcher->expects($this->any())->method("dispatch")->willReturnCallback(AbstractTestCase::getEventDispatcherDispatchFunction());
325
326
        $obj = new TestAbstractController();
327
        $obj->setContainer($this->containerBuilder);
328
329
        $res = $obj->toast("eventName", $toast);
330
        $this->assertNotNull($res);
331
332
        $this->assertInstanceOf(ToastEvent::class, $res);
333
        $this->assertEquals("eventName", $res->getEventName());
334
        $this->assertSame($toast, $res->getToast());
335
    }
336
337
    /**
338
     * Tests the toast() method.
339
     *
340
     * @return void
341
     */
342
    public function testToastWithoutListener(): void {
343
344
        // Set a Toast mock.
345
        $toast = $this->getMockBuilder(ToastInterface::class)->getMock();
346
347
        // Set the Event dispatcher mock.
348
        $this->eventDispatcher->expects($this->any())->method("hasListeners")->willReturn(false);
349
350
        $obj = new TestAbstractController();
351
        $obj->setContainer($this->containerBuilder);
352
353
        $res = $obj->toast("eventName", $toast);
354
        $this->assertNull($res);
355
    }
356
357
    /**
358
     * Tests the translate() method.
359
     *
360
     * @return void
361
     */
362
    public function testTranslate(): void {
363
364
        $obj = new TestAbstractController();
365
        $obj->setContainer($this->containerBuilder);
366
367
        $this->assertEquals("id", $obj->translate("id"));
368
    }
369
}
370