Skip to content

Commit 41b8ed7

Browse files
committed
Added Unit test cases for new methods
1 parent 21e15e1 commit 41b8ed7

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/PHPHtmlParser/Finder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ public function __construct($id)
1919

2020
/**
2121
*
22-
* Find node in tree
22+
* Find node in tree by id
2323
*
2424
* @param AbstractNode $node
2525
* @return bool|AbstractNode
2626
*/
2727
public function find(AbstractNode $node)
2828
{
29-
3029
if (!$node->id()) {
3130
return $this->find($node->firstChild());
3231
}
@@ -46,11 +45,10 @@ public function find(AbstractNode $node)
4645
if ($nextSibling->id() < $this->id) {
4746
return $this->find($nextSibling);
4847
}
49-
} else {
48+
} else if (!$node->isTextNode()) {
5049
return $this->find($node->firstChild());
5150
}
5251

5352
return false;
5453
}
55-
5654
}

tests/DomTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,52 @@ public function testDeleteNode()
319319
unset($a);
320320
$this->assertEquals('<div class="all"><p>Hey bro, <br /> :)</p></div>', (string) $dom);
321321
}
322+
323+
public function testCountChildren()
324+
{
325+
$dom = new Dom;
326+
$dom->load('<strong>hello</strong><code class="language-php">$foo = "bar";</code>');
327+
$this->assertEquals(2, $dom->countChildren());
328+
}
329+
330+
public function testGetChildrenArray()
331+
{
332+
$dom = new Dom;
333+
$dom->load('<strong>hello</strong><code class="language-php">$foo = "bar";</code>');
334+
$this->assertInternalType('array', $dom->getChildren());
335+
}
336+
337+
public function testHasChildren()
338+
{
339+
$dom = new Dom;
340+
$dom->load('<strong>hello</strong><code class="language-php">$foo = "bar";</code>');
341+
$this->assertTrue($dom->hasChildren());
342+
}
343+
344+
public function testFindByIdVar1()
345+
{
346+
$dom = new Dom;
347+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
348+
/** @var Dom\AbstractNode $result */
349+
$result = $dom->findById(4);
350+
$this->assertEquals(4, $result->id());
351+
}
352+
353+
public function testFindByIdVar2()
354+
{
355+
$dom = new Dom;
356+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
357+
/** @var Dom\AbstractNode $result */
358+
$result = $dom->findById(5);
359+
$this->assertEquals(5, $result->id());
360+
}
361+
362+
public function testFindByIdNotFountEleement()
363+
{
364+
$dom = new Dom;
365+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
366+
/** @var Dom\AbstractNode $result */
367+
$result = $dom->findById(8);
368+
$this->assertFalse($result);
369+
}
322370
}

0 commit comments

Comments
 (0)