Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Tests/unit/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
     * @param array|null $methods
20
     *
21
     * @return \PHPUnit\Framework\MockObject\MockObject|MenuBuilder
22
     */
23
    public function setUpMenuBuilderMock(?array $methods)
24
    {
25
        $menuBuilderMock = $this->getMockBuilder(MenuBuilder::class)
26
            ->setConstructorArgs([$this->createMock(RequestStack::class)])
27
            ->setMethods($methods)
28
            ->getMock()
29
        ;
30
31
        return $menuBuilderMock;
32
    }
33
34
    /**
35
     * @group legacy
36
     * @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.
37
     */
38
    public function testConstructorContainerDeprecation()
39
    {
40
        $container = $this->createMock(ContainerInterface::class);
41
        new MenuBuilder($container);
42
    }
43
44
    public function testGetChildrenAndTopChildren()
45
    {
46
        $menuBuilderMock = $this->setUpMenuBuilderMock(null);
47
48
        /** @var MenuAdaptorInterface $menuAdaptorInterfaceMock */
49
        $menuAdaptorInterfaceMock = $this->createMock(MenuAdaptorInterface::class);
50
        $menuAdaptorInterfaceMock
51
            ->expects($this->exactly(2))
52
            ->method('adaptChildren')
53
        ;
54
55
        $menuBuilderMock->addAdaptMenu($menuAdaptorInterfaceMock);
56
57
        $menuItemMock = $this->createMock(MenuItem::class);
58
        $this->assertIsArray($menuBuilderMock->getChildren($menuItemMock));
0 ignored issues
show
$menuItemMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Kunstmaan\Ad...e\Helper\Menu\MenuItem>.

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