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
|
|
|
* @var ContainerInterface (mock) |
20
|
|
|
*/ |
21
|
|
|
protected $container; |
22
|
|
|
|
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$container = $this->createMock(ContainerInterface::class); |
26
|
|
|
$this->container = $container; |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ContainerInterface $container |
31
|
|
|
* @param array|null $methods |
32
|
|
|
* |
33
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject|MenuBuilder |
34
|
|
|
*/ |
35
|
|
|
public function setUpMenuBuilderMock(ContainerInterface $container, ?array $methods) |
36
|
|
|
{ |
37
|
|
|
$menuBuilderMock = $this->getMockBuilder(MenuBuilder::class) |
38
|
|
|
->setConstructorArgs([$container]) |
39
|
|
|
->setMethods($methods) |
40
|
|
|
->getMock() |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
return $menuBuilderMock; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetChildrenAndTopChildren() |
47
|
|
|
{ |
48
|
|
|
$stack = $this->createMock(RequestStack::class); |
49
|
|
|
|
50
|
|
|
$this->container->expects($this->any())->method('get')->willReturn($stack); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$menuBuilderMock = $this->setUpMenuBuilderMock($this->container, null); |
53
|
|
|
|
54
|
|
|
/** @var MenuAdaptorInterface $menuAdaptorInterfaceMock */ |
55
|
|
|
$menuAdaptorInterfaceMock = $this->createMock(MenuAdaptorInterface::class); |
56
|
|
|
$menuAdaptorInterfaceMock |
|
|
|
|
57
|
|
|
->expects($this->exactly(2)) |
58
|
|
|
->method('adaptChildren') |
59
|
|
|
; |
60
|
|
|
|
61
|
|
|
$menuBuilderMock->addAdaptMenu($menuAdaptorInterfaceMock); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$menuItemMock = $this->createMock(MenuItem::class); |
64
|
|
|
$this->assertIsArray($menuBuilderMock->getChildren($menuItemMock)); |
|
|
|
|
65
|
|
|
$this->assertIsArray($menuBuilderMock->getChildren()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetCurrentAndBreadCrumb() |
69
|
|
|
{ |
70
|
|
|
$menuItemMock = $this->createMock(MenuItem::class); |
71
|
|
|
$menuItemMock |
72
|
|
|
->expects($this->any()) |
73
|
|
|
->method('getActive') |
74
|
|
|
->willReturn(true) |
75
|
|
|
; |
76
|
|
|
|
77
|
|
|
$menuBuilderMock = $this->setUpMenuBuilderMock($this->container, ['getChildren']); |
78
|
|
|
$menuBuilderMock |
|
|
|
|
79
|
|
|
->expects($this->any()) |
80
|
|
|
->method('getChildren') |
81
|
|
|
->will($this->onConsecutiveCalls([$menuItemMock], [])) |
82
|
|
|
; |
83
|
|
|
|
84
|
|
|
$current = $menuBuilderMock->getCurrent(); |
|
|
|
|
85
|
|
|
$this->assertInstanceOf(MenuItem::class, $current); |
86
|
|
|
$this->assertContainsOnlyInstancesOf(MenuItem::class, $menuBuilderMock->getBreadCrumb()); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testGetLowestTopChildWithCurrentTopMenuItem() |
90
|
|
|
{ |
91
|
|
|
$menuItemMock = $this->createMock(TopMenuItem::class); |
92
|
|
|
$menuItemMock |
93
|
|
|
->expects($this->any()) |
94
|
|
|
->method('getActive') |
95
|
|
|
->willReturn(true) |
96
|
|
|
; |
97
|
|
|
|
98
|
|
|
$menuBuilderMock = $this->setUpMenuBuilderMock($this->container, ['getChildren']); |
99
|
|
|
$menuBuilderMock |
|
|
|
|
100
|
|
|
->expects($this->any()) |
101
|
|
|
->method('getChildren') |
102
|
|
|
->will($this->onConsecutiveCalls([$menuItemMock], [])) |
103
|
|
|
; |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf(TopMenuItem::class, $menuBuilderMock->getLowestTopChild()); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetLowestTopChildWithMenuItem() |
109
|
|
|
{ |
110
|
|
|
$menuItemMock = $this->createMock(MenuItem::class); |
111
|
|
|
$menuItemMock |
112
|
|
|
->expects($this->any()) |
113
|
|
|
->method('getActive') |
114
|
|
|
->willReturn(true) |
115
|
|
|
; |
116
|
|
|
$menuItemMock |
117
|
|
|
->expects($this->once()) |
118
|
|
|
->method('getParent') |
119
|
|
|
->willReturn($this->createMock(TopMenuItem::class)) |
120
|
|
|
; |
121
|
|
|
|
122
|
|
|
$menuBuilderMock = $this->setUpMenuBuilderMock($this->container, ['getChildren']); |
123
|
|
|
$menuBuilderMock |
|
|
|
|
124
|
|
|
->expects($this->any()) |
125
|
|
|
->method('getChildren') |
126
|
|
|
->will($this->onConsecutiveCalls([$menuItemMock], [])) |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
$this->assertInstanceOf(TopMenuItem::class, $menuBuilderMock->getLowestTopChild()); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testGetLowestTopChildWithCurrentNull() |
133
|
|
|
{ |
134
|
|
|
$menuItemMock = $this->createMock(TopMenuItem::class); |
135
|
|
|
$menuItemMock |
136
|
|
|
->expects($this->any()) |
137
|
|
|
->method('getActive') |
138
|
|
|
->willReturn(false) |
139
|
|
|
; |
140
|
|
|
|
141
|
|
|
$menuBuilderMock = $this->setUpMenuBuilderMock($this->container, ['getChildren']); |
142
|
|
|
$menuBuilderMock |
|
|
|
|
143
|
|
|
->expects($this->any()) |
144
|
|
|
->method('getChildren') |
145
|
|
|
->will($this->onConsecutiveCalls([$menuItemMock], [])) |
146
|
|
|
; |
147
|
|
|
|
148
|
|
|
$this->assertNull($menuBuilderMock->getLowestTopChild()); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testGetTopChildren() |
152
|
|
|
{ |
153
|
|
|
$stack = $this->createMock(RequestStack::class); |
154
|
|
|
|
155
|
|
|
$this->container->expects($this->any())->method('get')->willReturn($stack); |
|
|
|
|
156
|
|
|
|
157
|
|
|
$menuBuilderMock = $this->setUpMenuBuilderMock($this->container, null); |
|
|
|
|
158
|
|
|
|
159
|
|
|
/** @var MenuAdaptorInterface $menuAdaptorInterfaceMock */ |
160
|
|
|
$menuAdaptorInterfaceMock = $this->createMock(MenuAdaptorInterface::class); |
161
|
|
|
$menuAdaptorInterfaceMock |
|
|
|
|
162
|
|
|
->expects($this->once()) |
163
|
|
|
->method('adaptChildren') |
164
|
|
|
; |
165
|
|
|
|
166
|
|
|
$menuBuilderMock = $this->setUpMenuBuilderMock($this->container, null); |
167
|
|
|
$menuBuilderMock->addAdaptMenu($menuAdaptorInterfaceMock); |
|
|
|
|
168
|
|
|
|
169
|
|
|
$this->assertIsArray($menuBuilderMock->getTopChildren()); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..