Completed
Push — master ( 79027c...8f1f38 )
by Oleg
03:47
created

RenderTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 27.78 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 30
loc 108
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 9

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\Attributes;
5
use Malezha\Menu\Contracts\Builder;
6
use Malezha\Menu\Contracts\MenuRender;
7
use Malezha\Menu\Element\Link;
8
use Malezha\Menu\Element\SubMenu;
9
use Malezha\Menu\Factory\LinkFactory;
10
use Malezha\Menu\Factory\SubMenuFactory;
11
use Malezha\Menu\Render\Basic;
12
use Malezha\Menu\Render\Illuminate;
13
14
class RenderTest extends TestCase
15
{
16
    protected function getBuilder()
17
    {
18
        /** @var Builder $builder */
19
        $builder = $this->app->makeWith(Builder::class, [
20
            'activeAttributes' => $this->app->makeWith(Attributes::class, ['attributes' => ['class' => 'active']]),
21
            'attributes' => $this->app->makeWith(Attributes::class, ['attributes' => ['class' => 'menu']]),
22
        ]);
23
24
        $builder->create('index', Link::class, function(LinkFactory $factory) {
25
            $factory->title = 'Index Page';
26
            $factory->url = url('/');
27
            $factory->linkAttributes->push(['class' => 'menu-link']);
28
29
            return $factory->build();
30
        });
31
32
        $builder->create('orders', SubMenu::class, function(SubMenuFactory $factory) {
33
            $factory->attributes->push(['class' => 'child-menu']);
34
            $factory->title = 'Orders';
35
            $factory->url = 'javascript:;';
36
37
            $factory->builder->create('all', Link::class, function(LinkFactory $factory) {
38
                $factory->title = 'All';
39
                $factory->url = url('/orders/all');
40
41
                return $factory->build();
42
            });
43
44
            $factory->builder->create('type_1', Link::class, function(LinkFactory $factory) {
45
                $factory->title = 'Type 1';
46
                $factory->url = url('/orders/1');
47
                $factory->linkAttributes->push(['class' => 'text-color-red']);
48
49
                return $factory->build();
50
            });
51
52
            $factory->builder->create('type_2', Link::class, function(LinkFactory $factory) {
53
                $factory->title = 'Type 2';
54
                $factory->url = url('/orders/2');
55
                $factory->linkAttributes->push(['data-attribute' => 'value']);
56
57
                return $factory->build();
58
            });
59
60
            return $factory->build();
61
        });
62
63
        return $builder;
64
    }
65
    
66
    protected function makeTest($render)
67
    {
68
        $this->app->instance('menu.render', $render);
69
        $this->app->alias('menu.render', MenuRender::class);
70
71
        $builder = $this->getBuilder();
72
        $this->assertAttributeInstanceOf(get_class($render), 'viewFactory', $builder);
73
        $this->assertEquals($this->getStub('menu.html'), $builder->render());
74
    }
75
76
    public function testBladeRender()
77
    {
78
        $this->makeTest(new Illuminate($this->app));
79
80
    }
81
82
    public function testBasicRender()
83
    {
84
        $this->makeTest(new Basic($this->app));
85
    }
86
87
    public function testDirectoryPath()
88
    {
89
        $path = __DIR__ . '/stub';
90
        $view = 'directory.view';
91
        $view2 = 'directory/view';
92
        $text = 'Hello, Menu builder!';
93
        
94
        $this->app['config']->prepend('menu.paths', $path);
95
        $this->app['view']->addLocation($path);
96
        
97
        $basic = new Basic($this->app);
98
        $this->assertEquals($text, $basic->make($view)->with('menu', 'Menu')->render());
99
        $this->assertEquals($text, $basic->make($view2)->with('menu', 'Menu')->render());
100
        
101
        $blade = new Illuminate($this->app);
102
        $this->assertEquals($text, $blade->make($view)->with('menu', 'Menu')->render());
103
        $this->assertEquals($text, $blade->make($view2)->with('menu', 'Menu')->render());
104
    }
105
106
    public function testMakeExceptionBasic()
107
    {
108
        $this->expectException(\Exception::class);
109
110
        $basic = new Basic($this->app);
111
        $basic->make('view_not_found');
112
    }
113
114
    public function testMakeExceptionIlluminate()
115
    {
116
        $this->expectException(\Exception::class);
117
118
        $blade = new Illuminate($this->app);
119
        $blade->make('view_not_found');
120
    }
121
}