diff --git a/.travis.yml b/.travis.yml
index d4e9c91..3d9850d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,6 +4,7 @@ php:
- 5.4
- 5.6
- 7.0
+ - 7.1
- 7.2
env:
diff --git a/Tests/HelpersTest.php b/Tests/HelpersTest.php
new file mode 100644
index 0000000..7949832
--- /dev/null
+++ b/Tests/HelpersTest.php
@@ -0,0 +1,27 @@
+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'
+ ]));
+ }
+}
diff --git a/Tests/HtmlPageCrawlerTest.php b/Tests/HtmlPageCrawlerTest.php
index 8890d7b..aebdefa 100644
--- a/Tests/HtmlPageCrawlerTest.php
+++ b/Tests/HtmlPageCrawlerTest.php
@@ -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
@@ -175,14 +176,16 @@ public function testCss()
public function testClasses()
{
$dom = new \DOMDocument('1.0', 'UTF-8');
- $dom->loadHTML('
Title
');
+ $dom->loadHTML('Title
');
$c = new HtmlPageCrawler($dom);
$t = $c->filter('h1');
$t->addClass('ueberschrift');
$t->addClass('nochneklasse');
- $this->assertEquals('Title
', $t->saveHTML());
+ $t->addClass('style_class');
+ $this->assertEquals('Title
', $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'));
@@ -240,9 +243,9 @@ public function testAfter()
$c->filter('h1')->after('Text after h1
');
$this->assertEquals('', $c->saveHTML());
- $c = new HtmlPageCrawler('Title
');
+ $c = new HtmlPageCrawler('Title
Title2
');
$c->filter('h1')->after(new HtmlPageCrawler('Text after h1
and more text after
'));
- $this->assertEquals('Title
Text after h1
and more text after
', $c->saveHTML());
+ $this->assertEquals('Title
Text after h1
and more text after
Title2
Text after h1
and more text after
', $c->saveHTML());
}
/**
@@ -254,11 +257,24 @@ public function testPrepend()
$c->filter('#content')->prepend('Text before h1
');
$this->assertEquals('', $c->saveHTML());
- $c = new HtmlPageCrawler('Title
');
+ $c = new HtmlPageCrawler('');
$c->filter('#content')->prepend(new HtmlPageCrawler('Text before h1
and more text before
'));
- $this->assertEquals('Text before h1
and more text before
Title
', $c->saveHTML());
+ $this->assertEquals('Text before h1
and more text before
', $c->saveHTML());
}
+ /**
+ * @covers Wa72\HtmlPageDom\HtmlPageCrawler::prependTo
+ */
+ public function testPrependTo()
+ {
+ $c = new HtmlPageCrawler('');
+ $c->filter('p')->prependTo('Text');
+ $this->assertEquals('', $c->saveHTML());
+
+ $c = new HtmlPageCrawler('Title
');
+ $c->filter('#content')->prependTo(new HtmlPageCrawler('paragraph
'));
+ $this->assertEquals('Title
', $c->saveHTML());
+ }
/**
* @covers Wa72\HtmlPageDom\HtmlPageCrawler::wrap
@@ -352,6 +368,18 @@ public function testUnwrap()
$this->assertEquals('', $c->saveHTML());
}
+ /**
+ * @expectedException \InvalidArgumentException
+ * @expectedExceptionMessage DOMElement does not have a parent DOMElement node.
+ */
+ public function testUnwrapInnerOnDOMElementExeption()
+ {
+ $c = HtmlPageCrawler::create('');
+ $p = $c->filter('div#content');
+ $p->unwrapInner();
+ $p->unwrapInner();
+ }
+
/**
* @covers Wa72\HtmlPageDom\HtmlPageCrawler::unwrapInner
*/
@@ -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('Title
');
+ $this->assertEquals('Title', $html->html());
+
+ $html = HtmlPageCrawler::create('Title
');
+ $this->assertInstanceOf('Wa72\HtmlPageDom\HtmlPageCrawler', $html->html('Title
'));
+ $this->assertEquals('Title
', $html->html());
+ }
+
+ public function testGetInnerHtml()
+ {
+ $html = HtmlPageCrawler::create(null);
+ $this->assertEquals('', $html->getInnerHtml());
+ }
+
+ public function testToString()
+ {
+ $html = HtmlPageCrawler::create('Title
');
+ $this->assertEquals('Title
', (string) $html);
+ }
+
+ public function testGetDOMDocument()
+ {
+ $html = HtmlPageCrawler::create('Title
');
+ $this->assertInstanceOf('\DOMDocument', $html->getDOMDocument());
+ }
+
+ public function testAddOnCrawlerInstance()
+ {
+ $html = HtmlPageCrawler::create('Title
');
+ $html->add($html);
+ $this->assertEquals('Title
', (string) $html);
+ }
+
public function testReturnValues()
{
// appendTo, insertBefore, insertAfter, replaceAll should always return new Crawler objects
diff --git a/Tests/HtmlPageTest.php b/Tests/HtmlPageTest.php
index 77d1b5a..cd438ad 100644
--- a/Tests/HtmlPageTest.php
+++ b/Tests/HtmlPageTest.php
@@ -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;
@@ -175,4 +182,160 @@ public function testIndent()
$this->assertEquals($expected, $hp->indent()->save());
}
+
+ public function testGetCrawler()
+ {
+ $html = <<
+
+
+
+
+
+
+ TEST
+
+ asdf jksdlf ajsfk
+ jasdf
+ jaksfd asdf
+ jasdf jaks
+
+
+
+
+END;
+
+ $hp = new HtmlPage($html);
+ $this->assertEquals('TEST
', $hp->getCrawler()->filter('h1')->saveHtml());
+ }
+
+ public function testGetDOMDocument()
+ {
+ $html = <<
+
+
+
+
+
+
+ TEST
+
+ asdf jksdlf ajsfk
+ jasdf
+ jaksfd asdf
+ jasdf jaks
+
+
+
+
+END;
+
+ $hp = new HtmlPage($html);
+ $this->assertInstanceOf('\DOMDocument', $hp->getDOMDocument());
+ }
+
+ public function testSetTitleOnNoTitleElement()
+ {
+ $html = <<
+
+
+
+
+
+ TEST
+
+ asdf jksdlf ajsfk
+ jasdf
+ jaksfd asdf
+ jasdf jaks
+
+
+
+
+END;
+
+ $hp = new HtmlPage($html);
+ $hp->setTitle('TEST');
+ $this->assertEquals('TEST', $hp->getTitle());
+ }
+
+ public function testGetTitleShouldReturnNull()
+ {
+ $html = <<
+
+
+
+
+
+ TEST
+
+ asdf jksdlf ajsfk
+ jasdf
+ jaksfd asdf
+ jasdf jaks
+
+
+
+
+END;
+
+ $hp = new HtmlPage($html);
+ $this->assertNull($hp->getTitle());
+ }
+
+ public function testGetBaseHrefShouldReturnNull()
+ {
+ $hp = new HtmlPage('TESTHello');
+ $this->assertNull($hp->getBaseHref());
+ }
+
+ public function testGetHeadNodeShouldAddTheHeadTag()
+ {
+ $hp = new HtmlPage('Hello');
+ $this->assertInstanceOf('\DOMElement', $hp->getHeadNode());
+ $this->assertEquals('', (string) $hp->getHead());
+ }
+
+ public function testGetBodyNodeShouldAddTheBodyTag()
+ {
+ $hp = new HtmlPage('');
+ $this->assertInstanceOf('\DOMElement', $hp->getBodyNode());
+ $this->assertEquals('', (string) $hp->getBody());
+ }
+
+ public function testTrimNewlines()
+ {
+ $html = <<
+
+
+ TEST
+
+
+END;
+
+ $this->assertEquals(' TEST ', (string) HtmlPage::trimNewlines($html));
+ }
+
+ public function testSaveOnFileName()
+ {
+ $hp = new HtmlPage('TEST');
+ $hp->save(vfsStream::url('root/save.html'));
+ $this->assertFileExists(vfsStream::url('root/save.html'));
+ }
}
diff --git a/composer.json b/composer.json
index bbb9119..58ddc11 100644
--- a/composer.json
+++ b/composer.json
@@ -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"
diff --git a/src/HtmlPage.php b/src/HtmlPage.php
index ac63bdf..e899555 100644
--- a/src/HtmlPage.php
+++ b/src/HtmlPage.php
@@ -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;
}
}