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