JsonXMLTest.php 19 KB

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