PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/tests/cases/libs/xml.test.php

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