ElementTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 12
Bugs 0 Features 3
Metric Value
eloc 81
c 12
b 0
f 3
dl 0
loc 157
rs 10
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testVoidElementWithAttribute() 0 10 1
A testVoidElement() 0 5 1
A testCanBeUsedAsString() 0 5 1
A testCanBeCreatedFromValidTagName() 0 5 1
A testCanHaveBooleanAttribute() 0 7 1
A testCanHaveArrayClassAttribute() 0 6 1
A testCanHaveMultipleAttributes() 0 7 1
A testCanHaveStringClassAttribute() 0 7 1
A testCanHaveMixedAttributes() 0 18 1
A testContentHasSpecialCaracters() 0 27 1
A testFormatterFalse() 0 28 1
1
<?php
2
3
declare(strict_types=1);
4
5
use PHPUnit\Framework\TestCase;
6
use HexMakina\Marker\Element;
7
8
final class ElementTest extends TestCase
9
{
10
    public function testCanBeCreatedFromValidTagName(): void
11
    {
12
        $this->assertInstanceOf(
13
            Element::class,
14
            new Element('div')
15
        );
16
    }
17
18
    public function testCanBeUsedAsString(): void
19
    {
20
        $this->assertEquals(
21
            '<div>test</div>',
22
            Element::div('test')->__toString()
0 ignored issues
show
Bug introduced by
The method div() does not exist on HexMakina\Marker\Element. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
            Element::/** @scrutinizer ignore-call */ 
23
                     div('test')->__toString()
Loading history...
23
        );
24
    }
25
26
    public function testVoidElement(): void
27
    {
28
        $this->assertEquals(
29
            '<hr/>',
30
            Element::hr()->__toString()
0 ignored issues
show
Bug introduced by
The method hr() does not exist on HexMakina\Marker\Element. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
            Element::/** @scrutinizer ignore-call */ 
31
                     hr()->__toString()
Loading history...
31
        );
32
    }
33
34
    public function testVoidElementWithAttribute(): void
35
    {
36
        $this->assertEquals(
37
            '<img src="test.jpg"/>',
38
            Element::img(null, ['src' => 'test.jpg'])->__toString()
0 ignored issues
show
Bug introduced by
The method img() does not exist on HexMakina\Marker\Element. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            Element::/** @scrutinizer ignore-call */ 
39
                     img(null, ['src' => 'test.jpg'])->__toString()
Loading history...
39
        );
40
41
        $this->assertEquals(
42
            '<img src="test.jpg"/>',
43
            (new Element('img', null, ['src' => 'test.jpg']))->__toString()
44
        );
45
    }
46
47
    public function testCanHaveBooleanAttribute(): void
48
    {
49
        $element = new Element('input', null, ['checked']);
50
51
        $this->assertEquals(
52
            '<input checked/>',
53
            $element->__toString()
54
        );
55
    }
56
57
    public function testCanHaveMixedAttributes(): void
58
    {
59
        $element = new Element('input', null, ['type' => 'radio', 'name' => 'gaga', 'checked']);
60
        $this->assertEquals(
61
            '<input type="radio" name="gaga" checked/>',
62
            $element->__toString()
63
        );
64
65
        $element->class = 'test';
0 ignored issues
show
Bug Best Practice introduced by
The property class does not exist on HexMakina\Marker\Element. Since you implemented __set, consider adding a @property annotation.
Loading history...
66
        $this->assertEquals(
67
            '<input type="radio" name="gaga" checked class="test"/>',
68
            $element->__toString()
69
        );
70
71
        $element->class = ['foo', 'bar'];
72
        $this->assertEquals(
73
            '<input type="radio" name="gaga" checked class="foo bar"/>',
74
            $element->__toString()
75
        );
76
    }
77
78
    public function testCanHaveStringClassAttribute(): void
79
    {
80
        $element = new Element('div', 'with class', ['class' => 'test']);
81
82
        $this->assertEquals(
83
            '<div class="test">with class</div>',
84
            $element->__toString()
85
        );
86
    }
87
88
    public function testCanHaveArrayClassAttribute(): void
89
    {
90
        $element = new Element('div', 'Hello, World!', ['class' => ['foo', 'bar']]);
91
        $this->assertEquals(
92
            '<div class="foo bar">Hello, World!</div>',
93
            $element->__toString()
94
        );
95
    }
96
97
    public function testCanHaveMultipleAttributes(): void
98
    {
99
        $element = new Element('div', 'Hello, World!', ['class' => ['foo', 'bar'], 'id' => 'test']);
100
101
        $this->assertEquals(
102
            '<div class="foo bar" id="test">Hello, World!</div>',
103
            $element->__toString()
104
        );
105
    }
106
107
    public function testContentHasSpecialCaracters(): void
108
    {
109
        $element = new Element('div', 'Hello, World! & < > " \' ', ['class' => ['foo', 'bar'], 'id' => 'test']);
110
111
        $this->assertEquals(
112
            '<div class="foo bar" id="test">Hello, World! &amp; &lt; &gt; &quot; &#039; </div>',
113
            $element->__toString()
114
        );
115
116
        $element = new Element('div', 'Hello, World! & < > " \' ', ['class' => [
117
            'foo',
118
            'bar'
119
        ], 'id' => 'test', 'data-attr' => 'Hello, World! & < > " \' ']);
120
121
        $this->assertEquals(
122
            '<div class="foo bar" id="test" data-attr="Hello, World! &amp; &lt; &gt; &quot; &#039; ">Hello, World! &amp; &lt; &gt; &quot; &#039; </div>',
123
            $element->__toString()
124
        );
125
126
        $element = new Element('div', 'Hello, World! & < > " \' ', ['class' => [
127
            'foo',
128
            'bar'
129
        ], 'id' => 'test', 'data-attr' => 'Hello, World! & < > " \' ', 'data-attr2' => 'Hello, World! & < > " \' ']);
130
131
        $this->assertEquals(
132
            '<div class="foo bar" id="test" data-attr="Hello, World! &amp; &lt; &gt; &quot; &#039; " data-attr2="Hello, World! &amp; &lt; &gt; &quot; &#039; ">Hello, World! &amp; &lt; &gt; &quot; &#039; </div>',
133
            $element->__toString()
134
        );
135
    }
136
137
    public function testFormatterFalse(): void
138
    {
139
        $content = 'Hello, World! & < > " \' ';
140
        $element = new Element('div', $content, ['class' => ['foo', 'bar'], 'id' => 'test'], false);
141
142
        $this->assertEquals(
143
            '<div class="foo bar" id="test">' . $content . '</div>',
144
            $element->__toString()
145
        );
146
        $content = 'Hello, World! & < > " \' ';
147
        $element = new Element('div', $content, ['class' => [
148
            'foo',
149
            'bar'
150
        ], 'id' => 'test', 'data-attr' => $content], false);
151
152
        $this->assertEquals(
153
            '<div class="foo bar" id="test" data-attr="' . $content . '">' . $content . '</div>',
154
            $element->__toString()
155
        );
156
157
        $content = 'Hello, World! & < > " \' ';
158
159
        $element = new Element('div', $content, ['class' => ['foo', 'bar'], 'id' => 'test', 'data-attr' => $content, 'data-attr2' => $content], false);
160
161
        $this->assertEquals(
162
            '<div class="foo bar" id="test" data-attr="' . $content . '" data-attr2="' . $content .
163
                '">' . $content . '</div>',
164
            $element->__toString()
165
        );
166
    }
167
}
168