Skip to content

Commit

Permalink
Updated the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paquettg committed Nov 17, 2015
1 parent 1cc4007 commit 3a80041
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
51 changes: 51 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use PHPHtmlParser\Selector;
use PHPHtmlParser\Dom\HtmlNode;
use PHPHtmlParser\Dom\Tag;
use PHPHtmlParser\Dom\Collection;

class CollectionTest extends PHPUnit_Framework_TestCase {

Expand All @@ -27,6 +28,15 @@ public function testEach()
$this->assertEquals(2, $count);
}

/**
* @expectedException PHPHtmlParser\Exceptions\EmptyCollectionException
*/
public function testCallNoNodes()
{
$collection = new Collection();
$collection->innerHtml();
}

public function testCallMagic()
{
$root = new HtmlNode(new Tag('root'));
Expand All @@ -43,6 +53,47 @@ public function testCallMagic()
$this->assertEquals($child3->id(), $selector->find($root)->id());
}

public function testGetMagic()
{
$root = new HtmlNode(new Tag('root'));
$parent = new HtmlNode(new Tag('div'));
$child1 = new HtmlNode(new Tag('a'));
$child2 = new HtmlNode(new Tag('p'));
$child3 = new HtmlNode(new Tag('a'));
$root->addChild($parent);
$parent->addChild($child1);
$parent->addChild($child2);
$child2->addChild($child3);

$selector = new Selector('div * a');
$this->assertEquals($child3->innerHtml, $selector->find($root)->innerHtml);
}

/**
* @expectedException PHPHtmlParser\Exceptions\EmptyCollectionException
*/
public function testGetNoNodes()
{
$collection = new Collection();
$collection->innerHtml;
}

public function testToStringMagic()
{
$root = new HtmlNode(new Tag('root'));
$parent = new HtmlNode(new Tag('div'));
$child1 = new HtmlNode(new Tag('a'));
$child2 = new HtmlNode(new Tag('p'));
$child3 = new HtmlNode(new Tag('a'));
$root->addChild($parent);
$parent->addChild($child1);
$parent->addChild($child2);
$child2->addChild($child3);

$selector = new Selector('div * a');
$this->assertEquals((string)$child3, (string)$selector->find($root));
}

public function testToArray()
{
$root = new HtmlNode(new Tag('root'));
Expand Down
40 changes: 40 additions & 0 deletions tests/Node/ChildrenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ public function testNextSibling()
$this->assertEquals($child2->id(), $child->nextSibling()->id());
}

/**
* @expectedException PHPHtmlParser\Exceptions\ChildNotFoundException
*/
public function testNextSiblingNotFound()
{
$parent = new Node;
$child = new Node;
$child->setParent($parent);
$child->nextSibling();
}

/**
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
*/
public function testNextSiblingNoParent()
{
$child = new Node;
$child->nextSibling();
}

public function testPreviousSibling()
{
$parent = new Node;
Expand All @@ -42,6 +62,26 @@ public function testPreviousSibling()
$this->assertEquals($child->id(), $child2->previousSibling()->id());
}

/**
* @expectedException PHPHtmlParser\Exceptions\ChildNotFoundException
*/
public function testPreviousSiblingNotFound()
{
$parent = new Node;
$node = new Node;
$node->setParent($parent);
$node->previousSibling();
}

/**
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
*/
public function testPreviousSiblingNoParent()
{
$child = new Node;
$child->previousSibling();
}

public function testGetChildren()
{
$parent = new Node;
Expand Down
12 changes: 11 additions & 1 deletion tests/Node/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function testTextNone()
{
$a = new Tag('a');
$node = new HtmlNode($a);

$this->assertEmpty($node->text());
}

Expand Down Expand Up @@ -431,4 +431,14 @@ public function testIterator()
}
$this->assertEquals(2, $children);
}

/**
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
*/
public function testAncestorByTagFailure()
{
$a = new Tag('a');
$node = new HtmlNode($a);
$node->ancestorByTag('div');
}
}
74 changes: 74 additions & 0 deletions tests/Node/ParentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public function testRemoveChild()
$this->assertFalse($parent->hasChildren());
}

public function testRemoveChildNotExists()
{
$parent = new Node;
$parent->removeChild(1);
$this->assertFalse($parent->hasChildren());
}

public function testNextChild()
{
$parent = new Node;
Expand Down Expand Up @@ -129,4 +136,71 @@ public function testLastChild()

$this->assertEquals($child3->id(), $parent->lastChild()->id());
}

/**
* @expectedException PHPHtmlParser\Exceptions\CircularException
*/
public function testSetParentDescendantException()
{
$parent = new Node;
$child = new Node;
$parent->addChild($child);
$parent->setParent($child);
}

/**
* @expectedException PHPHtmlParser\Exceptions\CircularException
*/
public function testAddChildAncestorException()
{
$parent = new Node;
$child = new Node;
$parent->addChild($child);
$child->addChild($parent);
}

/**
* @expectedException PHPHtmlParser\Exceptions\CircularException
*/
public function testAddItselfAsChild()
{
$parent = new Node;
$parent->addChild($parent);
}


public function testIsAncestorParent()
{
$parent = new Node;
$child = new Node;
$parent->addChild($child);
$this->assertTrue($child->isAncestor($parent->id()));
}

public function testGetAncestor()
{
$parent = new Node;
$child = new Node;
$parent->addChild($child);
$ancestor = $child->getAncestor($parent->id());
$this->assertEquals($parent->id(), $ancestor->id());
}

public function testGetGreatAncestor()
{
$parent = new Node;
$child = new Node;
$child2 = new Node;
$parent->addChild($child);
$child->addChild($child2);
$ancestor = $child2->getAncestor($parent->id());
$this->assertEquals($parent->id(), $ancestor->id());
}

public function testGetAncestorNotFound()
{
$parent = new Node;
$ancestor = $parent->getAncestor(1);
$this->assertNull($ancestor);
}
}

0 comments on commit 3a80041

Please sign in to comment.