Skip to content

Commit

Permalink
Merge pull request #27 from peter279k/test_enhancement
Browse files Browse the repository at this point in the history
Test enhancement
  • Loading branch information
Christoph Singer committed Jun 6, 2018
2 parents 5095204 + d933a7a commit 2e699e8
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -4,6 +4,7 @@ php:
- 5.4
- 5.6
- 7.0
- 7.1
- 7.2

env:
Expand Down
27 changes: 27 additions & 0 deletions Tests/HelpersTest.php
@@ -0,0 +1,27 @@
<?php
namespace Wa72\HtmlPageDom\Tests;

use Wa72\HtmlPageDom\Helpers;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

class HelpersTest extends TestCase
{
public function testCssStringToArray()
{
$this->assertEquals([
'font-size' => '15px',
'font-weight' => 'bold',
'font-color' => 'black'
], Helpers::cssStringToArray('invalid_css_string;font-size: 15px;font-weight: bold;font-color: black;'));
}

public function testCssArrayToString()
{
$this->assertEquals('font-size: 15px;font-weight: bold;font-color: black;', Helpers::cssArrayToString([
'font-size' => '15px',
'font-weight' => 'bold',
'font-color' => 'black'
]));
}
}
86 changes: 79 additions & 7 deletions Tests/HtmlPageCrawlerTest.php
Expand Up @@ -2,8 +2,9 @@
namespace Wa72\HtmlPageDom\Tests;

use Wa72\HtmlPageDom\HtmlPageCrawler;
use PHPUnit\Framework\TestCase;

class HtmlPageCrawlerTest extends \PHPUnit\Framework\TestCase
class HtmlPageCrawlerTest extends TestCase
{
/**
* @covers Wa72\HtmlPageDom\HtmlPageCrawler::__construct
Expand Down Expand Up @@ -175,14 +176,16 @@ public function testCss()
public function testClasses()
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->loadHTML('<!DOCTYPE html><html><body><div id="content"><h1>Title</h1></div></body></html>');
$dom->loadHTML('<!DOCTYPE html><html><body><div id="content"><h1 class="style_class">Title</h1></div></body></html>');
$c = new HtmlPageCrawler($dom);
$t = $c->filter('h1');
$t->addClass('ueberschrift');
$t->addClass('nochneklasse');
$this->assertEquals('<h1 class="ueberschrift nochneklasse">Title</h1>', $t->saveHTML());
$t->addClass('style_class');
$this->assertEquals('<h1 class="style_class ueberschrift nochneklasse">Title</h1>', $t->saveHTML());
$this->assertTrue($t->hasClass('ueberschrift'));
$this->assertTrue($t->hasClass('nochneklasse'));
$this->assertTrue($t->hasClass('style_class'));
$t->removeClass('nochneklasse');
$this->assertTrue($t->hasClass('ueberschrift'));
$this->assertFalse($t->hasClass('nochneklasse'));
Expand Down Expand Up @@ -240,9 +243,9 @@ public function testAfter()
$c->filter('h1')->after('<p>Text after h1</p>');
$this->assertEquals('<div id="content"><h1>Title</h1><p>Text after h1</p></div>', $c->saveHTML());

$c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>');
$c = new HtmlPageCrawler('<div id="content"><h1>Title</h1><h1>Title2</h1></div>');
$c->filter('h1')->after(new HtmlPageCrawler('<p>Text after h1</p><p>and more text after</p>'));
$this->assertEquals('<div id="content"><h1>Title</h1><p>Text after h1</p><p>and more text after</p></div>', $c->saveHTML());
$this->assertEquals('<div id="content"><h1>Title</h1><p>Text after h1</p><p>and more text after</p><h1>Title2</h1><p>Text after h1</p><p>and more text after</p></div>', $c->saveHTML());
}

/**
Expand All @@ -254,11 +257,24 @@ public function testPrepend()
$c->filter('#content')->prepend('<p>Text before h1</p>');
$this->assertEquals('<div id="content"><p>Text before h1</p><h1>Title</h1></div>', $c->saveHTML());

$c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>');
$c = new HtmlPageCrawler('<div id="content"></div>');
$c->filter('#content')->prepend(new HtmlPageCrawler('<p>Text before h1</p><p>and more text before</p>'));
$this->assertEquals('<div id="content"><p>Text before h1</p><p>and more text before</p><h1>Title</h1></div>', $c->saveHTML());
$this->assertEquals('<div id="content"><p>Text before h1</p><p>and more text before</p></div>', $c->saveHTML());
}

/**
* @covers Wa72\HtmlPageDom\HtmlPageCrawler::prependTo
*/
public function testPrependTo()
{
$c = new HtmlPageCrawler('<div id="content"><p>Text before</p></div>');
$c->filter('p')->prependTo('Text');
$this->assertEquals('<div id="content"><p>Text before</p></div>', $c->saveHTML());

$c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>');
$c->filter('#content')->prependTo(new HtmlPageCrawler('<p>paragraph</p>'));
$this->assertEquals('<div id="content"><h1>Title</h1></div>', $c->saveHTML());
}

/**
* @covers Wa72\HtmlPageDom\HtmlPageCrawler::wrap
Expand Down Expand Up @@ -352,6 +368,18 @@ public function testUnwrap()
$this->assertEquals('<div id="content"><div>Before</div><p>Absatz 1</p><div>After</div></div>', $c->saveHTML());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage DOMElement does not have a parent DOMElement node.
*/
public function testUnwrapInnerOnDOMElementExeption()
{
$c = HtmlPageCrawler::create('<div id="content"></div>');
$p = $c->filter('div#content');
$p->unwrapInner();
$p->unwrapInner();
}

/**
* @covers Wa72\HtmlPageDom\HtmlPageCrawler::unwrapInner
*/
Expand Down Expand Up @@ -462,6 +490,50 @@ public function testAttr()

}

