Completed
Push — master ( 7cecbb...791ec7 )
by Oleg
07:00
created

BuilderTest::builderFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace Malezha\Menu\Tests;
3
4
use Malezha\Menu\Contracts\Attributes;
5
use Malezha\Menu\Contracts\Builder;
6
use Malezha\Menu\Contracts\Item;
7
8
/**
9
 * Class BuilderTest
10
 * @package Malezha\Menu\Tests
11
 */
12
class BuilderTest extends TestCase
13
{
14
    /**
15
     * @return Builder
16
     */
17
    protected function builderFactory()
18
    {
19
        return $this->app->make(Builder::class, [
20
            'name' => 'test', 
21
            'activeAttributes' => $this->app->make(Attributes::class, ['attributes' => ['class' => 'active']]),
22
            'attributes' => $this->app->make(Attributes::class, ['attributes' => ['class' => 'menu']]),
23
        ]);
24
    }
25
    
26
    public function testConstructor()
27
    {
28
        $builder = $this->builderFactory();
29
30
        $this->assertAttributeEquals($this->app, 'app', $builder);
31
        $this->assertAttributeEquals('test', 'name', $builder);
32
        $this->assertAttributeEquals(Builder::UL, 'type', $builder);
33
        $this->assertAttributeInstanceOf(Attributes::class, 'attributes', $builder);
34
        $this->assertAttributeInternalType('array', 'items', $builder);
35
        $this->assertAttributeInstanceOf(Attributes::class, 'activeAttributes', $builder);
36
    }
37
38
    public function testCreate()
39
    {
40
        $builder = $this->builderFactory();
41
42
        $item = $builder->create('index', 'Index', '/', ['class' => 'main-menu'], ['class' => 'link'],
43
            function(Item $item) {
44
                $this->assertAttributeEquals('Index', 'title', $item->getLink());
45
                $item->getLink()->setTitle('Home');
46
            });
47
48
        $this->assertAttributeEquals($builder, 'builder', $item);
49
        $this->assertAttributeInstanceOf(Attributes::class, 'attributes', $item);
50
51
        $link = $item->getLink();
52
        $this->assertAttributeEquals('Home', 'title', $link);
53
        $this->assertAttributeEquals('/', 'url', $link);
54
    }
55
56
    public function testCreateIfExists()
57
    {
58
        $this->expectException(\RuntimeException::class);
59
        
60
        $builder = $this->builderFactory();
61
62
        $builder->create('index', 'Index', '/', ['class' => 'main-menu'], ['class' => 'link']);
63
        $builder->create('index', 'Index', '/', ['class' => 'main-menu'], ['class' => 'link']); // Duplicate
64
    }
65
    
66 View Code Duplication
    public function testGet()
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...
67
    {
68
        $builder = $this->builderFactory();
69
        
70
        $item = $builder->create('test', 'Test', '/test');
71
        
72
        $this->assertEquals($item, $builder->get('test'));
73
        $this->assertEquals(null, $builder->get('notFound'));
74
    }
75
76 View Code Duplication
    public function testGetByIndex()
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...
77
    {
78
        $builder = $this->builderFactory();
79
80
        $item = $builder->create('test', 'Test', '/test');
81
82
        $this->assertEquals($item, $builder->getByIndex(0));
83
        $this->assertEquals(null, $builder->getByIndex(1));
84
    }
85
    
86
    public function testHas()
87
    {
88
        $builder = $this->builderFactory();
89
90
        $this->assertFalse($builder->has('test'));
91
    }
92
    
93
    public function testType()
94
    {
95
        $builder = $this->builderFactory();
96
        
97
        $this->assertEquals(Builder::UL, $builder->getType());
98
        $builder->setType(Builder::OL);
99
        $this->assertAttributeEquals(Builder::OL, 'type', $builder);
100
    }
101
    
102 View Code Duplication
    public function testAll()
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...
103
    {
104
        $builder = $this->builderFactory();
105
        
106
        $this->assertEquals([], $builder->all());
107
        $item = $builder->create('test', 'Test', '/test');
108
        $this->assertEquals(['test' => $item], $builder->all());
109
    }
110
    
111
    public function testForget()
112
    {
113
        $builder = $this->builderFactory();
114
115
        $builder->create('test', 'Test', '/test');
116
        $this->assertTrue($builder->has('test'));
117
        $builder->forget('test');
118
        $this->assertFalse($builder->has('test'));
119
    }
120
    
121
    public function testActiveAttributes()
122
    {
123
        $builder = $this->builderFactory();
124
        $activeAttributes = $builder->activeAttributes();
125
        
126
        $this->assertInstanceOf(Attributes::class, $activeAttributes);
127
128
        $result = $builder->activeAttributes(function(Attributes $attributes) {
129
            $this->assertInstanceOf(Attributes::class, $attributes);
130
            
131
            return $attributes->get('class');
132
        });
133
        
134
        $this->assertEquals('active', $result);
135
    }
136
    
137
    public function testSubMenu()
138
    {
139
        $builder = $this->builderFactory();
140
        
141
        $group = $builder->submenu('test', function(Item $item) use ($builder) {
142
            $this->assertAttributeEquals($builder, 'builder', $item);
143
        }, function(Builder $menu) use ($builder) {
144
            $this->assertEquals($builder->activeAttributes()->all(), $menu->activeAttributes()->all());
145
        });
146
147
        $this->assertEquals($group, $builder->get('test'));
148
    }
149
150
    public function testRender()
151
    {
152
        $builder = $this->builderFactory();
153
154
        $index = $builder->create('index', 'Index Page', url('/'));
155
        $index->getLink()->getAttributes()->push(['class' => 'menu-link']);
156
157
        $builder->submenu('orders', function(Item $item) {
158
            $item->getAttributes()->push(['class' => 'child-menu']);
159
            
160
            $link = $item->getLink();
161
            $link->setTitle('Orders');
162
            $link->setUrl('javascript:;');
163
164 View Code Duplication
        }, function(Builder $menu) {
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...
165
            $menu->create('all', 'All', url('/orders/all'));
0 ignored issues
show
Bug introduced by
It seems like url('/orders/all') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
166
            $menu->create('type_1', 'Type 1', url('/orders/1'), [], ['class' => 'text-color-red']);
0 ignored issues
show
Bug introduced by
It seems like url('/orders/1') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
167
168
            $menu->create('type_2', 'Type 2', url('/orders/2'), [], [], function(Item $item) {
0 ignored issues
show
Bug introduced by
It seems like url('/orders/2') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
169
                $item->getLink()->getAttributes()->push(['data-attribute' => 'value']);
170
            });
171
        });
172
        
173
        $html = $builder->render();
174
        $file = file_get_contents(__DIR__ . '/stub/menu.html');
175
        
176
        $this->assertEquals($file, $html);
177
    }
178
    
179
    public function testDisplayRules()
180
    {
181
        $builder = $this->builderFactory();
182
183
        $builder->create('index', 'Index Page', url('/'));
184
        $builder->create('login', 'Login', url('/login'))->setDisplayRule(function() {
0 ignored issues
show
Bug introduced by
It seems like url('/login') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
185
            return true;
186
        });
187
        $builder->create('admin', 'Admin', url('/admin'))->setDisplayRule(false);
0 ignored issues
show
Bug introduced by
It seems like url('/admin') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
188
        $builder->create('logout', 'Logout', url('/logout'))->setDisplayRule(null);
0 ignored issues
show
Bug introduced by
It seems like url('/logout') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
Documentation introduced by
null is of type null, but the function expects a boolean|object<Closure>.

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...
189
190
        $html = $builder->render();
191
        $file = file_get_contents(__DIR__ . '/stub/display_rules.html');
192
193
        $this->assertEquals($file, $html);
194
    }
195
196
    public function testAnotherViewRender()
197
    {
198
        view()->addLocation(__DIR__ . '/stub');
199
        $this->app['config']->prepend('menu.paths', __DIR__ . '/stub');
200
        
201
        $builder = $this->builderFactory();
202
        $builder->create('index', 'Index Page', url('/'));
203
        $builder->submenu('group', function(Item $item){}, function(Builder $menu) {
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
204
            $menu->create('one', 'One', url('/one'));
0 ignored issues
show
Bug introduced by
It seems like url('/one') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
205
        });
206
207
        $html = $builder->render('another');
208
        $file = file_get_contents(__DIR__ . '/stub/another_menu.html');
209
        $this->assertEquals($file, $html);
210
211
        $builder->get('group')->getMenu()->setView('another');
212
        $html = $builder->render();
213
        $file = file_get_contents(__DIR__ . '/stub/another_sub_menu.html');
214
        $this->assertEquals($file, $html);
215
216
        $builder->setView('another');
217
        $builder->get('group')->getMenu()->setView('menu::view');
218
        $html = $builder->render();
219
        $file = file_get_contents(__DIR__ . '/stub/another_set_view_menu.html');
220
        $this->assertEquals($file, $html);
221
    }
222
    
223
    public function testInsert()
224
    {
225
        $builder = $this->builderFactory();
226
        $builder->create('index', 'Index Page', url('/'));
227
        $builder->create('logout', 'logout', url('logout'));
0 ignored issues
show
Bug introduced by
It seems like url('logout') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
228
        
229
        $builder->insertAfter('index', function (Builder $builder) {
230
            $builder->create('users', 'Users', url('users'));
0 ignored issues
show
Bug introduced by
It seems like url('users') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
231
        });
232
        
233
        $builder->insertBefore('users', function (Builder $builder) {
234
            $builder->create('profile', 'Profile', url('profile'));
0 ignored issues
show
Bug introduced by
It seems like url('profile') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Contracts\Builder::create() 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...
235
        });
236
237
        $html = $builder->render('another');
238
        $file = file_get_contents(__DIR__ . '/stub/insert.html');
239
        $this->assertEquals($file, $html);
240
    }
241
}