Completed
Push — refonte ( 5fcd2e...480e31 )
by Arnaud
02:16
created

AdminSubscriberTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 618
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 618
rs 9.982
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetSubscribedEvents() 0 9 1
A testHandleRequest() 0 45 1
A testHandleRequestWithoutRequestParameter() 0 27 1
B testCreateView() 0 99 1
B testLoadEntitiesWithNoneStrategy() 0 73 1
B testLoadEntitiesWithMultipleStrategy() 0 80 1
B testLoadEntitiesWithUniqueStrategy() 0 85 1
B testLoadEntitiesWithoutIdentifier() 0 79 1
B testSaveEntity() 0 83 1
1
<?php
2
3
namespace LAG\AdminBundle\Tests\Event\Subscriber;
4
5
use LAG\AdminBundle\Admin\ActionInterface;
6
use LAG\AdminBundle\Admin\AdminInterface;
7
use LAG\AdminBundle\Configuration\ActionConfiguration;
8
use LAG\AdminBundle\Configuration\AdminConfiguration;
9
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
10
use LAG\AdminBundle\DataProvider\DataProviderInterface;
11
use LAG\AdminBundle\Event\AdminEvent;
12
use LAG\AdminBundle\Event\AdminEvents;
13
use LAG\AdminBundle\Event\EntityEvent;
14
use LAG\AdminBundle\Event\Subscriber\AdminSubscriber;
15
use LAG\AdminBundle\Event\ViewEvent;
16
use LAG\AdminBundle\Exception\Exception;
17
use LAG\AdminBundle\Factory\ActionFactory;
18
use LAG\AdminBundle\Factory\DataProviderFactory;
19
use LAG\AdminBundle\Factory\ViewFactory;
20
use LAG\AdminBundle\LAGAdminBundle;
21
use LAG\AdminBundle\Tests\AdminTestBase;
22
use LAG\AdminBundle\View\ViewInterface;
23
use stdClass;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
27
use Symfony\Component\HttpFoundation\Session\Session;
28
use Symfony\Component\Translation\TranslatorInterface;
29
30
class AdminSubscriberTest extends AdminTestBase
31
{
32
    /**
33
     * Test subscribed events.
34
     */
35
    public function testGetSubscribedEvents()
36
    {
37
        $events = AdminSubscriber::getSubscribedEvents();
38
39
        $this->assertArrayHasKey(AdminEvents::HANDLE_REQUEST, $events);
40
        $this->assertArrayHasKey(AdminEvents::VIEW, $events);
41
        $this->assertArrayHasKey(AdminEvents::ENTITY_LOAD, $events);
42
        $this->assertArrayHasKey(AdminEvents::ENTITY_SAVE, $events);
43
    }
44
45
    /**
46
     * Test the way the subscriber handle a request.
47
     */
48
    public function testHandleRequest()
49
    {
50
        $action = $this->getMockWithoutConstructor(ActionInterface::class);
51
52
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
53
        $actionFactory
54
            ->method('create')
55
            ->with('list', 'panda')
56
            ->willReturn($action)
57
        ;
58
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
59
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
60
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
61
        $session = $this->getMockWithoutConstructor(Session::class);
62
        $translator = $this->getMockWithoutConstructor(TranslatorInterface::class);
63
64
        $subscriber = new AdminSubscriber(
65
            $actionFactory,
66
            $viewFactory,
67
            $dataProviderFactory,
68
            $eventDispatcher,
69
            $session,
70
            $translator
71
        );
72
73
        $request = new Request([], [], [
74
            '_action' => 'list',
75
        ]);
76
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
77
        $admin
78
            ->expects($this->once())
79
            ->method('getName')
80
            ->willReturn('panda')
81
        ;
82
        $admin
83
            ->expects($this->once())
84
            ->method('getConfiguration')
85
            ->willReturn(new AdminConfiguration(new ApplicationConfiguration()))
86
        ;
87
        $event = new AdminEvent($admin, $request);
88
89
        $subscriber->handleRequest($event);
90
91
        $this->assertEquals($action, $event->getAction());
92
    }
93
94
    /**
95
     * Test the way the subscriber handle a request without admin parameters.
96
     */
97
    public function testHandleRequestWithoutRequestParameter()
98
    {
99
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
100
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
101
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
102
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
103
        $session = $this->getMockWithoutConstructor(Session::class);
104
        $translator = $this->getMockWithoutConstructor(TranslatorInterface::class);
105
106
        $subscriber = new AdminSubscriber(
107
            $actionFactory,
108
            $viewFactory,
109
            $dataProviderFactory,
110
            $eventDispatcher,
111
            $session,
112
            $translator
113
        );
114
115
        $request = new Request();
116
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
117
118
        $event = new AdminEvent($admin, $request);
119
120
        $this->assertExceptionRaised(Exception::class, function () use ($subscriber, $event) {
121
            $subscriber->handleRequest($event);
122
        });
123
    }
124
125
    /**
126
     * Test the view creation.
127
     */
128
    public function testCreateView()
129
    {
130
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
131
        $view = $this->getMockWithoutConstructor(ViewInterface::class);
132
133
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
134
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
135
136
        $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class);
137
        $actionConfiguration
138
            ->expects($this->atLeastOnce())
139
            ->method('getParameter')
140
            ->with('menus')
141
            ->willReturn([])
142
        ;
143
144
        $action = $this->getMockWithoutConstructor(ActionInterface::class);
145
        $action
146
            ->expects($this->once())
147
            ->method('getName')
148
            ->willReturn('edit')
149
        ;
150
        $action
151
            ->expects($this->atLeastOnce())
152
            ->method('getConfiguration')
153
            ->willReturn($actionConfiguration)
154
        ;
155
156
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
157
158
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
159
        $admin
160
            ->expects($this->atLeastOnce())
161
            ->method('getAction')
162
            ->willReturn($action)
163
        ;
164
        $admin
165
            ->expects($this->atLeastOnce())
166
            ->method('getName')
167
            ->willReturn('pandas')
168
        ;
169
        $admin
170
            ->expects($this->once())
171
            ->method('getConfiguration')
172
            ->willReturn($adminConfiguration)
173
        ;
174
        $admin
175
            ->expects($this->once())
176
            ->method('getEntities')
177
            ->willReturn([
178
                'entity',
179
            ])
180
        ;
181
        $admin
182
            ->expects($this->once())
183
            ->method('getForms')
184
            ->willReturn([
185
                'form',
186
            ])
187
        ;
188
        $request = new Request();
189
190
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
191
        $viewFactory
192
            ->expects($this->once())
193
            ->method('create')
194
            ->with(
195
                $request,
196
                'edit',
197
                'pandas',
198
                $adminConfiguration,
199
                $actionConfiguration,
200
                [
201
                    'entity',
202
                ],
203
                [
204
                    'form',
205
                ]
206
            )
207
            ->willReturn($view)
208
        ;
209
210
        $session = $this->getMockWithoutConstructor(Session::class);
211
        $translator = $this->getMockWithoutConstructor(TranslatorInterface::class);
212
213
        $subscriber = new AdminSubscriber(
214
            $actionFactory,
215
            $viewFactory,
216
            $dataProviderFactory,
217
            $eventDispatcher,
218
            $session,
219
            $translator
220
        );
221
        $event = new ViewEvent($admin, $request);
222
223
        $subscriber->createView($event);
224
225
        $this->assertEquals($view, $event->getView());
226
    }
