diff --git a/src/PHPHtmlParser/StaticDom.php b/src/PHPHtmlParser/StaticDom.php index 41939b1..0f053cf 100644 --- a/src/PHPHtmlParser/StaticDom.php +++ b/src/PHPHtmlParser/StaticDom.php @@ -98,4 +98,12 @@ public static function loadFromUrl($url, CurlInterface $curl = null) return $dom->loadFromUrl($url, $curl); } + + /** + * Sets the $dom variable to null. + */ + public static function unload() + { + self::$dom = null; + } } diff --git a/tests/ContentTest.php b/tests/ContentTest.php index 6617a54..ef8fbff 100644 --- a/tests/ContentTest.php +++ b/tests/ContentTest.php @@ -31,6 +31,14 @@ public function testRewind() $this->assertEquals('b', $content->char()); } + public function testRewindNegative() + { + $content = new Content('abcde'); + $content->fastForward(2) + ->rewind(100); + $this->assertEquals('a', $content->char()); + } + public function testCopyUntil() { $content = new Content('abcdeedcba'); @@ -49,6 +57,12 @@ public function testCopyUntilEscape() $this->assertEquals('foo\"bar', $content->copyUntil('"', false, true)); } + public function testCopyUntilNotFound() + { + $content = new Content('foo\"bar"bax'); + $this->assertEquals('foo\"bar"bax', $content->copyUntil('baz')); + } + public function testCopyByToken() { $content = new Content(''); diff --git a/tests/DomTest.php b/tests/DomTest.php index d9fd67d..8453de6 100644 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -12,6 +12,23 @@ public function testLoad() $this->assertEquals('

Hey bro, click here
:)

', $div->outerHtml); } + /** + * @expectedException PHPHtmlParser\Exceptions\NotLoadedException + */ + public function testNotLoaded() + { + $dom = new Dom; + $div = $dom->find('div', 0); + } + + public function testIncorrectAccess() + { + $dom = new Dom; + $dom->load('

Hey bro, click here
:)

'); + $div = $dom->find('div', 0); + $this->assertEquals(null, $div->foo); + } + public function testLoadSelfclosingAttr() { $dom = new Dom; @@ -20,6 +37,14 @@ public function testLoadSelfclosingAttr() $this->assertEquals('
', $br->outerHtml); } + public function testLoadSelfclosingAttrToString() + { + $dom = new Dom; + $dom->load("

baz
"); + $br = $dom->find('br', 0); + $this->assertEquals('
', (string) $br); + } + public function testLoadEscapeQuotes() { $dom = new Dom; @@ -58,6 +83,41 @@ public function testLoadClosingTagOnSelfClosing() $this->assertEquals('

Hey bro, click here

', $dom->find('div', 0)->innerHtml); } + public function testLoadClosingTagAddSelfClosingTag() + { + $dom = new Dom; + $dom->addSelfClosingTag('mytag'); + $dom->load('

Hey bro, click here

'); + $this->assertEquals('

Hey bro, click here

', $dom->find('div', 0)->innerHtml); + } + + public function testLoadClosingTagAddSelfClosingTagArray() + { + $dom = new Dom; + $dom->addSelfClosingTag([ + 'mytag', + 'othertag' + ]); + $dom->load('

Hey bro, click here

'); + $this->assertEquals('

Hey bro, click here

', $dom->find('div', 0)->innerHtml); + } + + public function testLoadClosingTagRemoveSelfClosingTag() + { + $dom = new Dom; + $dom->removeSelfClosingTag('br'); + $dom->load('

Hey bro, click here

'); + $this->assertEquals('

Hey bro, click here


', $dom->find('div', 0)->innerHtml); + } + + public function testLoadClosingTagClearSelfClosingTag() + { + $dom = new Dom; + $dom->clearSelfClosingTags(); + $dom->load('

Hey bro, click here

'); + $this->assertEquals('

Hey bro, click here


