Completed
Push — refonte ( 7173e3...bffa4c )
by Arnaud
02:33
created

AdminSubscriberTest::testLoadEntitiesWithUniqueStrategy()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 8.6763
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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