Completed
Pull Request — master (#6093)
by Mathieu
02:42
created

MenuBuilderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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;
15
16
use Knp\Menu\ItemInterface;
17
use Knp\Menu\MenuFactory;
18
use Knp\Menu\MenuItem;
19
use Knp\Menu\Provider\MenuProviderInterface;
20
use PHPUnit\Framework\TestCase;
21
use Sonata\AdminBundle\Admin\Pool;
22
use Sonata\AdminBundle\Event\ConfigureMenuEvent;
23
use Sonata\AdminBundle\Menu\MenuBuilder;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
26
class MenuBuilderTest extends TestCase
27
{
28
    private $pool;
29
    private $provider;
30
    private $factory;
31
    private $eventDispatcher;
32
    /**
33
     * @var MenuBuilder
34
     */
35
    private $builder;
36
37
    protected function setUp(): void
38
    {
39
        $this->pool = $this->getMockBuilder(Pool::class)->disableOriginalConstructor()->getMock();
40
        $this->provider = $this->getMockForAbstractClass(MenuProviderInterface::class);
41
        $this->factory = new MenuFactory();
42
        $this->eventDispatcher = $this->getMockForAbstractClass(EventDispatcherInterface::class);
43
44
        $this->builder = new MenuBuilder($this->pool, $this->factory, $this->provider, $this->eventDispatcher);
45
    }
46
47
    public function testGetKnpMenuWithDefaultProvider(): void
48
    {
49
        $adminGroups = [
50
            'bar' => [
51
                'icon' => '<i class="fa fa-edit"></i>',
52
                'label_catalogue' => '',
53
                'roles' => [],
54
            ],
55
        ];
56
57
        $this->provider
58
            ->expects($this->once())
59
            ->method('get')
60
            ->with('sonata_group_menu')
61
            ->willReturn($this->factory->createItem('bar')->addChild('foo')->getParent());
62
63
        $this->preparePool($adminGroups);
64
        $menu = $this->builder->createSidebarMenu();
65
66
        $this->assertInstanceOf(ItemInterface::class, $menu);
67
        $this->assertArrayHasKey('bar', $menu->getChildren());
68
69
        foreach ($menu->getChildren() as $key => $child) {
70
            $this->assertInstanceOf(MenuItem::class, $child);
71
            $this->assertSame('bar', $child->getName());
72
            $this->assertSame('bar', $child->getLabel());
73
74
            // menu items
75
            $children = $child->getChildren();
76
            $this->assertCount(1, $children);
77
            $this->assertArrayHasKey('foo', $children);
78
            $this->assertInstanceOf(MenuItem::class, $child['foo']);
79
            $this->assertSame('foo', $child['foo']->getLabel());
80
        }
81
    }
82
83
    public function testGetKnpMenuWithSpecifiedProvider(): void
84
    {
85
        $adminGroups = [
86
            'bar' => [
87
                'provider' => 'my_menu',
88
                'label_catalogue' => '',
89
                'icon' => '<i class="fa fa-edit"></i>',
90
                'roles' => [],
91
            ],
92
        ];
93
94
        $this->provider
95
            ->expects($this->once())
96
            ->method('get')
97
            ->with('my_menu')
98
            ->willReturn($this->factory->createItem('bar')->addChild('foo')->getParent());
99
100
        $this->preparePool($adminGroups);
101
        $menu = $this->builder->createSidebarMenu();
102
103
        $this->assertInstanceOf(ItemInterface::class, $menu);
104
        $this->assertArrayHasKey('bar', $menu->getChildren());
105
106
        foreach ($menu->getChildren() as $key => $child) {
107
            $this->assertInstanceOf(MenuItem::class, $child);
108
            $this->assertSame('bar', $child->getName());
109
            $this->assertSame('bar', $child->getLabel());
110
111
            // menu items
112
            $children = $child->getChildren();
113
            $this->assertCount(1, $children);
114
            $this->assertArrayHasKey('foo', $children);
115
            $this->assertInstanceOf(MenuItem::class, $child['foo']);
116
            $this->assertSame('foo', $child['foo']->getLabel());
117
        }
118
    }
119
120
    public function testGetKnpMenuAndDispatchEvent(): void
121
    {
122
        $adminGroups = [
123
            'bar' => [
124
                'label' => 'foo',
125
                'icon' => '<i class="fa fa-edit"></i>',
126
                'label_catalogue' => 'SonataAdminBundle',
127
                'items' => [],
128
                'item_adds' => [],
129
                'roles' => [],
130
            ],
131
        ];
132
133
        $this->preparePool($adminGroups);
134
135
        $this->eventDispatcher
136
            ->expects($this->once())
137
            ->method('dispatch')
138
            ->with(
139
                $this->isInstanceOf(ConfigureMenuEvent::class),
140
            );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
141
142
        $this->provider
143
            ->expects($this->once())
144
            ->method('get')
145
            ->with('sonata_group_menu')
146
            ->willReturn($this->factory->createItem('bar'));
147
148
        $this->builder->createSidebarMenu();
149
    }
150
151
    private function preparePool(array $adminGroups, ?AdminInterface $admin = null): void
152
    {
153
        $this->pool->expects($this->once())
154
            ->method('getAdminGroups')
155
            ->willReturn($adminGroups);
156
157
        if (null !== $admin) {
158
            $this->pool->expects($this->once())
159
                ->method('getInstance')
160
                ->with($this->equalTo('sonata_admin_foo_service'))
161
                ->willReturn($admin);
162
        }
163
    }
164
}
165