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.
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
23 error_reporting( E_ALL | E_STRICT
); // now required for each test suite
28 require_once 'Zend/Json.php';
33 require_once 'PHPUnit/Framework.php';
38 * @subpackage UnitTests
39 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
40 * @license http://framework.zend.com/license/new-bsd New BSD License
43 class Zend_Json_JsonXMLTest
extends PHPUnit_Framework_TestCase
47 * It tests the conversion of a contact list xml into Json format.
49 * XML characteristic to be tested: XML containing an array of child elements.
52 public function testUsingXML1()
54 // Set the XML contents that will be tested here.
55 $xmlStringContents = <<<EOT
56 <?xml version="1.0" encoding="UTF-8"?>
98 // There are not going to be any XML attributes in this test XML.
99 // Hence, set the flag to ignore XML attributes.
100 $ignoreXmlAttributes = true;
104 // Convert XNL to JSON now.
105 // fromXml function simply takes a String containing XML contents as input.
107 $jsonContents = Zend_Json
::fromXml($xmlStringContents, $ignoreXmlAttributes);
108 } catch (Exception
$ex) {
112 $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
114 // Convert the JSON string into a PHP array.
115 $phpArray = Zend_Json
::decode($jsonContents);
116 // Test if it is not a NULL object.
117 $this->assertNotNull($phpArray, "JSON result for XML input 1 is NULL");
118 // Test for one of the expected fields in the JSON result.
119 $this->assertSame("Jane Smith", $phpArray['contacts']['contact'][3]['name'], "The last contact name converted from XML input 1 is not correct");
120 } // End of function testUsingXML1
124 * It tests the conversion of book publication xml into Json format.
126 * XML characteristic to be tested: XML containing an array of child elements with XML attributes.
129 public function testUsingXML2()
131 // Set the XML contents that will be tested here.
132 $xmlStringContents = <<<EOT
133 <?xml version="1.0" encoding="UTF-8"?>
136 <title>Code Generation in Action</title>
137 <author><first>Jack</first><last>Herrington</last></author>
138 <publisher>Manning</publisher>
141 <title>PHP Hacks</title>
142 <author><first>Jack</first><last>Herrington</last></author>
143 <publisher>O'Reilly</publisher>
146 <title>Podcasting Hacks</title>
147 <author><first>Jack</first><last>Herrington</last></author>
148 <publisher>O'Reilly</publisher>
154 // There are going to be XML attributes in this test XML.
155 // Hence, set the flag NOT to ignore XML attributes.
156 $ignoreXmlAttributes = false;
160 // Convert XNL to JSON now.
161 // fromXml function simply takes a String containing XML contents as input.
163 $jsonContents = Zend_Json
::fromXml($xmlStringContents, $ignoreXmlAttributes);
164 } catch (Exception
$ex) {
168 $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
170 // Convert the JSON string into a PHP array.
171 $phpArray = Zend_Json
::decode($jsonContents);
172 // Test if it is not a NULL object.
173 $this->assertNotNull($phpArray, "JSON result for XML input 2 is NULL");
174 // Test for one of the expected fields in the JSON result.
175 $this->assertSame("Podcasting Hacks", $phpArray['books']['book'][2]['title'], "The last book title converted from XML input 2 is not correct");
176 // Test one of the expected XML attributes carried over in the JSON result.
177 $this->assertSame("3", $phpArray['books']['book'][2]['@attributes']['id'], "The last id attribute converted from XML input 2 is not correct");
178 } // End of function testUsingXML2
182 * It tests the conversion of food menu xml into Json format.
184 * XML characteristic to be tested: XML containing an array of child elements.
187 public function testUsingXML3()
189 // Set the XML contents that will be tested here.
190 $xmlStringContents = <<<EOT
191 <?xml version="1.0" encoding="ISO-8859-1" ?>
194 <name>Belgian Waffles</name>
197 two of our famous Belgian Waffles with plenty of real maple
200 <calories>650</calories>
203 <name>Strawberry Belgian Waffles</name>
206 light Belgian waffles covered with strawberries and whipped
209 <calories>900</calories>
212 <name>Berry-Berry Belgian Waffles</name>
215 light Belgian waffles covered with an assortment of fresh
216 berries and whipped cream
218 <calories>900</calories>
221 <name>French Toast</name>
224 thick slices made from our homemade sourdough bread
226 <calories>600</calories>
229 <name>Homestyle Breakfast</name>
232 two eggs, bacon or sausage, toast, and our ever-popular hash
235 <calories>950</calories>
241 // There are not going to be any XML attributes in this test XML.
242 // Hence, set the flag to ignore XML attributes.
243 $ignoreXmlAttributes = true;
247 // Convert XNL to JSON now.
248 // fromXml function simply takes a String containing XML contents as input.
250 $jsonContents = Zend_Json
::fromXml($xmlStringContents, $ignoreXmlAttributes);
251 } catch (Exception
$ex) {
255 $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
257 // Convert the JSON string into a PHP array.
258 $phpArray = Zend_Json
::decode($jsonContents);
259 // Test if it is not a NULL object.
260 $this->assertNotNull($phpArray, "JSON result for XML input 3 is NULL");
261 // Test for one of the expected fields in the JSON result.
262 $this->assertContains("Homestyle Breakfast", $phpArray['breakfast_menu']['food'][4], "The last breakfast item name converted from XML input 3 is not correct");
263 } // End of function testUsingXML3
267 * It tests the conversion of RosettaNet purchase order xml into Json format.
269 * XML characteristic to be tested: XML containing an array of child elements and multiple attributes.
272 public function testUsingXML4()
274 // Set the XML contents that will be tested here.
275 $xmlStringContents = <<<EOT
276 <?xml version="1.0" encoding="UTF-8"?>
277 <PurchaseRequisition>
279 <SubmittorName>John Doe</SubmittorName>
280 <SubmittorEmail>john@nodomain.net</SubmittorEmail>
281 <SubmittorTelephone>1-123-456-7890</SubmittorTelephone>
286 <ItemType>Electronic Component</ItemType>
287 <ItemDescription>25 microfarad 16 volt surface-mount tantalum capacitor</ItemDescription>
288 <ItemQuantity>42</ItemQuantity>
290 <Category type="UNSPSC" value="32121501" name="Fixed capacitors"/>
291 <RosettaNetSpecification>
292 <query max.records="1">
293 <element dicRef="XJA039">
294 <name>CAPACITOR - FIXED - TANTAL - SOLID</name>
297 <name>Specific Features</name>
301 <name>Body Material</name>
305 <name>Terminal Position</name>
309 <name>Package: Outline Style</name>
313 <name>Lead Form</name>
317 <name>Rated Capacitance</name>
318 <value>0.000025</value>
321 <name>Tolerance On Rated Capacitance (%)</name>
325 <name>Leakage Current (Short Term)</name>
326 <value>0.0000001</value>
329 <name>Rated Voltage</name>
333 <name>Operating Temperature</name>
334 <value type="max">140</value>
335 <value type="min">-10</value>
338 <name>Mounting</name>
339 <value>Surface</value>
342 </RosettaNetSpecification>
345 <VendorName>Capacitors 'R' Us, Inc.</VendorName>
346 <VendorIdentifier>98-765-4321</VendorIdentifier>
347 <VendorImplementation>http://sylviaearle/capaciorsRus/wsdl/buyerseller-implementation.wsdl</VendorImplementation>
350 </PurchaseRequisition>
354 // There are going to be XML attributes in this test XML.
355 // Hence, set the flag NOT to ignore XML attributes.
356 $ignoreXmlAttributes = false;
360 // Convert XNL to JSON now.
361 // fromXml function simply takes a String containing XML contents as input.
363 $jsonContents = Zend_Json
::fromXml($xmlStringContents, $ignoreXmlAttributes);
364 } catch (Exception
$ex) {
368 $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
370 // Convert the JSON string into a PHP array.
371 $phpArray = Zend_Json
::decode($jsonContents);
372 // Test if it is not a NULL object.
373 $this->assertNotNull($phpArray, "JSON result for XML input 4 is NULL");
374 // Test for one of the expected fields in the JSON result.
375 $this->assertContains("98-765-4321", $phpArray['PurchaseRequisition']['Item']['Vendor'], "The vendor id converted from XML input 4 is not correct");
376 // Test for the presence of multiple XML attributes present that were carried over in the JSON result.
377 $this->assertContains("UNSPSC", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The type attribute converted from XML input 4 is not correct");
378 $this->assertContains("32121501", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The value attribute converted from XML input 4 is not correct");
379 $this->assertContains("Fixed capacitors", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The name attribute converted from XML input 4 is not correct");
380 } // End of function testUsingXML4
384 * It tests the conversion of TV shows xml into Json format.
386 * XML characteristic to be tested: XML containing simple CDATA.
389 public function testUsingXML5()
391 // Set the XML contents that will be tested here.
392 $xmlStringContents = <<<EOT
393 <?xml version="1.0"?>
396 <name>The Simpsons</name>
400 <name><![CDATA[Lois & Clark]]></name>
406 // There are not going to be any XML attributes in this test XML.
407 // Hence, set the flag to ignore XML attributes.
408 $ignoreXmlAttributes = true;
412 // Convert XNL to JSON now.
413 // fromXml function simply takes a String containing XML contents as input.
415 $jsonContents = Zend_Json
::fromXml($xmlStringContents, $ignoreXmlAttributes);
416 } catch (Exception
$ex) {
420 $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
422 // Convert the JSON string into a PHP array.
423 $phpArray = Zend_Json
::decode($jsonContents);
424 // Test if it is not a NULL object.
425 $this->assertNotNull($phpArray, "JSON result for XML input 5 is NULL");
426 // Test for one of the expected CDATA fields in the JSON result.
427 $this->assertContains("Lois & Clark", $phpArray['tvshows']['show'][1]['name'], "The CDATA name converted from XML input 5 is not correct");
428 } // End of function testUsingXML5
432 * It tests the conversion of demo application xml into Json format.
434 * XML characteristic to be tested: XML containing a large CDATA.
437 public function testUsingXML6()
439 // Set the XML contents that will be tested here.
440 $xmlStringContents = <<<EOT
441 <?xml version="1.0"?>
444 <name>Killer Demo</name>
448 <name>John Doe</name>
467 It may not be a syntactically valid PHP code.
468 It is used here just to illustrate the CDATA feature of Zend_Xml2Json
471 include 'example.php';
472 new SimpleXMLElement();
473 echo(getMovies()->movie[0]->characters->addChild('character'));
474 getMovies()->movie[0]->characters->character->addChild('name', "Mr. Parser");
475 getMovies()->movie[0]->characters->character->addChild('actor', "John Doe");
476 // Add it as a child element.
477 getMovies()->movie[0]->addChild('rating', 'PG');
478 getMovies()->movie[0]->rating->addAttribute("type", 'mpaa');
479 echo getMovies()->asXML();
488 // There are not going to be any XML attributes in this test XML.
489 // Hence, set the flag to ignore XML attributes.
490 $ignoreXmlAttributes = true;
494 // Convert XNL to JSON now.
495 // fromXml function simply takes a String containing XML contents as input.
497 $jsonContents = Zend_Json
::fromXml($xmlStringContents, $ignoreXmlAttributes);
498 } catch (Exception
$ex) {
502 $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
504 // Convert the JSON string into a PHP array.
505 $phpArray = Zend_Json
::decode($jsonContents);
506 // Test if it is not a NULL object.
507 $this->assertNotNull($phpArray, "JSON result for XML input 6 is NULL");
508 // Test for one of the expected fields in the JSON result.
509 $this->assertContains("Zend", $phpArray['demo']['framework']['name'], "The framework name field converted from XML input 6 is not correct");
510 // Test for one of the expected CDATA fields in the JSON result.
511 $this->assertContains('echo getMovies()->asXML();', $phpArray['demo']['listing']['code'], "The CDATA code converted from XML input 6 is not correct");
512 } // End of function testUsingXML6
516 * It tests the conversion of an invalid xml into Json format.
518 * XML characteristic to be tested: XML containing invalid syntax.
522 public function testUsingXML7()
524 // Set the XML contents that will be tested here.
525 $xmlStringContents = <<<EOT
526 This is an invalid XML file.
527 Use this file to test the xml2json feature in the Zend_Json class.
528 Since it is an invalid XML file, an appropriate exception should be
529 thrown by the Zend_Json::fromXml function.
530 <?xml version="1.0"?>
538 // There are not going to be any XML attributes in this test XML.
539 // Hence, set the flag to ignore XML attributes.
540 $ignoreXmlAttributes = true;
544 // Convert XNL to JSON now.
545 // fromXml function simply takes a String containing XML contents as input.
547 $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes);
548 } catch (Exception $ex) {
552 $this->assertNotSame($ex, null, "Zend_JSON::fromXml returned an exception.");
553 } // End of function testUsingXML7
555 } // End of class Zend_Json_JsonXMLTest