1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* __ ___ ____ _ ___ _ __ |
4
|
|
|
* / |/ /_ __/ / /_(_)___/ (_)___ ___ ___ ____ _____(_)___ ____ ____ _/ / |
5
|
|
|
* / /|_/ / / / / / __/ / __ / / __ `__ \/ _ \/ __ \/ ___/ / __ \/ __ \ / __ `/ / |
6
|
|
|
* / / / / /_/ / / /_/ / /_/ / / / / / / / __/ / / (__ ) / /_/ / / / // /_/ / / |
7
|
|
|
* /_/ /_/\__,_/_/\__/_/\__,_/_/_/ /_/ /_/\___/_/ /_/____/_/\____/_/ /_(_)__,_/_/ |
8
|
|
|
* |
9
|
|
|
* XML to Array Library |
10
|
|
|
* Copyright (c) Multidimension.al (http://multidimension.al) |
11
|
|
|
* Github : https://github.com/multidimension-al/xml-array |
12
|
|
|
* |
13
|
|
|
* Licensed under The MIT License |
14
|
|
|
* For full copyright and license information, please see the LICENSE file |
15
|
|
|
* Redistributions of files must retain the above copyright notice. |
16
|
|
|
* |
17
|
|
|
* @copyright Copyright © 2017-2019 Multidimension.al (http://multidimension.al) |
18
|
|
|
* @link https://github.com/multidimension-al/xml-array Github |
19
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Multidimensional\XmlArray\Test; |
23
|
|
|
|
24
|
|
|
use Multidimensional\XmlArray\XMLArray; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
|
27
|
|
|
class XMLArrayTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
public function testSimple() |
31
|
|
|
{ |
32
|
|
|
$string = '<simple>true</simple>'; |
33
|
|
|
$result = XMLArray::generateArray($string); |
34
|
|
|
$expected = ['simple' => [0 => 'true']]; |
35
|
|
|
$this->assertEquals($expected, $result); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testComplex() |
39
|
|
|
{ |
40
|
|
|
$string = '<person><firstname>Test</firstname><lastname>Man</lastname><address><street>123 Fake St</street><city>Springfield</city><state>USA</state></address></person>'; |
41
|
|
|
$result = XMLArray::generateArray($string); |
42
|
|
|
$expected = ['person' => ['firstname' => 'Test', 'lastname' => 'Man', 'address' => ['street' => '123 Fake St', 'city' => 'Springfield', 'state' => 'USA']]]; |
43
|
|
|
$this->assertEquals($expected, $result); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testStandardAttributes() |
47
|
|
|
{ |
48
|
|
|
$string = '<xml><person ID="1"><firstname>Test</firstname><lastname>Man</lastname><address><street>123 Fake St</street><city>Springfield</city><state>USA</state></address></person></xml>'; |
49
|
|
|
$result = XMLArray::generateArray($string, false); |
50
|
|
|
$expected = ['xml' => ['person' => ['firstname' => 'Test', 'lastname' => 'Man', 'address' => ['street' => '123 Fake St', 'city' => 'Springfield', 'state' => 'USA'], '@attributes' => ['ID' => '1']]]]; |
51
|
|
|
$this->assertEquals($expected, $result); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testAttributes() |
55
|
|
|
{ |
56
|
|
|
$string = '<xml><person ID="1"><firstname>Test</firstname><lastname>Man</lastname><address><street>123 Fake St</street><city>Springfield</city><state>USA</state></address></person></xml>'; |
57
|
|
|
$result = XMLArray::generateArray($string); |
58
|
|
|
$expected = ['xml' => ['person' => ['firstname' => 'Test', 'lastname' => 'Man', 'address' => ['street' => '123 Fake St', 'city' => 'Springfield', 'state' => 'USA'], '@ID' => '1']]]; |
59
|
|
|
$this->assertEquals($expected, $result); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testComplexWithAttributes() |
63
|
|
|
{ |
64
|
|
|
$string = '<?xml version="1.0" encoding="UTF-8"?><xml><person ID="1"><name>John Smith</name></person><person ID="2"><name>Jane Smith</name></person></xml>'; |
65
|
|
|
$result = XMLArray::generateArray($string); |
66
|
|
|
$expected = ['xml' => ['person' => [0 => ['@ID' => '1', 'name' => 'John Smith'], 1 => ['@ID' => '2', 'name' => 'Jane Smith']]]]; |
67
|
|
|
$this->assertEquals($expected, $result); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testMoreComplexAttributes() |
71
|
|
|
{ |
72
|
|
|
$string = '<?xml version="1.0" encoding="UTF-8"?><RateV4Response><Package ID="123"><ZipOrigination>20500</ZipOrigination><ZipDestination>90210</ZipDestination><Pounds>0</Pounds><Ounces>32</Ounces><Size>REGULAR</Size><Machinable>TRUE</Machinable><Zone>8</Zone><Postage CLASSID="1"><MailService>Priority Mail 2-Day<sup>™</sup></MailService><Rate>12.75</Rate></Postage><Postage CLASSID="22"><MailService>Priority Mail 2-Day<sup>™</sup> Large Flat Rate Box</MailService><Rate>18.85</Rate></Postage><Postage CLASSID="17"><MailService>Priority Mail 2-Day<sup>™</sup> Medium Flat Rate Box</MailService><Rate>13.60</Rate></Postage><Postage CLASSID="28"><MailService>Priority Mail 2-Day<sup>™</sup> Small Flat Rate Box</MailService><Rate>7.15</Rate></Postage></Package></RateV4Response>'; |
73
|
|
|
$result = XMLArray::generateArray($string); |
74
|
|
|
$expected = ['RateV4Response' => ['Package' => ['@ID' => '123', 'ZipOrigination' => '20500', 'ZipDestination' => '90210', 'Pounds' => '0', 'Ounces' => '32', 'Size' => 'REGULAR', 'Machinable' => 'TRUE', 'Zone' => '8', 'Postage' => [0 => ['@CLASSID' => '1', 'MailService' => 'Priority Mail 2-Day<sup>™</sup>', 'Rate' => '12.75'], 1 => ['@CLASSID' => '22', 'MailService' => 'Priority Mail 2-Day<sup>™</sup> Large Flat Rate Box', 'Rate' => '18.85'], 2 => ['@CLASSID' => '17', 'MailService' => 'Priority Mail 2-Day<sup>™</sup> Medium Flat Rate Box', 'Rate' => '13.60'], 3 => ['@CLASSID' => '28', 'MailService' => 'Priority Mail 2-Day<sup>™</sup> Small Flat Rate Box', 'Rate' => '7.15']]]]]; |
75
|
|
|
$this->assertEquals($expected, $result); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testFalse() |
79
|
|
|
{ |
80
|
|
|
$string = ''; |
81
|
|
|
$result = XMLArray::generateArray($string); |
82
|
|
|
$this->assertNull($result); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testNull() |
86
|
|
|
{ |
87
|
|
|
$string = null; |
88
|
|
|
$result = XMLArray::generateArray($string); |
89
|
|
|
$this->assertNull($result); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|