Skip to content

Commit 3a80041

Browse files
committed
Updated the tests
1 parent 1cc4007 commit 3a80041

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

tests/CollectionTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use PHPHtmlParser\Selector;
44
use PHPHtmlParser\Dom\HtmlNode;
55
use PHPHtmlParser\Dom\Tag;
6+
use PHPHtmlParser\Dom\Collection;
67

78
class CollectionTest extends PHPUnit_Framework_TestCase {
89

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

31+
/**
32+
* @expectedException PHPHtmlParser\Exceptions\EmptyCollectionException
33+
*/
34+
public function testCallNoNodes()
35+
{
36+
$collection = new Collection();
37+
$collection->innerHtml();
38+
}
39+
3040
public function testCallMagic()
3141
{
3242
$root = new HtmlNode(new Tag('root'));
@@ -43,6 +53,47 @@ public function testCallMagic()
4353
$this->assertEquals($child3->id(), $selector->find($root)->id());
4454
}
4555

56+
public function testGetMagic()
57+
{
58+
$root = new HtmlNode(new Tag('root'));
59+
$parent = new HtmlNode(new Tag('div'));
60+
$child1 = new HtmlNode(new Tag('a'));
61+
$child2 = new HtmlNode(new Tag('p'));
62+
$child3 = new HtmlNode(new Tag('a'));
63+
$root->addChild($parent);
64+
$parent->addChild($child1);
65+
$parent->addChild($child2);
66+
$child2->addChild($child3);
67+
68+
$selector = new Selector('div * a');
69+
$this->assertEquals($child3->innerHtml, $selector->find($root)->innerHtml);
70+
}
71+
72+
/**
73+
* @expectedException PHPHtmlParser\Exceptions\EmptyCollectionException
74+
*/
75+
public function testGetNoNodes()
76+
{
77+
$collection = new Collection();
78+
$collection->innerHtml;
79+
}
80+
81+
public function testToStringMagic()
82+
{
83+
$root = new HtmlNode(new Tag('root'));
84+
$parent = new HtmlNode(new Tag('div'));
85+
$child1 = new HtmlNode(new Tag('a'));
86+
$child2 = new HtmlNode(new Tag('p'));
87+
$child3 = new HtmlNode(new Tag('a'));
88+
$root->addChild($parent);
89+
$parent->addChild($child1);
90+
$parent->addChild($child2);
91+
$child2->addChild($child3);
92+
93+
$selector = new Selector('div * a');
94+
$this->assertEquals((string)$child3, (string)$selector->find($root));
95+
}
96+
4697
public function testToArray()
4798
{
4899
$root = new HtmlNode(new Tag('root'));

tests/Node/ChildrenTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ public function testNextSibling()
3232
$this->assertEquals($child2->id(), $child->nextSibling()->id());
3333
}
3434

35+
/**
36+
* @expectedException PHPHtmlParser\Exceptions\ChildNotFoundException
37+
*/
38+
public function testNextSiblingNotFound()
39+
{
40+
$parent = new Node;
41+
$child = new Node;
42+
$child->setParent($parent);
43+
$child->nextSibling();
44+
}
45+
46+
/**
47+
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
48+
*/
49+
public function testNextSiblingNoParent()
50+
{
51+
$child = new Node;
52+
$child->nextSibling();
53+
}
54+
3555
public function testPreviousSibling()
3656
{
3757
$parent = new Node;
@@ -42,6 +62,26 @@ public function testPreviousSibling()
4262
$this->assertEquals($child->id(), $child2->previousSibling()->id());
4363
}
4464

65+
/**
66+
* @expectedException PHPHtmlParser\Exceptions\ChildNotFoundException
67+
*/
68+
public function testPreviousSiblingNotFound()
69+
{
70+
$parent = new Node;
71+
$node = new Node;
72+
$node->setParent($parent);
73+
$node->previousSibling();
74+
}
75+
76+
/**
77+
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
78+
*/
79+
public function testPreviousSiblingNoParent()
80+
{
81+
$child = new Node;
82+
$child->previousSibling();
83+
}
84+
4585
public function testGetChildren()
4686
{
4787
$parent = new Node;

tests/Node/HtmlTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function testTextNone()
271271
{
272272
$a = new Tag('a');
273273
$node = new HtmlNode($a);
274-
274+
275275
$this->assertEmpty($node->text());
276276
}
277277

@@ -431,4 +431,14 @@ public function testIterator()
431431
}
432432
$this->assertEquals(2, $children);
433433
}
434+
435+
/**
436+
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
437+
*/
438+
public function testAncestorByTagFailure()
439+
{
440+
$a = new Tag('a');
441+
$node = new HtmlNode($a);
442+
$node->ancestorByTag('div');
443+
}
434444
}

tests/Node/ParentTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public function testRemoveChild()
5454
$this->assertFalse($parent->hasChildren());
5555
}
5656

57+
public function testRemoveChildNotExists()
58+
{
59+
$parent = new Node;
60+
$parent->removeChild(1);
61+
$this->assertFalse($parent->hasChildren());
62+
}
63+
5764
public function testNextChild()
5865
{
5966
$parent = new Node;
@@ -129,4 +136,71 @@ public function testLastChild()
129136

130137
$this->assertEquals($child3->id(), $parent->lastChild()->id());
131138
}
139+
140+
/**
141+
* @expectedException PHPHtmlParser\Exceptions\CircularException
142+
*/
143+
public function testSetParentDescendantException()
144+
{
145+
$parent = new Node;
146+
$child = new Node;
147+
$parent->addChild($child);
148+
$parent->setParent($child);
149+
}
150+
151+
/**
152+
* @expectedException PHPHtmlParser\Exceptions\CircularException
153+
*/
154+
public function testAddChildAncestorException()
155+
{
156+
$parent = new Node;
157+
$child = new Node;
158+
$parent->addChild($child);
159+
$child->addChild($parent);
160+
}
161+
162+
/**
163+
* @expectedException PHPHtmlParser\Exceptions\CircularException
164+
*/
165+
public function testAddItselfAsChild()
166+
{
167+
$parent = new Node;
168+
$parent->addChild($parent);
169+
}
170+
171+
172+
public function testIsAncestorParent()
173+
{
174+
$parent = new Node;
175+
$child = new Node;
176+
$parent->addChild($child);
177+
$this->assertTrue($child->isAncestor($parent->id()));
178+
}
179+
180+
public function testGetAncestor()
181+
{
182+
$parent = new Node;
183+
$child = new Node;
184+
$parent->addChild($child);
185+
$ancestor = $child->getAncestor($parent->id());
186+
$this->assertEquals($parent->id(), $ancestor->id());
187+
}
188+
189+
public function testGetGreatAncestor()
190+
{
191+
$parent = new Node;
192+
$child = new Node;
193+
$child2 = new Node;
194+
$parent->addChild($child);
195+
$child->addChild($child2);
196+
$ancestor = $child2->getAncestor($parent->id());
197+
$this->assertEquals($parent->id(), $ancestor->id());
198+
}
199+
200+
public function testGetAncestorNotFound()
201+
{
202+
$parent = new Node;
203+
$ancestor = $parent->getAncestor(1);
204+
$this->assertNull($ancestor);
205+
}
132206
}

0 commit comments

Comments
 (0)