Completed
Push — master ( 5a9d2e...4895be )
by Oleg
04:31
created

RenderTest::testMakeExceptionIlluminate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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\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->make(Builder::class, [
20
            'activeAttributes' => $this->app->make(Attributes::class, ['attributes' => ['class' => 'active']]),
21
            'attributes' => $this->app->make(Attributes::class, ['attributes' => ['class' => 'menu']]),
22
        ]);
23
        $builder->create('index', Link::class, function(LinkFactory $factory) {
24
            $factory->title = 'Index Page';
25
            $factory->url = url('/');
26
            $factory->linkAttributes->push(['class' => 'menu-link']);
27
        });
28 View Code Duplication
        $builder->create('orders', 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...
29
            $factory->attributes->push(['class' => 'child-menu']);
30
            $factory->title = 'Orders';
31
            $factory->url = 'javascript:;';
32
33
            $factory->builder->create('all', Link::class, function(LinkFactory $factory) {
34
                $factory->title = 'All';
35
                $factory->url = url('/orders/all');
36
            });
37
            $factory->builder->create('type_1', Link::class, function(LinkFactory $factory) {
38
                $factory->title = 'Type 1';
39
                $factory->url = url('/orders/1');
40
                $factory->linkAttributes->push(['class' => 'text-color-red']);
41
            });
42
            $factory->builder->create('type_2', Link::class, function(LinkFactory $factory) {
43
                $factory->title = 'Type 2';
44
                $factory->url = url('/orders/2');
45
                $factory->linkAttributes->push(['data-attribute' => 'value']);
46
            });
47
        });
48
49
        return $builder;
50
    }
51
    
52
    protected function makeTest($render)
53
    {
54
        $this->app->instance('menu.render', $render);
55
        $this->app->alias('menu.render', MenuRender::class);
56
57
        $builder = $this->getBuilder();
58
        $this->assertAttributeInstanceOf(get_class($render), 'viewFactory', $builder);
59
        $this->assertEquals($this->getStub('menu.html'), $builder->render());
60
    }
61
62
    public function testBladeRender()
63
    {
64
        $this->makeTest(new Illuminate($this->app));
65
66
    }
67
68
    public function testBasicRender()
69
    {
70
        $this->makeTest(new Basic($this->app));
71
    }
72
73
    public function testDirectoryPath()
74
    {
75
        $path = __DIR__ . '/stub';
76
        $view = 'directory.view';
77
        $view2 = 'directory/view';
78
        $text = 'Hello, Menu builder!';
79
        
80
        $this->app['config']->prepend('menu.paths', $path);
81
        $this->app['view']->addLocation($path);
82
        
83
        $basic = new Basic($this->app);
84
        $this->assertEquals($text, $basic->make($view)->with('menu', 'Menu')->render());
85
        $this->assertEquals($text, $basic->make($view2)->with('menu', 'Menu')->render());
86
        
87
        $blade = new Illuminate($this->app);
88
        $this->assertEquals($text, $blade->make($view)->with('menu', 'Menu')->render());
89
        $this->assertEquals($text, $blade->make($view2)->with('menu', 'Menu')->render());
90
    }
91
92
    public function testMakeExceptionBasic()
93
    {
94
        $this->expectException(\Exception::class);
95
96
        $basic = new Basic($this->app);
97
        $basic->make('view_not_found');
98
    }
99
100
    public function testMakeExceptionIlluminate()
101
    {
102
        $this->expectException(\Exception::class);
103
104
        $blade = new Illuminate($this->app);
105
        $blade->make('view_not_found');
106
    }
107
}