1
|
|
|
<?php |
2
|
|
|
namespace Malezha\Menu\Tests; |
3
|
|
|
|
4
|
|
|
use Malezha\Menu\Contracts\Builder; |
5
|
|
|
use Malezha\Menu\Contracts\Menu; |
6
|
|
|
use Malezha\Menu\Element\Link; |
7
|
|
|
use Malezha\Menu\Element\SubMenu; |
8
|
|
|
use Malezha\Menu\Element\Text; |
9
|
|
|
use Malezha\Menu\Factory\LinkFactory; |
10
|
|
|
use Malezha\Menu\Factory\SubMenuFactory; |
11
|
|
|
use Malezha\Menu\Factory\TextFactory; |
12
|
|
|
use Malezha\Menu\Support\Attributes; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class MenuTest |
16
|
|
|
* @package Malezha\Menu\Tests |
17
|
|
|
*/ |
18
|
|
|
class MenuTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return Menu |
22
|
|
|
*/ |
23
|
|
|
protected function menuFactory() |
24
|
|
|
{ |
25
|
|
|
/** @var Menu $menu */ |
26
|
|
|
$menu = $this->app->make(Menu::class, ['container' => $this->app]); |
27
|
|
|
$menu->make('test', function(Builder $builder) { |
28
|
|
|
$builder->create('one', Link::class, function(LinkFactory $factory) { |
29
|
|
|
$factory->title = 'One'; |
30
|
|
|
$factory->url = '/one'; |
31
|
|
|
}); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
return $menu; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testFacade() |
38
|
|
|
{ |
39
|
|
|
$this->assertInstanceOf(Menu::class, \Menu::getFacadeRoot()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testGet() |
43
|
|
|
{ |
44
|
|
|
$menu = $this->menuFactory(); |
45
|
|
|
$builder = $menu->get('test'); |
46
|
|
|
$this->assertInstanceOf(Builder::class, $builder); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @expectedException \RuntimeException |
51
|
|
|
*/ |
52
|
|
|
public function testGetException() |
53
|
|
|
{ |
54
|
|
|
$menu = \Menu::get('test'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testHas() |
58
|
|
|
{ |
59
|
|
|
$menu = $this->menuFactory(); |
60
|
|
|
$this->assertFalse($menu->has('another')); |
61
|
|
|
$this->assertTrue($menu->has('test')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testForget() |
65
|
|
|
{ |
66
|
|
|
$menu = $this->menuFactory(); |
67
|
|
|
$menu->forget('test'); |
68
|
|
|
$this->assertAttributeEquals([], 'menuList', $menu); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testRender() |
72
|
|
|
{ |
73
|
|
|
$menu = $this->menuFactory(); |
74
|
|
|
$file = file_get_contents(__DIR__ . '/stub/facade_render.html'); |
75
|
|
|
$this->assertEquals($file, $menu->render('test')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testFromArray() |
79
|
|
|
{ |
80
|
|
|
/** @var Builder $builder */ |
81
|
|
|
$builder = $this->app->make(Builder::class, [ |
82
|
|
|
'attributes' => new Attributes(['class' => 'menu']), |
83
|
|
|
'activeAttributes' => new Attributes(['class' => 'active']), |
84
|
|
|
]); |
85
|
|
|
$builder->create('index', Link::class, function(LinkFactory $factory) { |
86
|
|
|
$factory->title = 'Index'; |
87
|
|
|
$factory->url = '/'; |
88
|
|
|
}); |
89
|
|
|
$builder->create('deliver_1', Text::class, function(TextFactory $factory) { |
90
|
|
|
$factory->text = null; |
91
|
|
|
$factory->attributes->put('class', 'deliver'); |
92
|
|
|
}); |
93
|
|
View Code Duplication |
$builder->create('settings', SubMenu::class, function(SubMenuFactory $factory) { |
|
|
|
|
94
|
|
|
$factory->title = 'Index'; |
95
|
|
|
$factory->builder->create('some', Link::class, function(LinkFactory $factory) { |
96
|
|
|
$factory->title = 'Some setting'; |
97
|
|
|
$factory->url = '/settings/some'; |
98
|
|
|
}); |
99
|
|
|
}); |
100
|
|
|
$builder->create('deliver_2', Text::class, function(TextFactory $factory) { |
101
|
|
|
$factory->text = null; |
102
|
|
|
$factory->attributes->put('class', 'deliver'); |
103
|
|
|
}); |
104
|
|
|
$builder->create('logout', Link::class, function(LinkFactory $factory) { |
105
|
|
|
$factory->title = 'Logout'; |
106
|
|
|
$factory->url = '/logout'; |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
/** @var Menu $menu */ |
110
|
|
|
$menu = $this->app->make(Menu::class); |
111
|
|
|
$menu->fromArray('from_array', $builder->toArray()); |
112
|
|
|
|
113
|
|
|
$this->assertEquals($builder, $menu->get('from_array')); |
114
|
|
|
} |
115
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.