/**
* @expectedException \InvalidArgumentException
*/
public function testAttrOnInvalidNodeList()
{
$c = HtmlPageCrawler::create(null);
$c->attr('data-foo');
}

public function testHtml()
{
$html = HtmlPageCrawler::create('<h1>Title</h1>');
$this->assertEquals('Title', $html->html());

$html = HtmlPageCrawler::create('<h1>Title</h1>');
$this->assertInstanceOf('Wa72\HtmlPageDom\HtmlPageCrawler', $html->html('<h2>Title</h2>'));
$this->assertEquals('<h2>Title</h2>', $html->html());
}

public function testGetInnerHtml()
{
$html = HtmlPageCrawler::create(null);
$this->assertEquals('', $html->getInnerHtml());
}

public function testToString()
{
$html = HtmlPageCrawler::create('<h2>Title</h2>');
$this->assertEquals('<h2>Title</h2>', (string) $html);
}

public function testGetDOMDocument()
{
$html = HtmlPageCrawler::create('<h2>Title</h2>');
$this->assertInstanceOf('\DOMDocument', $html->getDOMDocument());
}

public function testAddOnCrawlerInstance()
{
$html = HtmlPageCrawler::create('<h1>Title</h1>');
$html->add($html);
$this->assertEquals('<h1>Title</h1>', (string) $html);
}

