Completed
Push — master ( 15f051...29343b )
by Oleg
05:33
created

BuilderTest::testHas()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Malezha\Menu\Tests;
4
5
use Illuminate\Support\Collection;
6
use Malezha\Menu\Contracts\Builder;
7
use Malezha\Menu\Entity\Attributes;
8
use Malezha\Menu\Entity\Item;
9
10
class BuilderTest extends TestCase
11
{
12
    /**
13
     * @return Builder
14
     */
15
    protected function builderFactory()
16
    {
17
        return $this->app->make(Builder::class, ['name' => 'test', 'activeAttributes' => ['class' => 'active']]);
18
    }
19
    
20
    public function testConstructor()
21
    {
22
        $builder = $this->builderFactory();
23
24
        $this->assertAttributeEquals($this->app, 'container', $builder);
25
        $this->assertAttributeEquals('test', 'name', $builder);
26
        $this->assertAttributeEquals(Builder::UL, 'type', $builder);
27
        $this->assertAttributeInstanceOf(Attributes::class, 'attributes', $builder);
28
        $this->assertAttributeInstanceOf(Collection::class, 'items', $builder);
29
        $this->assertAttributeInstanceOf(Attributes::class, 'activeAttributes', $builder);
30
    }
31
32
    public function testAdd()
33
    {
34
        $builder = $this->builderFactory();
35
36
        $item = $builder->add('index', 'Index', url('/'), ['class' => 'main-menu'], ['class' => 'link'],
1 ignored issue
show
Bug introduced by
It seems like url('/') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::add() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
37
            function (Item $item) {
38
                $this->assertAttributeEquals('Index', 'title', $item->getLink());
39
                $item->getLink()->setTitle('Home');
40
            });
41
42
        $this->assertAttributeEquals($builder, 'builder', $item);
43
        $this->assertAttributeInstanceOf(Attributes::class, 'attributes', $item);
44
45
        $link = $item->getLink();
46
        $this->assertAttributeEquals('Home', 'title', $link);
47
        $this->assertAttributeEquals(url('/'), 'url', $link);
48
    }
49
    
50
    public function testGet()
51
    {
52
        $builder = $this->builderFactory();
53
        
54
        $item = $builder->add('test', 'Test', '/test');
55
        
56
        $this->assertEquals($item, $builder->get('test'));
57
    }
58
    
59
    public function testHas()
60
    {
61
        $builder = $this->builderFactory();
62
63
        $this->assertFalse($builder->has('test'));
64
    }
65
    
66
    public function testType()
67
    {
68
        $builder = $this->builderFactory();
69
        
70
        $this->assertEquals(Builder::UL, $builder->getType());
71
        $builder->setType(Builder::OL);
72
        $this->assertAttributeEquals(Builder::OL, 'type', $builder);
73
    }
74
    
75
    public function testAll()
76
    {
77
        $builder = $this->builderFactory();
78
        
79
        $this->assertEquals([], $builder->all());
80
        $item = $builder->add('test', 'Test', '/test');
81
        $this->assertEquals(['test' => $item], $builder->all());
82
    }
83
    
84
    public function testForget()
85
    {
86
        $builder = $this->builderFactory();
87
88
        $builder->add('test', 'Test', '/test');
89
        $this->assertTrue($builder->has('test'));
90
        $builder->forget('test');
91
        $this->assertFalse($builder->has('test'));
92
    }
93
    
94
    public function testActiveAttributes()
95
    {
96
        $builder = $this->builderFactory();
97
        $activeAttributes = $builder->activeAttributes();
98
        
99
        $this->assertInstanceOf(Attributes::class, $activeAttributes);
100
101
        $result = $builder->activeAttributes(function ($attributes) {
102
            $this->assertInstanceOf(Attributes::class, $attributes);
103
            
104
            return $attributes->get('class');
105
        });
106
        
107
        $this->assertEquals('active', $result);
108
    }
109
    
110
    public function testGroup()
111
    {
112
        $builder = $this->builderFactory();
113
        
114
        $group = $builder->group('test', function (Item $item) use ($builder) {
115
            $this->assertAttributeEquals($builder, 'builder', $item);
116
        }, function (Builder $menu) use ($builder) {
117
            $this->assertEquals($builder->activeAttributes()->all(), $menu->activeAttributes()->all());
118
        });
119
120
        $this->assertEquals($group, $builder->get('test'));
121
    }
122
123
    public function testRender()
124
    {
125
        $builder = $this->builderFactory();
126
127
        $index = $builder->add('index', 'Index Page', '/');
128
        $index->getLink()->getAttributes()->push(['class' => 'menu-link']);
129
130
        $builder->group('orders', function ($item) {
131
            $item->getAttributes()->push(['class' => 'child-menu']);
132
            
133
            $link = $item->getLink();
134
            $link->setTitle('Orders');
135
            $link->setUrl('javascript:;');
136
137
        }, function ($menu) {
138
            $menu->add('all', 'All', '/orders/all');
139
            $menu->add('type_1', 'Type 1', '/orders/1', [], ['class' => 'text-color-red']);
140
141
            $menu->add('type_2', 'Type 2', '/orders/2', [], [], function ($item) {
142
                $item->getLink()->getAttributes()->push(['data-attribute' => 'value']);
143
            });
144
        });
145
        
146
        $html = $builder->render();
147
        $file = file_get_contents(__DIR__ . '/stub/menu.html');
148
        
149
        $this->assertEquals($file, $html);
150
    }
151
    
152
    public function testDisplayRules()
153
    {
154
        $builder = $this->builderFactory();
155
156
        $builder->add('index', 'Index Page', '/');
157
        $builder->add('login', 'Login', '/login')->setDisplayRule(function () {
158
            return true;
159
        });
160
        $builder->add('admin', 'Admin', '/admin')->setDisplayRule(false);
161
        $builder->add('logout', 'Logout', '/logout')->setDisplayRule(null);
162
163
        $html = $builder->render();
164
        $file = file_get_contents(__DIR__ . '/stub/display_rules.html');
165
166
        $this->assertEquals($file, $html);
167
    }
168
}