DomTest.php
3.7 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* PHPTAL templating engine
*
* PHP Version 5
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id: $
* @link http://phptal.org/
*/
require_once dirname(__FILE__)."/config.php";
PHPTAL::setIncludePath();
require_once 'PHPTAL/Dom/DocumentBuilder.php';
PHPTAL::restoreIncludePath();
class DOMTest extends PHPTAL_TestCase
{
private function newElement($name = 'foo',$ns = '')
{
$xmlns = new PHPTAL_Dom_XmlnsState(array(),'');
return new PHPTAL_Dom_Element($name,$ns,array(),$xmlns);
}
function testAppendChild()
{
$el1 = $this->newElement();
$el2 = $this->newElement();
$this->assertType('array',$el1->childNodes);
$this->assertNull($el2->parentNode);
$el1->appendChild($el2);
$this->assertNull($el1->parentNode);
$this->assertSame($el1,$el2->parentNode);
$this->assertEquals(1,count($el1->childNodes));
$this->assertTrue(isset($el1->childNodes[0]));
$this->assertSame($el2,$el1->childNodes[0]);
}
function testAppendChildChangesParent()
{
$el1 = $this->newElement();
$el2 = $this->newElement();
$ch = $this->newElement();
$el1->appendChild($ch);
$this->assertTrue(isset($el1->childNodes[0]));
$this->assertSame($ch,$el1->childNodes[0]);
$el2->appendChild($ch);
$this->assertTrue(isset($el2->childNodes[0]));
$this->assertSame($ch,$el2->childNodes[0]);
$this->assertFalse(isset($el1->childNodes[0]));
$this->assertEquals(0,count($el1->childNodes));
$this->assertEquals(1,count($el2->childNodes));
}
function testRemoveChild()
{
$el1 = $this->newElement();
$el2 = $this->newElement();
$el3 = $this->newElement();
$el4 = $this->newElement();
$el1->appendChild($el2);
$el1->appendChild($el3);
$el1->appendChild($el4);
$this->assertEquals(3,count($el1->childNodes));
$this->assertTrue(isset($el1->childNodes[2]));
$this->assertFalse(isset($el1->childNodes[3]));
$this->assertSame($el1,$el4->parentNode);
$el1->removeChild($el4);
$this->assertNull($el4->parentNode);
$this->assertEquals(2,count($el1->childNodes));
$this->assertTrue(isset($el1->childNodes[1]));
$this->assertFalse(isset($el1->childNodes[2]));
$this->assertSame($el3,end($el1->childNodes));
$el1->removeChild($el2);
$this->assertEquals(1,count($el1->childNodes));
$this->assertTrue(isset($el1->childNodes[0]));
$this->assertFalse(isset($el1->childNodes[1]));
}
function testReplaceChild()
{
$el1 = $this->newElement();
$el2 = $this->newElement();
$el3 = $this->newElement();
$el4 = $this->newElement();
$r = $this->newElement();
$el1->appendChild($el2);
$el1->appendChild($el3);
$el1->appendChild($el4);
$this->assertEquals(3,count($el1->childNodes));
$this->assertSame($el3,$el1->childNodes[1]);
$el1->replaceChild($r, $el3);
$this->assertEquals(3,count($el1->childNodes));
$this->assertSame($el2,$el1->childNodes[0]);
$this->assertSame($r,$el1->childNodes[1]);
$this->assertSame($el4,$el1->childNodes[2]);
$this->assertNull($el3->parentNode);
$this->assertSame($el1,$r->parentNode);
}
}