PageRenderTime 76ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

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