227
228
    /**
229
     * Test entity loading with the none strategy.
230
     */
231
    public function testLoadEntitiesWithNoneStrategy()
232
    {
233
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
234
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
235
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
236
237
        $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class);
238
        $actionConfiguration
239
            ->expects($this->atLeastOnce())
240
            ->method('getParameter')
241
            ->willReturnMap([
242
                ['load_strategy', LAGAdminBundle::LOAD_STRATEGY_NONE],
243
            ])
244
        ;
245
246
        $action = $this->getMockWithoutConstructor(ActionInterface::class);
247
        $action
248
            ->expects($this->atLeastOnce())
249
            ->method('getConfiguration')
250
            ->willReturn($actionConfiguration)
251
        ;
252
253
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
254
        $adminConfiguration
255
            ->expects($this->atLeastOnce())
256
            ->method('getParameter')
257
            ->willReturnMap([
258
                ['data_provider', 'my_data_provider'],
259
                ['entity', 'MyClass'],
260
            ])
261
        ;
262
263
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
264
        $admin
265
            ->expects($this->atLeastOnce())
266
            ->method('getAction')
267
            ->willReturn($action)
268
        ;
269
        $admin
270
            ->expects($this->atLeastOnce())
271
            ->method('getConfiguration')
272
            ->willReturn($adminConfiguration)
