Total Complexity | 15 |
Total Lines | 114 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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(); |
||
|
|||
27 | } |
||
28 | |||
29 | public function testStaticCall(): void |
||
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 |
||
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"'; |
||
147 |
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.