Completed
Push — 3.x ( 24debf...05b755 )
by Grégoire
03:31
created

testGetMenuProviderWithAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Menu\Provider;
15
16
use Knp\Menu\FactoryInterface;
17
use Knp\Menu\Integration\Symfony\RoutingExtension;
18
use Knp\Menu\ItemInterface;
19
use Knp\Menu\MenuFactory;
20
use Knp\Menu\MenuItem;
21
use Knp\Menu\Provider\MenuProviderInterface;
22
use PHPUnit\Framework\MockObject\MockObject;
23
use PHPUnit\Framework\TestCase;
24
use Sonata\AdminBundle\Admin\AbstractAdmin;
25
use Sonata\AdminBundle\Admin\Pool;
26
use Sonata\AdminBundle\Menu\Provider\GroupMenuProvider;
27
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
28
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
29
30
class GroupMenuProviderTest extends TestCase
31
{
32
    /**
33
     * @var MockObject|Pool
34
     */
35
    private $pool;
36
    /**
37
     * @var MockObject|MenuProviderInterface
38
     */
39
    private $provider;
40
    /**
41
     * @var MockObject|FactoryInterface
42
     */
43
    private $factory;
44
45
    /**
46
     * @var MockObject
47
     */
48
    private $checker;
49
50
    protected function setUp(): void
51
    {
52
        $this->pool = $this->getMockBuilder(Pool::class)->disableOriginalConstructor()->getMock();
53
        $this->checker = $this
54
            ->getMockBuilder(AuthorizationCheckerInterface::class)
55
            ->setMethods(['isGranted'])
56
            ->disableOriginalConstructor()
57
            ->getMock();
58
59
        $this->factory = new MenuFactory();
60
61
        $urlGenerator = $this->createStub(UrlGeneratorInterface::class);
62
        $urlGenerator->method('generate')->willReturnCallback(static function (string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string {
63
            switch ($referenceType) {
64
                case UrlGeneratorInterface::ABSOLUTE_URL:
65
                    return 'http://sonata-project/'.$name.($parameters ? '?'.http_build_query($parameters) : '');
66
                case UrlGeneratorInterface::ABSOLUTE_PATH:
67
                    return '/'.$name.($parameters ? '?'.http_build_query($parameters) : '');
68
                default:
69
                    throw new \InvalidArgumentException(sprintf('Dummy router does not support the reference type "%s".', $referenceType));
70
            }
71
        });
72
73
        $this->factory->addExtension(new RoutingExtension($urlGenerator));
74
75
        $this->provider = new GroupMenuProvider($this->factory, $this->pool, $this->checker);
76
    }
77
78
    public function testGroupMenuProviderName(): void
79
    {
80
        $this->assertTrue($this->provider->has('sonata_group_menu'));
81
    }
82
83
    /**
84
     * NEXT_MAJOR: Remove this test.
85
     *
86
     * @group legacy
87
     * @dataProvider getAdminGroups
88
     */
89
    public function testGroupMenuProviderWithoutChecker(array $adminGroups): void
90
    {
91
        $provider = new GroupMenuProvider($this->factory, $this->pool);
92
93
        $this->pool
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
            ->method('getInstance')
95
            ->with($this->equalTo('sonata_admin_foo_service'))
96
            ->willReturn($this->getAdminMock());
97
98
        $menu = $provider->get(
99
            'providerFoo',
100
            [
101
                'name' => 'foo',
102
                'group' => $adminGroups,
103
            ]
104
        );
105
106
        $this->assertInstanceOf(ItemInterface::class, $menu);
107
        $this->assertSame('foo', $menu->getName());
108
109
        $children = $menu->getChildren();
110
111
        $this->assertCount(3, $children);
112
        $this->assertArrayHasKey('foo_admin_label', $children);
113
        $this->assertArrayHasKey('route_label', $children);
114
        $this->assertInstanceOf(MenuItem::class, $menu['foo_admin_label']);
115
        $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
116
117
        $extras = $menu['foo_admin_label']->getExtras();
118
        $this->assertArrayHasKey('label_catalogue', $extras);
119
        $this->assertSame($extras['label_catalogue'], 'SonataAdminBundle');
120
121
        $extras = $menu['route_label']->getExtras();
122
        $this->assertArrayHasKey('label_catalogue', $extras);
123
        $this->assertSame($extras['label_catalogue'], 'SonataAdminBundle');
124
    }
125
126
    /**
127
     * @dataProvider getAdminGroups
128
     */
129
    public function testGetMenuProviderWithCheckerGrantedGroupRoles(array $adminGroups): void
130
    {
131
        $this->pool
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
            ->method('getInstance')
133
            ->with($this->equalTo('sonata_admin_foo_service'))
134
            ->willReturn($this->getAdminMock());
135
136
        $this->checker
137
            ->method('isGranted')
138
            ->willReturn(false);
139
140
        $menu = $this->provider->get(
141
            'providerFoo',
142
            [
143
                'name' => 'foo',
144
                'group' => $adminGroups,
145
            ]
146
        );
147
148
        $this->assertInstanceOf(ItemInterface::class, $menu);
149
        $this->assertSame('foo', $menu->getName());
150
151
        $children = $menu->getChildren();
152
153
        $this->assertCount(1, $children);
154
        $this->assertArrayHasKey('foo_admin_label', $children);
155
        $this->assertArrayNotHasKey('route_label', $children);
156
        $this->assertInstanceOf(MenuItem::class, $menu['foo_admin_label']);
157
        $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
158
159
        $extras = $menu['foo_admin_label']->getExtras();
160
        $this->assertArrayHasKey('label_catalogue', $extras);
161
        $this->assertSame($extras['label_catalogue'], 'SonataAdminBundle');
162
    }
163
164
    public function unanimousGrantCheckerMock(string $role): bool
165
    {
166
        return \in_array($role, ['foo', 'bar', 'baz'], true);
167
    }
168
169
    public function unanimousGrantCheckerNoBazMock(string $role): bool
170
    {
171
        return \in_array($role, ['foo', 'bar'], true);
172
    }
173
174
    /**
175
     * @dataProvider getAdminGroupsMultipleRoles
176
     */
177
    public function testGetMenuProviderWithCheckerGrantedMultipleGroupRoles(
178
        array $adminGroups
179
    ): void {
180
        $this->checker
181
            ->method('isGranted')
182
            ->willReturnCallback([$this, 'unanimousGrantCheckerMock']);
183
184
        $menu = $this->provider->get(
185
            'providerFoo',
186
            [
187
                'name' => 'foo',
188
                'group' => $adminGroups,
189
            ]
190
        );
191
192
        $this->assertInstanceOf(ItemInterface::class, $menu);
193
194
        $children = $menu->getChildren();
195
196
        $this->assertCount(4, $children);
197
    }
198
199
    /**
200
     * @dataProvider getAdminGroupsMultipleRoles
201
     */
202
    public function testGetMenuProviderWithCheckerGrantedGroupAndItemRoles(
203
        array $adminGroups
204
    ): void {
205
        $this->checker
206
            ->method('isGranted')
207
            ->willReturnCallback([$this, 'unanimousGrantCheckerNoBazMock']);
208
209
        $menu = $this->provider->get(
210
            'providerFoo',
211
            [
212
                'name' => 'foo',
213
                'group' => $adminGroups,
214
            ]
215
        );
216
        $isBazItem = $adminGroups['roles'] === ['baz'];
217
218
        $this->assertInstanceOf(ItemInterface::class, $menu);
219
        $this->assertSame(!$isBazItem, $menu->isDisplayed());
220
221
        $children = $menu->getChildren();
222
        $this->assertCount($isBazItem ? 0 : 3, $children);
223
    }
224
225
    /**
226
     * @dataProvider getAdminGroupsMultipleRolesOnTop
227
     */
228
    public function testGetMenuProviderWithCheckerGrantedMultipleGroupRolesOnTop(
229
        array $adminGroups
230
    ): void {
231
        $this->checker
232
            ->method('isGranted')
233
            ->willReturnCallback([$this, 'unanimousGrantCheckerMock']);
234
235
        $menu = $this->provider->get(
236
            'providerFoo',
237
            [
238
                'name' => 'foo',
239
                'group' => $adminGroups,
240
            ]
241
        );
242
        $this->assertInstanceOf(ItemInterface::class, $menu);
243
244
        $this->assertTrue($menu->isDisplayed());
245
    }
246
247
    /**
248
     * @dataProvider getAdminGroups
249
     */
250
    public function testGetMenuProviderWithAdmin(array $adminGroups): void
251
    {
252
        $this->pool
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
253
            ->method('getInstance')
254
            ->with($this->equalTo('sonata_admin_foo_service'))
255
            ->willReturn($this->getAdminMock());
256
257
        $this->checker
258
            ->method('isGranted')
259
            ->willReturn(true);
260
261
        $menu = $this->provider->get(
262
            'providerFoo',
263
            [
264
                'name' => 'foo',
265
                'group' => $adminGroups,
266
            ]
267
        );
268
269
        $this->assertInstanceOf(ItemInterface::class, $menu);
270
        $this->assertSame('foo', $menu->getName());
271
272
        $children = $menu->getChildren();
273
274
        $this->assertCount(3, $children);
275
        $this->assertArrayHasKey('foo_admin_label', $children);
276
        $this->assertArrayHasKey('route_label', $children);
277
        $this->assertInstanceOf(MenuItem::class, $menu['foo_admin_label']);
278
        $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
279
280
        $extras = $menu['foo_admin_label']->getExtras();
281
        $this->assertArrayHasKey('label_catalogue', $extras);
282
        $this->assertSame('SonataAdminBundle', $extras['label_catalogue']);
283
284
        $extras = $menu['route_label']->getExtras();
285
        $this->assertArrayHasKey('label_catalogue', $extras);
286
        $this->assertSame('SonataAdminBundle', $extras['label_catalogue']);
287
288
        $this->assertSame('http://sonata-project/FooRoute?foo=bar', $menu['route_label']->getUri());
289
        $this->assertSame('/FooRelativeRoute?baz=qux', $menu['relative_route']->getUri());
290
    }
291
292
    /**
293
     * @dataProvider getAdminGroups
294
     */
295
    public function testGetKnpMenuWithListRoute(array $adminGroups): void
296
    {
297
        $this->pool
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
298
            ->method('getInstance')
299
            ->with($this->equalTo('sonata_admin_foo_service'))
300
            ->willReturn($this->getAdminMock(false));
301
302
        $this->checker
303
            ->method('isGranted')
304
            ->willReturn(true);
305
306
        $menu = $this->provider->get(
307
            'providerFoo',
308
            [
309
                'name' => 'foo',
310
                'group' => $adminGroups,
311
            ]
312
        );
313
314
        $this->assertInstanceOf(ItemInterface::class, $menu);
315
        $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
316
        $this->assertArrayHasKey('route_label', $menu->getChildren());
317
        $this->assertCount(2, $menu->getChildren());
318
    }
319
320
    /**
321
     * @dataProvider getAdminGroups
322
     */
323
    public function testGetKnpMenuWithGrantedList(array $adminGroups): void
324
    {
325
        $this->pool
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
326
            ->method('getInstance')
327
            ->with($this->equalTo('sonata_admin_foo_service'))
328
            ->willReturn($this->getAdminMock(true, false));
329
330
        $this->checker
331
            ->method('isGranted')
332
            ->willReturn(true);
333
334
        $menu = $this->provider->get(
335
            'providerFoo',
336
            [
337
                'name' => 'foo',
338
                'group' => $adminGroups,
339
            ]
340
        );
341
342
        $this->assertInstanceOf(ItemInterface::class, $menu);
343
        $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
344
        $this->assertArrayHasKey('route_label', $menu->getChildren());
345
        $this->assertCount(2, $menu->getChildren());
346
    }
347
348
    /**
349
     * @dataProvider getAdminGroupsWithOnTopOption
350
     */
351
    public function testGetMenuProviderOnTopOptions(array $adminGroupsOnTopOption): void
352
    {
353
        $this->pool
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
354
            ->method('getInstance')
355
            ->with($this->equalTo('sonata_admin_foo_service'))
356
            ->willReturn($this->getAdminMock(true, false));
357
358
        $menu = $this->provider->get(
359
            'providerFoo',
360
            [
361
                'name' => 'foo',
362
                'group' => $adminGroupsOnTopOption,
363
            ]
364
        );
365
366
        $this->assertInstanceOf(ItemInterface::class, $menu);
367
        $this->assertCount(0, $menu->getChildren());
368
    }
369
370
    /**
371
     * @dataProvider getAdminGroups
372
     */
373
    public function testGetMenuProviderKeepOpenOption(array $adminGroups): void
374
    {
375
        $this->pool
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
376
            ->method('getInstance')
377
            ->with($this->equalTo('sonata_admin_foo_service'))
378
            ->willReturn($this->getAdminMock());
379
380
        $this->checker
381
            ->method('isGranted')
382
            ->willReturn(true);
383
384
        $adminGroups['keep_open'] = true;
385
386
        $menu = $this->provider->get(
387
            'providerFoo',
388
            [
389
                'name' => 'foo',
390
                'group' => $adminGroups,
391
            ]
392
        );
393
394
        $this->assertInstanceOf(ItemInterface::class, $menu);
395
        $this->assertSame('keep-open', $menu->getAttribute('class'));
396
        $this->assertTrue($menu->getExtra('keep_open'));
397
    }
398
399
    /**
400
     * @dataProvider getRootMenuItemWithDifferentUrlTypes
401
     */
402
    public function testRootMenuItemUrl(string $expectedUrl, array $item): void
403
    {
404
        $this->pool
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
405
            ->method('getInstance')
406
            ->with($this->equalTo('sonata_admin_absolute_url'))
407
            ->willReturn($this->getAdminMock());
408
409
        $this->checker
410
            ->method('isGranted')
411
            ->willReturn(true);
412
413
        $menu = $this->provider->get(
414
            'providerFoo',
415
            [
416
                'name' => 'foo',
417
                'group' => $item,
418
            ]
419
        );
420
421
        $this->assertInstanceOf(ItemInterface::class, $menu);
422
        $this->assertSame('foo', $menu->getName());
423
        $this->assertSame($expectedUrl, $menu['foo_admin_label']->getUri());
424
    }
425
426
    public function getAdminGroups(): array
427
    {
428
        return [
429
            [
430
                'bar' => [
431
                    'label' => 'foo',
432
                    'icon' => '<i class="fa fa-edit"></i>',
433
                    'label_catalogue' => 'SonataAdminBundle',
434
                    'items' => [
435
                        [
436
                            'admin' => 'sonata_admin_foo_service',
437
                            'label' => 'fooLabel',
438
                            'route' => 'FooServiceRoute',
439
                            'route_absolute' => true,
440
                        ],
441
                        [
442
                            'admin' => '',
443
                            'label' => 'route_label',
444
                            'route' => 'FooRoute',
445
                            'route_params' => ['foo' => 'bar'],
446
                            'route_absolute' => true,
447
                            'roles' => [],
448
                        ],
449
                        [
450
                            'admin' => '',
451
                            'label' => 'relative_route',
452
                            'route' => 'FooRelativeRoute',
453
                            'route_params' => ['baz' => 'qux'],
454
                            'route_absolute' => false,
455
                            'roles' => [],
456
                        ],
457
                    ],
458
                    'item_adds' => [],
459
                    'roles' => ['foo'],
460
                ],
461
            ],
462
        ];
463
    }
464
465
    public function getAdminGroupsMultipleRoles(): array
466
    {
467
        return [
468
            [
469
                // group for all roles, children with different roles
470
                [
471
                    'label' => 'foo',
472
                    'icon' => '<i class="fa fa-edit"></i>',
473
                    'label_catalogue' => 'SonataAdminBundle',
474
                    'items' => [
475
                        [
476
                            'admin' => '',
477
                            'label' => 'route_label1',
478
                            'route' => 'FooRoute1',
479
                            'route_params' => ['foo' => 'bar'],
480
                            'route_absolute' => true,
481
                            'roles' => ['foo', 'bar'],
482
                        ],
483
                        [
484
                            'admin' => '',
485
                            'label' => 'route_label2',
486
                            'route' => 'FooRoute2',
487
                            'route_params' => ['foo' => 'bar'],
488
                            'route_absolute' => true,
489
                            'roles' => ['foo'],
490
                        ],
491
                        [
492
                            'admin' => '',
493
                            'label' => 'route_label3',
494
                            'route' => 'FooRoute3',
495
                            'route_params' => ['foo' => 'bar'],
496
                            'route_absolute' => true,
497
                            'roles' => ['bar'],
498
                        ],
499
                        [
500
                            'admin' => '',
501
                            'label' => 'route_label4',
502
                            'route' => 'FooRoute4',
503
                            'route_params' => ['foo' => 'bar'],
504
                            'route_absolute' => true,
505
                            'roles' => ['baz'],
506
                        ],
507
                    ],
508
                    'roles' => ['foo', 'bar'],
509
                    'item_adds' => [],
510
                ],
511
            ], [
512
                // group for one role, children with different roles
513
                [
514
                    'label' => 'foo',
515
                    'icon' => '<i class="fa fa-edit"></i>',
516
                    'label_catalogue' => 'SonataAdminBundle',
517
                    'items' => [
518
                        [
519
                            'admin' => '',
520
                            'label' => 'route_label1',
521
                            'route' => 'FooRoute1',
522
                            'route_params' => ['foo' => 'bar'],
523
                            'route_absolute' => true,
524
                            'roles' => ['foo', 'bar'],
525
                        ],
526
                        [
527
                            'admin' => '',
528
                            'label' => 'route_label2',
529
                            'route' => 'FooRoute2',
530
                            'route_params' => ['foo' => 'bar'],
531
                            'route_absolute' => true,
532
                            'roles' => ['foo'],
533
                        ],
534
                        [
535
                            'admin' => '',
536
                            'label' => 'route_label3',
537
                            'route' => 'FooRoute3',
538
                            'route_params' => ['foo' => 'bar'],
539
                            'route_absolute' => true,
540
                            'roles' => ['bar'],
541
                        ],
542
                        [
543
                            'admin' => '',
544
                            'label' => 'route_label4',
545
                            'route' => 'FooRoute4',
546
                            'route_params' => ['foo' => 'bar'],
547
                            'route_absolute' => true,
548
                            'roles' => ['baz'],
549
                        ],
550
                    ],
551
                    'roles' => ['baz'],
552
                    'item_adds' => [],
553
                ],
554
            ],
555
        ];
556
    }
557
558
    public function getAdminGroupsMultipleRolesOnTop(): array
559
    {
560
        return [
561
            [
562
                [
563
                    'label' => 'foo1',
564
                    'icon' => '<i class="fa fa-edit"></i>',
565
                    'label_catalogue' => 'SonataAdminBundle',
566
                    'items' => [
567
                        [
568
                            'admin' => '',
569
                            'label' => 'route_label1',
570
                            'route' => 'FooRoute1',
571
                            'route_params' => ['foo' => 'bar'],
572
                            'route_absolute' => true,
573
                        ],
574
                    ],
575
                    'item_adds' => [],
576
                    'roles' => ['foo', 'bar'],
577
                    'on_top' => true,
578
                ],
579
            ], [
580
                [
581
                    'label' => 'foo2',
582
                    'icon' => '<i class="fa fa-edit"></i>',
583
                    'label_catalogue' => 'SonataAdminBundle',
584
                    'items' => [
585
                        [
586
                            'admin' => '',
587
                            'label' => 'route_label2',
588
                            'route' => 'FooRoute2',
589
                            'route_params' => ['foo' => 'bar'],
590
                            'route_absolute' => true,
591
                        ],
592
                    ],
593
                    'item_adds' => [],
594
                    'roles' => ['foo'],
595
                    'on_top' => true,
596
                ],
597
            ], [
598
                [
599
                    'label' => 'foo3',
600
                    'icon' => '<i class="fa fa-edit"></i>',
601
                    'label_catalogue' => 'SonataAdminBundle',
602
                    'items' => [
603
                        [
604
                            'admin' => '',
605
                            'label' => 'route_label3',
606
                            'route' => 'FooRoute3',
607
                            'route_params' => ['foo' => 'bar'],
608
                            'route_absolute' => true,
609
                        ],
610
                    ],
611
                    'item_adds' => [],
612
                    'roles' => ['bar'],
613
                    'on_top' => true,
614
                ],
615
            ],
616
        ];
617
    }
618
619
    public function getAdminGroupsWithOnTopOption(): array
620
    {
621
        return [
622
            [
623
                'foo' => [
624
                    'label' => 'foo_on_top',
625
                    'icon' => '<i class="fa fa-edit"></i>',
626
                    'label_catalogue' => 'SonataAdminBundle',
627
                    'on_top' => true,
628
                    'items' => [
629
                        [
630
                            'admin' => 'sonata_admin_foo_service',
631
                            'label' => 'fooLabel',
632
                            'route_absolute' => true,
633
                            'route_params' => [],
634
                        ],
635
                    ],
636
                    'item_adds' => [],
637
                    'roles' => [],
638
                ],
639
            ],
640
        ];
641
    }
642
643
    public function getRootMenuItemWithDifferentUrlTypes(): iterable
644
    {
645
        yield 'absolute_url' => [
646
            'http://sonata-project/list',
647
            [
648
                'label' => 'foo',
649
                'icon' => '<i class="fa fa-edit"></i>',
650
                'label_catalogue' => 'SonataAdminBundle',
651
                'items' => [
652
                    [
653
                        'admin' => 'sonata_admin_absolute_url',
654
                        'label' => 'fooLabel',
655
                        'route' => 'FooAbsoulteRoute',
656
                        'route_absolute' => true,
657
                    ],
658
                ],
659
                'item_adds' => [],
660
                'roles' => ['foo'],
661
            ],
662
        ];
663
664
        yield 'absolute_path' => [
665
            '/list',
666
            [
667
                'label' => 'foo',
668
                'icon' => '<i class="fa fa-edit"></i>',
669
                'label_catalogue' => 'SonataAdminBundle',
670
                'items' => [
671
                    [
672
                        'admin' => 'sonata_admin_absolute_url',
673
                        'label' => 'fooLabel',
674
                        'route' => 'FooAbsolutePath',
675
                        'route_absolute' => false,
676
                    ],
677
                ],
678
                'item_adds' => [],
679
                'roles' => ['foo'],
680
            ],
681
        ];
682
    }
683
684
    private function getAdminMock(bool $hasRoute = true, bool $isGranted = true): AbstractAdmin
685
    {
686
        $admin = $this->createMock(AbstractAdmin::class);
687
        $admin->expects($this->once())
688
            ->method('hasRoute')
689
            ->with($this->equalTo('list'))
690
            ->willReturn($hasRoute);
691
692
        $admin
693
            ->method('hasAccess')
694
            ->with($this->equalTo('list'))
695
            ->willReturn($isGranted);
696
697
        $admin
698
            ->method('getLabel')
699
            ->willReturn('foo_admin_label');
700
701
        $admin
702
            ->method('generateMenuUrl')
703
            ->willReturnCallback(static function (string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): array {
704
                if (!\in_array($referenceType, [UrlGeneratorInterface::ABSOLUTE_URL, UrlGeneratorInterface::ABSOLUTE_PATH], true)) {
705
                    throw new \InvalidArgumentException(sprintf('Dummy router does not support the reference type "%s".', $referenceType));
706
                }
707
708
                return [
709
                    'route' => $name,
710
                    'routeParameters' => $parameters,
711
                    'routeAbsolute' => UrlGeneratorInterface::ABSOLUTE_URL === $referenceType,
712
                ];
713
            });
714
715
        $admin
716
            ->method('getTranslationDomain')
717
            ->willReturn('SonataAdminBundle');
718
719
        return $admin;
720
    }
721
}
722