JsonXMLTest.php 17 KB

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