Completed
Push — master ( 3afbe4...79027c )
by Oleg
07:17
created

MenuTest::testHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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->makeWith(Menu::class, ['container' => $this->app]);
0 ignored issues
show
Bug introduced by
The method makeWith() does not seem to exist on object<Illuminate\Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27 View Code Duplication
        $menu->make('test', function(Builder $builder) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
28
            $builder->create('one', Link::class, function(LinkFactory $factory) {
29
                $factory->title = 'One';
30
                $factory->url = '/one';
31
32
                return $factory->build();
33
            });
34
        });
35
        
36
        return $menu;
37
    }
38
    
39
    public function testFacade()
40
    {
41
        $this->assertInstanceOf(Menu::class, \Menu::getFacadeRoot());
42
    }
43
    
44
    public function testGet()
45
    {
46
        $menu = $this->menuFactory();
47
        $builder = $menu->get('test');
48
        $this->assertInstanceOf(Builder::class, $builder);
49
    }
50
51
    /**
52
     * @expectedException \RuntimeException
53
     */
54
    public function testGetException()
55
    {
56
        $menu = \Menu::get('test');
0 ignored issues
show
Unused Code introduced by
$menu is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
57
    }
58
    
59
    public function testHas()
60
    {
61
        $menu = $this->menuFactory();
62
        $this->assertFalse($menu->has('another'));
63
        $this->assertTrue($menu->has('test'));
64
    }
65
    
66
    public function testForget()
67
    {
68
        $menu = $this->menuFactory();
69
        $menu->forget('test');
70
        $this->assertAttributeEquals([], 'menuList', $menu);
71
    }
72
    
73
    public function testRender()
74
    {
75
        $menu = $this->menuFactory();
76
        $file = file_get_contents(__DIR__ . '/stub/facade_render.html');
77
        $this->assertEquals($file, $menu->render('test'));
78
    }
79
    
80
    public function testFromArray()
81
    {
82
        /** @var Builder $builder */
83
        $builder = $this->app->makeWith(Builder::class, [
0 ignored issues
show
Bug introduced by
The method makeWith() does not seem to exist on object<Illuminate\Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
            'attributes' => new Attributes(['class' => 'menu']),
85
            'activeAttributes' => new Attributes(['class' => 'active']),
86
        ]);
87
88
        $builder->create('index', Link::class, function(LinkFactory $factory) {
89
            $factory->title = 'Index';
90
            $factory->url = '/';
91
92
            return $factory->build();
93
        });
94
95
        $builder->create('deliver_1', Text::class, function(TextFactory $factory) {
96
            $factory->text = null;
97
            $factory->attributes->put('class', 'deliver');
98
99
            return $factory->build();
100
        });
101
102
        $builder->create('settings', SubMenu::class, function(SubMenuFactory $factory) {
103
            $factory->title = 'Index';
104
            $factory->builder->create('some', Link::class, function(LinkFactory $factory) {
105
                $factory->title = 'Some setting';
106
                $factory->url = '/settings/some';
107
108
                return $factory->build();
109
            });
110
111
            return $factory->build();
112
        });
113
114
        $builder->create('deliver_2', Text::class, function(TextFactory $factory) {
115
            $factory->text = null;
116
            $factory->attributes->put('class', 'deliver');
117
118
            return $factory->build();
119
        });
120
121
        $builder->create('logout', Link::class, function(LinkFactory $factory) {
122
            $factory->title = 'Logout';
123
            $factory->url = '/logout';
124
125
            return $factory->build();
126
        });
127
        
128
        /** @var Menu $menu */
129
        $menu = $this->app->make(Menu::class);
130
        $menu->fromArray('from_array', $builder->toArray());
131
        
132
        $this->assertEquals($builder->render(), $menu->get('from_array')->render());
133
        $this->assertEquals($builder->toArray(), $menu->toArray('from_array'));
134
    }
135
136
    public function testHelper()
137
    {
138
        $this->assertInstanceOf(Menu::class, menu());
139
        $this->assertInstanceOf(Builder::class, menu('test'));
140
    }
141
}