Adding enclosures now treats type and length parameters as optional for Atom, but...
[zend.git] / tests / Zend / Json / JsonXMLTest.php
bloba81b07701c6030934048807c4f721c471cc8f0ff
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
23 error_reporting( E_ALL | E_STRICT ); // now required for each test suite
25 /**
26 * Zend_Json
28 require_once 'Zend/Json.php';
30 /**
31 * PHPUnit test case
33 require_once 'PHPUnit/Framework.php';
35 /**
36 * @category Zend
37 * @package Zend_Json
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
41 * @group Zend_Json
43 class Zend_Json_JsonXMLTest extends PHPUnit_Framework_TestCase
45 /**
46 * xml2json Test 1
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"?>
57 <contacts>
58 <contact>
59 <name>
60 John Doe
61 </name>
62 <phone>
63 123-456-7890
64 </phone>
65 </contact>
67 <contact>
68 <name>
69 Jane Doe
70 </name>
71 <phone>
72 123-456-0000
73 </phone>
74 </contact>
76 <contact>
77 <name>
78 John Smith
79 </name>
80 <phone>
81 123-456-1111
82 </phone>
83 </contact>
85 <contact>
86 <name>
87 Jane Smith
88 </name>
89 <phone>
90 123-456-9999
91 </phone>
92 </contact>
94 </contacts>
96 EOT;
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;
101 $jsonContents = "";
102 $ex = null;
104 // Convert XNL to JSON now.
105 // fromXml function simply takes a String containing XML contents as input.
106 try {
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
123 * xml2json Test 2
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"?>
134 <books>
135 <book id="1">
136 <title>Code Generation in Action</title>
137 <author><first>Jack</first><last>Herrington</last></author>
138 <publisher>Manning</publisher>
139 </book>
140 <book id="2">
141 <title>PHP Hacks</title>
142 <author><first>Jack</first><last>Herrington</last></author>
143 <publisher>O'Reilly</publisher>
144 </book>
145 <book id="3">
146 <title>Podcasting Hacks</title>
147 <author><first>Jack</first><last>Herrington</last></author>
148 <publisher>O'Reilly</publisher>
149 </book>
150 </books>
152 EOT;
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;
157 $jsonContents = "";
158 $ex = null;
160 // Convert XNL to JSON now.
161 // fromXml function simply takes a String containing XML contents as input.
162 try {
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
181 * xml2json Test 3
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" ?>
192 <breakfast_menu>
193 <food>
194 <name>Belgian Waffles</name>
195 <price>$5.95</price>
196 <description>
197 two of our famous Belgian Waffles with plenty of real maple
198 syrup
199 </description>
200 <calories>650</calories>
201 </food>
202 <food>
203 <name>Strawberry Belgian Waffles</name>
204 <price>$7.95</price>
205 <description>
206 light Belgian waffles covered with strawberries and whipped
207 cream
208 </description>
209 <calories>900</calories>
210 </food>
211 <food>
212 <name>Berry-Berry Belgian Waffles</name>
213 <price>$8.95</price>
214 <description>
215 light Belgian waffles covered with an assortment of fresh
216 berries and whipped cream
217 </description>
218 <calories>900</calories>
219 </food>
220 <food>
221 <name>French Toast</name>
222 <price>$4.50</price>
223 <description>
224 thick slices made from our homemade sourdough bread
225 </description>
226 <calories>600</calories>
227 </food>
228 <food>
229 <name>Homestyle Breakfast</name>
230 <price>$6.95</price>
231 <description>
232 two eggs, bacon or sausage, toast, and our ever-popular hash
233 browns
234 </description>
235 <calories>950</calories>
236 </food>
237 </breakfast_menu>
239 EOT;
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;
244 $jsonContents = "";
245 $ex = null;
247 // Convert XNL to JSON now.
248 // fromXml function simply takes a String containing XML contents as input.
249 try {
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
266 * xml2json Test 4
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>
278 <Submittor>
279 <SubmittorName>John Doe</SubmittorName>
280 <SubmittorEmail>john@nodomain.net</SubmittorEmail>
281 <SubmittorTelephone>1-123-456-7890</SubmittorTelephone>
282 </Submittor>
283 <Billing/>
284 <Approval/>
285 <Item number="1">
286 <ItemType>Electronic Component</ItemType>
287 <ItemDescription>25 microfarad 16 volt surface-mount tantalum capacitor</ItemDescription>
288 <ItemQuantity>42</ItemQuantity>
289 <Specification>
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>
295 </element>
296 <element>
297 <name>Specific Features</name>
298 <value>R</value>
299 </element>
300 <element>
301 <name>Body Material</name>
302 <value>C</value>
303 </element>
304 <element>
305 <name>Terminal Position</name>
306 <value>A</value>
307 </element>
308 <element>
309 <name>Package: Outline Style</name>
310 <value>CP</value>
311 </element>
312 <element>
313 <name>Lead Form</name>
314 <value>D</value>
315 </element>
316 <element>
317 <name>Rated Capacitance</name>
318 <value>0.000025</value>
319 </element>
320 <element>
321 <name>Tolerance On Rated Capacitance (%)</name>
322 <value>10</value>
323 </element>
324 <element>
325 <name>Leakage Current (Short Term)</name>
326 <value>0.0000001</value>
327 </element>
328 <element>
329 <name>Rated Voltage</name>
330 <value>16</value>
331 </element>
332 <element>
333 <name>Operating Temperature</name>
334 <value type="max">140</value>
335 <value type="min">-10</value>
336 </element>
337 <element>
338 <name>Mounting</name>
339 <value>Surface</value>
340 </element>
341 </query>
342 </RosettaNetSpecification>
343 </Specification>
344 <Vendor number="1">
345 <VendorName>Capacitors 'R' Us, Inc.</VendorName>
346 <VendorIdentifier>98-765-4321</VendorIdentifier>
347 <VendorImplementation>http://sylviaearle/capaciorsRus/wsdl/buyerseller-implementation.wsdl</VendorImplementation>
348 </Vendor>
349 </Item>
350 </PurchaseRequisition>
352 EOT;
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;
357 $jsonContents = "";
358 $ex = null;
360 // Convert XNL to JSON now.
361 // fromXml function simply takes a String containing XML contents as input.
362 try {
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
383 * xml2json Test 5
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"?>
394 <tvshows>
395 <show>
396 <name>The Simpsons</name>
397 </show>
399 <show>
400 <name><![CDATA[Lois & Clark]]></name>
401 </show>
402 </tvshows>
404 EOT;
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;
409 $jsonContents = "";
410 $ex = null;
412 // Convert XNL to JSON now.
413 // fromXml function simply takes a String containing XML contents as input.
414 try {
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
431 * xml2json Test 6
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"?>
442 <demo>
443 <application>
444 <name>Killer Demo</name>
445 </application>
447 <author>
448 <name>John Doe</name>
449 </author>
451 <platform>
452 <name>LAMP</name>
453 </platform>
455 <framework>
456 <name>Zend</name>
457 </framework>
459 <language>
460 <name>PHP</name>
461 </language>
463 <listing>
464 <code>
465 <![CDATA[
467 It may not be a syntactically valid PHP code.
468 It is used here just to illustrate the CDATA feature of Zend_Xml2Json
470 <?php
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();
482 </code>
483 </listing>
484 </demo>
486 EOT;
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;
491 $jsonContents = "";
492 $ex = null;
494 // Convert XNL to JSON now.
495 // fromXml function simply takes a String containing XML contents as input.
496 try {
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
515 * xml2json Test 7
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"?>
531 <invalidxml>
532 </code>
533 </listing>
534 </invalidxml>
536 EOT;
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;
541 $jsonContents = "";
542 $ex = null;
544 // Convert XNL to JSON now.
545 // fromXml function simply takes a String containing XML contents as input.
546 try {
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