Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

AdminBundle/Tests/Helper/Menu/MenuBuilderTest.php (1 issue)

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
namespace Kunstmaan\AdminBundle\Tests\Helper\Menu;
4
5
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface;
6
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder;
7
use Kunstmaan\AdminBundle\Helper\Menu\MenuItem;
8
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
13
/**
14
 * Class MenuBuilderTest
15
 */
16
class MenuBuilderTest extends TestCase
17
{
18
    /**
19
     * @return \PHPUnit\Framework\MockObject\MockObject|MenuBuilder
20
     */
21
    public function setUpMenuBuilderMock(?array $methods)
22
    {
23
        $menuBuilderMock = $this->getMockBuilder(MenuBuilder::class)
24
            ->setConstructorArgs([$this->createMock(RequestStack::class)])
25
            ->setMethods($methods)
26
            ->getMock()
27
        ;
28
29
        return $menuBuilderMock;
30
    }
31
32
    /**
33
     * @group legacy
34
     * @expectedDeprecation Passing the container as the first argument of "Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder" is deprecated in KunstmaanAdminBundle 5.4 and will be removed in KunstmaanAdminBundle 6.0. Inject the "request_stack" service instead.
35
     */
36
    public function testConstructorContainerDeprecation()
37
    {
38
        $container = $this->createMock(ContainerInterface::class);
39
        new MenuBuilder($container);
0 ignored issues
show
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...oundation\RequestStack>.

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...
40
    }
41
42
    public function testGetChildrenAndTopChildren()
43
    {
44
        $menuBuilderMock = $this->setUpMenuBuilderMock(null);
45
46
        /** @var MenuAdaptorInterface $menuAdaptorInterfaceMock */
47
        $menuAdaptorInterfaceMock = $this->createMock(MenuAdaptorInterface::class);
48
        $menuAdaptorInterfaceMock
49
            ->expects($this->exactly(2))
50
            ->method('adaptChildren')
51
        ;
52
53
        $menuBuilderMock->addAdaptMenu($menuAdaptorInterfaceMock);
54
55
        $menuItemMock = $this->createMock(MenuItem::class);
56
        $this->assertIsArray($menuBuilderMock->getChildren($menuItemMock));
57
        $this->assertIsArray($menuBuilderMock->getChildren());
58
    }
59
60
    public function testGetCurrentAndBreadCrumb()
61
    {
62
        $menuItemMock = $this->createMock(MenuItem::class);
63
        $menuItemMock
64
            ->expects($this->any())
65
            ->method('getActive')
66
            ->willReturn(true)
67
        ;
68
69
        $menuBuilderMock = $this->setUpMenuBuilderMock(['getChildren']);
70
        $menuBuilderMock
71
            ->expects($this->any())
72
            ->method('getChildren')
73
            ->will($this->onConsecutiveCalls([$menuItemMock], []))
74
        ;
75
76
        $current = $menuBuilderMock->getCurrent();
77
        $this->assertInstanceOf(MenuItem::class, $current);
78
        $this->assertContainsOnlyInstancesOf(MenuItem::class, $menuBuilderMock->getBreadCrumb());
79
    }
80
81
    public function testGetLowestTopChildWithCurrentTopMenuItem()
82
    {
83
        $menuItemMock = $this->createMock(TopMenuItem::class);
84
        $menuItemMock
85
            ->expects($this->any())
86
            ->method('getActive')
87
            ->willReturn(true)
88
        ;
89
90
        $menuBuilderMock = $this->setUpMenuBuilderMock(['getChildren']);
91
        $menuBuilderMock
92
            ->expects($this->any())
93
            ->method('getChildren')
94
            ->will($this->onConsecutiveCalls([$menuItemMock], []))
95
        ;
96
97
        $this->assertInstanceOf(TopMenuItem::class, $menuBuilderMock->getLowestTopChild());
98
    }
99
100
    public function testGetLowestTopChildWithMenuItem()
101
    {
102
        $menuItemMock = $this->createMock(MenuItem::class);
103
        $menuItemMock
104
            ->expects($this->any())
105
            ->method('getActive')
106
            ->willReturn(true)
107
        ;
108
        $menuItemMock
109
            ->expects($this->once())
110
            ->method('getParent')
111
            ->willReturn($this->createMock(TopMenuItem::class))
112
        ;
113
114
        $menuBuilderMock = $this->setUpMenuBuilderMock(['getChildren']);
115
        $menuBuilderMock
116
            ->expects($this->any())
117
            ->method('getChildren')
118
            ->will($this->onConsecutiveCalls([$menuItemMock], []))
119
        ;
120
121
        $this->assertInstanceOf(TopMenuItem::class, $menuBuilderMock->getLowestTopChild());
122
    }
123
124
    public function testGetLowestTopChildWithCurrentNull()
125
    {
126
        $menuItemMock = $this->createMock(TopMenuItem::class);
127
        $menuItemMock
128
            ->expects($this->any())
129
            ->method('getActive')
130
            ->willReturn(false)
131
        ;
132
133
        $menuBuilderMock = $this->setUpMenuBuilderMock(['getChildren']);
134
        $menuBuilderMock
135
            ->expects($this->any())
136
            ->method('getChildren')
137
            ->will($this->onConsecutiveCalls([$menuItemMock], []))
138
        ;
139
140
        $this->assertNull($menuBuilderMock->getLowestTopChild());
141
    }
142
143
    public function testGetTopChildren()
144
    {
145
        /** @var MenuAdaptorInterface $menuAdaptorInterfaceMock */
146
        $menuAdaptorInterfaceMock = $this->createMock(MenuAdaptorInterface::class);
147
        $menuAdaptorInterfaceMock
148
            ->expects($this->once())
149
            ->method('adaptChildren')
150
        ;
151
152
        $menuBuilderMock = $this->setUpMenuBuilderMock(null);
153
        $menuBuilderMock->addAdaptMenu($menuAdaptorInterfaceMock);
154
155
        $this->assertIsArray($menuBuilderMock->getTopChildren());
156
    }
157
}
158