Skip to content

Commit 758e2a8

Browse files
authored
Merge pull request #2 from jjgrainger/feature/add-element-tests
Feature/add element tests
2 parents 0809b52 + 463f13d commit 758e2a8

5 files changed

Lines changed: 384 additions & 2 deletions

File tree

src/Traits/HasAttributes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getAttributes()
7979
*/
8080
public function getAttribute($key)
8181
{
82-
($this->hasAttribute($key)) ? $this->attribute[$key] : null;
82+
return ($this->hasAttribute($key)) ? $this->attributes[$key] : null;
8383
}
8484

8585
/**
@@ -101,7 +101,7 @@ public function hasAttributes()
101101
*/
102102
public function hasAttribute(string $key)
103103
{
104-
return !isset($this->attributes[$key]);
104+
return isset($this->attributes[$key]);
105105
}
106106

107107
/**

tests/Unit/HTML/ElementTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,58 @@ public function test_element_can_be_instantiated()
1212

1313
$this->assertInstanceOf(Element::class, $element);
1414
}
15+
16+
public function test_element_can_set_tag()
17+
{
18+
$element = new Element();
19+
20+
$element->setTag('a');
21+
22+
$this->assertEquals($element->getTag(), 'a');
23+
}
24+
25+
public function test_element_can_render()
26+
{
27+
$element = new Element();
28+
29+
$this->assertEquals($element->render(), '<div></div>');
30+
}
31+
32+
public function test_element_can_render_with_attributes()
33+
{
34+
$element = new Element();
35+
36+
$element->setAttributes([
37+
'id' => 'main',
38+
'class' => 'content',
39+
]);
40+
41+
$this->assertEquals($element->render(), '<div id="main" class="content"></div>');
42+
}
43+
44+
public function test_element_can_render_with_content()
45+
{
46+
$element = new Element('p');
47+
48+
$element->setContent('Hello World!');
49+
50+
$this->assertEquals($element->render(), '<p>Hello World!</p>');
51+
}
52+
53+
public function test_element_can_render_with_children()
54+
{
55+
$element = new Element('ol');
56+
57+
$element->addChild((new Element('li'))->setContent('List item 1'));
58+
$element->addChild((new Element('li'))->setContent('List item 2'));
59+
60+
$this->assertEquals($element->render(), '<ol><li>List item 1</li><li>List item 2</li></ol>');
61+
}
62+
63+
public function test_can_echo_to_string()
64+
{
65+
$element = new Element();
66+
67+
$this->assertEquals($element, '<div></div>');
68+
}
1569
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
2+
<?php
3+
4+
use HTML\Element;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class HasAttributesTest extends TestCase
8+
{
9+
public function test_can_set_attributes()
10+
{
11+
$element = new Element();
12+
13+
$attributes = [
14+
'class' => 'container',
15+
];
16+
17+
$element->setAttributes($attributes);
18+
19+
$this->assertEquals($element->getAttributes(), $attributes);
20+
}
21+
22+
public function test_can_set_attribute()
23+
{
24+
$element = new Element();
25+
26+
$attributes = [
27+
'class' => 'container',
28+
];
29+
30+
$element->setAttributes($attributes);
31+
32+
$element->setAttribute('id', 'main');
33+
34+
$attributes['id'] = 'main';
35+
36+
$this->assertEquals($element->getAttributes(), $attributes);
37+
}
38+
39+
public function test_can_append_attribute()
40+
{
41+
$element = new Element();
42+
43+
$attributes = [
44+
'class' => 'container',
45+
];
46+
47+
$element->setAttributes($attributes);
48+
49+
$element->appendAttribute('class', 'active');
50+
51+
$attributes['class'] .= ' active';
52+
53+
$this->assertEquals($element->getAttributes(), $attributes);
54+
}
55+
56+
public function test_can_append_attributes_when_empty()
57+
{
58+
$element = new Element();
59+
60+
61+
$element->appendAttribute('class', 'active');
62+
63+
$this->assertEquals($element->getAttributes(), ['class' => 'active']);
64+
}
65+
66+
public function test_can_get_attributes()
67+
{
68+
$element = new Element();
69+
70+
$attributes = [
71+
'class' => 'container',
72+
];
73+
74+
$element->setAttributes($attributes);
75+
76+
$this->assertEquals($element->getAttributes(), $attributes);
77+
}
78+
79+
public function test_can_get_attribute()
80+
{
81+
$element = new Element();
82+
83+
$attributes = [
84+
'class' => 'container',
85+
];
86+
87+
$element->setAttributes($attributes);
88+
89+
$this->assertEquals($element->getAttribute('class'), 'container');
90+
}
91+
92+
public function test_has_attributes()
93+
{
94+
$element = new Element();
95+
96+
$empty = $element->hasAttributes();
97+
98+
$attributes = [
99+
'class' => 'container',
100+
];
101+
102+
$element->setAttributes($attributes);
103+
104+
$this->assertTrue($element->hasAttributes());
105+
$this->assertFalse($empty);
106+
}
107+
108+
public function test_has_attribute_by_key()
109+
{
110+
$element = new Element();
111+
112+
$attributes = [
113+
'class' => 'container',
114+
];
115+
116+
$element->setAttributes($attributes);
117+
118+
$this->assertTrue($element->hasAttribute('class'));
119+
$this->assertFalse($element->hasAttribute('id'));
120+
}
121+
122+
public function test_can_remove_attributes()
123+
{
124+
$element = new Element();
125+
126+
$attributes = [
127+
'class' => 'container',
128+
];
129+
130+
$element->setAttributes($attributes);
131+
132+
$this->assertTrue($element->hasAttributes());
133+
134+
$element->removeAttributes();
135+
136+
$this->assertFalse($element->hasAttributes());
137+
$this->assertEquals($element->getAttributes(), []);
138+
}
139+
140+
public function test_can_remove_attribute_by_key()
141+
{
142+
$element = new Element();
143+
144+
$attributes = [
145+
'class' => 'container',
146+
'id' => 'main',
147+
];
148+
149+
$element->setAttributes($attributes);
150+
151+
$element->removeAttribute('id');
152+
153+
$this->assertFalse($element->hasAttribute('id'));
154+
$this->assertEquals($element->getAttributes(), ['class' => 'container']);
155+
}
156+
157+
public function test_can_render_attributes()
158+
{
159+
$element = new Element();
160+
161+
$attributes = [
162+
'class' => 'container',
163+
'id' => 'main',
164+
];
165+
166+
$element->setAttributes($attributes);
167+
168+
$this->assertEquals($element->renderAttributes(), 'class="container" id="main"');
169+
}
170+
}
171+
172+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
use HTML\Element;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class HasChildrenTest extends TestCase
7+
{
8+
public function test_can_set_children()
9+
{
10+
$element = new Element();
11+
12+
$children = [
13+
new Element(),
14+
];
15+
16+
$element->setChildren($children);
17+
18+
$this->assertEquals($element->getChildren(), $children);
19+
}
20+
21+
public function test_can_add_children()
22+
{
23+
$element = new Element();
24+
25+
$child = new Element();
26+
27+
$element->addChild($child);
28+
29+
$this->assertEquals($element->getChildren(), [$child]);
30+
}
31+
32+
public function test_can_get_children()
33+
{
34+
$element = new Element();
35+
36+
$children = [
37+
new Element(),
38+
new Element(),
39+
new Element(),
40+
];
41+
42+
$element->setChildren($children);
43+
44+
$this->assertEquals($element->getChildren(), $children);
45+
}
46+
47+
public function test_has_children()
48+
{
49+
$element = new Element();
50+
51+
$this->assertFalse($element->hasChildren());
52+
53+
$element->addChild(new Element());
54+
55+
$this->assertTrue($element->hasChildren());
56+
}
57+
58+
public function test_can_render_children()
59+
{
60+
$element = new Element();
61+
62+
$children = [
63+
(new Element('li'))->setContent('List item 1'),
64+
(new Element('li'))->setContent('List item 2'),
65+
];
66+
67+
$element->setChildren($children);
68+
69+
$this->assertEquals($element->renderChildren(), '<li>List item 1</li><li>List item 2</li>');
70+
}
71+
}

0 commit comments

Comments
 (0)