273
        ;
274
275
        $dataProvider = $this->getMockWithoutConstructor(DataProviderInterface::class);
276
277
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
278
        $dataProviderFactory
279
            ->expects($this->atLeastOnce())
280
            ->method('get')
281
            ->with('my_data_provider')
282
            ->willReturn($dataProvider)
283
        ;
284
285
        $session = $this->getMockWithoutConstructor(Session::class);
286
        $translator = $this->getMockWithoutConstructor(TranslatorInterface::class);
287
288
        $subscriber = new AdminSubscriber(
289
            $actionFactory,
290
            $viewFactory,
291
            $dataProviderFactory,
292
            $eventDispatcher,
293
            $session,
294
            $translator
295
        );
296
297
        $request = new Request();
298
        $event = new EntityEvent($admin, $request);
299
300
        $subscriber->loadEntities($event);
301
302
        $this->assertEquals(null, $event->getEntities());
303
    }
304
305
    /**
306
     * Test entity loading with the multiple strategy.
307
     */
308
    public function testLoadEntitiesWithMultipleStrategy()
309
    {
310
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
311
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
312
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
313
314
        $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class);
315
        $actionConfiguration
316
            ->expects($this->atLeastOnce())
317
            ->method('getParameter')
318
            ->willReturnMap([
319
                ['load_strategy', LAGAdminBundle::LOAD_STRATEGY_MULTIPLE],
320
            ])
321
        ;
322
323
        $action = $this->getMockWithoutConstructor(ActionInterface::class);
324
        $action
325
            ->expects($this->atLeastOnce())
326
            ->method('getConfiguration')
327
            ->willReturn($actionConfiguration)
328
        ;
329
330
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
331
        $adminConfiguration
332
            ->expects($this->atLeastOnce())
333
            ->method('getParameter')
334
            ->willReturnMap([
335
                ['data_provider', 'my_data_provider'],
336
                ['entity', 'MyClass'],
337
            ])
338
        ;
339
340
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
341
        $admin
342
            ->expects($this->atLeastOnce())
343
            ->method('getAction')
344
            ->willReturn($action)
345
        ;
346
        $admin
347
            ->expects($this->atLeastOnce())
348
            ->method('getConfiguration')
349
            ->willReturn($adminConfiguration)
350
        ;
351
352
        $test = new stdClass();
353
354
        $dataProvider = $this->getMockWithoutConstructor(DataProviderInterface::class);
355
        $dataProvider
356
            ->expects($this->atLeastOnce())
357
            ->method('getCollection')
358
            ->with($admin, [])
359
            ->willReturn($test)
360
        ;
361
362
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
363
        $dataProviderFactory
364
            ->expects($this->atLeastOnce())
365
            ->method('get')
366
            ->with('my_data_provider')
367
            ->willReturn($dataProvider)
368
        ;
369
        $session = $this->getMockWithoutConstructor(Session::class);
370
        $translator = $this->getMockWithoutConstructor(TranslatorInterface::class);
371
372
        $subscriber = new AdminSubscriber(
373
            $actionFactory,
374
            $viewFactory,
375
            $dataProviderFactory,
376
            $eventDispatcher,
377
            $session,
378
            $translator
379
        );
380
381
        $request = new Request();