', $dom->find('div', 0)->innerHtml); + } + public function testLoadUpperCase() { $dom = new Dom; diff --git a/tests/Node/HtmlTest.php b/tests/Node/HtmlTest.php index 006faba..e251339 100644 --- a/tests/Node/HtmlTest.php +++ b/tests/Node/HtmlTest.php @@ -35,6 +35,36 @@ public function testInnerHtml() $this->assertEquals("link
", $parent->innerHtml()); } + public function testInnerHtmlTwice() + { + $div = new Tag('div'); + $div->setAttributes([ + 'class' => [ + 'value' => 'all', + 'doubleQuote' => true, + ], + ]); + $a = new Tag('a'); + $a->setAttributes([ + 'href' => [ + 'value' => 'http://google.com', + 'doubleQuote' => false, + ], + ]); + $br = new Tag('br'); + $br->selfClosing(); + + $parent = new HtmlNode($div); + $childa = new HtmlNode($a); + $childbr = new HtmlNode($br); + $parent->addChild($childa); + $parent->addChild($childbr); + $childa->addChild(new TextNode('link')); + + $inner = $parent->innerHtml(); + $this->assertEquals($inner, $parent->innerHtml()); + } + public function testInnerHtmlMagic() { $parent = new HtmlNode('div'); @@ -90,6 +120,36 @@ public function testOuterHtml() $this->assertEquals('', $parent->outerHtml()); } + public function testOuterHtmlTwice() + { + $div = new Tag('div'); + $div->setAttributes([ + 'class' => [ + 'value' => 'all', + 'doubleQuote' => true, + ], + ]); + $a = new Tag('a'); + $a->setAttributes([ + 'href' => [ + 'value' => 'http://google.com', + 'doubleQuote' => false, + ], + ]); + $br = new Tag('br'); + $br->selfClosing(); + + $parent = new HtmlNode($div); + $childa = new HtmlNode($a); + $childbr = new HtmlNode($br); + $parent->addChild($childa); + $parent->addChild($childbr); + $childa->addChild(new TextNode('link')); + + $outer = $parent->outerHtml(); + $this->assertEquals($outer, $parent->outerHtml()); + } + public function testOuterHtmlEmpty() { $a = new Tag('a'); @@ -139,6 +199,16 @@ public function testText() $this->assertEquals('link', $node->text()); } + public function testTextTwice() + { + $a = new Tag('a'); + $node = new HtmlNode($a); + $node->addChild(new TextNode('link')); + + $text = $node->text(); + $this->assertEquals($text, $node->text()); + } + public function testTextNone() { $a = new Tag('a'); diff --git a/tests/Node/TagTest.php b/tests/Node/TagTest.php index f9fb70d..cf29cf8 100644 --- a/tests/Node/TagTest.php +++ b/tests/Node/TagTest.php @@ -25,6 +25,12 @@ public function testSetAttributes() $this->assertEquals('http://google.com', $tag->getAttribute('href')['value']); } + public function testNoise() + { + $tag = new Tag('a'); + $this->assertTrue($tag->noise('noise') instanceof Tag); + } + public function testGetAttributeMagic() { $attr = [ diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php index 27b91ac..b5398c1 100644 --- a/tests/OptionsTest.php +++ b/tests/OptionsTest.php @@ -33,5 +33,11 @@ public function testAddingOver() $this->assertFalse($options->get('whitespaceTextNode')); } + + public function testGettingNoOption() + { + $options = new Options; + $this->assertEquals(null, $options->get('doesnotexist')); + } } diff --git a/tests/StaticDomTest.php b/tests/StaticDomTest.php index 1245fa1..d87671e 100644 --- a/tests/StaticDomTest.php +++ b/tests/StaticDomTest.php @@ -9,6 +9,19 @@ public function setUp() StaticDom::mount(); } + public function tearDown() + { + StaticDom::unload(); + } + + public function testMountWithDom() + { + $dom = new PHPHtmlParser\Dom; + StaticDom::unload(); + $status = StaticDom::mount('newDom', $dom); + $this->assertTrue($status); + } + public function testLoad() { $dom = Dom::load('

Hey bro, click here
:)

'); @@ -28,6 +41,14 @@ public function testFind() $this->assertEquals('', Dom::find('table input', 1)->outerHtml); } + /** + * @expectedException PHPHtmlParser\Exceptions\NotLoadedException + */ + public function testFindNoLoad() + { + Dom::find('.post-user font', 0); + } + public function testFindI() { Dom::load('tests/horrible.html');