This repository has been archived by the owner on May 8, 2021. It is now read-only.
/
treeTest.php
357 lines (293 loc) · 10.3 KB
/
treeTest.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/*
* XML TEST
* ------------------------------------
* These tests check whether the XML tree API works properly.
*/
require_once('PHPUnit/Framework.php');
if(version_compare(phpversion(), '5.3.0-dev', '<'))
{
die("This test suite requires at least PHP 5.3.0. Your version: ".phpversion()."\r\n");
}
if(!defined('GROUPED'))
{
$config = parse_ini_file('../paths.ini', true);
require($config['libraries']['Opl'].'Base.php');
Opl_Loader::loadPaths($config);
Opl_Loader::register();
}
class treeTest extends PHPUnit_Framework_TestCase
{
protected $tpl;
protected function setUp()
{
$this->tpl = new Opt_Class;
$this->tpl->sourceDir = './templates';
$this->tpl->compileDir = './templates_c';
} // end setUp();
protected function tearDown()
{
$this->tpl = NULL;
} // end tearDown();
public function testBufferGetBuffer()
{
$buffer = new Opt_Xml_Root();
if(!is_array($buffer->getBuffer(Opt_Xml_Buffer::TAG_BEFORE)))
{
$this->fail('The buffer value is not an array.');
}
return true;
} // end testBufferGetBuffer();
public function testBufferAddAfter1()
{
$buffer = new Opt_Xml_Root();
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$property = new ReflectionProperty('Opt_Xml_Buffer', '_buffers');
$property->setAccessible(true);
$value = $property->getValue($buffer);
if(is_array($value) && is_array($value[Opt_Xml_Buffer::TAG_BEFORE])
&& $value[Opt_Xml_Buffer::TAG_BEFORE][0] == 'foo')
{
return true;
}
$this->fail('The value did not reach the code buffer.');
} // end testBufferAddAfter1();
public function testBufferAddAfter2()
{
$buffer = new Opt_Xml_Root();
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'bar');
$property = new ReflectionProperty('Opt_Xml_Buffer', '_buffers');
$property->setAccessible(true);
$value = $property->getValue($buffer);
if(is_array($value) && is_array($value[Opt_Xml_Buffer::TAG_BEFORE])
&& $value[Opt_Xml_Buffer::TAG_BEFORE][0] == 'foo'
&& $value[Opt_Xml_Buffer::TAG_BEFORE][1] == 'bar'
)
{
return true;
}
$this->fail('The values did not reach the code buffer in the correct order.');
} // end testBufferAddAfter2();
public function testBufferAddBefore1()
{
$buffer = new Opt_Xml_Root();
$buffer->addBefore(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$property = new ReflectionProperty('Opt_Xml_Buffer', '_buffers');
$property->setAccessible(true);
$value = $property->getValue($buffer);
if(is_array($value) && is_array($value[Opt_Xml_Buffer::TAG_BEFORE]) && $value[Opt_Xml_Buffer::TAG_BEFORE][0] == 'foo')
{
return true;
}
$this->fail('The value did not reach the code buffer.');
} // end testBufferAddBefore1();
public function testBufferAddBefore2()
{
$buffer = new Opt_Xml_Root();
$buffer->addBefore(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$buffer->addBefore(Opt_Xml_Buffer::TAG_BEFORE, 'bar');
$property = new ReflectionProperty('Opt_Xml_Buffer', '_buffers');
$property->setAccessible(true);
$value = $property->getValue($buffer);
if(is_array($value) && is_array($value[Opt_Xml_Buffer::TAG_BEFORE])
&& $value[Opt_Xml_Buffer::TAG_BEFORE][0] == 'bar'
&& $value[Opt_Xml_Buffer::TAG_BEFORE][1] == 'foo'
)
{
return true;
}
$this->fail('The values did not reach the code buffer in the correct order.');
} // end testBufferAddBefore2();
public function testBufferAddMixed()
{
$buffer = new Opt_Xml_Root();
$buffer->addBefore(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'bar');
$buffer->addBefore(Opt_Xml_Buffer::TAG_BEFORE, 'joe');
$property = new ReflectionProperty('Opt_Xml_Buffer', '_buffers');
$property->setAccessible(true);
$value = $property->getValue($buffer);
if(is_array($value) && is_array($value[Opt_Xml_Buffer::TAG_BEFORE])
&& $value[Opt_Xml_Buffer::TAG_BEFORE][0] == 'joe'
&& $value[Opt_Xml_Buffer::TAG_BEFORE][1] == 'foo'
&& $value[Opt_Xml_Buffer::TAG_BEFORE][2] == 'bar'
)
{
return true;
}
$this->fail('The values did not reach the code buffer in the correct order.');
} // end testBufferAddMixed();
public function testBufferCopy1()
{
$buffer1 = new Opt_Xml_Root();
$buffer1->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$buffer1->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'bar');
$buffer2 = new Opt_Xml_Root();
$buffer2->copyBuffer($buffer1, Opt_Xml_Buffer::TAG_BEFORE, Opt_Xml_Buffer::TAG_BEFORE);
$property = new ReflectionProperty('Opt_Xml_Buffer', '_buffers');
$property->setAccessible(true);
$value = $property->getValue($buffer2);
if(is_array($value) && is_array($value[Opt_Xml_Buffer::TAG_BEFORE])
&& $value[Opt_Xml_Buffer::TAG_BEFORE][0] == 'foo'
&& $value[Opt_Xml_Buffer::TAG_BEFORE][1] == 'bar'
)
{
return true;
}
$this->fail('The values have not been copied between nodes.');
} // end testBufferCopy1();
public function testBufferCopy2()
{
$buffer1 = new Opt_Xml_Root();
$buffer1->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$buffer1->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'bar');
$buffer2 = new Opt_Xml_Root();
$buffer2->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'joe');
$buffer2->copyBuffer($buffer1, Opt_Xml_Buffer::TAG_BEFORE, Opt_Xml_Buffer::TAG_BEFORE);
$property = new ReflectionProperty('Opt_Xml_Buffer', '_buffers');
$property->setAccessible(true);
$value = $property->getValue($buffer2);
if(is_array($value) && is_array($value[Opt_Xml_Buffer::TAG_BEFORE])
&& $value[Opt_Xml_Buffer::TAG_BEFORE][0] == 'foo'
&& $value[Opt_Xml_Buffer::TAG_BEFORE][1] == 'bar'
&& $value[Opt_Xml_Buffer::TAG_BEFORE][2] == 'joe'
)
{
return true;
}
echo "testBufferCopy list:\r\n";
var_dump($value);
$this->fail('The copied values have not been added before the existing values.');
} // end testBufferCopy2();
public function testBufferSize()
{
$buffer = new Opt_Xml_Root();
$size0 = $buffer->bufferSize(Opt_Xml_Buffer::TAG_BEFORE);
$buffer->addAfter(Opt_Xml_Buffer::TAG_AFTER, 'foo');
$size1 = $buffer->bufferSize(Opt_Xml_Buffer::TAG_BEFORE);
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$size2 = $buffer->bufferSize(Opt_Xml_Buffer::TAG_BEFORE);
if($size0 == 0 && $size1 == 0 && $size2 == 1)
{
return true;
}
$this->fail('Invalid buffer size reported by bufferSize().');
} // end testBufferSize();
public function testBufferBuildCode1()
{
$buffer = new Opt_Xml_Root();
$this->assertEquals('', $buffer->buildCode('foo'));
} // end testBufferBuildCode1();
public function testBufferBuildCode2()
{
$buffer = new Opt_Xml_Root();
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$this->assertEquals('<?php foo echo \'foo\'; ?>', $buffer->buildCode(Opt_Xml_Buffer::TAG_BEFORE, 'foo'));
} // end testBufferBuildCode2();
public function testBufferBuildCode3()
{
$buffer = new Opt_Xml_Root();
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'bar');
$buffer->addAfter(Opt_Xml_Buffer::TAG_AFTER, 'joe');
$this->assertEquals('<?php foo bar joe ?>', $buffer->buildCode(Opt_Xml_Buffer::TAG_BEFORE, Opt_Xml_Buffer::TAG_AFTER));
} // end testBufferBuildCode3();
public function testBufferBuildCode4()
{
$buffer = new Opt_Xml_Root();
$buffer->set('nophp', true);
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'foo');
$buffer->addAfter(Opt_Xml_Buffer::TAG_BEFORE, 'bar');
$buffer->addAfter(Opt_Xml_Buffer::TAG_AFTER, 'joe');
$this->assertEquals('foo bar joe', $buffer->buildCode(Opt_Xml_Buffer::TAG_BEFORE, Opt_Xml_Buffer::TAG_AFTER));
} // end testBufferBuildCode4();
public function testNodeSetParent()
{
$buffer = new Opt_Xml_Root();
$element = new Opt_Xml_Element('foo');
$element->setParent($buffer);
if($element->getParent() === $buffer)
{
return true;
}
$this->fail('The element parent has not been set properly.');
} // end testNodeSetParent();
public function testRootGetType()
{
$buffer = new Opt_Xml_Root();
$this->assertEquals('Opt_Xml_Root', $buffer->getType());
} // end testRootGetType();
public function testElementGetType()
{
$buffer = new Opt_Xml_Element('foo');
$this->assertEquals('Opt_Xml_Element', $buffer->getType());
} // end testElementGetType();
public function testTextGetType()
{
$buffer = new Opt_Xml_Text();
$this->assertEquals('Opt_Xml_Text', $buffer->getType());
} // end testTextGetType();
public function testCdataGetType()
{
$buffer = new Opt_Xml_Cdata('foo');
$this->assertEquals('Opt_Xml_Cdata', $buffer->getType());
} // end testTextGetType();
public function testScannableAppendChild()
{
$topNode = new Opt_Xml_Element('foo');
$subNode = new Opt_Xml_Element('bar');
$topNode->appendChild($subNode);
foreach($topNode as $n)
{
$this->assertTrue($n === $subNode);
}
} // end testScannableAppendChild();
public function testScannableInsertBefore1()
{
$topNode = new Opt_Xml_Element('foo');
$subnodes = array(0 =>
new Opt_Xml_Element('bar'),
new Opt_Xml_Element('joe'),
new Opt_Xml_Element('goo'),
);
$topNode->appendChild($subnodes[0]);
$topNode->appendChild($subnodes[2]);
$topNode->insertBefore($subnodes[1], 1);
$ok = true;
$i = 0;
foreach($topNode as $n)
{
if($n !== $subnodes[$i])
{
$ok = false;
}
$i++;
}
$this->assertTrue($ok);
} // end testScannableInsertBefore1();
public function testScannableInsertBefore2()
{
$topNode = new Opt_Xml_Element('foo');
$subnodes = array(0 =>
new Opt_Xml_Element('bar'),
new Opt_Xml_Element('joe'),
new Opt_Xml_Element('goo'),
);
$topNode->appendChild($subnodes[0]);
$topNode->appendChild($subnodes[1]);
$topNode->insertBefore($subnodes[2]);
$ok = true;
$i = 0;
foreach($topNode as $n)
{
if($n !== $subnodes[$i])
{
$ok = false;
}
$i++;
}
$this->assertTrue($ok);
} // end testScannableInsertBefore2();
} // end treeTest;
?>