Skip to content

Commit 2b9651c

Browse files
committed
Added tests for the new array access
1 parent 2f78ee0 commit 2b9651c

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

tests/Node/ChildrenTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,24 @@ public function testPreviousSibling()
4141
$child2->setParent($parent);
4242
$this->assertEquals($child->id(), $child2->previousSibling()->id());
4343
}
44+
45+
public function testGetChildren()
46+
{
47+
$parent = new Node;
48+
$child = new Node;
49+
$child2 = new Node;
50+
$child->setParent($parent);
51+
$child2->setParent($parent);
52+
$this->assertEquals($child->id(), $parent->getChildren()[0]->id());
53+
}
54+
55+
public function testCountChildren()
56+
{
57+
$parent = new Node;
58+
$child = new Node;
59+
$child2 = new Node;
60+
$child->setParent($parent);
61+
$child2->setParent($parent);
62+
$this->assertEquals(2, $parent->countChildren());
63+
}
4464
}

tests/Node/HtmlTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
34
use PHPHtmlParser\Dom\HtmlNode;
45
use PHPHtmlParser\Dom\TextNode;
56
use PHPHtmlParser\Dom\MockNode;
@@ -368,4 +369,66 @@ public function testSetAttribute()
368369
$node->setAttribute('class', 'foo');
369370
$this->assertEquals('foo', $node->getAttribute('class'));
370371
}
372+
373+
public function testCountable()
374+
{
375+
$div = new Tag('div');
376+
$div->setAttributes([
377+
'class' => [
378+
'value' => 'all',
379+
'doubleQuote' => true,
380+
],
381+
]);
382+
$a = new Tag('a');
383+
$a->setAttributes([
384+
'href' => [
385+
'value' => 'http://google.com',
386+
'doubleQuote' => false,
387+
],
388+
]);
389+
$br = new Tag('br');
390+
$br->selfClosing();
391+
392+
$parent = new HtmlNode($div);
393+
$childa = new HtmlNode($a);
394+
$childbr = new HtmlNode($br);
395+
$parent->addChild($childa);
396+
$parent->addChild($childbr);
397+
$childa->addChild(new TextNode('link'));
398+
399+
$this->assertEquals(count($parent->getChildren()), count($parent));
400+
}
401+
402+
public function testIterator()
403+
{
404+
$div = new Tag('div');
405+
$div->setAttributes([
406+
'class' => [
407+
'value' => 'all',
408+
'doubleQuote' => true,
409+
],
410+
]);
411+
$a = new Tag('a');
412+
$a->setAttributes([
413+
'href' => [
414+
'value' => 'http://google.com',
415+
'doubleQuote' => false,
416+
],
417+
]);
418+
$br = new Tag('br');
419+
$br->selfClosing();
420+
421+
$parent = new HtmlNode($div);
422+
$childa = new HtmlNode($a);
423+
$childbr = new HtmlNode($br);
424+
$parent->addChild($childa);
425+
$parent->addChild($childbr);
426+
$childa->addChild(new TextNode('link'));
427+
428+
$children = 0;
429+
foreach ($parent as $child) {
430+
++$children;
431+
}
432+
$this->assertEquals(2, $children);
433+
}
371434
}

0 commit comments

Comments
 (0)