Completed
Push — master ( 3fd2dd...aba624 )
by Grégoire
04:19
created

tests/Menu/MenuBuilderTest.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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);
0 ignored issues
show
$this->pool is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundle\Admin\Pool>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
$this->provider is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Knp\Menu\Provider\MenuProviderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
$this->eventDispatcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...entDispatcherInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
            ->will($this->returnValue($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);
0 ignored issues
show
$children is of type array<integer,object<Knp\Menu\ItemInterface>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
            ->will($this->returnValue($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);
0 ignored issues
show
$children is of type array<integer,object<Knp\Menu\ItemInterface>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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->equalTo('sonata.admin.event.configure.menu.sidebar'),
140
                $this->isInstanceOf(ConfigureMenuEvent::class)
141
            );
142
143
        $this->builder->createSidebarMenu();
144
    }
145
146
    private function preparePool(array $adminGroups, ?AdminInterface $admin = null): void
147
    {
148
        $this->pool->expects($this->once())
149
            ->method('getAdminGroups')
150
            ->will($this->returnValue($adminGroups));
151
152
        if (null !== $admin) {
153
            $this->pool->expects($this->once())
154
                ->method('getInstance')
155
                ->with($this->equalTo('sonata_admin_foo_service'))
156
                ->will($this->returnValue($admin));
157
        }
158
    }
159
}
160