MenuItemTest::it_can_add_a_menu_item_divider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Nwidart\Menus\Tests;
4
5
use Illuminate\Support\Arr;
6
use Nwidart\Menus\Menu;
7
use Nwidart\Menus\MenuItem;
8
9
class MenuItemTest extends BaseTestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
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->getChilds());
0 ignored issues
show
Documentation introduced by
$menuItem->getChilds() is of type array, 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...
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->getChilds();
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->getChilds());
0 ignored issues
show
Documentation introduced by
$menuItem->getChilds() is of type array, 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->assertCount(2, $menuItem->getChilds()[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->getChilds()[0]->getChilds();
125
126
        $this->assertCount(1, $children);
127
        $childMenuItem = Arr::first($children);
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->getChilds()[0]->getChilds();
140
141
        $this->assertCount(1, $children);
142
        $childMenuItem = Arr::first($children);
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->getChilds()[0]->getChilds();
157
158
        $this->assertCount(1, $children);
159
        $childMenuItem = Arr::first($children);
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->getChilds()[0]->getChilds();
172
173
        $this->assertCount(1, $children);
174
        $childMenuItem = Arr::first($children);
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 View Code Duplication
    public function it_can_add_a_menu_item_divider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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->getChilds()[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 View Code Duplication
    public function it_can_add_a_header_menu_item()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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->getChilds()[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->getChilds()[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