382
        $event = new EntityEvent($admin, $request);
383
384
        $subscriber->loadEntities($event);
385
386
        $this->assertEquals($test, $event->getEntities());
387
    }
388
389
    /**
390
     * Test entity loading with the unique strategy.
391
     */
392
    public function testLoadEntitiesWithUniqueStrategy()
393
    {
394
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
395
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
396
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
397
398
        $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class);
399
        $actionConfiguration
400
            ->expects($this->atLeastOnce())
401
            ->method('getParameter')
402
            ->willReturnMap([
403
                ['load_strategy', LAGAdminBundle::LOAD_STRATEGY_UNIQUE],
404
                ['route_requirements', [
405
                    'id' => '~',
406
                ]],
407
            ])
408
        ;
409
410
        $action = $this->getMockWithoutConstructor(ActionInterface::class);
411
        $action
412
            ->expects($this->atLeastOnce())
413
            ->method('getConfiguration')
414
            ->willReturn($actionConfiguration)
415
        ;
416
417
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
418
        $adminConfiguration
419
            ->expects($this->atLeastOnce())
420
            ->method('getParameter')
421
            ->willReturnMap([
422
                ['data_provider', 'my_data_provider'],
423
                ['entity', 'MyClass'],
424
            ])
425
        ;
426
427
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
428
        $admin
429
            ->expects($this->atLeastOnce())
430
            ->method('getAction')
431
            ->willReturn($action)
432
        ;
433
        $admin
434
            ->expects($this->atLeastOnce())
435
            ->method('getConfiguration')
436
            ->willReturn($adminConfiguration)
437
        ;
438
439
        $test = new stdClass();
440
441
        $dataProvider = $this->getMockWithoutConstructor(DataProviderInterface::class);
442
        $dataProvider
443
            ->expects($this->atLeastOnce())
444
            ->method('getItem')
445
            ->with($admin, 42)
446
            ->willReturn($test)
447
        ;
448
449
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
450
        $dataProviderFactory
451
            ->expects($this->atLeastOnce())
452
            ->method('get')
453
            ->with('my_data_provider')
454
            ->willReturn($dataProvider)
455
        ;
456
        $session = $this->getMockWithoutConstructor(Session::class);
457
        $translator = $this->getMockWithoutConstructor(TranslatorInterface::class);
458
459
        $subscriber = new AdminSubscriber(
460
            $actionFactory,
461
            $viewFactory,
462
            $dataProviderFactory,
463
            $eventDispatcher,
464
            $session,
465
            $translator
466
        );
467
468
        $request = new Request([
469
            'id' => 42,
470
        ]);
471
        $event = new EntityEvent($admin, $request);
472
473
        $subscriber->loadEntities($event);
474
475
        $this->assertEquals($test, $event->getEntities()->first());
476
    }
477
478
    /**
479
     * Test entity loading without identifier parameter.
480
     */
481
    public function testLoadEntitiesWithoutIdentifier()
482
    {
483
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
484
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
485
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
486
487
        $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class);
488
        $actionConfiguration
489
            ->expects($this->atLeastOnce())
490
            ->method('getParameter')
491
            ->willReturnMap([
492
                ['load_strategy', LAGAdminBundle::LOAD_STRATEGY_UNIQUE],
493
                ['route_requirements', [
494
                    'id' => '~',
495
                ]],
496
            ])
497
        ;
498
499
        $action = $this->getMockWithoutConstructor(ActionInterface::class);
500
        $action
501
            ->expects($this->atLeastOnce())
502
            ->method('getConfiguration')
503
            ->willReturn($actionConfiguration)
504
        ;
505
506
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
507
        $adminConfiguration
508
            ->expects($this->atLeastOnce())
509
            ->method('getParameter')
510
            ->willReturnMap([
511
                ['data_provider', 'my_data_provider'],
512
                ['entity', 'MyClass'],
513
            ])
514
        ;
515
516
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
517
        $admin
518
            ->expects($this->atLeastOnce())
519
            ->method('getAction')
520
            ->willReturn($action)
521
        ;
