Passed
Push — main ( f51ec7...6cfe97 )
by Sammy
01:30
created

ElementTest::testIsVoid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace HexMakina\Marker\tests;
5
6
use PHPUnit\Framework\TestCase;
7
use HexMakina\Marker\Element;
8
9
final class ElementTest extends TestCase
10
{
11
  /**
12
   * @var string[]
13
   */
14
    private array $tags = [];
15
16
17
    protected function setUp(): void
18
    {
19
      // https://html.spec.whatwg.org/multipage/indices.html#elements-3
20
        $this->tags = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'slot', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'SVG svg', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr'];
21
    }
22
23
    public function testEmptyConstructor(): void
24
    {
25
        $this->expectException(\ArgumentCountError::class);
26
        new Element();
0 ignored issues
show
Bug introduced by
The call to HexMakina\Marker\Element::__construct() has too few arguments starting with tag. ( Ignorable by Annotation )

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

26
        /** @scrutinizer ignore-call */ 
27
        new Element();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
27
    }
28
29
    public function testStaticCall(): void
30
    {
31
        foreach ($this->tags as $tag) {
32
            $e = new Element($tag);
33
            $e_static = Element::$tag();
34
            $this->assertEquals("$e", "$e_static");
35
        }
36
    }
37
38
    public function testIsVoid(): void
39
    {
40
        foreach ($this->tags as $tag) {
41
            $e = new Element($tag);
42
            $this->assertEquals($e->isVoid(), in_array($tag, Element::VOID_ELEMENTS));
43
        }
44
    }
45
46
    public function testCreateEmptyElement(): void
47
    {
48
        foreach ($this->tags as $tag) {
49
            $e = new Element($tag);
50
            if ($e->isVoid()) {
51
                $this->assertEquals('<' . $tag . '/>', $e->__toString());
52
            } else {
53
                $this->assertEquals(sprintf('<%s></%s>', $tag, $tag), $e->__toString());
54
            }
55
        }
56
    }
57
58
    public function testCreateElementWithContent(): void
59
    {
60
        $messages = [
61
        'lorem ipsum' => 'lorem ipsum',
62
        '' => '',
63
        null => ''
64
        ];
65
66
        foreach ($messages as $message => $expected) {
67
            foreach ($this->tags as $tag) {
68
                $e = new Element($tag, $message);
69
70
                if ($e->isVoid()) {
71
                    $this->assertEquals('<' . $tag . '/>', $e->__toString());
72
                } else {
73
                    $this->assertEquals(sprintf('<%s>%s</%s>', $tag, $expected, $tag), $e->__toString());
74
                }
75
            }
76
        }
77
    }
78
79
  // Attributes
80
    public function testCreateElementWithAttributes(): void
81
    {
82
        $message = 'lorem ipsum';
83
        $attributes = ['id' => 'test_id'];
84
        $attributes_expected_string = ' id="test_id"';
85
86
      // testing attributes string generator
87
        $this->assertEquals(Element::attributesAsString($attributes), $attributes_expected_string);
88
89
      // testing all HTML tags
90
        $this->assertForAllHTMLTags($message, $attributes, $attributes_expected_string);
91
    }
92
93
    public function testCreateElementWithBooleanAttributes(): void
94
    {
95
        $message = 'lorem ipsum';
96
        $attributes = ['id' => 'test_id', 'checked', 'class' => 'test_class', 'required'];
97
        $attributes_expected_string = ' id="test_id" checked class="test_class" required';
98
99
      // testing attributes string generator
100
        $this->assertEquals(Element::attributesAsString($attributes), $attributes_expected_string);
101
102
      // testing all HTML tags
103
        $this->assertForAllHTMLTags($message, $attributes, $attributes_expected_string);
104
    }
105
106
    public function testAttributesOrdering(): void
107
    {
108
        $message = 'lorem ipsum';
109
110
        $attributes = ['id' => 'test_id', 'class' => 'test_class', 'style="color:red;"'];
111
        $attributes_expected_string = ' id="test_id" class="test_class" style="color:red;"';
112
113
      // testing attributes string generator
114
        $this->assertEquals(Element::attributesAsString($attributes), $attributes_expected_string);
115
116
      // testing all HTML tags
117
        $this->assertForAllHTMLTags($message, $attributes, $attributes_expected_string);
118
119
120
        $attributes = ['class' => 'test_class', 'style="color:red;"','id' => 'test_id'];
121
        $attributes_expected_string = ' class="test_class" style="color:red;" id="test_id"';
122
123
      // testing attributes string generator
124
        $this->assertEquals(Element::attributesAsString($attributes), $attributes_expected_string);
125
126
      // testing all HTML tags
127
        $this->assertForAllHTMLTags($message, $attributes, $attributes_expected_string);
128
    }
129
130
131
  /**
132
   * @param mixed[] $attributes
133
   */
134
    private function assertForAllHTMLTags(string $message, array $attributes, string $attributes_expected_string): void
135
    {
136
        foreach ($this->tags as $tag) {
137
          // testing by instantiation
138
            $e = new Element($tag, $message, $attributes);
139
            if (in_array($tag, Element::VOID_ELEMENTS)) {
140
                $this->assertEquals(sprintf(Element::FORMAT_VOID, $tag, $attributes_expected_string), $e->__toString());
141
            } else {
142
                $this->assertEquals(sprintf(Element::FORMAT_ELEMENT, $tag, $attributes_expected_string, $message, $tag), $e->__toString());
143
            }
144
        }
145
    }
146
}
147