Completed
Push — master ( df2920...e54ea7 )
by Oleg
06:17
created

RenderTest::testDirectoryPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 18
rs 9.4285
cc 1
eloc 13
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
use Malezha\Menu\Contracts\MenuRender;
8
use Malezha\Menu\Render\Basic;
9
use Malezha\Menu\Render\Blade;
10
11
class RenderTest extends TestCase
12
{
13
    protected function getBuilder()
14
    {
15
        /** @var Builder $builder */
16
        $builder = $this->app->make(Builder::class, [
17
            'name' => 'test',
18
            'activeAttributes' => $this->app->make(Attributes::class, ['attributes' => ['class' => 'active']]),
19
            'attributes' => $this->app->make(Attributes::class, ['attributes' => ['class' => 'menu']]),
20
        ]);
21
        $index = $builder->add('index', 'Index Page', url('/'));
22
        $index->getLink()->getAttributes()->push(['class' => 'menu-link']);
23
24
        $builder->group('orders', function(Item $item) {
25
            $item->getAttributes()->push(['class' => 'child-menu']);
26
27
            $link = $item->getLink();
28
            $link->setTitle('Orders');
29
            $link->setUrl('javascript:;');
30 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...
31
            $menu->add('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::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...
32
            $menu->add('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::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...
33
34
            $menu->add('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::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...
35
                $item->getLink()->getAttributes()->push(['data-attribute' => 'value']);
36
            });
37
        });
38
39
        return $builder;
40
    }
41
42
    protected function getStub()
43
    {
44
        return $file = file_get_contents(__DIR__ . '/stub/menu.html');
0 ignored issues
show
Unused Code introduced by
$file 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...
45
    }
46
47
    protected function makeTest($render)
48
    {
49
        $this->app->instance('menu.render', $render);
50
        $this->app->alias('menu.render', MenuRender::class);
51
52
        $builder = $this->getBuilder();
53
        $stub = $this->getStub();
54
        $this->assertAttributeInstanceOf(get_class($render), 'viewFactory', $builder);
55
        $this->assertEquals($stub, $builder->render());
56
    }
57
58
    public function testBladeRender()
59
    {
60
        $this->makeTest(new Blade($this->app));
61
62
    }
63
64
    public function testBasicRender()
65
    {
66
        $this->makeTest(new Basic($this->app));
67
    }
68
69
    public function testDirectoryPath()
70
    {
71
        $path = __DIR__ . '/stub';
72
        $view = 'directory.view';
73
        $view2 = 'directory/view';
74
        $text = 'Hello, Menu builder!';
75
        
76
        $this->app['config']->prepend('menu.paths', $path);
77
        $this->app['view']->addLocation($path);
78
        
79
        $basic = new Basic($this->app);
80
        $this->assertEquals($text, $basic->make($view)->render());
81
        $this->assertEquals($text, $basic->make($view2)->render());
82
        
83
        $blade = new Blade($this->app);
84
        $this->assertEquals($text, $blade->make($view)->render());
85
        $this->assertEquals($text, $blade->make($view2)->render());
86
    }
87
}