522
        $admin
523
            ->expects($this->atLeastOnce())
524
            ->method('getConfiguration')
525
            ->willReturn($adminConfiguration)
526
        ;
527
528
        $dataProvider = $this->getMockWithoutConstructor(DataProviderInterface::class);
529
        $dataProvider
530
            ->expects($this->never())
531
            ->method('getItem')
532
        ;
533
534
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
535
        $dataProviderFactory
536
            ->expects($this->atLeastOnce())
537
            ->method('get')
538
            ->with('my_data_provider')
539
            ->willReturn($dataProvider)
540
        ;
541
        $session = $this->getMockWithoutConstructor(Session::class);
542
        $translator = $this->getMockWithoutConstructor(TranslatorInterface::class);
543
544
        $subscriber = new AdminSubscriber(
545
            $actionFactory,
546
            $viewFactory,
547
            $dataProviderFactory,
548
            $eventDispatcher,
549
            $session,
550
            $translator
551
        );
552
553
        $request = new Request();
554
        $event = new EntityEvent($admin, $request);
555
556
        $this->assertExceptionRaised(Exception::class, function () use ($subscriber, $event) {
557
            $subscriber->loadEntities($event);
558
        });
559
    }
560
561
    /**
562
     * Test the save item process.
563
     */
564
    public function testSaveEntity()
565
    {
566
        $actionFactory = $this->getMockWithoutConstructor(ActionFactory::class);
567
        $viewFactory = $this->getMockWithoutConstructor(ViewFactory::class);
568
569
        $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class);
570
        $adminConfiguration
571
            ->expects($this->atLeastOnce())
572
            ->method('getParameter')
573
            ->willReturnMap([
574
                ['data_provider', 'my_data_provider'],
575
                ['entity', 'MyClass'],
576
                ['translation_pattern', 'test.{admin}.{key}'],
577
            ])
578
        ;
579
580
        $admin = $this->getMockWithoutConstructor(AdminInterface::class);
581
        $admin
582
            ->expects($this->atLeastOnce())
583
            ->method('getConfiguration')
584
            ->willReturn($adminConfiguration)
585
        ;
586
        $admin
587
            ->expects($this->atLeastOnce())
588
            ->method('getName')
589
            ->willReturn('stefany')
590
        ;
591
592
        $dataProvider = $this->getMockWithoutConstructor(DataProviderInterface::class);
593
        $dataProvider
594
            ->expects($this->atLeastOnce())
595
            ->method('saveItem')
596
            ->with($admin)
597
        ;
598
599
        $dataProviderFactory = $this->getMockWithoutConstructor(DataProviderFactory::class);
600
        $dataProviderFactory
601
            ->expects($this->atLeastOnce())
602
            ->method('get')
603
            ->with('my_data_provider')
604
            ->willReturn($dataProvider)
605
        ;
606
        $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class);
607
608
        $bag = $this->getMockWithoutConstructor(FlashBag::class);
609
        $bag
610
            ->expects($this->atLeastOnce())
611
            ->method('add')
612
            ->with('success', 'Save')
613
        ;
614
615
        $session = $this->getMockWithoutConstructor(Session::class);
616
        $session
617
            ->expects($this->atLeastOnce())
618
            ->method('getFlashBag')
619
            ->willReturn($bag)
620
        ;
621
622
        $translator = $this->getMockWithoutConstructor(TranslatorInterface::class);
623
        $translator
624
            ->expects($this->atLeastOnce())
625
            ->method('trans')
626
            ->with('test.stefany.save_success')
627
            ->willReturn('Save')
628
        ;
629
630
        $subscriber = new AdminSubscriber(
631
            $actionFactory,
632
            $viewFactory,
633
            $dataProviderFactory,
634
            $eventDispatcher,
635
            $session,
636
            $translator
637
        );
638
639
        $request = new Request([], [], [
640
            '_action' => 'list',
641
        ]);
642
643
        $event = new EntityEvent($admin, $request);
644
645
        $subscriber->saveEntity($event);
646
    }
647
}
648