1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\XmlUtils\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Graze\XmlUtils\XmlConverter; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use SimpleXMLElement; |
8
|
|
|
|
9
|
|
|
class XmlConverterTest extends PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @return mixed[] |
13
|
|
|
*/ |
14
|
|
|
public function addArrayAsChildrenDataProvider() |
15
|
|
|
{ |
16
|
|
|
$data = []; |
17
|
|
|
|
18
|
|
|
// Basic test |
19
|
|
|
$array = [ |
20
|
|
|
'child' => 123 |
21
|
|
|
]; |
22
|
|
|
$expectedChildXml = '<child>123</child>'; |
23
|
|
|
$data[] = [$array, $expectedChildXml]; |
24
|
|
|
|
25
|
|
|
// Attributes |
26
|
|
|
$array = [ |
27
|
|
|
'child' => [ |
28
|
|
|
'@attributes' => [ |
29
|
|
|
'id' => '1', |
30
|
|
|
'name' => 'foo' |
31
|
|
|
] |
32
|
|
|
] |
33
|
|
|
]; |
34
|
|
|
$expectedChildXml = '<child id="1" name="foo"/>'; |
35
|
|
|
$data[] = [$array, $expectedChildXml]; |
36
|
|
|
|
37
|
|
|
// Attibute with value |
38
|
|
|
$array = [ |
39
|
|
|
'child' => [ |
40
|
|
|
'@attributes' => [ |
41
|
|
|
'id' => '1' |
42
|
|
|
], |
43
|
|
|
'@value' => 123 |
44
|
|
|
] |
45
|
|
|
]; |
46
|
|
|
$expectedChildXml = '<child id="1">123</child>'; |
47
|
|
|
$data[] = [$array, $expectedChildXml]; |
48
|
|
|
|
49
|
|
|
// Multiple children |
50
|
|
|
$array = [ |
51
|
|
|
'child1' => 111, |
52
|
|
|
'child2' => 222 |
53
|
|
|
]; |
54
|
|
|
$expectedChildXml = '<child1>111</child1><child2>222</child2>'; |
55
|
|
|
$data[] = [$array, $expectedChildXml]; |
56
|
|
|
|
57
|
|
|
// Multiple children with the name node name |
58
|
|
|
$array = [ |
59
|
|
|
'child' => [ |
60
|
|
|
[ |
61
|
|
|
'item' => 111 |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'item' => 222 |
65
|
|
|
] |
66
|
|
|
] |
67
|
|
|
]; |
68
|
|
|
$expectedChildXml = '<child><item>111</item></child><child><item>222</item></child>'; |
69
|
|
|
$data[] = [$array, $expectedChildXml]; |
70
|
|
|
|
71
|
|
|
// Ignore empty value |
72
|
|
|
$array = [ |
73
|
|
|
'child1' => 111, |
74
|
|
|
'child2' => '' |
75
|
|
|
]; |
76
|
|
|
$expectedChildXml = '<child1>111</child1>'; |
77
|
|
|
$data[] = [$array, $expectedChildXml]; |
78
|
|
|
|
79
|
|
|
// Complex example |
80
|
|
|
$array = [ |
81
|
|
|
'child1' => 111, |
82
|
|
|
'child2' => [ |
83
|
|
|
'item' => [ |
84
|
|
|
[ |
85
|
|
|
'@attributes' => [ |
86
|
|
|
'id' => 123 |
87
|
|
|
] |
88
|
|
|
], |
89
|
|
|
[ |
90
|
|
|
'property' => 456 |
91
|
|
|
] |
92
|
|
|
] |
93
|
|
|
] |
94
|
|
|
]; |
95
|
|
|
$expectedChildXml = '<child1>111</child1><child2><item id="123"/><item><property>456</property></item></child2>'; |
96
|
|
|
$data[] = [$array, $expectedChildXml]; |
97
|
|
|
|
98
|
|
|
return $data; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @dataProvider addArrayAsChildrenDataProvider |
103
|
|
|
* @param array $array |
104
|
|
|
* @param string $expectedChildXml |
105
|
|
|
*/ |
106
|
|
|
public function testAddArrayAsChildren(array $array, $expectedChildXml) |
107
|
|
|
{ |
108
|
|
|
$templateXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>%s</root>\n"; |
109
|
|
|
|
110
|
|
|
$expectedXml = sprintf($templateXml, $expectedChildXml); |
111
|
|
|
|
112
|
|
|
$xmlElement = new SimpleXMLElement(sprintf($templateXml, '')); |
113
|
|
|
|
114
|
|
|
$xmlConverter = new XmlConverter(); |
115
|
|
|
$xmlConverter->addArrayAsChildren($array, $xmlElement); |
116
|
|
|
|
117
|
|
|
$this->assertEquals($expectedXml, $xmlElement->asXml()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testConvertToArray() |
121
|
|
|
{ |
122
|
|
|
$xml = '<?xml version="1.0" encoding="UTF-8"?><root><child1>123</child1><child2 id="1"><subchild1>456</subchild1></child2></root>'; |
123
|
|
|
|
124
|
|
|
$xmlElement = new SimpleXMLElement($xml); |
125
|
|
|
|
126
|
|
|
$expectedArray = [ |
127
|
|
|
'child1' => '123', |
128
|
|
|
'child2' => [ |
129
|
|
|
'@attributes' => [ |
130
|
|
|
'id' => '1' |
131
|
|
|
], |
132
|
|
|
'subchild1' => '456' |
133
|
|
|
] |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
$xmlConverter = new XmlConverter(); |
137
|
|
|
$array = $xmlConverter->convertToArray($xmlElement); |
138
|
|
|
|
139
|
|
|
$this->assertEquals($expectedArray, $array); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|