Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/xml.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * XmlTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs | |
17 * @since CakePHP(tm) v 1.2.0.5432 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 App::import('Core', 'Xml'); | |
21 | |
22 /** | |
23 * XmlTest class | |
24 * | |
25 * @package cake | |
26 * @subpackage cake.tests.cases.libs | |
27 */ | |
28 class XmlTest extends CakeTestCase { | |
29 | |
30 /** | |
31 * setUp method | |
32 * | |
33 * @access public | |
34 * @return void | |
35 */ | |
36 function setUp() { | |
37 $manager =& new XmlManager(); | |
38 $manager->namespaces = array(); | |
39 } | |
40 | |
41 /** | |
42 * testRootTagParsing method | |
43 * | |
44 * @access public | |
45 * @return void | |
46 */ | |
47 function testRootTagParsing() { | |
48 $input = '<' . '?xml version="1.0" encoding="UTF-8" ?' . '>' . "\n" . | |
49 '<plugin id="1" version_id="1" name="my_plugin" title="My Plugin" author="Me" author_email="me@cakephp.org" description="My awesome package" created="2008-01-28 18:21:13" updated="2008-01-28 18:21:13">' | |
50 .'<current id="1" plugin_id="1" name="1.0" file="" created="2008-01-28 18:21:13" updated="2008-01-28 18:21:13" />' | |
51 .'<version id="1" plugin_id="1" name="1.0" file="" created="2008-01-28 18:21:13" updated="2008-01-28 18:21:13" />' | |
52 .'</plugin>'; | |
53 $xml = new Xml($input); | |
54 $this->assertEqual($xml->children[0]->name, 'plugin'); | |
55 $this->assertEqual($xml->children[0]->children[0]->name, 'current'); | |
56 $this->assertEqual($xml->toString(true), $input); | |
57 } | |
58 | |
59 /** | |
60 * testSerialization method | |
61 * | |
62 * @access public | |
63 * @return void | |
64 */ | |
65 function testSerialization() { | |
66 $input = array( | |
67 array( | |
68 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null), | |
69 'Style' => array('id' => null, 'name' => null), | |
70 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
71 'Industry' => array('id' => 1, 'name' => 'Financial') | |
72 ), | |
73 array( | |
74 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
75 'Style' => array('id' => null, 'name' => null), | |
76 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
77 'Industry' => array('id' => 2, 'name' => 'Education') | |
78 ) | |
79 ); | |
80 | |
81 $xml = new Xml($input); | |
82 $result = preg_replace("/\n/",'', $xml->toString(false)); | |
83 $expected = '<project id="1" title="" client_id="1" show="1" is_spotlight="" style_id="0" job_type_id="1" industry_id="1" modified="" created=""><style id="" name="" /><job_type id="1" name="Touch Screen Kiosk" /><industry id="1" name="Financial" /></project><project id="2" title="" client_id="2" show="1" is_spotlight="" style_id="0" job_type_id="2" industry_id="2" modified="2007-11-26 14:48:36" created=""><style id="" name="" /><job_type id="2" name="Awareness Campaign" /><industry id="2" name="Education" /></project>'; | |
84 $this->assertEqual($result, $expected); | |
85 | |
86 $input = array( | |
87 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null), | |
88 'Style' => array('id' => null, 'name' => null), | |
89 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
90 'Industry' => array('id' => 1, 'name' => 'Financial') | |
91 ); | |
92 $expected = '<project id="1" title="" client_id="1" show="1" is_spotlight="" style_id="0" job_type_id="1" industry_id="1" modified="" created=""><style id="" name="" /><job_type id="1" name="Touch Screen Kiosk" /><industry id="1" name="Financial" /></project>'; | |
93 $xml = new Xml($input); | |
94 $result = preg_replace("/\n/",'', $xml->toString(false)); | |
95 $this->assertEqual($result, $expected); | |
96 } | |
97 | |
98 /** | |
99 * testSerializeOnMultiDimensionalArray method | |
100 * | |
101 * @access public | |
102 * @return void | |
103 */ | |
104 function testSerializeOnMultiDimensionalArray() { | |
105 $data = array( | |
106 'Statuses' => array( | |
107 array('Status' => array('id' => 1)), | |
108 array('Status' => array('id' => 2)) | |
109 ) | |
110 ); | |
111 $result =& new Xml($data, array('format' => 'tags')); | |
112 $expected = '<statuses><status><id>1</id></status><status><id>2</id></status></statuses>'; | |
113 $this->assertIdentical($result->toString(), $expected); | |
114 } | |
115 | |
116 /** | |
117 * testSerializeCapsWithoutSlug method | |
118 * | |
119 * @access public | |
120 * @return void | |
121 */ | |
122 function testSerializeCapsWithoutSlug() { | |
123 $data = array( | |
124 'USERS' => array( | |
125 array('USER' => array('ID' => 1)), | |
126 array('USER' => array('ID' => 2)) | |
127 ) | |
128 ); | |
129 $result =& new Xml($data, array('format' => 'tags', 'slug' => false)); | |
130 $expected = '<USERS><USER><ID>1</ID></USER><USER><ID>2</ID></USER></USERS>'; | |
131 $this->assertIdentical($result->toString(), $expected); | |
132 } | |
133 | |
134 /** | |
135 * test serialization of boolean and null values. false = 0, true = 1, null = '' | |
136 * | |
137 * @return void | |
138 */ | |
139 function testSerializationOfBooleanAndBooleanishValues() { | |
140 $xml =& new Xml(array('data' => array('example' => false))); | |
141 $result = $xml->toString(false); | |
142 $expected = '<data example="0" />'; | |
143 $this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s'); | |
144 | |
145 $xml =& new Xml(array('data' => array('example' => true))); | |
146 $result = $xml->toString(false); | |
147 $expected = '<data example="1" />'; | |
148 $this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s'); | |
149 | |
150 $xml =& new Xml(array('data' => array('example' => null))); | |
151 $result = $xml->toString(false); | |
152 $expected = '<data example="" />'; | |
153 $this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s'); | |
154 | |
155 $xml =& new Xml(array('data' => array('example' => 0))); | |
156 $result = $xml->toString(false); | |
157 $expected = '<data example="0" />'; | |
158 $this->assertEqual($result, $expected, 'Boolean-ish values incorrectly handled. %s'); | |
159 | |
160 $xml =& new Xml(array('data' => array('example' => 1))); | |
161 $result = $xml->toString(false); | |
162 $expected = '<data example="1" />'; | |
163 $this->assertEqual($result, $expected, 'Boolean-ish values incorrectly handled. %s'); | |
164 } | |
165 | |
166 /** | |
167 * testSimpleArray method | |
168 * | |
169 * @access public | |
170 * @return void | |
171 */ | |
172 function testSimpleArray() { | |
173 $xml = new Xml(array('hello' => 'world'), array('format' => 'tags')); | |
174 | |
175 $result = $xml->toString(false); | |
176 $expected = '<hello><![CDATA[world]]></hello>'; | |
177 $this->assertEqual($expected, $result); | |
178 } | |
179 | |
180 /** | |
181 * testSimpleObject method | |
182 * | |
183 * @access public | |
184 * @return void | |
185 */ | |
186 function testSimpleObject() { | |
187 $input = new StdClass(); | |
188 $input->hello = 'world'; | |
189 $xml = new Xml($input, array('format' => 'tags')); | |
190 | |
191 $result = $xml->toString(false); | |
192 $expected = '<hello><![CDATA[world]]></hello>'; | |
193 $this->assertEqual($expected, $result); | |
194 } | |
195 | |
196 /** | |
197 * testSimpleArrayWithZeroValues method | |
198 * | |
199 * @access public | |
200 * @return void | |
201 */ | |
202 function testSimpleArrayWithZeroValues() { | |
203 $xml = new Xml(array('zero_string' => '0', 'zero_integer' => 0), array('format' => 'tags')); | |
204 | |
205 $result = $xml->toString(false); | |
206 $expected = '<zero_string>0</zero_string><zero_integer>0</zero_integer>'; | |
207 $this->assertEqual($expected, $result); | |
208 | |
209 $data = array( | |
210 'Client' => array( | |
211 'id' => 3, | |
212 'object_id' => 9, | |
213 'key' => 'alt', | |
214 'name' => 'Client Two', | |
215 'created_by' => 4, | |
216 'status' => '0', | |
217 'num_projects' => 0 | |
218 ) | |
219 ); | |
220 $xml = new Xml($data, array('format' => 'tags')); | |
221 $result = $xml->toString(array('format' => 'tags', 'header' => false)); | |
222 $this->assertPattern('/<status>0<\/status>/', $result); | |
223 $this->assertPattern('/<num_projects>0<\/num_projects>/', $result); | |
224 } | |
225 /** | |
226 * testHeader method | |
227 * | |
228 * @access public | |
229 * @return void | |
230 */ | |
231 function testHeader() { | |
232 $input = new stdClass(); | |
233 $input->hello = 'world'; | |
234 $xml = new Xml($input, array('format' => 'tags')); | |
235 | |
236 $result = $xml->toString(array('header' => true)); | |
237 $expected = '<'.'?xml version="1.0" encoding="UTF-8" ?'.'>'."\n".'<hello><![CDATA[world]]></hello>'; | |
238 $this->assertEqual($expected, $result); | |
239 } | |
240 | |
241 /** | |
242 * testOwnerAssignment method | |
243 * | |
244 * @access public | |
245 * @return void | |
246 */ | |
247 function testOwnerAssignment() { | |
248 $xml = new Xml(); | |
249 $node =& $xml->createElement('hello', 'world'); | |
250 $owner =& $node->document(); | |
251 $this->assertTrue($xml === $owner); | |
252 | |
253 $children =& $node->children; | |
254 $childOwner =& $children[0]->document(); | |
255 $this->assertTrue($xml === $childOwner); | |
256 } | |
257 | |
258 /** | |
259 * testArraySingleSerialization method | |
260 * | |
261 * @access public | |
262 * @return void | |
263 */ | |
264 function testArraySingleSerialization() { | |
265 $input = array( | |
266 'Post' => array( | |
267 'id' => '1', 'author_id' => '1', 'title' => 'First Post', | |
268 'body' => 'First Post Body', 'published' => 'Y', | |
269 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' | |
270 ), | |
271 'Author' => array( | |
272 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | |
273 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working' | |
274 ) | |
275 ); | |
276 | |
277 $expected = '<post><id>1</id><author_id>1</author_id><title><![CDATA[First Post]]>'; | |
278 $expected .= '</title><body><![CDATA[First Post Body]]></body><published><![CDATA[Y]]>'; | |
279 $expected .= '</published><created><![CDATA[2007-03-18 10:39:23]]></created><updated>'; | |
280 $expected .= '<![CDATA[2007-03-18 10:41:31]]></updated><author><id>1</id><user>'; | |
281 $expected .= '<![CDATA[mariano]]></user><password><![CDATA[5f4dcc3b5aa765d61d8327deb882'; | |
282 $expected .= 'cf99]]></password><created><![CDATA[2007-03-17 01:16:23]]></created>'; | |
283 $expected .= '<updated><![CDATA[2007-03-17 01:18:31]]></updated><test><![CDATA[working]]>'; | |
284 $expected .= '</test></author></post>'; | |
285 | |
286 $xml = new Xml($input, array('format' => 'tags')); | |
287 $result = $xml->toString(false); | |
288 $this->assertEqual($expected, $result); | |
289 } | |
290 | |
291 /** | |
292 * testArraySerialization method | |
293 * | |
294 * @access public | |
295 * @return void | |
296 */ | |
297 function testSerializationArray() { | |
298 $input = array( | |
299 array( | |
300 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null), | |
301 'Style' => array('id' => null, 'name' => null), | |
302 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
303 'Industry' => array('id' => 1, 'name' => 'Financial') | |
304 ), | |
305 array( | |
306 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
307 'Style' => array('id' => null, 'name' => null), | |
308 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
309 'Industry' => array('id' => 2, 'name' => 'Education'), | |
310 ) | |
311 ); | |
312 $expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry></project>'; | |
313 | |
314 $xml = new Xml($input, array('format' => 'tags')); | |
315 $result = $xml->toString(array('header' => false, 'cdata' => false)); | |
316 $this->assertEqual($expected, $result); | |
317 } | |
318 | |
319 /** | |
320 * testNestedArraySerialization method | |
321 * | |
322 * @access public | |
323 * @return void | |
324 */ | |
325 function testSerializationNestedArray() { | |
326 $input = array( | |
327 array( | |
328 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null), | |
329 'Style' => array('id' => null, 'name' => null), | |
330 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
331 'Industry' => array('id' => 1, 'name' => 'Financial'), | |
332 'BusinessSolution' => array(array('id' => 6, 'name' => 'Convert Sales')), | |
333 'MediaType' => array( | |
334 array('id' => 15, 'name' => 'Print'), | |
335 array('id' => 7, 'name' => 'Web Demo'), | |
336 array('id' => 6, 'name' => 'CD-ROM') | |
337 ) | |
338 ), | |
339 array( | |
340 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
341 'Style' => array('id' => null, 'name' => null), | |
342 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
343 'Industry' => array('id' => 2, 'name' => 'Education'), | |
344 'BusinessSolution' => array( | |
345 array('id' => 4, 'name' => 'Build Relationship'), | |
346 array('id' => 6, 'name' => 'Convert Sales') | |
347 ), | |
348 'MediaType' => array( | |
349 array('id' => 17, 'name' => 'Web'), | |
350 array('id' => 6, 'name' => 'CD-ROM') | |
351 ) | |
352 ) | |
353 ); | |
354 $expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type><id>15</id><name>Print</name></media_type><media_type><id>7</id><name>Web Demo</name></media_type><media_type><id>6</id><name>CD-ROM</name></media_type></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry><business_solution><id>4</id><name>Build Relationship</name></business_solution><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type><id>17</id><name>Web</name></media_type><media_type><id>6</id><name>CD-ROM</name></media_type></project>'; | |
355 | |
356 $xml = new Xml($input, array('format' => 'tags')); | |
357 $result = $xml->toString(array('header' => false, 'cdata' => false)); | |
358 $this->assertEqual($expected, $result); | |
359 } | |
360 | |
361 /** | |
362 * Prove that serialization with a given root node works | |
363 * as expected. | |
364 * | |
365 * @access public | |
366 * @return void | |
367 * @link https://trac.cakephp.org/ticket/6294 | |
368 */ | |
369 function testArraySerializationWithRoot() { | |
370 $input = array( | |
371 array('Shirt' => array('id' => 1, 'color' => 'green')), | |
372 array('Shirt' => array('id' => 2, 'color' => 'blue')), | |
373 ); | |
374 $expected = '<collection><shirt id="1" color="green" />'; | |
375 $expected .= '<shirt id="2" color="blue" /></collection>'; | |
376 | |
377 $Xml = new Xml($input, array('root' => 'collection')); | |
378 $result = $Xml->toString(array('header' => false)); | |
379 $this->assertEqual($expected, $result); | |
380 } | |
381 | |
382 /** | |
383 * testCloneNode | |
384 * | |
385 * @access public | |
386 * @return void | |
387 */ | |
388 function testCloneNode() { | |
389 $node =& new XmlNode('element', 'myValue'); | |
390 $twin =& $node->cloneNode(); | |
391 $this->assertEqual($node, $twin); | |
392 } | |
393 | |
394 /** | |
395 * testNextSibling | |
396 * | |
397 * @access public | |
398 * @return void | |
399 */ | |
400 function testNextSibling() { | |
401 $input = array( | |
402 array( | |
403 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null), | |
404 'Style' => array('id' => null, 'name' => null), | |
405 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
406 'Industry' => array('id' => 1, 'name' => 'Financial') | |
407 ), | |
408 array( | |
409 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
410 'Style' => array('id' => null, 'name' => null), | |
411 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
412 'Industry' => array('id' => 2, 'name' => 'Education'), | |
413 ) | |
414 ); | |
415 $xml =& new Xml($input, array('format' => 'tags')); | |
416 $node =& $xml->children[0]->children[0]; | |
417 | |
418 $nextSibling =& $node->nextSibling(); | |
419 $this->assertEqual($nextSibling, $xml->children[0]->children[1]); | |
420 | |
421 $nextSibling2 =& $nextSibling->nextSibling(); | |
422 $this->assertEqual($nextSibling2, $xml->children[0]->children[2]); | |
423 | |
424 $noFriends =& $xml->children[0]->children[12]; | |
425 $this->assertNull($noFriends->nextSibling()); | |
426 } | |
427 | |
428 /** | |
429 * testPreviousSibling | |
430 * | |
431 * @access public | |
432 * @return void | |
433 */ | |
434 function testPreviousSibling() { | |
435 $input = array( | |
436 array( | |
437 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null), | |
438 'Style' => array('id' => null, 'name' => null), | |
439 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
440 'Industry' => array('id' => 1, 'name' => 'Financial') | |
441 ), | |
442 array( | |
443 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
444 'Style' => array('id' => null, 'name' => null), | |
445 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
446 'Industry' => array('id' => 2, 'name' => 'Education'), | |
447 ) | |
448 ); | |
449 $xml =& new Xml($input, array('format' => 'tags')); | |
450 $node =& $xml->children[0]->children[1]; | |
451 | |
452 $prevSibling =& $node->previousSibling(); | |
453 $this->assertEqual($prevSibling, $xml->children[0]->children[0]); | |
454 | |
455 $this->assertNull($prevSibling->previousSibling()); | |
456 } | |
457 | |
458 /** | |
459 * testAddAndRemoveAttributes | |
460 * | |
461 * @access public | |
462 * @return void | |
463 */ | |
464 function testAddAndRemoveAttributes() { | |
465 $node =& new XmlElement('myElement', 'superValue'); | |
466 $this->assertTrue(empty($node->attributes)); | |
467 | |
468 $attrs = array( | |
469 'id' => 'test', | |
470 'show' => 1, | |
471 'is_spotlight' => 1, | |
472 ); | |
473 $node->addAttribute($attrs); | |
474 $this->assertEqual($node->attributes, $attrs); | |
475 | |
476 $node =& new XmlElement('myElement', 'superValue'); | |
477 $node->addAttribute('test', 'value'); | |
478 $this->assertTrue(isset($node->attributes['test'])); | |
479 | |
480 $node =& new XmlElement('myElement', 'superValue'); | |
481 $obj =& new StdClass(); | |
482 $obj->class = 'info'; | |
483 $obj->id = 'primaryInfoBox'; | |
484 $node->addAttribute($obj); | |
485 $expected = array( | |
486 'class' => 'info', | |
487 'id' => 'primaryInfoBox', | |
488 ); | |
489 $this->assertEqual($node->attributes, $expected); | |
490 | |
491 $result = $node->removeAttribute('class'); | |
492 $this->assertTrue($result); | |
493 $this->assertFalse(isset($node->attributes['class'])); | |
494 | |
495 $result = $node->removeAttribute('missing'); | |
496 $this->assertFalse($result); | |
497 } | |
498 | |
499 /** | |
500 * Tests that XML documents with non-standard spacing (i.e. leading whitespace, whole document | |
501 * on one line) still parse properly. | |
502 * | |
503 * @return void | |
504 */ | |
505 function testParsingWithNonStandardWhitespace() { | |
506 $raw = '<?xml version="1.0" encoding="ISO-8859-1" ?><prices><price>1.0</price></prices>'; | |
507 $array = array('Prices' => array('price' => 1.0)); | |
508 | |
509 $xml = new Xml($raw); | |
510 $this->assertEqual($xml->toArray(), $array); | |
511 $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"'); | |
512 | |
513 $xml = new Xml(' ' . $raw); | |
514 $this->assertEqual($xml->toArray(), $array); | |
515 $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"'); | |
516 | |
517 $xml = new Xml("\n" . $raw); | |
518 $this->assertEqual($xml->toArray(), $array); | |
519 $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"'); | |
520 } | |
521 | |
522 /* Not implemented yet */ | |
523 /* function testChildFilter() { | |
524 $input = array( | |
525 array( | |
526 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null), | |
527 'Style' => array('id' => null, 'name' => null), | |
528 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
529 'Industry' => array('id' => 1, 'name' => 'Financial'), | |
530 'BusinessSolution' => array(array('id' => 6, 'name' => 'Convert Sales')), | |
531 'MediaType' => array( | |
532 array('id' => 15, 'name' => 'Print'), | |
533 array('id' => 7, 'name' => 'Web Demo'), | |
534 array('id' => 6, 'name' => 'CD-ROM') | |
535 ) | |
536 ), | |
537 array( | |
538 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
539 'Style' => array('id' => null, 'name' => null), | |
540 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
541 'Industry' => array('id' => 2, 'name' => 'Education'), | |
542 'BusinessSolution' => array( | |
543 array('id' => 4, 'name' => 'Build Relationship'), | |
544 array('id' => 6, 'name' => 'Convert Sales') | |
545 ), | |
546 'MediaType' => array( | |
547 array('id' => 17, 'name' => 'Web'), | |
548 array('id' => 6, 'name' => 'CD-ROM') | |
549 ) | |
550 ) | |
551 ); | |
552 | |
553 $xml = new Xml($input, array('format' => 'tags', 'tags' => array( | |
554 'MediaType' => array('value' => 'id', 'children' => false), | |
555 'JobType' => array('children' => array()), | |
556 'Industry' => array('children' => array('name')), | |
557 'show' => false | |
558 ))); | |
559 | |
560 $result = $xml->toString(array('header' => false, 'cdata' => false)); | |
561 $expected = '<project><id>1</id><title /><client_id>1</client_id><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><name>Financial</name></industry><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type>15</media_type><media_type>7</media_type><media_type>6</media_type></project><project><id>2</id><title /><client_id>2</client_id><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><name>Education</name></industry><business_solution><id>4</id><name>Build Relationship</name></business_solution><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type>17</media_type><media_type>6</media_type></project>'; | |
562 $this->assertEqual($expected, $result); | |
563 } */ | |
564 | |
565 /* Broken due to a Set class issue */ | |
566 /* function testMixedArray() { | |
567 $input = array('OptionGroup' => array( | |
568 array('name' => 'OptA', 'id' => 12, 'OptA 1', 'OptA 2', 'OptA 3', 'OptA 4', 'OptA 5', 'OptA 6'), | |
569 array('name' => 'OptB', 'id' => 12, 'OptB 1', 'OptB 2', 'OptB 3', 'OptB 4', 'OptB 5', 'OptB 6') | |
570 )); | |
571 $expected = '<option_group><name>OptA</name><id>12</id><option_group>OptA 1</option_group><option_group>OptA 2</option_group><option_group>OptA 3</option_group><option_group>OptA 4</option_group><option_group>OptA 5</option_group><option_group>OptA 6</option_group></option_group><option_group><name>OptB</name><id>12</id><option_group>OptB 1</option_group><option_group>OptB 2</option_group><option_group>OptB 3</option_group><option_group>OptB 4</option_group><option_group>OptB 5</option_group><option_group>OptB 6</option_group></option_group>'; | |
572 $xml = new Xml($input, array('format' => 'tags')); | |
573 $result = $xml->toString(array('header' => false, 'cdata' => false)); | |
574 $this->assertEqual($expected, $result); | |
575 } */ | |
576 | |
577 /* function testMixedNestedArray() { | |
578 $input = array( | |
579 'OptionA' => array( | |
580 'name' => 'OptA', | |
581 'id' => 12, | |
582 'opt' => array('OptA 1', 'OptA 2', 'OptA 3', 'OptA 4', 'OptA 5', 'OptA 6') | |
583 ), | |
584 'OptionB' => array( | |
585 'name' => 'OptB', | |
586 'id' => 12, | |
587 'opt' => array('OptB 1', 'OptB 2', 'OptB 3', 'OptB 4', 'OptB 5', 'OptB 6') | |
588 ) | |
589 ); | |
590 $expected = '<option_a><name>OptA</name><id>12</id><opt>OptA 1</opt><opt>OptA 2</opt><opt>OptA 3</opt><opt>OptA 4</opt><opt>OptA 5</opt><opt>OptA 6</opt></option_a><option_b><name>OptB</name><id>12</id><opt>OptB 1</opt><opt>OptB 2</opt><opt>OptB 3</opt><opt>OptB 4</opt><opt>OptB 5</opt><opt>OptB 6</opt></option_b>'; | |
591 $xml = new Xml($input, array('format' => 'tags')); | |
592 $result = $xml->toString(array('header' => false, 'cdata' => false)); | |
593 $this->assertEqual($expected, $result); | |
594 } */ | |
595 | |
596 /* function testMixedArrayAttributes() { | |
597 $input = array('OptionGroup' => array( | |
598 array( | |
599 'name' => 'OptA', | |
600 'id' => 12, | |
601 array('opt' => 'OptA 1'), | |
602 array('opt' => 'OptA 2'), | |
603 array('opt' => 'OptA 3'), | |
604 array('opt' => 'OptA 4'), | |
605 array('opt' => 'OptA 5'), | |
606 array('opt' => 'OptA 6') | |
607 ), | |
608 array( | |
609 'name' => 'OptB', | |
610 'id' => 12, | |
611 array('opt' => 'OptB 1'), | |
612 array('opt' => 'OptB 2'), | |
613 array('opt' => 'OptB 3'), | |
614 array('opt' => 'OptB 4'), | |
615 array('opt' => 'OptB 5'), | |
616 array('opt' => 'OptB 6') | |
617 ) | |
618 )); | |
619 $expected = '<option_group name="OptA" id="12"><opt>OptA 1</opt><opt>OptA 2</opt><opt>OptA 3</opt><opt>OptA 4</opt><opt>OptA 5</opt><opt>OptA 6</opt></option_group><option_group name="OptB" id="12"><opt>OptB 1</opt><opt>OptB 2</opt><opt>OptB 3</opt><opt>OptB 4</opt><opt>OptB 5</opt><opt>OptB 6</opt></option_group>'; | |
620 | |
621 $options = array('tags' => array('option_group' => array('attributes' => array('id', 'name')))); | |
622 $xml = new Xml($input, $options); | |
623 $result = $xml->toString(false); | |
624 | |
625 $this->assertEqual($expected, $result); | |
626 } */ | |
627 | |
628 /* Not implemented yet */ | |
629 /* function testTagMap() { | |
630 $input = array( | |
631 array( | |
632 'Project' => array('id' => 1, 'title' => null, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null), | |
633 'Style' => array('id' => null, 'name' => null), | |
634 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
635 'Industry' => array('id' => 1, 'name' => 'Financial') | |
636 ), | |
637 array( | |
638 'Project' => array('id' => 2, 'title' => null, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
639 'Style' => array('id' => null, 'name' => null), | |
640 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
641 'Industry' => array('id' => 2, 'name' => 'Education'), | |
642 ) | |
643 ); | |
644 $expected = '<project id="1"><title /><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style id=""><name /></style><jobtype id="1">Touch Screen Kiosk</jobtype><industry id="1"><name>Financial</name></industry></project><project id="2"><title /><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style id=""><name /></style><jobtype id="2">Awareness Campaign</jobtype><industry id="2"><name>Education</name></industry></project>'; | |
645 | |
646 $xml = new Xml($input, array('tags' => array( | |
647 'Project' => array('attributes' => array('id')), | |
648 'style' => array('attributes' => array('id')), | |
649 'JobType' => array('name' => 'jobtype', 'attributes' => array('id'), 'value' => 'name'), | |
650 'Industry' => array('attributes' => array('id')) | |
651 ))); | |
652 $result = $xml->toString(array('header' => false, 'cdata' => false)); | |
653 $this->assertEqual($expected, $result); | |
654 } */ | |
655 | |
656 /** | |
657 * testAllCData method | |
658 * | |
659 * @access public | |
660 * @return void | |
661 */ | |
662 function testAllCData() { | |
663 $input = array( | |
664 array( | |
665 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null), | |
666 'Style' => array('id' => null, 'name' => null), | |
667 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
668 'Industry' => array('id' => 1, 'name' => 'Financial') | |
669 ), | |
670 array( | |
671 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
672 'Style' => array('id' => null, 'name' => null), | |
673 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
674 'Industry' => array('id' => 2, 'name' => 'Education'), | |
675 ) | |
676 ); | |
677 $expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1.89</job_type_id><industry_id>1.56</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name><![CDATA[Touch Screen Kiosk]]></name></job_type><industry><id>1</id><name><![CDATA[Financial]]></name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2.2</job_type_id><industry_id>2.2</industry_id><modified><![CDATA[2007-11-26 14:48:36]]></modified><created /><style><id /><name /></style><job_type><id>2</id><name><![CDATA[Awareness Campaign]]></name></job_type><industry><id>2</id><name><![CDATA[Education]]></name></industry></project>'; | |
678 $xml = new Xml($input, array('format' => 'tags')); | |
679 $result = $xml->toString(array('header' => false, 'cdata' => true)); | |
680 $this->assertEqual($expected, $result); | |
681 } | |
682 /* PHP-native Unicode support pending */ | |
683 /* function testConvertEntities() { | |
684 $input = array('project' => 'écît'); | |
685 $xml = new Xml($input); | |
686 | |
687 $result = $xml->toString(array('header' => false, 'cdata' => false, 'convertEntities' => true)); | |
688 $expected = '<project>écît</project>'; | |
689 $this->assertEqual($result, $expected); | |
690 } */ | |
691 | |
692 /** | |
693 * testWhitespace method | |
694 * | |
695 * @access public | |
696 * @return void | |
697 */ | |
698 function testWhitespace() { | |
699 $input = array( | |
700 array( | |
701 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null), | |
702 'Style' => array('id' => null, 'name' => null), | |
703 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
704 'Industry' => array('id' => 1, 'name' => 'Financial') | |
705 ), | |
706 array( | |
707 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
708 'Style' => array('id' => null, 'name' => null), | |
709 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
710 'Industry' => array('id' => 2, 'name' => 'Education'), | |
711 ) | |
712 ); | |
713 $expected = "\n\t<project>\n\t\t<id>\n\t\t\t1\n\t\t</id>\n\t\t<title />\n\t\t<client_id>\n\t\t\t1\n\t\t</client_id>\n\t\t<show>\n\t\t\t1\n\t\t</show>\n\t\t<is_spotlight />\n\t\t<style_id>\n\t\t\t0\n\t\t</style_id>\n\t\t<job_type_id>\n\t\t\t1\n\t\t</job_type_id>\n\t\t<industry_id>\n\t\t\t1\n\t\t</industry_id>\n\t\t<modified />\n\t\t<created />\n\t\t<style>\n\t\t\t<id />\n\t\t\t<name />\n\t\t</style>\n\t\t<job_type>\n\t\t\t<id>\n\t\t\t\t1\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tTouch Screen Kiosk\n\t\t\t</name>\n\t\t</job_type>\n\t\t<industry>\n\t\t\t<id>\n\t\t\t\t1\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tFinancial\n\t\t\t</name>\n\t\t</industry>\n\t</project>\n\t<project>\n\t\t<id>\n\t\t\t2\n\t\t</id>\n\t\t<title />\n\t\t<client_id>\n\t\t\t2\n\t\t</client_id>\n\t\t<show>\n\t\t\t1\n\t\t</show>\n\t\t<is_spotlight />\n\t\t<style_id>\n\t\t\t0\n\t\t</style_id>\n\t\t<job_type_id>\n\t\t\t2\n\t\t</job_type_id>\n\t\t<industry_id>\n\t\t\t2\n\t\t</industry_id>\n\t\t<modified>\n\t\t\t2007-11-26 14:48:36\n\t\t</modified>\n\t\t<created />\n\t\t<style>\n\t\t\t<id />\n\t\t\t<name />\n\t\t</style>\n\t\t<job_type>\n\t\t\t<id>\n\t\t\t\t2\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tAwareness Campaign\n\t\t\t</name>\n\t\t</job_type>\n\t\t<industry>\n\t\t\t<id>\n\t\t\t\t2\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tEducation\n\t\t\t</name>\n\t\t</industry>\n\t</project>\n"; | |
714 | |
715 $xml = new Xml($input, array('format' => 'tags')); | |
716 $result = $xml->toString(array('header' => false, 'cdata' => false, 'whitespace' => true)); | |
717 $this->assertEqual($expected, $result); | |
718 } | |
719 | |
720 /** | |
721 * testSetSerialization method | |
722 * | |
723 * @access public | |
724 * @return void | |
725 */ | |
726 function testSetSerialization() { | |
727 $input = array( | |
728 array( | |
729 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null), | |
730 'Style' => array('id' => null, 'name' => null), | |
731 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'), | |
732 'Industry' => array('id' => 1, 'name' => 'Financial') | |
733 ), | |
734 array( | |
735 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null), | |
736 'Style' => array('id' => null, 'name' => null), | |
737 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'), | |
738 'Industry' => array('id' => 2, 'name' => 'Education'), | |
739 ) | |
740 ); | |
741 $expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry></project>'; | |
742 | |
743 $xml = new Xml(Set::map($input), array('format' => 'tags')); | |
744 $result = $xml->toString(array('header' => false, 'cdata' => false)); | |
745 $this->assertEqual($expected, $result); | |
746 } | |
747 | |
748 /** | |
749 * ensure that normalize does not add _name_ elements that come from Set::map sometimes. | |
750 * | |
751 * @return void | |
752 */ | |
753 function testNormalizeNotAdding_name_Element() { | |
754 $input = array( | |
755 'output' => array( | |
756 'Vouchers' => array( | |
757 array('Voucher' => array('id' => 1)), | |
758 array('Voucher' => array('id' => 2)), | |
759 ), | |
760 ) | |
761 ); | |
762 $xml = new Xml($input, array('attributes' => false, 'format' => 'tags')); | |
763 $this->assertFalse(isset($xml->children[0]->children[0]->children[1]), 'Too many children %s'); | |
764 $this->assertEqual($xml->children[0]->children[0]->children[0]->name, 'voucher'); | |
765 } | |
766 /** | |
767 * testSimpleParsing method | |
768 * | |
769 * @access public | |
770 * @return void | |
771 */ | |
772 function testSimpleParsing() { | |
773 $source = '<response><hello><![CDATA[happy world]]></hello><goodbye><![CDATA[cruel world]]></goodbye></response>'; | |
774 $xml = new Xml($source); | |
775 $result = $xml->toString(); | |
776 $this->assertEqual($source, $result); | |
777 } | |
778 | |
779 /** | |
780 * test that elements with empty tag values do not collapse and corrupt data structures | |
781 * | |
782 * @access public | |
783 * @return void | |
784 */ | |
785 function testElementCollapsing() { | |
786 $xmlDataThatFails = '<resultpackage> | |
787 <result qid="46b1c46ed6208"><![CDATA[46b1c46ed3af9]]></result> | |
788 <result qid="46b1c46ed332a"><![CDATA[]]></result> | |
789 <result qid="46b1c46ed90e6"><![CDATA[46b1c46ed69d8]]></result> | |
790 <result qid="46b1c46ed71a7"><![CDATA[46b1c46ed5a38]]></result> | |
791 <result qid="46b1c46ed8146"><![CDATA[46b1c46ed98b6]]></result> | |
792 <result qid="46b1c46ed7978"><![CDATA[]]></result> | |
793 <result qid="46b1c46ed4a98"><![CDATA[]]></result> | |
794 <result qid="46b1c46ed42c8"><![CDATA[]]></result> | |
795 <result qid="46b1c46ed5268"><![CDATA[46b1c46ed8917]]></result> | |
796 </resultpackage>'; | |
797 | |
798 $Xml = new Xml(); | |
799 $Xml->load('<?xml version="1.0" encoding="UTF-8" ?>' . $xmlDataThatFails); | |
800 $result = $Xml->toArray(false); | |
801 | |
802 $this->assertTrue(is_array($result)); | |
803 $expected = array( | |
804 'resultpackage' => array( | |
805 'result' => array( | |
806 0 => array( | |
807 'value' => '46b1c46ed3af9', | |
808 'qid' => '46b1c46ed6208'), | |
809 1 => array( | |
810 'qid' => '46b1c46ed332a'), | |
811 2 => array( | |
812 'value' => '46b1c46ed69d8', | |
813 'qid' => '46b1c46ed90e6'), | |
814 3 => array( | |
815 'value' => '46b1c46ed5a38', | |
816 'qid' => '46b1c46ed71a7'), | |
817 4 => array( | |
818 'value' => '46b1c46ed98b6', | |
819 'qid' => '46b1c46ed8146'), | |
820 5 => array( | |
821 'qid' => '46b1c46ed7978'), | |
822 6 => array( | |
823 'qid' => '46b1c46ed4a98'), | |
824 7 => array( | |
825 'qid' => '46b1c46ed42c8'), | |
826 8 => array( | |
827 'value' => '46b1c46ed8917', | |
828 'qid' => '46b1c46ed5268'), | |
829 ) | |
830 )); | |
831 $this->assertEqual( | |
832 count($result['resultpackage']['result']), count($expected['resultpackage']['result']), | |
833 'Incorrect array length %s'); | |
834 | |
835 $this->assertFalse( | |
836 isset($result['resultpackage']['result'][0][0]['qid']), 'Nested array exists, data is corrupt. %s'); | |
837 | |
838 $this->assertEqual($result, $expected); | |
839 } | |
840 | |
841 /** | |
842 * test that empty values do not casefold collapse | |
843 * | |
844 * @see http://code.cakephp.org/tickets/view/8 | |
845 * @return void | |
846 */ | |
847 function testCaseFoldingWithEmptyValues() { | |
848 $filledValue = '<method name="set_user_settings"> | |
849 <title>update user information</title> | |
850 <user>1</user> | |
851 <User> | |
852 <id>1</id> | |
853 <name>varchar(45)</name> | |
854 </User> | |
855 </method>'; | |
856 $xml =& new XML($filledValue); | |
857 $expected = array( | |
858 'Method' => array( | |
859 'name' => 'set_user_settings', | |
860 'title' => 'update user information', | |
861 'user' => '1', | |
862 'User' => array( | |
863 'id' => 1, | |
864 'name' => 'varchar(45)', | |
865 ), | |
866 ) | |
867 ); | |
868 $result = $xml->toArray(); | |
869 $this->assertEqual($result, $expected); | |
870 | |
871 $emptyValue ='<method name="set_user_settings"> | |
872 <title>update user information</title> | |
873 <user></user> | |
874 <User> | |
875 <id>1</id> | |
876 <name>varchar(45)</name> | |
877 </User> | |
878 </method>'; | |
879 | |
880 $xml =& new XML($emptyValue); | |
881 $expected = array( | |
882 'Method' => array( | |
883 'name' => 'set_user_settings', | |
884 'title' => 'update user information', | |
885 'user' => array(), | |
886 'User' => array( | |
887 'id' => 1, | |
888 'name' => 'varchar(45)', | |
889 ), | |
890 ) | |
891 ); | |
892 $result = $xml->toArray(); | |
893 $this->assertEqual($result, $expected); | |
894 } | |
895 /** | |
896 * testMixedParsing method | |
897 * | |
898 * @access public | |
899 * @return void | |
900 */ | |
901 function testMixedParsing() { | |
902 $source = '<response><body><hello><![CDATA[happy world]]></hello><![CDATA[in between]]><goodbye><![CDATA[cruel world]]></goodbye></body></response>'; | |
903 $xml = new Xml($source); | |
904 $result = $xml->toString(); | |
905 $this->assertEqual($source, $result); | |
906 } | |
907 | |
908 /** | |
909 * testComplexParsing method | |
910 * | |
911 * @access public | |
912 * @return void | |
913 */ | |
914 function testComplexParsing() { | |
915 $source = '<projects><project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry></project></projects>'; | |
916 $xml = new Xml($source); | |
917 $result = $xml->toString(array('cdata' => false)); | |
918 $this->assertEqual($source, $result); | |
919 } | |
920 | |
921 /** | |
922 * testNamespaceParsing method | |
923 * | |
924 * @access public | |
925 * @return void | |
926 */ | |
927 function testNamespaceParsing() { | |
928 $source = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>'; | |
929 $xml = new Xml($source); | |
930 | |
931 $result = $xml->toString(array('cdata' => false)); | |
932 $this->assertEqual($source, $result); | |
933 | |
934 $children = $xml->children('container'); | |
935 $this->assertEqual($children[0]->namespace, 'a'); | |
936 | |
937 $children = $children[0]->children('rule'); | |
938 $this->assertEqual($children[0]->namespace, 'b'); | |
939 } | |
940 | |
941 /** | |
942 * testNamespaces method | |
943 * | |
944 * @access public | |
945 * @return void | |
946 */ | |
947 function testNamespaces() { | |
948 $source = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>'; | |
949 $xml = new Xml($source); | |
950 | |
951 $expects = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e" xmlns:f="http://example.com/f"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>'; | |
952 | |
953 $_xml =& XmlManager::getInstance(); | |
954 $xml->addNamespace('f', 'http://example.com/f'); | |
955 $result = $xml->toString(array('cdata' => false)); | |
956 $this->assertEqual($expects, $result); | |
957 } | |
958 | |
959 /** | |
960 * testEscapeCharSerialization method | |
961 * | |
962 * @access public | |
963 * @return void | |
964 */ | |
965 function testEscapeCharSerialization() { | |
966 $xml = new Xml(array('text' => 'JavaScript & DHTML'), array('attributes' => false, 'format' => 'attributes')); | |
967 | |
968 $result = $xml->toString(false); | |
969 $expected = '<std_class text="JavaScript & DHTML" />'; | |
970 $this->assertEqual($expected, $result); | |
971 } | |
972 | |
973 /** | |
974 * testCompleteEscapeCharSerialization method | |
975 * | |
976 * @access public | |
977 * @return void | |
978 */ | |
979 function testCompleteEscapeCharSerialization() { | |
980 $xml = new Xml(array('text' => '<>&"\''), array('attributes' => false, 'format' => 'attributes')); | |
981 | |
982 $result = $xml->toString(false); | |
983 $expected = '<std_class text="<>&"'" />'; | |
984 $this->assertEqual($expected, $result); | |
985 } | |
986 | |
987 /** | |
988 * testToArray method | |
989 * | |
990 * @access public | |
991 * @return void | |
992 */ | |
993 function testToArray() { | |
994 App::import('Set'); | |
995 $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
996 <rss version="2.0"> | |
997 <channel> | |
998 <title>Cake PHP Google Group</title> | |
999 <link>http://groups.google.com/group/cake-php</link> | |
1000 <description>Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description> | |
1001 <language>en</language> | |
1002 <item> | |
1003 <title>constructng result array when using findall</title> | |
1004 <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link> | |
1005 <description>i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] =&gt; Array(</description> | |
1006 <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid> | |
1007 <author>bmil...@gmail.com(bpscrugs)</author> | |
1008 <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate> | |
1009 </item> | |
1010 <item> | |
1011 <title>Re: share views between actions?</title> | |
1012 <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link> | |
1013 <description>Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I'll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other</description> | |
1014 <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid> | |
1015 <author>subtropolis.z...@gmail.com(subtropolis zijn)</author> | |
1016 <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate> | |
1017 </item> | |
1018 </channel> | |
1019 </rss>'; | |
1020 $xml = new Xml($string); | |
1021 $result = $xml->toArray(); | |
1022 $expected = array('Rss' => array( | |
1023 'version' => '2.0', | |
1024 'Channel' => array( | |
1025 'title' => 'Cake PHP Google Group', | |
1026 'link' => 'http://groups.google.com/group/cake-php', | |
1027 'description' => 'Search this group before posting anything. There are over 20,000 posts and it's very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.', | |
1028 'language' => 'en', | |
1029 'Item' => array( | |
1030 array( | |
1031 'title' => 'constructng result array when using findall', | |
1032 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f', | |
1033 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [0] => Array(", | |
1034 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'), | |
1035 'author' => 'bmil...@gmail.com(bpscrugs)', | |
1036 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT', | |
1037 ), | |
1038 array( | |
1039 'title' => 'Re: share views between actions?', | |
1040 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8', | |
1041 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple "RTFM" would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other', | |
1042 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'), | |
1043 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)', | |
1044 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT' | |
1045 ) | |
1046 ) | |
1047 ) | |
1048 )); | |
1049 $this->assertEqual($result, $expected); | |
1050 | |
1051 $string ='<data><post title="Title of this post" description="cool"/></data>'; | |
1052 $xml = new Xml($string); | |
1053 $result = $xml->toArray(); | |
1054 $expected = array('Data' => array('Post' => array('title' => 'Title of this post', 'description' => 'cool'))); | |
1055 $this->assertEqual($result, $expected); | |
1056 | |
1057 $xml = new Xml('<example><item><title>An example of a correctly reversed XMLNode</title><desc/></item></example>'); | |
1058 $result = Set::reverse($xml); | |
1059 $expected = array( | |
1060 'Example' => array( | |
1061 'Item' => array( | |
1062 'title' => 'An example of a correctly reversed XMLNode', | |
1063 'desc' => array(), | |
1064 ) | |
1065 ) | |
1066 ); | |
1067 $this->assertIdentical($result, $expected); | |
1068 | |
1069 $xml = new Xml('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>'); | |
1070 $result = $xml->toArray(); | |
1071 $expected = array( | |
1072 'Example' => array( | |
1073 'Item' => array( | |
1074 'attr' => '123', | |
1075 'Titles' => array( | |
1076 'Title' => array('title1', 'title2') | |
1077 ) | |
1078 ) | |
1079 ) | |
1080 ); | |
1081 $this->assertIdentical($result, $expected); | |
1082 | |
1083 $xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>'); | |
1084 $result = $xml->toArray(); | |
1085 $expected = array( | |
1086 'Example' => array( | |
1087 'attr' => 'ex_attr', | |
1088 'Item' => array( | |
1089 'attr' => '123', | |
1090 'titles' => 'list', | |
1091 'value' => 'textforitems' | |
1092 ) | |
1093 ) | |
1094 ); | |
1095 $this->assertIdentical($result, $expected); | |
1096 | |
1097 $xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>'); | |
1098 $example = $xml->child('example'); | |
1099 $item = $example->child('item'); | |
1100 $result = $item->toArray(); | |
1101 | |
1102 $expected = array( | |
1103 'attr' => '123', | |
1104 'titles' => 'list', | |
1105 'value' => 'textforitems' | |
1106 ); | |
1107 $this->assertIdentical($result, $expected); | |
1108 | |
1109 $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
1110 <rss version="2.0"> | |
1111 <channel> | |
1112 <title>Cake PHP Google Group</title> | |
1113 <link>http://groups.google.com/group/cake-php</link> | |
1114 <description>Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description> | |
1115 <language>en</language> | |
1116 <item> | |
1117 <title>constructng result array when using findall</title> | |
1118 <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link> | |
1119 <description>i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] =&gt; Array(</description> | |
1120 <dc:creator>cakephp</dc:creator> | |
1121 <category><![CDATA[cakephp]]></category> | |
1122 <category><![CDATA[model]]></category> | |
1123 <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid> | |
1124 <author>bmil...@gmail.com(bpscrugs)</author> | |
1125 <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate> | |
1126 </item> | |
1127 <item> | |
1128 <title>Re: share views between actions?</title> | |
1129 <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link> | |
1130 <description>Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I'll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other</description> | |
1131 <dc:creator>cakephp</dc:creator> | |
1132 <category><![CDATA[cakephp]]></category> | |
1133 <category><![CDATA[model]]></category> | |
1134 <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid> | |
1135 <author>subtropolis.z...@gmail.com(subtropolis zijn)</author> | |
1136 <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate> | |
1137 </item> | |
1138 </channel> | |
1139 </rss>'; | |
1140 | |
1141 $xml = new Xml($string); | |
1142 $result = $xml->toArray(); | |
1143 | |
1144 $expected = array('Rss' => array( | |
1145 'version' => '2.0', | |
1146 'Channel' => array( | |
1147 'title' => 'Cake PHP Google Group', | |
1148 'link' => 'http://groups.google.com/group/cake-php', | |
1149 'description' => 'Search this group before posting anything. There are over 20,000 posts and it's very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.', | |
1150 'language' => 'en', | |
1151 'Item' => array( | |
1152 array( | |
1153 'title' => 'constructng result array when using findall', | |
1154 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f', | |
1155 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [0] => Array(", | |
1156 'creator' => 'cakephp', | |
1157 'Category' => array('cakephp', 'model'), | |
1158 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'), | |
1159 'author' => 'bmil...@gmail.com(bpscrugs)', | |
1160 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT', | |
1161 ), | |
1162 array( | |
1163 'title' => 'Re: share views between actions?', | |
1164 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8', | |
1165 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple "RTFM" would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other', | |
1166 'creator' => 'cakephp', | |
1167 'Category' => array('cakephp', 'model'), | |
1168 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'), | |
1169 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)', | |
1170 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT' | |
1171 ) | |
1172 ) | |
1173 ) | |
1174 )); | |
1175 $this->assertEqual($result, $expected); | |
1176 | |
1177 $text = "<?xml version='1.0' encoding='utf-8'?> | |
1178 <course> | |
1179 <comps> | |
1180 <comp>1</comp> | |
1181 <comp>2</comp> | |
1182 <comp>3</comp> | |
1183 <comp>4</comp> | |
1184 </comps> | |
1185 </course>"; | |
1186 $xml = new Xml($text); | |
1187 $result = $xml->toArray(); | |
1188 | |
1189 $expected = array('Course' => array( | |
1190 'Comps' => array( | |
1191 'Comp' => array( | |
1192 1, 2, 3, 4 | |
1193 ) | |
1194 ) | |
1195 )); | |
1196 | |
1197 $this->assertEqual($result, $expected); | |
1198 | |
1199 $text = '<?xml version="1.0" encoding="UTF-8"?> | |
1200 <XRDS xmlns="xri://$xrds"> | |
1201 <XRD xml:id="oauth" xmlns="xri://$XRD*($v*2.0)" version="2.0"> | |
1202 <Type>xri://$xrds*simple</Type> | |
1203 <Expires>2008-04-13T07:34:58Z</Expires> | |
1204 <Service> | |
1205 <Type>http://oauth.net/core/1.0/endpoint/authorize</Type> | |
1206 <Type>http://oauth.net/core/1.0/parameters/auth-header</Type> | |
1207 <Type>http://oauth.net/core/1.0/parameters/uri-query</Type> | |
1208 <URI priority="10">https://ma.gnolia.com/oauth/authorize</URI> | |
1209 <URI priority="20">http://ma.gnolia.com/oauth/authorize</URI> | |
1210 </Service> | |
1211 </XRD> | |
1212 <XRD xmlns="xri://$XRD*($v*2.0)" version="2.0"> | |
1213 <Type>xri://$xrds*simple</Type> | |
1214 <Service priority="10"> | |
1215 <Type>http://oauth.net/discovery/1.0</Type> | |
1216 <URI>#oauth</URI> | |
1217 </Service> | |
1218 </XRD> | |
1219 </XRDS>'; | |
1220 | |
1221 $xml = new Xml($text); | |
1222 $result = $xml->toArray(); | |
1223 | |
1224 $expected = array('XRDS' => array( | |
1225 'xmlns' => 'xri://$xrds', | |
1226 'XRD' => array( | |
1227 array( | |
1228 'xml:id' => 'oauth', | |
1229 'xmlns' => 'xri://$XRD*($v*2.0)', | |
1230 'version' => '2.0', | |
1231 'Type' => 'xri://$xrds*simple', | |
1232 'Expires' => '2008-04-13T07:34:58Z', | |
1233 'Service' => array( | |
1234 'Type' => array( | |
1235 'http://oauth.net/core/1.0/endpoint/authorize', | |
1236 'http://oauth.net/core/1.0/parameters/auth-header', | |
1237 'http://oauth.net/core/1.0/parameters/uri-query' | |
1238 ), | |
1239 'URI' => array( | |
1240 array( | |
1241 'value' => 'https://ma.gnolia.com/oauth/authorize', | |
1242 'priority' => '10', | |
1243 ), | |
1244 array( | |
1245 'value' => 'http://ma.gnolia.com/oauth/authorize', | |
1246 'priority' => '20' | |
1247 ) | |
1248 ) | |
1249 ) | |
1250 ), | |
1251 array( | |
1252 'xmlns' => 'xri://$XRD*($v*2.0)', | |
1253 'version' => '2.0', | |
1254 'Type' => 'xri://$xrds*simple', | |
1255 'Service' => array( | |
1256 'priority' => '10', | |
1257 'Type' => 'http://oauth.net/discovery/1.0', | |
1258 'URI' => '#oauth' | |
1259 ) | |
1260 ) | |
1261 ) | |
1262 )); | |
1263 $this->assertEqual($result, $expected); | |
1264 | |
1265 $text = '<?xml version="1.0" encoding="UTF-8"?> | |
1266 <root> | |
1267 <child id="1" other="1" /> | |
1268 <child id="2" other="1" /> | |
1269 <child id="3" other="1" /> | |
1270 <child id="4" other="1" /> | |
1271 <child id="5" other="1" /> | |
1272 </root>'; | |
1273 $xml = new Xml($text); | |
1274 $result = $xml->toArray(); | |
1275 $expected = array( | |
1276 'Root' => array( | |
1277 'Child' => array( | |
1278 array('id' => 1, 'other' => 1), | |
1279 array('id' => 2, 'other' => 1), | |
1280 array('id' => 3, 'other' => 1), | |
1281 array('id' => 4, 'other' => 1), | |
1282 array('id' => 5, 'other' => 1) | |
1283 ) | |
1284 ) | |
1285 ); | |
1286 $this->assertEqual($result, $expected); | |
1287 | |
1288 $text = '<main><first label="first type node 1" /><first label="first type node 2" /><second label="second type node" /></main>'; | |
1289 $xml = new Xml($text); | |
1290 $result = $xml->toArray(); | |
1291 $expected = array( | |
1292 'Main' => array( | |
1293 'First' => array( | |
1294 array('label' => 'first type node 1'), | |
1295 array('label' => 'first type node 2') | |
1296 ), | |
1297 'Second' => array('label'=>'second type node') | |
1298 ) | |
1299 ); | |
1300 $this->assertIdentical($result,$expected); | |
1301 | |
1302 $text = '<main><first label="first type node 1" /><first label="first type node 2" /><second label="second type node" /><collection><fifth label="fifth type node"/><third label="third type node 1"/><third label="third type node 2"/><third label="third type node 3"/><fourth label="fourth type node"/></collection></main>'; | |
1303 $xml = new Xml($text); | |
1304 $result = $xml->toArray(); | |
1305 $expected = array( | |
1306 'Main' => array( | |
1307 'First' => array( | |
1308 array('label' => 'first type node 1'), | |
1309 array('label' => 'first type node 2') | |
1310 ), | |
1311 'Second' => array('label'=>'second type node'), | |
1312 'Collection' => array( | |
1313 'Fifth' => array('label' => 'fifth type node'), | |
1314 'Third' => array( | |
1315 array('label' => 'third type node 1'), | |
1316 array('label' => 'third type node 2'), | |
1317 array('label' => 'third type node 3'), | |
1318 ), | |
1319 'Fourth' => array('label' => 'fourth type node'), | |
1320 ) | |
1321 ) | |
1322 ); | |
1323 $this->assertIdentical($result,$expected); | |
1324 } | |
1325 | |
1326 /** | |
1327 * testAppend method | |
1328 * | |
1329 * @access public | |
1330 * @return void | |
1331 */ | |
1332 function testAppend() { | |
1333 $parentNode = new XmlNode('ourParentNode'); | |
1334 $parentNode->append( new XmlNode('ourChildNode')); | |
1335 $first =& $parentNode->first(); | |
1336 $this->assertEqual($first->name, 'ourChildNode'); | |
1337 | |
1338 $string = 'ourChildNode'; | |
1339 $parentNode = new XmlNode('ourParentNode'); | |
1340 $parentNode->append($string); | |
1341 $last =& $parentNode->last(); | |
1342 $this->assertEqual($last->name, 'ourChildNode'); | |
1343 | |
1344 $this->expectError(); | |
1345 $parentNode->append($parentNode); | |
1346 } | |
1347 | |
1348 /** | |
1349 * testNamespacing method | |
1350 * | |
1351 * @access public | |
1352 * @return void | |
1353 */ | |
1354 function testNamespacing() { | |
1355 $node = new Xml('<xml></xml>'); | |
1356 $node->addNamespace('cake', 'http://cakephp.org'); | |
1357 $this->assertEqual($node->toString(), '<xml xmlns:cake="http://cakephp.org" />'); | |
1358 | |
1359 $this->assertTrue($node->removeNamespace('cake')); | |
1360 $this->assertEqual($node->toString(), '<xml />'); | |
1361 | |
1362 | |
1363 $node = new Xml('<xml xmlns:cake="http://cakephp.org" />'); | |
1364 $this->assertTrue($node->removeNamespace('cake')); | |
1365 $this->assertEqual($node->toString(), '<xml />'); | |
1366 | |
1367 $node->addNamespace('cake', 'http://cakephp.org'); | |
1368 $this->assertEqual($node->toString(), '<xml xmlns:cake="http://cakephp.org" />'); | |
1369 } | |
1370 | |
1371 /** | |
1372 * testCamelize method | |
1373 * | |
1374 * @access public | |
1375 * @return void | |
1376 */ | |
1377 function testCamelize() { | |
1378 $xmlString = '<methodCall><methodName>examples.getStateName</methodName>' . | |
1379 '<params><param><value><i4>41</i4></value></param></params></methodCall>'; | |
1380 | |
1381 $Xml = new Xml($xmlString); | |
1382 $expected = array( | |
1383 'methodCall' => array( | |
1384 'methodName' => 'examples.getStateName', | |
1385 'params' => array( | |
1386 'param' => array('value' => array('i4' => 41))))); | |
1387 $this->assertEqual($expected, $Xml->toArray(false)); | |
1388 | |
1389 $Xml = new Xml($xmlString); | |
1390 $expected = array( | |
1391 'MethodCall' => array( | |
1392 'methodName' => 'examples.getStateName', | |
1393 'Params' => array( | |
1394 'Param' => array('Value' => array('i4' => 41))))); | |
1395 $this->assertEqual($expected, $Xml->toArray()); | |
1396 } | |
1397 | |
1398 /** | |
1399 * testNumericDataHandling method | |
1400 * | |
1401 * @access public | |
1402 * @return void | |
1403 */ | |
1404 function testNumericDataHandling() { | |
1405 $data = '<xml><data>012345</data></xml>'; | |
1406 | |
1407 $node = new Xml(); | |
1408 $node->load($data); | |
1409 $node->parse(); | |
1410 | |
1411 $result = $node->first(); | |
1412 $result = $result->children("data"); | |
1413 | |
1414 $result = $result[0]->first(); | |
1415 $this->assertEqual($result->value, '012345'); | |
1416 } | |
1417 | |
1418 /** | |
1419 * test that creating an xml object does not leak memory | |
1420 * | |
1421 * @return void | |
1422 */ | |
1423 function testMemoryLeakInConstructor() { | |
1424 if ($this->skipIf(!function_exists('memory_get_usage'), 'Cannot test memory leaks without memory_get_usage')) { | |
1425 return; | |
1426 } | |
1427 $data = '<?xml version="1.0" encoding="UTF-8"?><content>TEST</content>'; | |
1428 $start = memory_get_usage(); | |
1429 for ($i = 0; $i <= 300; $i++) { | |
1430 $test =& new XML($data); | |
1431 $test->__destruct(); | |
1432 unset($test); | |
1433 } | |
1434 $end = memory_get_usage(); | |
1435 $this->assertWithinMargin($start, $end, 3600, 'Memory leaked %s'); | |
1436 } | |
1437 | |
1438 /** | |
1439 * Test toArray with alternate inputs. | |
1440 * | |
1441 * @return void | |
1442 */ | |
1443 function testToArrayAlternate() { | |
1444 $sXml = | |
1445 '<t1> | |
1446 <t2>A</t2> | |
1447 <t2><t3>AAA</t3>B</t2> | |
1448 <t2>C</t2> | |
1449 </t1>'; | |
1450 $xml = new Xml($sXml); | |
1451 $result = $xml->toArray(); | |
1452 $expected = array( | |
1453 'T1' => array( | |
1454 'T2' => array( | |
1455 'A', | |
1456 array('t3' => 'AAA', 'value' => 'B'), | |
1457 'C' | |
1458 ) | |
1459 ) | |
1460 ); | |
1461 $this->assertIdentical($result, $expected); | |
1462 $result = $xml->toArray(false); | |
1463 $expected = array( | |
1464 't1' => array( | |
1465 't2' => array( | |
1466 'A', | |
1467 array('t3' => 'AAA', 'value' => 'B'), | |
1468 'C' | |
1469 ) | |
1470 ) | |
1471 ); | |
1472 $this->assertIdentical($result, $expected); | |
1473 | |
1474 $sXml = | |
1475 '<t1> | |
1476 <t2>A</t2> | |
1477 <t2>B</t2> | |
1478 <t2> | |
1479 <t3>CCC</t3> | |
1480 </t2> | |
1481 </t1>'; | |
1482 $xml = new Xml($sXml); | |
1483 $result = $xml->toArray(); | |
1484 $expected = array( | |
1485 'T1' => array( | |
1486 'T2' => array( | |
1487 'A', | |
1488 'B', | |
1489 array('t3' => 'CCC'), | |
1490 ) | |
1491 ) | |
1492 ); | |
1493 $this->assertIdentical($result, $expected); | |
1494 $result = $xml->toArray(false); | |
1495 $expected = array( | |
1496 't1' => array( | |
1497 't2' => array( | |
1498 'A', | |
1499 'B', | |
1500 array('t3' => 'CCC'), | |
1501 ) | |
1502 ) | |
1503 ); | |
1504 $this->assertIdentical($result, $expected); | |
1505 | |
1506 $sXml = | |
1507 '<t1> | |
1508 <t2>A</t2> | |
1509 <t2></t2> | |
1510 <t2>C</t2> | |
1511 </t1>'; | |
1512 $xml = new Xml($sXml); | |
1513 $result = $xml->toArray(); | |
1514 $expected = array( | |
1515 'T1' => array( | |
1516 'T2' => array( | |
1517 'A', | |
1518 array(), | |
1519 'C' | |
1520 ) | |
1521 ) | |
1522 ); | |
1523 $this->assertIdentical($result, $expected); | |
1524 | |
1525 $result = $xml->toArray(false); | |
1526 $expected = array( | |
1527 't1' => array( | |
1528 't2' => array( | |
1529 'A', | |
1530 array(), | |
1531 'C' | |
1532 ) | |
1533 ) | |
1534 ); | |
1535 $this->assertIdentical($result, $expected); | |
1536 | |
1537 $sXml = | |
1538 '<stuff> | |
1539 <foo name="abc-16" profile-id="Default" /> | |
1540 <foo name="abc-17" profile-id="Default" > | |
1541 <bar id="HelloWorld" /> | |
1542 </foo> | |
1543 <foo name="abc-asdf" profile-id="Default" /> | |
1544 <foo name="cba-1A" profile-id="Default"> | |
1545 <bar id="Baz" /> | |
1546 </foo> | |
1547 <foo name="cba-2A" profile-id="Default"> | |
1548 <bar id="Baz" /> | |
1549 </foo> | |
1550 <foo name="qa" profile-id="Default" /> | |
1551 </stuff>'; | |
1552 $xml = new Xml($sXml); | |
1553 $result = $xml->toArray(); | |
1554 $expected = array( | |
1555 'Stuff' => array( | |
1556 'Foo' => array( | |
1557 array('name' => 'abc-16', 'profile-id' => 'Default'), | |
1558 array('name' => 'abc-17', 'profile-id' => 'Default', | |
1559 'Bar' => array('id' => 'HelloWorld')), | |
1560 array('name' => 'abc-asdf', 'profile-id' => 'Default'), | |
1561 array('name' => 'cba-1A', 'profile-id' => 'Default', | |
1562 'Bar' => array('id' => 'Baz')), | |
1563 array('name' => 'cba-2A', 'profile-id' => 'Default', | |
1564 'Bar' => array('id' => 'Baz')), | |
1565 array('name' => 'qa', 'profile-id' => 'Default'), | |
1566 ) | |
1567 ) | |
1568 ); | |
1569 $this->assertIdentical($result, $expected); | |
1570 $result = $xml->toArray(false); | |
1571 $expected = array( | |
1572 'stuff' => array( | |
1573 'foo' => array( | |
1574 array('name' => 'abc-16', 'profile-id' => 'Default'), | |
1575 array('name' => 'abc-17', 'profile-id' => 'Default', | |
1576 'bar' => array('id' => 'HelloWorld')), | |
1577 array('name' => 'abc-asdf', 'profile-id' => 'Default'), | |
1578 array('name' => 'cba-1A', 'profile-id' => 'Default', | |
1579 'bar' => array('id' => 'Baz')), | |
1580 array('name' => 'cba-2A', 'profile-id' => 'Default', | |
1581 'bar' => array('id' => 'Baz')), | |
1582 array('name' => 'qa', 'profile-id' => 'Default'), | |
1583 ) | |
1584 ) | |
1585 ); | |
1586 $this->assertIdentical($result, $expected); | |
1587 | |
1588 | |
1589 $sXml = | |
1590 '<root> | |
1591 <node name="first" /> | |
1592 <node name="second"><subnode name="first sub" /><subnode name="second sub" /></node> | |
1593 <node name="third" /> | |
1594 </root>'; | |
1595 $xml = new Xml($sXml); | |
1596 $result = $xml->toArray(); | |
1597 $expected = array( | |
1598 'Root' => array( | |
1599 'Node' => array( | |
1600 array('name' => 'first'), | |
1601 array('name' => 'second', | |
1602 'Subnode' => array( | |
1603 array('name' => 'first sub'), | |
1604 array('name' => 'second sub'))), | |
1605 array('name' => 'third'), | |
1606 ) | |
1607 ) | |
1608 ); | |
1609 $this->assertIdentical($result, $expected); | |
1610 | |
1611 $result = $xml->toArray(false); | |
1612 $expected = array( | |
1613 'root' => array( | |
1614 'node' => array( | |
1615 array('name' => 'first'), | |
1616 array('name' => 'second', | |
1617 'subnode' => array( | |
1618 array('name' => 'first sub'), | |
1619 array('name' => 'second sub'))), | |
1620 array('name' => 'third'), | |
1621 ) | |
1622 ) | |
1623 ); | |
1624 $this->assertIdentical($result, $expected); | |
1625 } | |
1626 | |
1627 } |