Total Complexity | 15 |
Total Lines | 125 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php declare(strict_types=1); |
||
7 | final class ElementTest extends TestCase |
||
8 | { |
||
9 | private $tags; |
||
10 | private $tags_void; |
||
|
|||
11 | |||
12 | public function setUp(): void |
||
13 | { |
||
14 | // https://html.spec.whatwg.org/multipage/indices.html#elements-3 |
||
15 | $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']; |
||
16 | |||
17 | } |
||
18 | public function testEmptyConstructor(): void |
||
19 | { |
||
20 | $this->expectException(ArgumentCountError::class); |
||
21 | new Element(); |
||
22 | } |
||
23 | |||
24 | public function testCreateEmptyElement(): void |
||
25 | { |
||
26 | foreach($this->tags as $tag) |
||
27 | { |
||
28 | $e = new Element($tag); |
||
29 | |||
30 | if(in_array($tag, Element::VOID_ELEMENTS)){ |
||
31 | $this->assertEquals('<'.$tag.'/>', $e->__toString()); |
||
32 | } |
||
33 | else { |
||
34 | $this->assertEquals("<$tag></$tag>", $e->__toString()); |
||
35 | } |
||
36 | |||
37 | $e_stat = Element::$tag(); |
||
38 | $this->assertEquals($e_stat, $e->__toString()); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | |||
43 | public function testCreateElementWithContent(): void |
||
44 | { |
||
45 | $messages = ['lorem ipsum' => 'lorem ipsum', '' => '', null => '']; |
||
46 | foreach($messages as $message => $expected){ |
||
47 | foreach($this->tags as $tag) |
||
48 | { |
||
49 | $e = new Element($tag, $message); |
||
50 | |||
51 | if(in_array($tag, Element::VOID_ELEMENTS)){ |
||
52 | $this->assertEquals('<'.$tag.'/>', $e->__toString()); |
||
53 | } |
||
54 | else { |
||
55 | $this->assertEquals("<$tag>$expected</$tag>", $e->__toString()); |
||
56 | } |
||
57 | |||
58 | $e_stat = Element::$tag($message); |
||
59 | $this->assertEquals($e_stat, $e->__toString()); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | public function testCreateElementWithAttributes(): void |
||
65 | { |
||
66 | $message = 'lorem ipsum'; |
||
67 | $attributes = ['id' => 'test_id']; |
||
68 | $attributes_expected_string = ' id="test_id"'; |
||
69 | |||
70 | // testing attributes string generator |
||
71 | $this->assertEquals(Element::attributesAsString($attributes), $attributes_expected_string); |
||
72 | |||
73 | // testing all HTML tags |
||
74 | $this->assertForAllHTMLTags($message, $attributes, $attributes_expected_string); |
||
75 | } |
||
76 | |||
77 | public function testCreateElementWithBooleanAttributes(): void |
||
78 | { |
||
79 | $message = 'lorem ipsum'; |
||
80 | $attributes = ['id' => 'test_id', 'checked', 'class' => 'test_class', 'required']; |
||
81 | $attributes_expected_string = ' id="test_id" checked class="test_class" required'; |
||
82 | |||
83 | // testing attributes string generator |
||
84 | $this->assertEquals(Element::attributesAsString($attributes), $attributes_expected_string); |
||
85 | |||
86 | // testing all HTML tags |
||
87 | $this->assertForAllHTMLTags($message, $attributes, $attributes_expected_string); |
||
88 | } |
||
89 | |||
90 | |||
91 | public function testAttributesOrdering(): void |
||
113 | } |
||
114 | |||
115 | |||
116 | private function assertForAllHTMLTags($message, $attributes, $attributes_expected_string) |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 |