JsonXMLTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Json
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. error_reporting( E_ALL | E_STRICT ); // now required for each test suite
  23. /**
  24. * Zend_Json
  25. */
  26. require_once 'Zend/Json.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Json
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Json
  34. */
  35. class Zend_Json_JsonXMLTest extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * xml2json Test 1
  39. * It tests the conversion of a contact list xml into Json format.
  40. *
  41. * XML characteristic to be tested: XML containing an array of child elements.
  42. *
  43. */
  44. public function testUsingXML1()
  45. {
  46. // Set the XML contents that will be tested here.
  47. $xmlStringContents = <<<EOT
  48. <?xml version="1.0" encoding="UTF-8"?>
  49. <contacts>
  50. <contact>
  51. <name>
  52. John Doe
  53. </name>
  54. <phone>
  55. 123-456-7890
  56. </phone>
  57. </contact>
  58. <contact>
  59. <name>
  60. Jane Doe
  61. </name>
  62. <phone>
  63. 123-456-0000
  64. </phone>
  65. </contact>
  66. <contact>
  67. <name>
  68. John Smith
  69. </name>
  70. <phone>
  71. 123-456-1111
  72. </phone>
  73. </contact>
  74. <contact>
  75. <name>
  76. Jane Smith
  77. </name>
  78. <phone>
  79. 123-456-9999
  80. </phone>
  81. </contact>
  82. </contacts>
  83. EOT;
  84. // There are not going to be any XML attributes in this test XML.
  85. // Hence, set the flag to ignore XML attributes.
  86. $ignoreXmlAttributes = true;
  87. $jsonContents = "";
  88. $ex = null;
  89. // Convert XNL to JSON now.
  90. // fromXml function simply takes a String containing XML contents as input.
  91. try {
  92. $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes);
  93. } catch (Exception $ex) {
  94. ;
  95. }
  96. $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
  97. // Convert the JSON string into a PHP array.
  98. $phpArray = Zend_Json::decode($jsonContents);
  99. // Test if it is not a NULL object.
  100. $this->assertNotNull($phpArray, "JSON result for XML input 1 is NULL");
  101. // Test for one of the expected fields in the JSON result.
  102. $this->assertSame("Jane Smith", $phpArray['contacts']['contact'][3]['name'], "The last contact name converted from XML input 1 is not correct");
  103. } // End of function testUsingXML1
  104. /**
  105. * xml2json Test 2
  106. * It tests the conversion of book publication xml into Json format.
  107. *
  108. * XML characteristic to be tested: XML containing an array of child elements with XML attributes.
  109. *
  110. */
  111. public function testUsingXML2()
  112. {
  113. // Set the XML contents that will be tested here.
  114. $xmlStringContents = <<<EOT
  115. <?xml version="1.0" encoding="UTF-8"?>
  116. <books>
  117. <book id="1">
  118. <title>Code Generation in Action</title>
  119. <author><first>Jack</first><last>Herrington</last></author>
  120. <publisher>Manning</publisher>
  121. </book>
  122. <book id="2">
  123. <title>PHP Hacks</title>
  124. <author><first>Jack</first><last>Herrington</last></author>
  125. <publisher>O'Reilly</publisher>
  126. </book>
  127. <book id="3">
  128. <title>Podcasting Hacks</title>
  129. <author><first>Jack</first><last>Herrington</last></author>
  130. <publisher>O'Reilly</publisher>
  131. </book>
  132. </books>
  133. EOT;
  134. // There are going to be XML attributes in this test XML.
  135. // Hence, set the flag NOT to ignore XML attributes.
  136. $ignoreXmlAttributes = false;
  137. $jsonContents = "";
  138. $ex = null;
  139. // Convert XNL to JSON now.
  140. // fromXml function simply takes a String containing XML contents as input.
  141. try {
  142. $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes);
  143. } catch (Exception $ex) {
  144. ;
  145. }
  146. $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
  147. // Convert the JSON string into a PHP array.
  148. $phpArray = Zend_Json::decode($jsonContents);
  149. // Test if it is not a NULL object.
  150. $this->assertNotNull($phpArray, "JSON result for XML input 2 is NULL");
  151. // Test for one of the expected fields in the JSON result.
  152. $this->assertSame("Podcasting Hacks", $phpArray['books']['book'][2]['title'], "The last book title converted from XML input 2 is not correct");
  153. // Test one of the expected XML attributes carried over in the JSON result.
  154. $this->assertSame("3", $phpArray['books']['book'][2]['@attributes']['id'], "The last id attribute converted from XML input 2 is not correct");
  155. } // End of function testUsingXML2
  156. /**
  157. * xml2json Test 3
  158. * It tests the conversion of food menu xml into Json format.
  159. *
  160. * XML characteristic to be tested: XML containing an array of child elements.
  161. *
  162. */
  163. public function testUsingXML3()
  164. {
  165. // Set the XML contents that will be tested here.
  166. $xmlStringContents = <<<EOT
  167. <?xml version="1.0" encoding="ISO-8859-1" ?>
  168. <breakfast_menu>
  169. <food>
  170. <name>Belgian Waffles</name>
  171. <price>$5.95</price>
  172. <description>
  173. two of our famous Belgian Waffles with plenty of real maple
  174. syrup
  175. </description>
  176. <calories>650</calories>
  177. </food>
  178. <food>
  179. <name>Strawberry Belgian Waffles</name>
  180. <price>$7.95</price>
  181. <description>
  182. light Belgian waffles covered with strawberries and whipped
  183. cream
  184. </description>
  185. <calories>900</calories>
  186. </food>
  187. <food>
  188. <name>Berry-Berry Belgian Waffles</name>
  189. <price>$8.95</price>
  190. <description>
  191. light Belgian waffles covered with an assortment of fresh
  192. berries and whipped cream
  193. </description>
  194. <calories>900</calories>
  195. </food>
  196. <food>
  197. <name>French Toast</name>
  198. <price>$4.50</price>
  199. <description>
  200. thick slices made from our homemade sourdough bread
  201. </description>
  202. <calories>600</calories>
  203. </food>
  204. <food>
  205. <name>Homestyle Breakfast</name>
  206. <price>$6.95</price>
  207. <description>
  208. two eggs, bacon or sausage, toast, and our ever-popular hash
  209. browns
  210. </description>
  211. <calories>950</calories>
  212. </food>
  213. </breakfast_menu>
  214. EOT;
  215. // There are not going to be any XML attributes in this test XML.
  216. // Hence, set the flag to ignore XML attributes.
  217. $ignoreXmlAttributes = true;
  218. $jsonContents = "";
  219. $ex = null;
  220. // Convert XNL to JSON now.
  221. // fromXml function simply takes a String containing XML contents as input.
  222. try {
  223. $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes);
  224. } catch (Exception $ex) {
  225. ;
  226. }
  227. $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
  228. // Convert the JSON string into a PHP array.
  229. $phpArray = Zend_Json::decode($jsonContents);
  230. // Test if it is not a NULL object.
  231. $this->assertNotNull($phpArray, "JSON result for XML input 3 is NULL");
  232. // Test for one of the expected fields in the JSON result.
  233. $this->assertContains("Homestyle Breakfast", $phpArray['breakfast_menu']['food'][4], "The last breakfast item name converted from XML input 3 is not correct");
  234. } // End of function testUsingXML3
  235. /**
  236. * xml2json Test 4
  237. * It tests the conversion of RosettaNet purchase order xml into Json format.
  238. *
  239. * XML characteristic to be tested: XML containing an array of child elements and multiple attributes.
  240. *
  241. */
  242. public function testUsingXML4()
  243. {
  244. // Set the XML contents that will be tested here.
  245. $xmlStringContents = <<<EOT
  246. <?xml version="1.0" encoding="UTF-8"?>
  247. <PurchaseRequisition>
  248. <Submittor>
  249. <SubmittorName>John Doe</SubmittorName>
  250. <SubmittorEmail>john@nodomain.net</SubmittorEmail>
  251. <SubmittorTelephone>1-123-456-7890</SubmittorTelephone>
  252. </Submittor>
  253. <Billing/>
  254. <Approval/>
  255. <Item number="1">
  256. <ItemType>Electronic Component</ItemType>
  257. <ItemDescription>25 microfarad 16 volt surface-mount tantalum capacitor</ItemDescription>
  258. <ItemQuantity>42</ItemQuantity>
  259. <Specification>
  260. <Category type="UNSPSC" value="32121501" name="Fixed capacitors"/>
  261. <RosettaNetSpecification>
  262. <query max.records="1">
  263. <element dicRef="XJA039">
  264. <name>CAPACITOR - FIXED - TANTAL - SOLID</name>
  265. </element>
  266. <element>
  267. <name>Specific Features</name>
  268. <value>R</value>
  269. </element>
  270. <element>
  271. <name>Body Material</name>
  272. <value>C</value>
  273. </element>
  274. <element>
  275. <name>Terminal Position</name>
  276. <value>A</value>
  277. </element>
  278. <element>
  279. <name>Package: Outline Style</name>
  280. <value>CP</value>
  281. </element>
  282. <element>
  283. <name>Lead Form</name>
  284. <value>D</value>
  285. </element>
  286. <element>
  287. <name>Rated Capacitance</name>
  288. <value>0.000025</value>
  289. </element>
  290. <element>
  291. <name>Tolerance On Rated Capacitance (%)</name>
  292. <value>10</value>
  293. </element>
  294. <element>
  295. <name>Leakage Current (Short Term)</name>
  296. <value>0.0000001</value>
  297. </element>
  298. <element>
  299. <name>Rated Voltage</name>
  300. <value>16</value>
  301. </element>
  302. <element>
  303. <name>Operating Temperature</name>
  304. <value type="max">140</value>
  305. <value type="min">-10</value>
  306. </element>
  307. <element>
  308. <name>Mounting</name>
  309. <value>Surface</value>
  310. </element>
  311. </query>
  312. </RosettaNetSpecification>
  313. </Specification>
  314. <Vendor number="1">
  315. <VendorName>Capacitors 'R' Us, Inc.</VendorName>
  316. <VendorIdentifier>98-765-4321</VendorIdentifier>
  317. <VendorImplementation>http://sylviaearle/capaciorsRus/wsdl/buyerseller-implementation.wsdl</VendorImplementation>
  318. </Vendor>
  319. </Item>
  320. </PurchaseRequisition>
  321. EOT;
  322. // There are going to be XML attributes in this test XML.
  323. // Hence, set the flag NOT to ignore XML attributes.
  324. $ignoreXmlAttributes = false;
  325. $jsonContents = "";
  326. $ex = null;
  327. // Convert XNL to JSON now.
  328. // fromXml function simply takes a String containing XML contents as input.
  329. try {
  330. $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes);
  331. } catch (Exception $ex) {
  332. ;
  333. }
  334. $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
  335. // Convert the JSON string into a PHP array.
  336. $phpArray = Zend_Json::decode($jsonContents);
  337. // Test if it is not a NULL object.
  338. $this->assertNotNull($phpArray, "JSON result for XML input 4 is NULL");
  339. // Test for one of the expected fields in the JSON result.
  340. $this->assertContains("98-765-4321", $phpArray['PurchaseRequisition']['Item']['Vendor'], "The vendor id converted from XML input 4 is not correct");
  341. // Test for the presence of multiple XML attributes present that were carried over in the JSON result.
  342. $this->assertContains("UNSPSC", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The type attribute converted from XML input 4 is not correct");
  343. $this->assertContains("32121501", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The value attribute converted from XML input 4 is not correct");
  344. $this->assertContains("Fixed capacitors", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The name attribute converted from XML input 4 is not correct");
  345. } // End of function testUsingXML4
  346. /**
  347. * xml2json Test 5
  348. * It tests the conversion of TV shows xml into Json format.
  349. *
  350. * XML characteristic to be tested: XML containing simple CDATA.
  351. *
  352. */
  353. public function testUsingXML5()
  354. {
  355. // Set the XML contents that will be tested here.
  356. $xmlStringContents = <<<EOT
  357. <?xml version="1.0"?>
  358. <tvshows>
  359. <show>
  360. <name>The Simpsons</name>
  361. </show>
  362. <show>
  363. <name><![CDATA[Lois & Clark]]></name>
  364. </show>
  365. </tvshows>
  366. EOT;
  367. // There are not going to be any XML attributes in this test XML.
  368. // Hence, set the flag to ignore XML attributes.
  369. $ignoreXmlAttributes = true;
  370. $jsonContents = "";
  371. $ex = null;
  372. // Convert XNL to JSON now.
  373. // fromXml function simply takes a String containing XML contents as input.
  374. try {
  375. $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes);
  376. } catch (Exception $ex) {
  377. ;
  378. }
  379. $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
  380. // Convert the JSON string into a PHP array.
  381. $phpArray = Zend_Json::decode($jsonContents);
  382. // Test if it is not a NULL object.
  383. $this->assertNotNull($phpArray, "JSON result for XML input 5 is NULL");
  384. // Test for one of the expected CDATA fields in the JSON result.
  385. $this->assertContains("Lois & Clark", $phpArray['tvshows']['show'][1]['name'], "The CDATA name converted from XML input 5 is not correct");
  386. } // End of function testUsingXML5
  387. /**
  388. * xml2json Test 6
  389. * It tests the conversion of demo application xml into Json format.
  390. *
  391. * XML characteristic to be tested: XML containing a large CDATA.
  392. *
  393. */
  394. public function testUsingXML6()
  395. {
  396. // Set the XML contents that will be tested here.
  397. $xmlStringContents = <<<EOT
  398. <?xml version="1.0"?>
  399. <demo>
  400. <application>
  401. <name>Killer Demo</name>
  402. </application>
  403. <author>
  404. <name>John Doe</name>
  405. </author>
  406. <platform>
  407. <name>LAMP</name>
  408. </platform>
  409. <framework>
  410. <name>Zend</name>
  411. </framework>
  412. <language>
  413. <name>PHP</name>
  414. </language>
  415. <listing>
  416. <code>
  417. <![CDATA[
  418. /*
  419. It may not be a syntactically valid PHP code.
  420. It is used here just to illustrate the CDATA feature of Zend_Xml2Json
  421. */
  422. <?php
  423. include 'example.php';
  424. new SimpleXMLElement();
  425. echo(getMovies()->movie[0]->characters->addChild('character'));
  426. getMovies()->movie[0]->characters->character->addChild('name', "Mr. Parser");
  427. getMovies()->movie[0]->characters->character->addChild('actor', "John Doe");
  428. // Add it as a child element.
  429. getMovies()->movie[0]->addChild('rating', 'PG');
  430. getMovies()->movie[0]->rating->addAttribute("type", 'mpaa');
  431. echo getMovies()->asXML();
  432. ?>
  433. ]]>
  434. </code>
  435. </listing>
  436. </demo>
  437. EOT;
  438. // There are not going to be any XML attributes in this test XML.
  439. // Hence, set the flag to ignore XML attributes.
  440. $ignoreXmlAttributes = true;
  441. $jsonContents = "";
  442. $ex = null;
  443. // Convert XNL to JSON now.
  444. // fromXml function simply takes a String containing XML contents as input.
  445. try {
  446. $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes);
  447. } catch (Exception $ex) {
  448. ;
  449. }
  450. $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
  451. // Convert the JSON string into a PHP array.
  452. $phpArray = Zend_Json::decode($jsonContents);
  453. // Test if it is not a NULL object.
  454. $this->assertNotNull($phpArray, "JSON result for XML input 6 is NULL");
  455. // Test for one of the expected fields in the JSON result.
  456. $this->assertContains("Zend", $phpArray['demo']['framework']['name'], "The framework name field converted from XML input 6 is not correct");
  457. // Test for one of the expected CDATA fields in the JSON result.
  458. $this->assertContains('echo getMovies()->asXML();', $phpArray['demo']['listing']['code'], "The CDATA code converted from XML input 6 is not correct");
  459. } // End of function testUsingXML6
  460. /**
  461. * xml2json Test 7
  462. * It tests the conversion of an invalid xml into Json format.
  463. *
  464. * XML characteristic to be tested: XML containing invalid syntax.
  465. *
  466. */
  467. /*
  468. public function testUsingXML7()
  469. {
  470. // Set the XML contents that will be tested here.
  471. $xmlStringContents = <<<EOT
  472. This is an invalid XML file.
  473. Use this file to test the xml2json feature in the Zend_Json class.
  474. Since it is an invalid XML file, an appropriate exception should be
  475. thrown by the Zend_Json::fromXml function.
  476. <?xml version="1.0"?>
  477. <invalidxml>
  478. </code>
  479. </listing>
  480. </invalidxml>
  481. EOT;
  482. // There are not going to be any XML attributes in this test XML.
  483. // Hence, set the flag to ignore XML attributes.
  484. $ignoreXmlAttributes = true;
  485. $jsonContents = "";
  486. $ex = null;
  487. // Convert XNL to JSON now.
  488. // fromXml function simply takes a String containing XML contents as input.
  489. try {
  490. $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes);
  491. } catch (Exception $ex) {
  492. ;
  493. }
  494. $this->assertNotSame($ex, null, "Zend_JSON::fromXml returned an exception.");
  495. } // End of function testUsingXML7
  496. */
  497. } // End of class Zend_Json_JsonXMLTest