1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* __ ___ ____ _ ___ _ __ |
4
|
|
|
* / |/ /_ __/ / /_(_)___/ (_)___ ___ ___ ____ _____(_)___ ____ ____ _/ / |
5
|
|
|
* / /|_/ / / / / / __/ / __ / / __ `__ \/ _ \/ __ \/ ___/ / __ \/ __ \ / __ `/ / |
6
|
|
|
* / / / / /_/ / / /_/ / /_/ / / / / / / / __/ / / (__ ) / /_/ / / / // /_/ / / |
7
|
|
|
* /_/ /_/\__,_/_/\__/_/\__,_/_/_/ /_/ /_/\___/_/ /_/____/_/\____/_/ /_(_)__,_/_/ |
8
|
|
|
* |
9
|
|
|
* Array to DOM Document Library |
10
|
|
|
* Copyright (c) Multidimension.al (http://multidimension.al) |
11
|
|
|
* Github : https://github.com/multidimension-al/dom-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/dom-array Github |
19
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Multidimensional\DomArray\Test; |
23
|
|
|
|
24
|
|
|
use Multidimensional\DomArray\DOMArray; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
|
27
|
|
|
class DOMArrayTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
public function testSimple() |
31
|
|
|
{ |
32
|
|
|
$array = ['simple' => 'true']; |
33
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
34
|
|
|
$dom->loadArray($array); |
35
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
36
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><simple>true</simple>'; |
37
|
|
|
$this->assertEquals($result, $expected); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testComplex() |
41
|
|
|
{ |
42
|
|
|
$array = ['person' => ['firstname' => 'Test', 'lastname' => 'Man', 'address' => ['street' => '123 Fake St', 'city' => 'Springfield', 'state' => 'USA']]]; |
43
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
44
|
|
|
$dom->loadArray($array); |
45
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
46
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><person><firstname>Test</firstname><lastname>Man</lastname><address><street>123 Fake St</street><city>Springfield</city><state>USA</state></address></person>'; |
47
|
|
|
$this->assertEquals($result, $expected); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testExtraComplex() |
51
|
|
|
{ |
52
|
|
|
$array = ['person' => ['firstname' => 'Test', 'lastname' => 'Man', 'address' => [0 => ['street' => '123 Fake St', 'city' => 'Springfield', 'state' => 'USA'], 1 => ['street' => '456 Real Ave', 'city' => 'New York', 'state' => 'NY']]]]; |
53
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
54
|
|
|
$dom->loadArray($array); |
55
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
56
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><person><firstname>Test</firstname><lastname>Man</lastname><address><street>123 Fake St</street><city>Springfield</city><state>USA</state></address><address><street>456 Real Ave</street><city>New York</city><state>NY</state></address></person>'; |
57
|
|
|
$this->assertEquals($result, $expected); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
public function testAttribute() |
62
|
|
|
{ |
63
|
|
|
$array = ['simple' => ['complex' => 'true', '@plan' => '123']]; |
64
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
65
|
|
|
$dom->loadArray($array); |
66
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
67
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><simple plan="123"><complex>true</complex></simple>'; |
68
|
|
|
$this->assertEquals($result, $expected); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testNullArray() |
72
|
|
|
{ |
73
|
|
|
$array = null; |
74
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
75
|
|
|
$dom->loadArray($array); |
76
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
77
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?>'; |
78
|
|
|
$this->assertEquals($result, $expected); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testDifferentVersion() |
82
|
|
|
{ |
83
|
|
|
$array = null; |
84
|
|
|
$dom = new DOMArray('1.1', 'utf-8'); |
85
|
|
|
$dom->loadArray($array); |
86
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
87
|
|
|
$expected = '<?xml version="1.1" encoding="utf-8"?>'; |
88
|
|
|
$this->assertEquals($result, $expected); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testDifferentEncoding() |
92
|
|
|
{ |
93
|
|
|
$array = null; |
94
|
|
|
$dom = new DOMArray('1.0', 'iso-8859-1'); |
95
|
|
|
$dom->loadArray($array); |
96
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
97
|
|
|
$expected = '<?xml version="1.0" encoding="iso-8859-1"?>'; |
98
|
|
|
$this->assertEquals($result, $expected); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testDefault() |
102
|
|
|
{ |
103
|
|
|
$array = null; |
104
|
|
|
$dom = new DOMArray(); |
105
|
|
|
$dom->loadArray($array); |
106
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
107
|
|
|
$expected = '<?xml version="1.0"?>'; |
108
|
|
|
$this->assertEquals($result, $expected); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testNothing() |
112
|
|
|
{ |
113
|
|
|
$dom = new DOMArray(); |
114
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
115
|
|
|
$expected = '<?xml version="1.0"?>'; |
116
|
|
|
$this->assertEquals($result, $expected); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testTrackRequest() |
120
|
|
|
{ |
121
|
|
|
$array = ['TrackRequest' => ['TrackID' => [0 => ['@ID' => 'EJ123456780US'], 1 => ['@ID' => 'EJ123456789US'], 2 => ['@ID' => 'EJ123456781US']]]]; |
122
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
123
|
|
|
$dom->loadArray($array); |
124
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
125
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><TrackRequest><TrackID ID="EJ123456780US"/><TrackID ID="EJ123456789US"/><TrackID ID="EJ123456781US"/></TrackRequest>'; |
126
|
|
|
$this->assertEquals($expected, $result); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testAddressValidateRequest() |
130
|
|
|
{ |
131
|
|
|
$array = ['AddressValidateRequest' => ['Address' => [0 => ['@ID' => 123, 'FirmName' => 'XYZ Corp', 'Address2' => '123 Fake St.', 'City' => 'Los Angeles', 'State' => 'NY', 'Zip5' => '90210', 'Address1' => null, 'Urbanization' => null, 'Zip4' => null], 1 => ['@ID' => 456, 'FirmName' => 'XYZ Corp', 'Address2' => '123 Fake St.', 'City' => 'Los Angeles', 'State' => 'NY', 'Zip5' => '90210', 'Address1' => null, 'Urbanization' => null, 'Zip4' => null], 2 => ['@ID' => 789, 'FirmName' => 'XYZ Corp', 'Address2' => '123 Fake St.', 'City' => 'Los Angeles', 'State' => 'NY', 'Zip5' => '90210', 'Address1' => null, 'Urbanization' => null, 'Zip4' => null]]]]; |
132
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
133
|
|
|
$dom->loadArray($array); |
134
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
135
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><AddressValidateRequest><Address ID="123"><FirmName>XYZ Corp</FirmName><Address2>123 Fake St.</Address2><City>Los Angeles</City><State>NY</State><Zip5>90210</Zip5><Address1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><Urbanization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><Zip4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/></Address><Address ID="456"><FirmName>XYZ Corp</FirmName><Address2>123 Fake St.</Address2><City>Los Angeles</City><State>NY</State><Zip5>90210</Zip5><Address1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><Urbanization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><Zip4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/></Address><Address ID="789"><FirmName>XYZ Corp</FirmName><Address2>123 Fake St.</Address2><City>Los Angeles</City><State>NY</State><Zip5>90210</Zip5><Address1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><Urbanization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><Zip4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/></Address></AddressValidateRequest>'; |
136
|
|
|
$this->assertEquals($expected, $result); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testBoolean() |
140
|
|
|
{ |
141
|
|
|
$array = ['boolean' => ['true' => true, 'false' => false]]; |
142
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
143
|
|
|
$dom->loadArray($array); |
144
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
145
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><boolean><true>true</true><false>false</false></boolean>'; |
146
|
|
|
$this->assertEquals($expected, $result); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testFlag() |
150
|
|
|
{ |
151
|
|
|
$array = ['xml' => ['nullthing' => null, 'nothing' => '', 'something' => 'hello']]; |
152
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
153
|
|
|
$dom->loadArray($array); |
154
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
155
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><xml><nullthing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><nothing/><something>hello</something></xml>'; |
156
|
|
|
$this->assertEquals($expected, $result); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testZero() |
160
|
|
|
{ |
161
|
|
|
$array = ['xml' => ['zero' => 0, 'zero2' => 0.0]]; |
162
|
|
|
$dom = new DOMArray('1.0', 'utf-8'); |
163
|
|
|
$dom->loadArray($array); |
164
|
|
|
$result = preg_replace("/\n/", '', $dom->saveXML()); |
165
|
|
|
$expected = '<?xml version="1.0" encoding="utf-8"?><xml><zero>0</zero><zero2>0</zero2></xml>'; |
166
|
|
|
$this->assertEquals($expected, $result); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|