Skip to content

Commit

Permalink
Updated some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paquettg committed Nov 19, 2014
1 parent 8f31eaa commit 61d093f
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/PHPHtmlParser/StaticDom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions tests/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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('<a href="google.com">');
Expand Down
60 changes: 60 additions & 0 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ public function testLoad()
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>', $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('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
$div = $dom->find('div', 0);
$this->assertEquals(null, $div->foo);
}

public function testLoadSelfclosingAttr()
{
$dom = new Dom;
Expand All @@ -20,6 +37,14 @@ public function testLoadSelfclosingAttr()
$this->assertEquals('<br foo bar />', $br->outerHtml);
}

public function testLoadSelfclosingAttrToString()
{
$dom = new Dom;
$dom->load("<div class='all'><br foo bar />baz</div>");
$br = $dom->find('br', 0);
$this->assertEquals('<br foo bar />', (string) $br);
}

public function testLoadEscapeQuotes()
{
$dom = new Dom;
Expand Down Expand Up @@ -58,6 +83,41 @@ public function testLoadClosingTagOnSelfClosing()
$this->assertEquals('<br /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>', $dom->find('div', 0)->innerHtml);
}

public function testLoadClosingTagAddSelfClosingTag()
{
$dom = new Dom;
$dom->addSelfClosingTag('mytag');
$dom->load('<div class="all"><mytag><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></mytag></div>');
$this->assertEquals('<mytag /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p>', $dom->find('div', 0)->innerHtml);
}

public function testLoadClosingTagAddSelfClosingTagArray()
{
$dom = new Dom;
$dom->addSelfClosingTag([
'mytag',
'othertag'
]);
$dom->load('<div class="all"><mytag><p>Hey bro, <a href="google.com" data-quote="\"">click here</a><othertag></div>');
$this->assertEquals('<mytag /><p>Hey bro, <a href="google.com" data-quote="\"">click here</a><othertag /></p>', $dom->find('div', 0)->innerHtml);
}

public function testLoadClosingTagRemoveSelfClosingTag()
{
$dom = new Dom;
$dom->removeSelfClosingTag('br');
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
$this->assertEquals('<br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></br>', $dom->find('div', 0)->innerHtml);
}

public function testLoadClosingTagClearSelfClosingTag()
{
$dom = new Dom;
$dom->clearSelfClosingTags();
$dom->load('<div class="all"><br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></br></div>');
$this->assertEquals('<br><p>Hey bro, <a href="google.com" data-quote="\"">click here</a></p></br>', $dom->find('div', 0)->innerHtml);
}

public function testLoadUpperCase()
{
$dom = new Dom;
Expand Down
70 changes: 70 additions & 0 deletions tests/Node/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@ public function testInnerHtml()
$this->assertEquals("<a href='http://google.com'>link</a><br />", $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');
Expand Down Expand Up @@ -90,6 +120,36 @@ public function testOuterHtml()
$this->assertEquals('<div class="all"><a href=\'http://google.com\'>link</a><br /></div>', $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');
Expand Down Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions tests/Node/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 6 additions & 0 deletions tests/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ public function testAddingOver()

$this->assertFalse($options->get('whitespaceTextNode'));
}

public function testGettingNoOption()
{
$options = new Options;
$this->assertEquals(null, $options->get('doesnotexist'));
}
}

21 changes: 21 additions & 0 deletions tests/StaticDomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
Expand All @@ -28,6 +41,14 @@ public function testFind()
$this->assertEquals('<input type="submit" tabindex="0" name="submit" value="Информации" />', 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');
Expand Down

0 comments on commit 61d093f

Please sign in to comment.