DOMDocumentTest::addXPathWithAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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