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

TextTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 9
c 3
b 1
f 0
lcom 1
cbo 4
dl 0
loc 94
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 4 1
A elementRender() 0 4 1
A serializeStub() 0 4 1
A toArrayStub() 0 11 1
A testFactory() 0 10 1
A testElement() 0 12 1
A testSerialization() 0 14 1
A testToArray() 0 9 1
A testView() 0 12 1
1
<?php
2
namespace Malezha\Menu\Tests\ElementTests;
3
4
use Malezha\Menu\Element\Text;
5
use Malezha\Menu\Factory\TextFactory;
6
use Malezha\Menu\Tests\TestCase;
7
8
class TextTest extends TestCase
9
{
10
    /**
11
     * @return TextFactory
12
     */
13
    protected function factory()
14
    {
15
        return new TextFactory($this->app);
16
    }
17
18
    protected function elementRender()
19
    {
20
        return "<li data-value=\"Element\">Element</li>\n";
21
    }
22
    
23
    protected function serializeStub()
24
    {
25
        return $this->getStub('serialized/text_element.txt');
26
    }
27
    
28
    protected function toArrayStub()
29
    {
30
        return [
31
            'view' => config('menu.elements')[Text::class]['view'],
32
            'text' => 'To array',
33
            'attributes' => [
34
                'class' => 'arrayable',
35
            ],
36
            'displayRule' => true
37
        ];
38
    }
39
    
40
    public function testFactory()
41
    {
42
        $factory = $this->factory();
43
        $factory->text = 'Some text';
44
        $factory->attributes->put('class', 'text-element');
45
        $text = $factory->build();
46
47
        $this->assertAttributeEquals('Some text', 'text', $text);
48
        $this->assertEquals('text-element', $text->attributes->get('class'));
49
    }
50
51
    public function testElement()
52
    {
53
        $factory = $this->factory();
54
        $element = $factory->build();
55
56
        $element->text = 'Element';
57
        $element->attributes->put('data-value', $element->text);
58
        
59
        $this->assertEquals('Element', $element->text);
60
        $this->assertEquals($element->text, $element->attributes->get('data-value'));
61
        $this->assertEquals($this->elementRender(), $element->render());
62
    }
63
    
64
    public function testSerialization()
65
    {
66
        $tmp = 0;
67
        $factory = $this->factory();
68
        $factory->text = 'Serialize';
69
        $factory->attributes->put('class', 'serialized');
70
        $factory->displayRule = function () use ($tmp) {
71
            return $tmp === 0;
72
        };
73
        $element = $factory->build();
74
75
        $this->assertEquals($this->serializeStub(), serialize($element));
76
        $this->assertEquals($element, unserialize($this->serializeStub()));
77
    }
78
    
79
    public function testToArray()
80
    {
81
        $factory = $this->factory();
82
        $factory->text = 'To array';
83
        $factory->attributes->put('class', 'arrayable');
84
        $element = $factory->build();
85
        
86
        $this->assertEquals($this->toArrayStub(), $element->toArray());
87
    }
88
    
89
    public function testView()
90
    {
91
        view()->addLocation(__DIR__ . '/../stub');
92
        $this->app['config']->prepend('menu.paths', __DIR__ . '/../stub');
93
        $view = 'text_element';
94
95
        $element = $this->factory()->build(['text' => 'Block']);
96
        $element->setView($view);
97
        $this->assertAttributeEquals($view, 'view', $element);
98
        $this->assertEquals($view, $element->getView());
99
        $this->assertEquals('<div>Block</div>', $element->render());
100
    }
101
}