Passed
Branch main (0138e9)
by Sammy
06:29
created

ElementTest::testEmptyConstructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
  public function testEmptyConstructor(): void
10
  {
11
      $this->expectException(ArgumentCountError::class);
12
      $e = 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

12
      $e = /** @scrutinizer ignore-call */ 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...
Unused Code introduced by
The assignment to $e is dead and can be removed.
Loading history...
13
  }
14
15
  public function testCreateEmptyElement(): void
16
  {
17
    $e = new Element('p');
18
    $this->assertEquals('<p></p>', $e->__toString());
19
20
    foreach(Element::VOID_ELEMENTS as $tag){
21
      $e = new Element($tag);
22
      $this->assertEquals('<'.$tag.'/>', $e->__toString());
23
    }
24
  }
25
26
27
  public function testCreateElementWithContent(): void
28
  {
29
    $message = 'lorem ipsum';
30
    $e = new Element('p', $message);
31
    $this->assertEquals('<p>'.$message.'</p>', $e->__toString());
32
33
    foreach(Element::VOID_ELEMENTS as $tag){
34
      $e = new Element($tag, $message);
35
      $this->assertEquals('<'.$tag.'/>', $e->__toString());
36
    }
37
38
39
    $message = null;
40
    $e = new Element('p', $message);
41
    $this->assertEquals('<p></p>', $e->__toString());
42
43
    foreach(Element::VOID_ELEMENTS as $tag){
44
      $e = new Element($tag, $message);
45
      $this->assertEquals('<'.$tag.'/>', $e->__toString());
46
    }
47
48
    $message = [];
49
    $e = new Element('p', $message);
50
    $this->assertEquals('<p></p>', $e->__toString());
51
52
    foreach(Element::VOID_ELEMENTS as $tag){
53
      $e = new Element($tag, $message);
54
      $this->assertEquals('<'.$tag.'/>', $e->__toString());
55
    }
56
  }
57
58
  public function testCreateElementWithAttributes(): void
59
  {
60
    $message = 'lorem ipsum';
61
    $attributes = ['id' => 'test_id'];
62
63
    $e = new Element('p', $message,  $attributes);
64
    $this->assertEquals('<p id="test_id">'.$message.'</p>', $e->__toString());
65
66
    foreach(Element::VOID_ELEMENTS as $tag){
67
      $e = new Element($tag, $message,  $attributes);
68
      $this->assertEquals('<'.$tag.' id="test_id"/>', $e->__toString());
69
    }
70
  }
71
72
  public function testCreateElementWithBooleanAttributes(): void
73
  {
74
    $message = 'lorem ipsum';
75
    $attributes = ['id' => 'test_id', 'checked', 'class' => 'test_class', 'required'];
76
77
    $e = new Element('p', $message,  $attributes);
78
    $this->assertEquals('<p id="test_id" checked class="test_class" required>'.$message.'</p>', $e->__toString());
79
80
    foreach(Element::VOID_ELEMENTS as $tag){
81
      $e = new Element($tag, $message,  $attributes);
82
      $this->assertEquals('<'.$tag.' id="test_id" checked class="test_class" required/>', $e->__toString());
83
    }
84
  }
85
86
  public function testAttributesOrdering(): void
87
  {
88
    $message = 'lorem ipsum';
89
    $attributes = ['id' => 'test_id', 'class' => 'test_class', 'style="color:red;"'];
90
    $e = new Element('p', $message,  $attributes);
91
    $this->assertEquals('<p id="test_id" class="test_class" style="color:red;">'.$message.'</p>', $e->__toString());
92
93
    $attributes = ['class' => 'test_class', 'style="color:red;"','id' => 'test_id'];
94
    $e = new Element('p', $message,  $attributes);
95
    $this->assertEquals('<p class="test_class" style="color:red;" id="test_id">'.$message.'</p>', $e->__toString());
96
  }
97
98
  public function testCallStatic(): void
99
  {
100
    $message = 'lorem ipsum';
101
    $attributes = ['id' => 'test_id', 'checked', 'class' => 'test_class', 'required'];
102
103
    $e = new Element('p', $message,  $attributes);
104
    $e_stat = Element::p($message,  $attributes);
0 ignored issues
show
Bug introduced by
The method p() 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

104
    /** @scrutinizer ignore-call */ 
105
    $e_stat = Element::p($message,  $attributes);
Loading history...
105
    $this->assertEquals($e->__toString(), $e_stat);
106
107
    foreach(Element::VOID_ELEMENTS as $tag){
108
      $e = new Element($tag, $message,  $attributes);
109
      $e_stat = Element::$tag($message,  $attributes);
110
      $this->assertEquals($e_stat, $e->__toString());
111
    }
112
  }
113
114
}
115