1
|
|
|
<?php |
2
|
|
|
namespace ChadicusTest\DOM; |
3
|
|
|
|
4
|
|
|
use Chadicus\Util\DOMDocument; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Unit tests for the \Chadicus\Util\DOMDocument class. |
8
|
|
|
* |
9
|
|
|
* @coversDefaultClass \Chadicus\Util\DOMDocument |
10
|
|
|
* @covers ::<private> |
11
|
|
|
*/ |
12
|
|
|
final class DOMDocumentTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Verify basic behavior of fromArray(). |
16
|
|
|
* |
17
|
|
|
* @test |
18
|
|
|
* @covers ::fromArray |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function fromArraySimpleStructure() |
23
|
|
|
{ |
24
|
|
|
$document = DOMDocument::fromArray(include __DIR__ . '/_files/simple.php'); |
25
|
|
|
$document->formatOutput = true; |
26
|
|
|
$this->assertSame( |
27
|
|
|
file_get_contents(__DIR__ . '/_files/simple.xml'), |
28
|
|
|
$document->saveXml() |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Verify behavior of fromArray() with a more complex structure. |
34
|
|
|
* |
35
|
|
|
* @test |
36
|
|
|
* @covers ::fromArray |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function fromArrayComplexStructure() |
41
|
|
|
{ |
42
|
|
|
$document = DOMDocument::fromArray(include __DIR__ . '/_files/complex.php'); |
43
|
|
|
$document->formatOutput = true; |
44
|
|
|
$this->assertSame( |
45
|
|
|
file_get_contents(__DIR__ . '/_files/complex.xml'), |
46
|
|
|
$document->saveXml() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Verify behavior of addXPath() when $xpath is not a valid xpath expression. |
52
|
|
|
* |
53
|
|
|
* @test |
54
|
|
|
* @covers ::addXPath |
55
|
|
|
* @expectedException \DOMException |
56
|
|
|
* @expectedExceptionMessage XPath [1]/foo is not valid. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function addXPathInvalidExpression() |
61
|
|
|
{ |
62
|
|
|
DOMDocument::addXPath(new \DOMDocument(), '[1]/foo'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Verify behavior of fromArray() with empty array. |
67
|
|
|
* |
68
|
|
|
* @test |
69
|
|
|
* @covers ::fromArray |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function fromArrayEmpty() |
74
|
|
|
{ |
75
|
|
|
$document = DOMDocument::fromArray([]); |
76
|
|
|
$document->formatOutput = true; |
77
|
|
|
$this->assertSame( |
78
|
|
|
"<?xml version=\"1.0\"?>\n", |
79
|
|
|
$document->saveXml() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Verify behavior of fromArray() with single element with attribute. |
85
|
|
|
* |
86
|
|
|
* @test |
87
|
|
|
* @covers ::fromArray |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function fromArraySingleElementWithAttribute() |
92
|
|
|
{ |
93
|
|
|
$document = DOMDocument::fromArray(['foo' => ['@id' => 'bar']]); |
94
|
|
|
$document->formatOutput = true; |
95
|
|
|
$this->assertSame( |
96
|
|
|
"<?xml version=\"1.0\"?>\n<foo id=\"bar\"/>\n", |
97
|
|
|
$document->saveXml() |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Verify basic behavior of toArray(). |
103
|
|
|
* |
104
|
|
|
* @test |
105
|
|
|
* @covers ::toArray |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function toArraySimpleStructure() |
110
|
|
|
{ |
111
|
|
|
$document = new \DOMDocument(); |
112
|
|
|
$document->load(__DIR__ . '/_files/simple.xml'); |
113
|
|
|
$array = DOMDocument::toArray($document); |
114
|
|
|
$expected = include __DIR__ . '/_files/simple.php'; |
115
|
|
|
$this->assertSame($expected, $array); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Verify behavior of toArray() with a more complex structure. |
120
|
|
|
* |
121
|
|
|
* @test |
122
|
|
|
* @covers ::toArray |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function toArrayComplexStructure() |
127
|
|
|
{ |
128
|
|
|
$document = new \DOMDocument(); |
129
|
|
|
$document->load(__DIR__ . '/_files/complex.xml'); |
130
|
|
|
$array = DOMDocument::toArray($document); |
131
|
|
|
$expected = include __DIR__ . '/_files/complex.php'; |
132
|
|
|
$this->assertSame($expected, $array); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Verify basic behavior of addXPath() with attribute |
137
|
|
|
* |
138
|
|
|
* @test |
139
|
|
|
* @covers ::addXPath |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function addXPathWithAttribute() |
144
|
|
|
{ |
145
|
|
|
$xpath = '/path/to/node/with/@attribute'; |
146
|
|
|
$document = new \DOMDocument(); |
147
|
|
|
DOMDocument::addXPath($document, $xpath, 'value'); |
148
|
|
|
$expected = <<<XML |
149
|
|
|
<?xml version="1.0"?> |
150
|
|
|
<path><to><node><with attribute="value"/></node></to></path> |
151
|
|
|
|
152
|
|
|
XML; |
153
|
|
|
$this->assertSame($expected, $document->saveXml()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Verify basic behavior of addXPath(). |
158
|
|
|
* |
159
|
|
|
* @test |
160
|
|
|
* @covers ::addXPath |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function addXPathExistingElement() |
165
|
|
|
{ |
166
|
|
|
$xpath = '/path/to/node'; |
167
|
|
|
$document = new \DOMDocument(); |
168
|
|
|
DOMDocument::addXPath($document, $xpath, 'value'); |
169
|
|
|
$expected = <<<XML |
170
|
|
|
<?xml version="1.0"?> |
171
|
|
|
<path><to><node>value</node></to></path> |
172
|
|
|
|
173
|
|
|
XML; |
174
|
|
|
$this->assertSame($expected, $document->saveXml()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Verify behavior of addXPath() when element exists. |
179
|
|
|
* |
180
|
|
|
* @test |
181
|
|
|
* @covers ::addXPath |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
|
public function addXPath() |
186
|
|
|
{ |
187
|
|
|
$xpath = '/path/to/node'; |
188
|
|
|
$document = new \DOMDocument(); |
189
|
|
|
DOMDocument::addXPath($document, $xpath, 'value'); |
190
|
|
|
$this->assertSame("<?xml version=\"1.0\"?>\n<path><to><node>value</node></to></path>\n", $document->saveXml()); |
191
|
|
|
DOMDocument::addXPath($document, $xpath, 'new value'); |
192
|
|
|
$this->assertSame("<?xml version=\"1.0\"?>\n<path><to><node>new value</node></to></path>\n", $document->saveXml()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Verify behavior of addXPath() when xpath contains child element with value. |
197
|
|
|
* |
198
|
|
|
* @test |
199
|
|
|
* @covers ::addXPath |
200
|
|
|
* |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
|
|
public function addXPathChildElementWithValue() |
204
|
|
|
{ |
205
|
|
|
$xpath = '/root/parent[child1 = "child 1 value"]/child2'; |
206
|
|
|
$document = new \DOMDocument(); |
207
|
|
|
DOMDocument::addXPath($document, $xpath, 'child 2 value'); |
208
|
|
|
$document->formatOutput = true; |
209
|
|
|
$expected = <<<XML |
210
|
|
|
<?xml version="1.0"?> |
211
|
|
|
<root> |
212
|
|
|
<parent> |
213
|
|
|
<child1>child 1 value</child1> |
214
|
|
|
<child2>child 2 value</child2> |
215
|
|
|
</parent> |
216
|
|
|
</root> |
217
|
|
|
|
218
|
|
|
XML; |
219
|
|
|
$this->assertSame($expected, $document->saveXml()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Verify behavior of addXPath() when xpath specifies the numeric index. |
224
|
|
|
* |
225
|
|
|
* @test |
226
|
|
|
* @covers ::addXPath |
227
|
|
|
* |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
|
|
public function addXPathWithNumericIndex() |
231
|
|
|
{ |
232
|
|
|
$xpath = '/root/parent/child[3]'; |
233
|
|
|
$document = new \DOMDocument(); |
234
|
|
|
DOMDocument::addXPath($document, $xpath, 'value'); |
235
|
|
|
$document->formatOutput = true; |
236
|
|
|
$expected = <<<XML |
237
|
|
|
<?xml version="1.0"?> |
238
|
|
|
<root> |
239
|
|
|
<parent> |
240
|
|
|
<child/> |
241
|
|
|
<child/> |
242
|
|
|
<child>value</child> |
243
|
|
|
</parent> |
244
|
|
|
</root> |
245
|
|
|
|
246
|
|
|
XML; |
247
|
|
|
$this->assertSame($expected, $document->saveXml()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Verify behavior of addXPath() when xpath specifies the numeric index that exists. |
252
|
|
|
* |
253
|
|
|
* @test |
254
|
|
|
* @covers ::addXPath |
255
|
|
|
* |
256
|
|
|
* @return void |
257
|
|
|
*/ |
258
|
|
|
public function addXPathWithExistingNumericIndex() |
259
|
|
|
{ |
260
|
|
|
$document = new \DOMDocument(); |
261
|
|
|
DOMDocument::addXPath($document, '/root/parent/child[3]', 'value 3'); |
262
|
|
|
DOMDocument::addXPath($document, '/root/parent/child[2]', 'value 2'); |
263
|
|
|
$document->formatOutput = true; |
264
|
|
|
$expected = <<<XML |
265
|
|
|
<?xml version="1.0"?> |
266
|
|
|
<root> |
267
|
|
|
<parent> |
268
|
|
|
<child/> |
269
|
|
|
<child>value 2</child> |
270
|
|
|
<child>value 3</child> |
271
|
|
|
</parent> |
272
|
|
|
</root> |
273
|
|
|
|
274
|
|
|
XML; |
275
|
|
|
$this->assertSame($expected, $document->saveXml()); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Verify behavior of addXPath() when xpath specifies attribute with value. |
280
|
|
|
* |
281
|
|
|
* @test |
282
|
|
|
* @covers ::addXPath |
283
|
|
|
* |
284
|
|
|
* @return void |
285
|
|
|
*/ |
286
|
|
|
public function addXPathWithAttributeValue() |
287
|
|
|
{ |
288
|
|
|
$xpath = "/root/parent[@attr='foo']/child"; |
289
|
|
|
$document = new \DOMDocument(); |
290
|
|
|
DOMDocument::addXPath($document, $xpath, 'value'); |
291
|
|
|
$document->formatOutput = true; |
292
|
|
|
$expected = <<<XML |
293
|
|
|
<?xml version="1.0"?> |
294
|
|
|
<root> |
295
|
|
|
<parent attr="foo"> |
296
|
|
|
<child>value</child> |
297
|
|
|
</parent> |
298
|
|
|
</root> |
299
|
|
|
|
300
|
|
|
XML; |
301
|
|
|
$this->assertSame($expected, $document->saveXml()); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Verify behavior of addXPath() when xpath specifies attribute with value and attribute exists. |
306
|
|
|
* |
307
|
|
|
* @test |
308
|
|
|
* @covers ::addXPath |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
|
|
public function addXPathWithAttributeValueExists() |
313
|
|
|
{ |
314
|
|
|
$document = new \DOMDocument(); |
315
|
|
|
$document->loadXml('<root><parent attr="foo" /></root>'); |
316
|
|
|
|
317
|
|
|
$xpath = "/root/parent[@attr='foo']/child"; |
318
|
|
|
DOMDocument::addXPath($document, $xpath, 'value'); |
319
|
|
|
$document->formatOutput = true; |
320
|
|
|
$expected = <<<XML |
321
|
|
|
<?xml version="1.0"?> |
322
|
|
|
<root> |
323
|
|
|
<parent attr="foo"> |
324
|
|
|
<child>value</child> |
325
|
|
|
</parent> |
326
|
|
|
</root> |
327
|
|
|
|
328
|
|
|
XML; |
329
|
|
|
$this->assertSame($expected, $document->saveXml()); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Verify value is encoding when calling addXPath(). |
334
|
|
|
* |
335
|
|
|
* @test |
336
|
|
|
* @covers ::addXPath |
337
|
|
|
* |
338
|
|
|
* @return void |
339
|
|
|
*/ |
340
|
|
|
public function addXPathEncodesValue() |
341
|
|
|
{ |
342
|
|
|
$document = new \DOMDocument(); |
343
|
|
|
$document->loadXml('<root><parent /></root>'); |
344
|
|
|
|
345
|
|
|
$xpath = "/root/parent/child"; |
346
|
|
|
DOMDocument::addXPath($document, $xpath, 'this & that'); |
347
|
|
|
$document->formatOutput = true; |
348
|
|
|
$expected = <<<XML |
349
|
|
|
<?xml version="1.0"?> |
350
|
|
|
<root> |
351
|
|
|
<parent> |
352
|
|
|
<child>this & that</child> |
353
|
|
|
</parent> |
354
|
|
|
</root> |
355
|
|
|
|
356
|
|
|
XML; |
357
|
|
|
$this->assertSame($expected, $document->saveXml()); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|