Skip to content

Commit

Permalink
Merge pull request #2 from jjgrainger/feature/add-element-tests
Browse files Browse the repository at this point in the history
Feature/add element tests
  • Loading branch information
jjgrainger authored May 18, 2022
2 parents 0809b52 + 463f13d commit 758e2a8
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getAttributes()
*/
public function getAttribute($key)
{
($this->hasAttribute($key)) ? $this->attribute[$key] : null;
return ($this->hasAttribute($key)) ? $this->attributes[$key] : null;
}

/**
Expand All @@ -101,7 +101,7 @@ public function hasAttributes()
*/
public function hasAttribute(string $key)
{
return !isset($this->attributes[$key]);
return isset($this->attributes[$key]);
}

/**
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/HTML/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,58 @@ public function test_element_can_be_instantiated()

$this->assertInstanceOf(Element::class, $element);
}

public function test_element_can_set_tag()
{
$element = new Element();

$element->setTag('a');

$this->assertEquals($element->getTag(), 'a');
}

public function test_element_can_render()
{
$element = new Element();

$this->assertEquals($element->render(), '<div></div>');
}

public function test_element_can_render_with_attributes()
{
$element = new Element();

$element->setAttributes([
'id' => 'main',
'class' => 'content',
]);

$this->assertEquals($element->render(), '<div id="main" class="content"></div>');
}

public function test_element_can_render_with_content()
{
$element = new Element('p');

$element->setContent('Hello World!');

$this->assertEquals($element->render(), '<p>Hello World!</p>');
}

public function test_element_can_render_with_children()
{
$element = new Element('ol');

$element->addChild((new Element('li'))->setContent('List item 1'));
$element->addChild((new Element('li'))->setContent('List item 2'));

$this->assertEquals($element->render(), '<ol><li>List item 1</li><li>List item 2</li></ol>');
}

public function test_can_echo_to_string()
{
$element = new Element();

$this->assertEquals($element, '<div></div>');
}
}
172 changes: 172 additions & 0 deletions tests/Unit/HTML/Traits/HasAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@

<?php

use HTML\Element;
use PHPUnit\Framework\TestCase;

class HasAttributesTest extends TestCase
{
public function test_can_set_attributes()
{
$element = new Element();

$attributes = [
'class' => 'container',
];

$element->setAttributes($attributes);

$this->assertEquals($element->getAttributes(), $attributes);
}

public function test_can_set_attribute()
{
$element = new Element();

$attributes = [
'class' => 'container',
];

$element->setAttributes($attributes);

$element->setAttribute('id', 'main');

$attributes['id'] = 'main';

$this->assertEquals($element->getAttributes(), $attributes);
}

public function test_can_append_attribute()
{
$element = new Element();

$attributes = [
'class' => 'container',
];

$element->setAttributes($attributes);

$element->appendAttribute('class', 'active');

$attributes['class'] .= ' active';

$this->assertEquals($element->getAttributes(), $attributes);
}

public function test_can_append_attributes_when_empty()
{
$element = new Element();


$element->appendAttribute('class', 'active');

$this->assertEquals($element->getAttributes(), ['class' => 'active']);
}

public function test_can_get_attributes()
{
$element = new Element();

$attributes = [
'class' => 'container',
];

$element->setAttributes($attributes);

$this->assertEquals($element->getAttributes(), $attributes);
}

public function test_can_get_attribute()
{
$element = new Element();

$attributes = [
'class' => 'container',
];

$element->setAttributes($attributes);

$this->assertEquals($element->getAttribute('class'), 'container');
}

public function test_has_attributes()
{
$element = new Element();

$empty = $element->hasAttributes();

$attributes = [
'class' => 'container',
];

$element->setAttributes($attributes);

$this->assertTrue($element->hasAttributes());
$this->assertFalse($empty);
}

public function test_has_attribute_by_key()
{
$element = new Element();

$attributes = [
'class' => 'container',
];

$element->setAttributes($attributes);

$this->assertTrue($element->hasAttribute('class'));
$this->assertFalse($element->hasAttribute('id'));
}

public function test_can_remove_attributes()
{
$element = new Element();

$attributes = [
'class' => 'container',
];

$element->setAttributes($attributes);

$this->assertTrue($element->hasAttributes());

$element->removeAttributes();

$this->assertFalse($element->hasAttributes());
$this->assertEquals($element->getAttributes(), []);
}

public function test_can_remove_attribute_by_key()
{
$element = new Element();

$attributes = [
'class' => 'container',
'id' => 'main',
];

$element->setAttributes($attributes);

$element->removeAttribute('id');

$this->assertFalse($element->hasAttribute('id'));
$this->assertEquals($element->getAttributes(), ['class' => 'container']);
}

public function test_can_render_attributes()
{
$element = new Element();

$attributes = [
'class' => 'container',
'id' => 'main',
];

$element->setAttributes($attributes);

$this->assertEquals($element->renderAttributes(), 'class="container" id="main"');
}
}


71 changes: 71 additions & 0 deletions tests/Unit/HTML/Traits/HasChildrentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

use HTML\Element;
use PHPUnit\Framework\TestCase;

class HasChildrenTest extends TestCase
{
public function test_can_set_children()
{
$element = new Element();

$children = [
new Element(),
];

$element->setChildren($children);

$this->assertEquals($element->getChildren(), $children);
}

public function test_can_add_children()
{
$element = new Element();

$child = new Element();

$element->addChild($child);

$this->assertEquals($element->getChildren(), [$child]);
}

public function test_can_get_children()
{
$element = new Element();

$children = [
new Element(),
new Element(),
new Element(),
];

$element->setChildren($children);

$this->assertEquals($element->getChildren(), $children);
}

public function test_has_children()
{
$element = new Element();

$this->assertFalse($element->hasChildren());

$element->addChild(new Element());

$this->assertTrue($element->hasChildren());
}

public function test_can_render_children()
{
$element = new Element();

$children = [
(new Element('li'))->setContent('List item 1'),
(new Element('li'))->setContent('List item 2'),
];

$element->setChildren($children);

$this->assertEquals($element->renderChildren(), '<li>List item 1</li><li>List item 2</li>');
}
}
Loading

0 comments on commit 758e2a8

Please sign in to comment.