/
NodeList.test.php
95 lines (75 loc) · 2.46 KB
/
NodeList.test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* PHP.Gt (http://php.gt)
* @copyright Copyright Ⓒ 2015 Bright Flair Ltd. (http://brightflair.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
*/
namespace Gt\Dom;
class NodeList_Test extends \PHPUnit_Framework_TestCase {
private $document;
public function setUp() {
$this->document = new Document("<!doctype html>"
. "<h1>Test heading</h1>"
. "<p id='para1'>Test paragraph one</p>"
. "<p id='para2'>Test paragraph two</p>");
}
public function tearDown() {}
public function testConstructsWithArray() {
$nodeList = new NodeList([
$this->document->getElementById("para1"),
$this->document->getElementById("para2"),
]);
$this->assertInstanceOf("\Gt\Dom\NodeList", $nodeList);
}
public function testConstructsWithNodeList() {
$nodeList = new NodeList([
$this->document->getElementById("para1"),
$this->document->getElementById("para2"),
]);
$anotherNodeList = new NodeList($nodeList);
$this->assertInstanceOf("\Gt\Dom\NodeList", $anotherNodeList);
}
public function testConstructsWithDomNodeList() {
$domNodeList = $this->document->domDocument->getElementsByTagName("p");
$this->assertInstanceOf("\DOMNodeList", $domNodeList);
$nodeList = new NodeList($domNodeList);
$this->assertInstanceOf("\Gt\Dom\NodeList", $nodeList);
}
/**
* @expectedException \Gt\Dom\InvalidNodeTypeException
*/
public function testDoesNotConstructWithInvalidParameter() {
$nodeList = new NodeList(7 * 7 + 7 * 7 + 7);
}
public function testCount() {
$nodeList = $this->document->getElementsByTagName("p");
$this->assertCount(2, $nodeList);
}
public function testForEach() {
$nodeList = $this->document->getElementsByTagName("p");
$previousNode = null;
foreach ($nodeList as $i => $node) {
$this->assertNotSame($previousNode, $node);
$this->assertSame($nodeList[$i], $node);
$previousNode = $node;
}
}
public function testArrayAccess() {
$nodeList = $this->document->getElementsByTagName("p");
$this->assertArrayHasKey(0, $nodeList);
$this->assertArrayHasKey(1, $nodeList);
$this->assertArrayNotHasKey(2, $nodeList);
$this->assertArrayNotHasKey("test", $nodeList);
$nodeList[2] = $this->document->createElement("button");
$this->assertArrayHasKey(2, $nodeList);
unset($nodeList[2]);
$this->assertArrayNotHasKey(2, $nodeList);
}
/**
* @expectedException \Gt\Dom\InvalidNodeTypeException
*/
public function testArrayAccessThrowsExceptionFromInvalidType() {
$nodeList = $this->document->getElementsByTagName("p");
$nodeList[9] = new \DateTime();
}
}#