public function testReturnValues()
{
// appendTo, insertBefore, insertAfter, replaceAll should always return new Crawler objects
Expand Down
165 changes: 164 additions & 1 deletion Tests/HtmlPageTest.php
Expand Up @@ -2,9 +2,16 @@
namespace Wa72\HtmlPageDom\Tests;

use Wa72\HtmlPageDom\HtmlPage;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

class HtmlPageTest extends \PHPUnit\Framework\TestCase
class HtmlPageTest extends TestCase
{
public function setUp()
{
$this->root = vfsStream::setup('root');
}

public function testHtmlPage()
{
$hp = new HtmlPage;
Expand Down Expand Up @@ -175,4 +182,160 @@ public function testIndent()
$this->assertEquals($expected, $hp->indent()->save());

}

public function testGetCrawler()
{
$html = <<<END
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// this will be awesome
alert('Hello world');
</script>
</head>
<body>
<h1>TEST</h1>
<p class="">
asdf jksdlf ajsfk
<b>jasdf
jaksfd asdf</b>
<a>jasdf jaks</a>
</p>
</body>
</html>
END;

$hp = new HtmlPage($html);
$this->assertEquals('<h1>TEST</h1>', $hp->getCrawler()->filter('h1')->saveHtml());
}

public function testGetDOMDocument()
{
$html = <<<END
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// this will be awesome
alert('Hello world');
</script>
</head>
<body>
<h1>TEST</h1>
<p class="">
asdf jksdlf ajsfk
<b>jasdf
jaksfd asdf</b>
<a>jasdf jaks</a>
</p>
</body>
</html>
END;

$hp = new HtmlPage($html);
$this->assertInstanceOf('\DOMDocument', $hp->getDOMDocument());
}

public function testSetTitleOnNoTitleElement()
{
$html = <<<END
<!DOCTYPE html>
<html>
<head>
<script>
// this will be awesome
alert('Hello world');
</script>
</head>
<body>
<h1>TEST</h1>
<p class="">
asdf jksdlf ajsfk
<b>jasdf
jaksfd asdf</b>
<a>jasdf jaks</a>
</p>
</body>
</html>
END;

$hp = new HtmlPage($html);
$hp->setTitle('TEST');
$this->assertEquals('TEST', $hp->getTitle());
}

public function testGetTitleShouldReturnNull()
{
$html = <<<END
<!DOCTYPE html>
<html>
<head>
<script>
// this will be awesome
alert('Hello world');
</script>
</head>
<body>
<h1>TEST</h1>
<p class="">
asdf jksdlf ajsfk
<b>jasdf
jaksfd asdf</b>
<a>jasdf jaks</a>
</p>
</body>
</html>
END;

$hp = new HtmlPage($html);
$this->assertNull($hp->getTitle());
}

public function testGetBaseHrefShouldReturnNull()
{
$hp = new HtmlPage('<!DOCTYPE html><html><head><title>TEST</title></head><body>Hello</body></html>');
$this->assertNull($hp->getBaseHref());
}

public function testGetHeadNodeShouldAddTheHeadTag()
{
$hp = new HtmlPage('<!DOCTYPE html><html><body>Hello</body></html>');
$this->assertInstanceOf('\DOMElement', $hp->getHeadNode());
$this->assertEquals('<head></head>', (string) $hp->getHead());
}

public function testGetBodyNodeShouldAddTheBodyTag()
{
$hp = new HtmlPage('<!DOCTYPE html><html></html>');
$this->assertInstanceOf('\DOMElement', $hp->getBodyNode());
$this->assertEquals('<body></body>', (string) $hp->getBody());
}

public function testTrimNewlines()
{
$html = <<<END
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
</head>
</html>
END;

$this->assertEquals('<!DOCTYPE html> <html> <head> <title>TEST</title> </head> </html>', (string) HtmlPage::trimNewlines($html));
}

public function testSaveOnFileName()
{
$hp = new HtmlPage('<!DOCTYPE html><html><head><title>TEST</title></head></html>');
$hp->save(vfsStream::url('root/save.html'));
$this->assertFileExists(vfsStream::url('root/save.html'));
}
}
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -19,7 +19,8 @@
},
"require-dev": {
"phpunit/phpunit": "^4|^5|^6|^7",
"wa72/html-pretty-min": "~0.1"
"wa72/html-pretty-min": "~0.1",
"mikey179/vfsStream": "^1.6"
},
"suggest": {
"wa72/html-pretty-min": "Minify or indent HTML documents"
Expand Down
4 changes: 2 additions & 2 deletions src/HtmlPage.php
Expand Up @@ -266,10 +266,10 @@ public function __toString()
public function save($filename = '')
{
if ($filename != '') {
file_put_contents($filename, $this->__toString());
file_put_contents($filename, (string) $this);
return;
} else {
return $this->__toString();
return (string) $this;
}
}

Expand Down

0 comments on commit 2e699e8

Please sign in to comment.