Passed
Pull Request — master (#9)
by Chad
01:27
created

DOMDocumentTest::fromArrayEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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(
193
            "<?xml version=\"1.0\"?>\n<path><to><node>new value</node></to></path>\n",
194
            $document->saveXml()
195
        );
196
    }
197
198
    /**
199
     * Verify behavior of addXPath() when xpath contains child element with value.
200
     *
201
     * @test
202
     * @covers ::addXPath
203
     *
204
     * @return void
205
     */
206
    public function addXPathChildElementWithValue()
207
    {
208
        $xpath = '/root/parent[child1 = "child 1 value"]/child2';
209
        $document = new \DOMDocument();
210
        DOMDocument::addXPath($document, $xpath, 'child 2 value');
211
        $document->formatOutput = true;
212
        $expected = <<<XML
213
<?xml version="1.0"?>
214
<root>
215
  <parent>
216
    <child1>child 1 value</child1>
217
    <child2>child 2 value</child2>
218
  </parent>
219
</root>
220
221
XML;
222
        $this->assertSame($expected, $document->saveXml());
223
    }
224
225
    /**
226
     * Verify behavior of addXPath() when xpath specifies the numeric index.
227
     *
228
     * @test
229
     * @covers ::addXPath
230
     *
231
     * @return void
232
     */
233
    public function addXPathWithNumericIndex()
234
    {
235
        $xpath = '/root/parent/child[3]';
236
        $document = new \DOMDocument();
237
        DOMDocument::addXPath($document, $xpath, 'value');
238
        $document->formatOutput = true;
239
        $expected = <<<XML
240
<?xml version="1.0"?>
241
<root>
242
  <parent>
243
    <child/>
244
    <child/>
245
    <child>value</child>
246
  </parent>
247
</root>
248
249
XML;
250
        $this->assertSame($expected, $document->saveXml());
251
    }
252
253
    /**
254
     * Verify behavior of addXPath() when xpath specifies the numeric index that exists.
255
     *
256
     * @test
257
     * @covers ::addXPath
258
     *
259
     * @return void
260
     */
261
    public function addXPathWithExistingNumericIndex()
262
    {
263
        $document = new \DOMDocument();
264
        DOMDocument::addXPath($document, '/root/parent/child[3]', 'value 3');
265
        DOMDocument::addXPath($document, '/root/parent/child[2]', 'value 2');
266
        $document->formatOutput = true;
267
        $expected = <<<XML
268
<?xml version="1.0"?>
269
<root>
270
  <parent>
271
    <child/>
272
    <child>value 2</child>
273
    <child>value 3</child>
274
  </parent>
275
</root>
276
277
XML;
278
        $this->assertSame($expected, $document->saveXml());
279
    }
280
281
    /**
282
     * Verify behavior of addXPath() when xpath specifies attribute with value.
283
     *
284
     * @test
285
     * @covers ::addXPath
286
     *
287
     * @return void
288
     */
289
    public function addXPathWithAttributeValue()
290
    {
291
        $xpath = "/root/parent[@attr='foo']/child";
292
        $document = new \DOMDocument();
293
        DOMDocument::addXPath($document, $xpath, 'value');
294
        $document->formatOutput = true;
295
        $expected = <<<XML
296
<?xml version="1.0"?>
297
<root>
298
  <parent attr="foo">
299
    <child>value</child>
300
  </parent>
301
</root>
302
303
XML;
304
        $this->assertSame($expected, $document->saveXml());
305
    }
306
307
    /**
308
     * Verify behavior of addXPath() when xpath specifies attribute with value and attribute exists.
309
     *
310
     * @test
311
     * @covers ::addXPath
312
     *
313
     * @return void
314
     */
315
    public function addXPathWithAttributeValueExists()
316
    {
317
        $document = new \DOMDocument();
318
        $document->loadXml('<root><parent attr="foo" /></root>');
319
320
        $xpath = "/root/parent[@attr='foo']/child";
321
        DOMDocument::addXPath($document, $xpath, 'value');
322
        $document->formatOutput = true;
323
        $expected = <<<XML
324
<?xml version="1.0"?>
325
<root>
326
  <parent attr="foo">
327
    <child>value</child>
328
  </parent>
329
</root>
330
331
XML;
332
        $this->assertSame($expected, $document->saveXml());
333
    }
334
335
    /**
336
     * Verify value is encoding when calling addXPath().
337
     *
338
     * @test
339
     * @covers ::addXPath
340
     *
341
     * @return void
342
     */
343
    public function addXPathEncodesValue()
344
    {
345
        $document = new \DOMDocument();
346
        $document->loadXml('<root><parent /></root>');
347
348
        $xpath = "/root/parent/child";
349
        DOMDocument::addXPath($document, $xpath, 'this & that');
350
        $document->formatOutput = true;
351
        $expected = <<<XML
352
<?xml version="1.0"?>
353
<root>
354
  <parent>
355
    <child>this &amp; that</child>
356
  </parent>
357
</root>
358
359
XML;
360
        $this->assertSame($expected, $document->saveXml());
361
    }
362
}
363