Skip to content

Commit

Permalink
update test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelroecker committed Aug 26, 2016
1 parent 7a31131 commit bff6ba5
Showing 1 changed file with 163 additions and 38 deletions.
201 changes: 163 additions & 38 deletions tests/GlHtmlNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,184 @@
/**
* @covers \GlHtml\GlHtmlNode
*/
class GlHtmlNodeTest extends \PHPUnit_Framework_TestCase {
class GlHtmlNodeTest extends \PHPUnit_Framework_TestCase
{

public function testReplaceMe() {
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";
$htmlresult = "<!DOCTYPE html><html><head></head><body><div><h1></h1></div></body></html>";
private function removeCR($html)
{
return str_replace(["\n", "\r"], '', $html);
}

public function testSetValue()
{
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";
$htmlresult = "<!DOCTYPE html><html><head></head><body><div><span>bonjour</span></div></body></html>";

$dom = new GlHtml($html);
$node = $dom->get("span")[0];

$node->setValue("bonjour");

$result = $dom->html();

$htmlresult = $this->removeCR($htmlresult);
$result = $this->removeCR($result);

$this->assertEquals($htmlresult, $result);
}

public function testAdd()
{
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";
$htmlresult = '<!DOCTYPE html><html><head></head><body><div><span><div></div><div class="super">test</div></span></div></body></html>';

$dom = new GlHtml($html);
$node = $dom->get("span")[0];

$node->add('<div class="super">test</div>');

$result = $dom->html();

$htmlresult = $this->removeCR($htmlresult);
$result = $this->removeCR($result);

$this->assertEquals($htmlresult, $result);
}

public function testReplaceInner()
{
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";
$htmlresult = '<!DOCTYPE html><html><head></head><body><div><span><hr></span></div></body></html>';

$dom = new GlHtml($html);
$node = $dom->get("span")[0];

$node->replaceInner('<hr/>');

$result = $dom->html();

$htmlresult = $this->removeCR($htmlresult);
$result = $this->removeCR($result);

$this->assertEquals($htmlresult, $result);
}

public function testGetDOMNode()
{

}

public function testGetName()
{
$html = '<!DOCTYPE html><html><head></head><body><div><span id="test"><div></div></span></div></body></html>';

$dom = new GlHtml($html);
$node = $dom->get("#test")[0];

$this->assertEquals('span', $node->getName());
}

public function testGetAttribute()
{
$html = '<!DOCTYPE html><html><head></head><body><div><span id="testid" class="testclass"><div></div></span></div></body></html>';

$dom = new GlHtml($html);
$node = $dom->get("#testid")[0];

$this->assertEquals('testclass', $node->getAttribute('class'));
}

public function testGetText()
{
$html = '<!DOCTYPE html><html><head></head><body><div><span>texte<div></div></span></div></body></html>';

$dom = new GlHtml($html);
$node = $dom->get("span")[0];

$this->assertEquals('texte', $node->getText());
}

public function testGetSentences()
{
$html = '<!DOCTYPE html><html><head></head><body><div>texte1<div id="test">texte2<h1>texte3</h1>texte4</div></div></body></html>';

$dom = new GlHtml($html);
$dom = new GlHtml($html);
$node = $dom->get("#test")[0];

$this->assertEquals(['texte2','texte3','texte4'], $node->getSentences());
}

public function testCallChild()
{
$html = '<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>';

$dom = new GlHtml($html);
$node = $dom->get("span")[0];


$node->callChild(
function (GlHtmlNode $childnode) {
$this->assertEquals('div', $childnode->getName());
}
);
}

public function testReplaceMe()
{
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";
$htmlresult = "<!DOCTYPE html><html><head></head><body><div><h1></h1></div></body></html>";

$dom = new GlHtml($html);
$node = $dom->get("span")[0];

$node->replaceMe("<h1></h1>");

$result = $dom->html();
$htmlresult = str_replace(["\n","\r"],'',$htmlresult);
$result = str_replace(["\n","\r"],'',$result);
$this->assertEquals($htmlresult,$result);

$htmlresult = str_replace(["\n", "\r"], '', $htmlresult);
$result = str_replace(["\n", "\r"], '', $result);

$this->assertEquals($htmlresult, $result);
}

public function testRemplaceMeParent() {
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";


public function testRemplaceMeParent()
{
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";
$htmlresult = "<!DOCTYPE html><html><head></head><body><h1></h1></body></html>";

$dom = new GlHtml($html);
$dom = new GlHtml($html);
$node = $dom->get("span")[0];

$node->getParent()->replaceMe("<h1></h1>");

$result = $dom->html();

$htmlresult = str_replace(["\n","\r"],'',$htmlresult);
$result = str_replace(["\n","\r"],'',$result);
$htmlresult = str_replace(["\n", "\r"], '', $htmlresult);
$result = str_replace(["\n", "\r"], '', $result);

$this->assertEquals($htmlresult,$result);
$this->assertEquals($htmlresult, $result);
}

public function testsetAttributes() {
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";

public function testsetAttributes()
{
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";
$htmlresult = '<!DOCTYPE html><html><head></head><body><div><span id="test"><div></div></span></div></body></html>';

$dom = new GlHtml($html);
$dom = new GlHtml($html);
$node = $dom->get("span")[0];

$node->setAttributes(['id' => 'test']);

$result = $dom->html();

$htmlresult = str_replace(["\n","\r"],'',$htmlresult);
$result = str_replace(["\n","\r"],'',$result);
$htmlresult = str_replace(["\n", "\r"], '', $htmlresult);
$result = str_replace(["\n", "\r"], '', $result);

$this->assertEquals($htmlresult,$result);
$this->assertEquals($htmlresult, $result);
}

public function testSelfClose() {

public function testSelfClose()
{
$html = <<<EOD
<!DOCTYPE html>
<html>
Expand All @@ -90,16 +214,17 @@ public function testSelfClose() {
</html>
EOD;

$dom = new GlHtml($html);
$dom = new GlHtml($html);
$node = $dom->get("div")[0];
$result = $node->getHtml();

$result = $node->getHtml();
$htmlresult = "<span></span>";

$this->assertEquals($htmlresult,$result);
$this->assertEquals($htmlresult, $result);
}

public function testDelete() {

public function testDelete()
{
$html = <<<EOD
<!DOCTYPE html>
<html>
Expand All @@ -111,7 +236,7 @@ public function testDelete() {
</html>
EOD;

$dom = new GlHtml($html);
$dom = new GlHtml($html);
$node = $dom->get("p")[0];
$node->delete();

Expand All @@ -128,8 +253,8 @@ public function testDelete() {

$result = $dom->html();

$result = str_replace([' ', "\n", "\t", "\r"], '', $result);
$result = str_replace([' ', "\n", "\t", "\r"], '', $result);
$htmlresult = str_replace([' ', "\n", "\t", "\r"], '', $htmlresult);
$this->assertEquals($htmlresult,$result);
$this->assertEquals($htmlresult, $result);
}
}

0 comments on commit bff6ba5

Please sign in to comment.