Completed
Push — refonte ( e91f8a...52808a )
by Arnaud
02:48
created

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