Passed
Push — main ( 5c15a6...ae0e18 )
by Sammy
01:48
created

ElementTest::testCallStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 13
rs 9.9666
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
use PHPUnit\Framework\TestCase;
4
5
use \HexMakina\Marker\Element;
6
7
final class ElementTest extends TestCase
8
{
9
  private $tags;
10
  private $tags_void;
0 ignored issues
show
introduced by
The private property $tags_void is not used, and could be removed.
Loading history...
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();
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

21
      /** @scrutinizer ignore-call */ 
22
      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...
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
92
  {
93
    $message = 'lorem ipsum';
94
95
    $attributes = ['id' => 'test_id', 'class' => 'test_class', 'style="color:red;"'];
96
    $attributes_expected_string = ' id="test_id" class="test_class" style="color:red;"';
97
98
    // testing attributes string generator
99
    $this->assertEquals(Element::attributesAsString($attributes), $attributes_expected_string);
100
101
    // testing all HTML tags
102
    $this->assertForAllHTMLTags($message, $attributes, $attributes_expected_string);
103
104
105
    $attributes = ['class' => 'test_class', 'style="color:red;"','id' => 'test_id'];
106
    $attributes_expected_string = ' class="test_class" style="color:red;" id="test_id"';
107
108
    // testing attributes string generator
109
    $this->assertEquals(Element::attributesAsString($attributes), $attributes_expected_string);
110
111
    // testing all HTML tags
112
    $this->assertForAllHTMLTags($message, $attributes, $attributes_expected_string);
113
  }
114
115
116
  private function assertForAllHTMLTags($message, $attributes, $attributes_expected_string)
117
  {
118
    foreach($this->tags as $tag){
119
120
      // testing by instantiation
121
      $e = new Element($tag, $message,  $attributes);
122
      if(in_array($tag, Element::VOID_ELEMENTS)){
123
        $this->assertEquals(sprintf(Element::FORMAT_VOID, $tag, $attributes_expected_string), $e->__toString());
124
      }
125
      else {
126
        $this->assertEquals(sprintf(Element::FORMAT_ELEMENT, $tag, $attributes_expected_string, $message, $tag), $e->__toString());
127
      }
128
129
      // testing by callStatic();
130
      $e_stat = Element::$tag($message,  $attributes);
131
      $this->assertEquals($e_stat, $e->__toString());
132
    }
133
  }
134
}
135