1 | <?php |
||||
2 | |||||
3 | namespace KyleMassacre\Menus\Tests; |
||||
4 | |||||
5 | use Illuminate\Support\Arr; |
||||
6 | use KyleMassacre\Menus\Menu; |
||||
7 | use KyleMassacre\Menus\MenuItem; |
||||
8 | |||||
9 | class MenuItemTest extends BaseTestCase |
||||
10 | { |
||||
11 | /** |
||||
12 | * @var Menu |
||||
13 | */ |
||||
14 | private $menu; |
||||
15 | |||||
16 | public function setUp() : void |
||||
17 | { |
||||
18 | parent::setUp(); |
||||
19 | $this->menu = app(Menu::class); |
||||
20 | } |
||||
21 | /** @test */ |
||||
22 | public function it_can_make_an_empty_menu_item() |
||||
23 | { |
||||
24 | $menuItem = MenuItem::make([]); |
||||
25 | |||||
26 | $this->assertInstanceOf(MenuItem::class, $menuItem); |
||||
27 | } |
||||
28 | |||||
29 | /** @test */ |
||||
30 | public function it_can_set_properties_on_menu_item() |
||||
31 | { |
||||
32 | $properties = [ |
||||
33 | 'url' => 'my.url', |
||||
34 | 'route' => 'my.route', |
||||
35 | 'title' => 'My Menu item', |
||||
36 | 'name' => 'my-menu-item', |
||||
37 | 'icon' => 'fa fa-user', |
||||
38 | 'parent' => 1, |
||||
39 | 'attributes' => [], |
||||
40 | 'active' => false, |
||||
41 | 'order' => 1, |
||||
42 | ]; |
||||
43 | $menuItem = MenuItem::make($properties); |
||||
44 | |||||
45 | $this->assertEquals($properties, $menuItem->getProperties()); |
||||
46 | } |
||||
47 | |||||
48 | /** @test */ |
||||
49 | public function it_can_fill_a_menu_item_with_allowed_properties() |
||||
50 | { |
||||
51 | $properties = [ |
||||
52 | 'url' => 'my.url', |
||||
53 | 'route' => 'my.route', |
||||
54 | 'title' => 'My Menu item', |
||||
55 | 'name' => 'my-menu-item', |
||||
56 | 'icon' => 'fa fa-user', |
||||
57 | 'parent' => 1, |
||||
58 | 'attributes' => [], |
||||
59 | 'active' => false, |
||||
60 | 'order' => 1, |
||||
61 | ]; |
||||
62 | $menuItem = MenuItem::make($properties); |
||||
63 | |||||
64 | $this->assertEquals('my.url', $menuItem->url); |
||||
65 | $this->assertEquals('my.route', $menuItem->route); |
||||
66 | $this->assertEquals('My Menu item', $menuItem->title); |
||||
67 | $this->assertEquals('my-menu-item', $menuItem->name); |
||||
68 | $this->assertEquals('fa fa-user', $menuItem->icon); |
||||
69 | $this->assertSame(1, $menuItem->parent); |
||||
70 | $this->assertSame([], $menuItem->attributes); |
||||
71 | $this->assertFalse($menuItem->active); |
||||
72 | $this->assertSame(1, $menuItem->order); |
||||
73 | } |
||||
74 | |||||
75 | /** @test */ |
||||
76 | public function it_can_set_icon_via_attributes() |
||||
77 | { |
||||
78 | $menuItem = MenuItem::make(['attributes' => ['icon' => 'fa fa-user']]); |
||||
79 | |||||
80 | $this->assertEquals('fa fa-user', $menuItem->icon); |
||||
81 | } |
||||
82 | |||||
83 | /** @test */ |
||||
84 | public function it_can_add_a_child_menu_item() |
||||
85 | { |
||||
86 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
87 | $menuItem->child(['title' => 'Child Item']); |
||||
88 | |||||
89 | $this->assertCount(1, $menuItem->getChildren()); |
||||
90 | } |
||||
91 | |||||
92 | /** @test */ |
||||
93 | public function it_can_get_ordered_children() |
||||
94 | { |
||||
95 | $this->app['config']->set('menus.ordering', true); |
||||
96 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
97 | $menuItem->child(['title' => 'Child Item', 'order' => 10]); |
||||
98 | $menuItem->child(['title' => 'First Child Item', 'order' => 1]); |
||||
99 | |||||
100 | $children = $menuItem->getChildren(); |
||||
101 | $this->assertEquals('First Child Item', $children[1]->title); |
||||
102 | $this->assertEquals('Child Item', $children[0]->title); |
||||
103 | } |
||||
104 | |||||
105 | /** @test */ |
||||
106 | public function it_can_create_a_dropdown_menu_item() |
||||
107 | { |
||||
108 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
109 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
110 | $sub->url('settings/account', 'Account'); |
||||
111 | $sub->url('settings/password', 'Password'); |
||||
112 | }); |
||||
113 | $this->assertCount(1, $menuItem->getChildren()); |
||||
114 | $this->assertCount(2, $menuItem->getChildren()[0]->getChilds()); |
||||
115 | } |
||||
116 | |||||
117 | /** @test */ |
||||
118 | public function it_can_make_a_simple_route_menu_item() |
||||
119 | { |
||||
120 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
121 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
122 | $sub->route('settings.account', 'Account', ['user_id' => 1]); |
||||
123 | }); |
||||
124 | $children = $menuItem->getChildren()[0]->getChilds(); |
||||
125 | |||||
126 | $this->assertCount(1, $children); |
||||
127 | $childMenuItem = Arr::first($children); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
128 | $this->assertEquals('settings.account', $childMenuItem->route[0]); |
||||
129 | $this->assertEquals(['user_id' => 1], $childMenuItem->route[1]); |
||||
130 | } |
||||
131 | |||||
132 | /** @test */ |
||||
133 | public function it_can_make_a_route_menu_item() |
||||
134 | { |
||||
135 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
136 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
137 | $sub->route('settings.account', 'Account', ['user_id' => 1], 1, ['my-attr' => 'value']); |
||||
138 | }); |
||||
139 | $children = $menuItem->getChildren()[0]->getChilds(); |
||||
140 | |||||
141 | $this->assertCount(1, $children); |
||||
142 | $childMenuItem = Arr::first($children); |
||||
0 ignored issues
–
show
It seems like
$children can also be of type Countable ; however, parameter $array of Illuminate\Support\Arr::first() does only seem to accept iterable , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
143 | $this->assertEquals('settings.account', $childMenuItem->route[0]); |
||||
144 | $this->assertEquals(['user_id' => 1], $childMenuItem->route[1]); |
||||
145 | $this->assertSame(1, $childMenuItem->order); |
||||
146 | $this->assertEquals(['my-attr' => 'value'], $childMenuItem->attributes); |
||||
147 | } |
||||
148 | |||||
149 | /** @test */ |
||||
150 | public function it_can_make_a_simple_url_menu_item() |
||||
151 | { |
||||
152 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
153 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
154 | $sub->url('settings/account', 'Account'); |
||||
155 | }); |
||||
156 | $children = $menuItem->getChildren()[0]->getChilds(); |
||||
157 | |||||
158 | $this->assertCount(1, $children); |
||||
159 | $childMenuItem = Arr::first($children); |
||||
0 ignored issues
–
show
It seems like
$children can also be of type Countable ; however, parameter $array of Illuminate\Support\Arr::first() does only seem to accept iterable , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
160 | $this->assertEquals('settings/account', $childMenuItem->url); |
||||
161 | $this->assertEquals('Account', $childMenuItem->title); |
||||
162 | } |
||||
163 | |||||
164 | /** @test */ |
||||
165 | public function it_can_make_a_url_menu_item() |
||||
166 | { |
||||
167 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
168 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
169 | $sub->url('settings/account', 'Account', 1, ['my-attr' => 'value']); |
||||
170 | }); |
||||
171 | $children = $menuItem->getChildren()[0]->getChilds(); |
||||
172 | |||||
173 | $this->assertCount(1, $children); |
||||
174 | $childMenuItem = Arr::first($children); |
||||
0 ignored issues
–
show
It seems like
$children can also be of type Countable ; however, parameter $array of Illuminate\Support\Arr::first() does only seem to accept iterable , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
175 | $this->assertEquals('settings/account', $childMenuItem->url); |
||||
176 | $this->assertEquals('Account', $childMenuItem->title); |
||||
177 | $this->assertSame(1, $childMenuItem->order); |
||||
178 | $this->assertEquals(['my-attr' => 'value'], $childMenuItem->attributes); |
||||
179 | } |
||||
180 | |||||
181 | /** @test */ |
||||
182 | public function it_can_add_a_menu_item_divider() |
||||
183 | { |
||||
184 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
185 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
186 | $sub->url('settings/account', 'Account'); |
||||
187 | $sub->divider(); |
||||
188 | }); |
||||
189 | |||||
190 | $children = $menuItem->getChildren()[0]->getChilds(); |
||||
191 | |||||
192 | $this->assertCount(2, $children); |
||||
193 | $dividerMenuItem = $children[1]; |
||||
194 | $this->assertEquals('divider', $dividerMenuItem->name); |
||||
195 | $this->assertTrue($dividerMenuItem->isDivider()); |
||||
196 | } |
||||
197 | |||||
198 | /** @test */ |
||||
199 | public function it_can_add_a_header_menu_item() |
||||
200 | { |
||||
201 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
202 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
203 | $sub->header('User Stuff'); |
||||
204 | $sub->url('settings/account', 'Account'); |
||||
205 | }); |
||||
206 | |||||
207 | $children = $menuItem->getChildren()[0]->getChilds(); |
||||
208 | |||||
209 | $this->assertCount(2, $children); |
||||
210 | $headerItem = $children[0]; |
||||
211 | $this->assertEquals('header', $headerItem->name); |
||||
212 | $this->assertEquals('User Stuff', $headerItem->title); |
||||
213 | $this->assertTrue($headerItem->isHeader()); |
||||
214 | } |
||||
215 | |||||
216 | /** @test */ |
||||
217 | public function it_can_get_the_correct_url_for_url_type() |
||||
218 | { |
||||
219 | $menuItem = MenuItem::make(['url' => 'settings/account', 'title' => 'Parent Item']); |
||||
220 | |||||
221 | $this->assertEquals('http://localhost/settings/account', $menuItem->getUrl()); |
||||
222 | } |
||||
223 | |||||
224 | /** @test */ |
||||
225 | public function it_can_get_the_correct_url_for_route_type() |
||||
226 | { |
||||
227 | $this->app['router']->get('settings/account', ['as' => 'settings.account']); |
||||
228 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
229 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
230 | $sub->route('settings.account', 'Account'); |
||||
231 | }); |
||||
232 | $children = $menuItem->getChildren()[0]->getChilds(); |
||||
233 | $childMenuItem = Arr::first($children); |
||||
234 | |||||
235 | $this->assertEquals('http://localhost/settings/account', $childMenuItem->getUrl()); |
||||
236 | } |
||||
237 | |||||
238 | /** @test */ |
||||
239 | public function it_can_get_request_uri() |
||||
240 | { |
||||
241 | $menuItem = MenuItem::make(['url' => 'settings/account', 'title' => 'Parent Item']); |
||||
242 | |||||
243 | $this->assertEquals('settings/account', $menuItem->getRequest()); |
||||
244 | } |
||||
245 | |||||
246 | /** @test */ |
||||
247 | public function it_can_get_the_icon_html_attribute() |
||||
248 | { |
||||
249 | $menuItem = MenuItem::make(['url' => 'settings/account', 'title' => 'Parent Item', 'icon' => 'fa fa-user']); |
||||
250 | |||||
251 | $this->assertEquals('<i class="fa fa-user"></i>', $menuItem->getIcon()); |
||||
252 | } |
||||
253 | |||||
254 | /** @test */ |
||||
255 | public function it_returns_no_icon_if_none_exist() |
||||
256 | { |
||||
257 | $menuItem = MenuItem::make(['url' => 'settings/account', 'title' => 'Parent Item']); |
||||
258 | |||||
259 | $this->assertNull($menuItem->getIcon()); |
||||
260 | } |
||||
261 | |||||
262 | /** @test */ |
||||
263 | public function it_returns_default_icon_if_none_exist() |
||||
264 | { |
||||
265 | $menuItem = MenuItem::make(['url' => 'settings/account', 'title' => 'Parent Item']); |
||||
266 | |||||
267 | $this->assertEquals('<i class="fa fa-user"></i>', $menuItem->getIcon('fa fa-user')); |
||||
268 | } |
||||
269 | |||||
270 | /** @test */ |
||||
271 | public function it_can_get_item_properties() |
||||
272 | { |
||||
273 | $menuItem = MenuItem::make(['url' => 'settings/account', 'title' => 'Parent Item']); |
||||
274 | |||||
275 | $this->assertEquals(['url' => 'settings/account', 'title' => 'Parent Item'], $menuItem->getProperties()); |
||||
276 | } |
||||
277 | |||||
278 | /** @test */ |
||||
279 | public function it_can_get_item_html_attributes() |
||||
280 | { |
||||
281 | $menuItem = MenuItem::make(['url' => 'settings/account', 'title' => 'Parent Item', 'attributes' => ['my-attr' => 'value']]); |
||||
282 | |||||
283 | $this->assertEquals(' my-attr="value"', $menuItem->getAttributes()); |
||||
284 | } |
||||
285 | |||||
286 | /** @test */ |
||||
287 | public function it_can_check_for_a_submenu() |
||||
288 | { |
||||
289 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||||
290 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||||
291 | $sub->header('User Stuff'); |
||||
292 | $sub->url('settings/account', 'Account'); |
||||
293 | }); |
||||
294 | |||||
295 | $this->assertTrue($menuItem->hasSubMenu()); |
||||
296 | $this->assertTrue($menuItem->hasChilds()); |
||||
297 | } |
||||
298 | |||||
299 | public function it_can_check_active_state_on_item() |
||||
300 | { |
||||
301 | } |
||||
302 | } |
||||
303 |