Completed
Push — master ( e84808...5a9d2e )
by Oleg
07:58
created

MenuTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 9
dl 7
loc 98
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A menuFactory() 0 13 1
A testFacade() 0 4 1
A testGet() 0 6 1
A testGetException() 0 4 1
A testHas() 0 6 1
A testForget() 0 6 1
A testRender() 0 6 1
B testFromArray() 7 37 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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');
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...
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) {
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...
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
}