Passed
Push — main ( c80162...2c6f81 )
by Sammy
01:46
created

ElementTest::testFormatterFalse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.6666
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' => ['foo',
117
            'bar'], 'id' => 'test', 'data-attr' => 'Hello, World! & < > " \' ']);   
118
119
        $this->assertEquals(
120
            '<div class="foo bar" id="test" data-attr="Hello, World! &amp; &lt; &gt; &quot; &#039; ">Hello, World! &amp; &lt; &gt; &quot; &#039; </div>',
121
            $element->__toString()
122
        );
123
124
        $element = new Element('div', 'Hello, World! & < > " \' ', ['class' => ['foo',
125
            'bar'], 'id' => 'test', 'data-attr' => 'Hello, World! & < > " \' ', 'data-attr2' => 'Hello, World! & < > " \' ']);
126
127
        $this->assertEquals(
128
            '<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>',
129
            $element->__toString()
130
        );
131
    }
132
133
    public function testFormatterFalse(): void
134
    {
135
        $element = new Element('div', 'Hello, World! & < > " \' ', ['class' => ['foo', 'bar'], 'id' => 'test'], false);
136
137
        $this->assertEquals(
138
            '<div class="foo bar" id="test">Hello, World! &amp; &lt; &gt; &quot; &#039; </div>',
139
            $element->__toString()
140
        );
141
142
        $element = new Element('div', 'Hello, World! & < > " \' ', ['class' => [
143
            'foo',
144
            'bar'
145
        ], 'id' => 'test', 'data-attr' => 'Hello, World! & < > " \' '], false);
146
147
        $this->assertEquals(
148
            '<div class="foo bar" id="test" data-attr="Hello, World! &amp; &lt; &gt; &quot; &#039; ">Hello, World! &amp; &lt; &gt; &quot; &#039; </div>',
149
            $element->__toString()
150
        );
151
152
        $element = new Element('div', 'Hello, World! & < > " \' ', ['class' => [
153
            'foo',
154
            'bar'
155
        ], 'id' => 'test', 'data-attr' => 'Hello, World! & < > " \' ', 'data-attr2' => 'Hello, World! & < > " \' '], false);
156
157
        $this->assertEquals(
158
            '<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>',
159
            $element->__toString()
160
        ); 
161
    }
162
}
163