1<?php
2
3namespace Sabre\Xml;
4
5class ReaderTest extends \PHPUnit_Framework_TestCase {
6
7    function testGetClark() {
8
9        $input = <<<BLA
10<?xml version="1.0"?>
11<root xmlns="http://sabredav.org/ns" />
12BLA;
13        $reader = new Reader();
14        $reader->xml($input);
15
16        $reader->next();
17
18        $this->assertEquals('{http://sabredav.org/ns}root', $reader->getClark());
19
20    }
21
22    function testGetClarkNoNS() {
23
24        $input = <<<BLA
25<?xml version="1.0"?>
26<root />
27BLA;
28        $reader = new Reader();
29        $reader->xml($input);
30
31        $reader->next();
32
33        $this->assertEquals('{}root', $reader->getClark());
34
35    }
36
37    function testGetClarkNotOnAnElement() {
38
39        $input = <<<BLA
40<?xml version="1.0"?>
41<root />
42BLA;
43        $reader = new Reader();
44        $reader->xml($input);
45
46        $this->assertNull($reader->getClark());
47    }
48
49    function testSimple() {
50
51        $input = <<<BLA
52<?xml version="1.0"?>
53<root xmlns="http://sabredav.org/ns">
54  <elem1 attr="val" />
55  <elem2>
56    <elem3>Hi!</elem3>
57  </elem2>
58</root>
59BLA;
60
61        $reader = new Reader();
62        $reader->xml($input);
63
64        $output = $reader->parse();
65
66        $expected = [
67            'name'  => '{http://sabredav.org/ns}root',
68            'value' => [
69                [
70                    'name'       => '{http://sabredav.org/ns}elem1',
71                    'value'      => null,
72                    'attributes' => [
73                        'attr' => 'val',
74                    ],
75                ],
76                [
77                    'name'  => '{http://sabredav.org/ns}elem2',
78                    'value' => [
79                        [
80                            'name'       => '{http://sabredav.org/ns}elem3',
81                            'value'      => 'Hi!',
82                            'attributes' => [],
83                        ],
84                    ],
85                    'attributes' => [],
86                ],
87
88            ],
89            'attributes' => [],
90
91        ];
92
93        $this->assertEquals($expected, $output);
94
95    }
96
97    function testCDATA() {
98
99        $input = <<<BLA
100<?xml version="1.0"?>
101<root xmlns="http://sabredav.org/ns">
102  <foo><![CDATA[bar]]></foo>
103</root>
104BLA;
105
106        $reader = new Reader();
107        $reader->xml($input);
108
109        $output = $reader->parse();
110
111        $expected = [
112            'name'  => '{http://sabredav.org/ns}root',
113            'value' => [
114                [
115                    'name'       => '{http://sabredav.org/ns}foo',
116                    'value'      => 'bar',
117                    'attributes' => [],
118                ],
119
120            ],
121            'attributes' => [],
122
123        ];
124
125        $this->assertEquals($expected, $output);
126
127    }
128
129    function testSimpleNamespacedAttribute() {
130
131        $input = <<<BLA
132<?xml version="1.0"?>
133<root xmlns="http://sabredav.org/ns" xmlns:foo="urn:foo">
134  <elem1 foo:attr="val" />
135</root>
136BLA;
137
138        $reader = new Reader();
139        $reader->xml($input);
140
141        $output = $reader->parse();
142
143        $expected = [
144            'name'  => '{http://sabredav.org/ns}root',
145            'value' => [
146                [
147                    'name'       => '{http://sabredav.org/ns}elem1',
148                    'value'      => null,
149                    'attributes' => [
150                        '{urn:foo}attr' => 'val',
151                    ],
152                ],
153            ],
154            'attributes' => [],
155        ];
156
157        $this->assertEquals($expected, $output);
158
159    }
160
161    function testMappedElement() {
162
163        $input = <<<BLA
164<?xml version="1.0"?>
165<root xmlns="http://sabredav.org/ns">
166  <elem1 />
167</root>
168BLA;
169
170        $reader = new Reader();
171        $reader->elementMap = [
172            '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Mock'
173        ];
174        $reader->xml($input);
175
176        $output = $reader->parse();
177
178        $expected = [
179            'name'  => '{http://sabredav.org/ns}root',
180            'value' => [
181                [
182                    'name'       => '{http://sabredav.org/ns}elem1',
183                    'value'      => 'foobar',
184                    'attributes' => [],
185                ],
186            ],
187            'attributes' => [],
188
189        ];
190
191        $this->assertEquals($expected, $output);
192
193    }
194
195    /**
196     * @depends testMappedElement
197     */
198    function testMappedElementCallBack() {
199
200        $input = <<<BLA
201<?xml version="1.0"?>
202<root xmlns="http://sabredav.org/ns">
203  <elem1 />
204</root>
205BLA;
206
207        $reader = new Reader();
208        $reader->elementMap = [
209            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
210                $reader->next();
211                return 'foobar';
212            }
213        ];
214        $reader->xml($input);
215
216        $output = $reader->parse();
217
218        $expected = [
219            'name'  => '{http://sabredav.org/ns}root',
220            'value' => [
221                [
222                    'name'       => '{http://sabredav.org/ns}elem1',
223                    'value'      => 'foobar',
224                    'attributes' => [],
225                ],
226            ],
227            'attributes' => [],
228
229        ];
230
231        $this->assertEquals($expected, $output);
232
233    }
234
235    /**
236     * @depends testMappedElementCallBack
237     */
238    function testReadText() {
239
240        $input = <<<BLA
241<?xml version="1.0"?>
242<root xmlns="http://sabredav.org/ns">
243  <elem1>
244    <elem2>hello </elem2>
245    <elem2>world</elem2>
246  </elem1>
247</root>
248BLA;
249
250        $reader = new Reader();
251        $reader->elementMap = [
252            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
253                return $reader->readText();
254            }
255        ];
256        $reader->xml($input);
257
258        $output = $reader->parse();
259
260        $expected = [
261            'name'  => '{http://sabredav.org/ns}root',
262            'value' => [
263                [
264                    'name'       => '{http://sabredav.org/ns}elem1',
265                    'value'      => 'hello world',
266                    'attributes' => [],
267                ],
268            ],
269            'attributes' => [],
270
271        ];
272
273        $this->assertEquals($expected, $output);
274
275    }
276
277    function testParseProblem() {
278
279        $input = <<<BLA
280<?xml version="1.0"?>
281<root xmlns="http://sabredav.org/ns">
282BLA;
283
284        $reader = new Reader();
285        $reader->elementMap = [
286            '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Mock'
287        ];
288        $reader->xml($input);
289
290        try {
291            $output = $reader->parse();
292            $this->fail('We expected a ParseException to be thrown');
293        } catch (LibXMLException $e) {
294
295            $this->assertInternalType('array', $e->getErrors());
296
297        }
298
299    }
300
301    /**
302     * @expectedException \Sabre\Xml\ParseException
303     */
304    function testBrokenParserClass() {
305
306        $input = <<<BLA
307<?xml version="1.0"?>
308<root xmlns="http://sabredav.org/ns">
309<elem1 />
310</root>
311BLA;
312
313        $reader = new Reader();
314        $reader->elementMap = [
315            '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Eater'
316        ];
317        $reader->xml($input);
318        $reader->parse();
319
320
321    }
322
323    /**
324     * Test was added for Issue #10.
325     *
326     * @expectedException Sabre\Xml\LibXMLException
327     */
328    function testBrokenXml() {
329
330        $input = <<<BLA
331<test>
332<hello>
333</hello>
334</sffsdf>
335BLA;
336
337        $reader = new Reader();
338        $reader->xml($input);
339        $reader->parse();
340
341    }
342
343    /**
344     * Test was added for Issue #45.
345     *
346     * @expectedException Sabre\Xml\LibXMLException
347     */
348    function testBrokenXml2() {
349
350        $input = <<<XML
351<?xml version="1.0" encoding="UTF-8"?>
352<definitions>
353    <collaboration>
354        <participant id="sid-A33D08EB-A2DE-448F-86FE-A2B62E98818" name="Company" processRef="sid-A0A6A196-3C9A-4C69-88F6-7ED7DDFDD264">
355            <extensionElements>
356                <signavio:signavioMetaData metaKey="bgcolor" />
357                ""Administrative w">
358                <extensionElements>
359                    <signavio:signavioMetaData metaKey="bgcolor" metaValue=""/>
360                </extensionElements>
361                </lan
362XML;
363        $reader = new Reader();
364        $reader->xml($input);
365        $reader->parse();
366
367    }
368
369
370    /**
371     * @depends testMappedElement
372     */
373    function testParseInnerTree() {
374
375        $input = <<<BLA
376<?xml version="1.0"?>
377<root xmlns="http://sabredav.org/ns">
378  <elem1>
379     <elem1 />
380  </elem1>
381</root>
382BLA;
383
384        $reader = new Reader();
385        $reader->elementMap = [
386            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
387
388                $innerTree = $reader->parseInnerTree(['{http://sabredav.org/ns}elem1' => function(Reader $reader) {
389                    $reader->next();
390                    return "foobar";
391                }]);
392
393                return $innerTree;
394            }
395        ];
396        $reader->xml($input);
397
398        $output = $reader->parse();
399
400        $expected = [
401            'name'  => '{http://sabredav.org/ns}root',
402            'value' => [
403                [
404                    'name'  => '{http://sabredav.org/ns}elem1',
405                    'value' => [
406                        [
407                            'name'       => '{http://sabredav.org/ns}elem1',
408                            'value'      => 'foobar',
409                            'attributes' => [],
410                        ]
411                    ],
412                    'attributes' => [],
413                ],
414            ],
415            'attributes' => [],
416
417        ];
418
419        $this->assertEquals($expected, $output);
420
421    }
422
423    /**
424     * @depends testParseInnerTree
425     */
426    function testParseGetElements() {
427
428        $input = <<<BLA
429<?xml version="1.0"?>
430<root xmlns="http://sabredav.org/ns">
431  <elem1>
432     <elem1 />
433  </elem1>
434</root>
435BLA;
436
437        $reader = new Reader();
438        $reader->elementMap = [
439            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
440
441                $innerTree = $reader->parseGetElements(['{http://sabredav.org/ns}elem1' => function(Reader $reader) {
442                    $reader->next();
443                    return "foobar";
444                }]);
445
446                return $innerTree;
447            }
448        ];
449        $reader->xml($input);
450
451        $output = $reader->parse();
452
453        $expected = [
454            'name'  => '{http://sabredav.org/ns}root',
455            'value' => [
456                [
457                    'name'  => '{http://sabredav.org/ns}elem1',
458                    'value' => [
459                        [
460                            'name'       => '{http://sabredav.org/ns}elem1',
461                            'value'      => 'foobar',
462                            'attributes' => [],
463                        ]
464                    ],
465                    'attributes' => [],
466                ],
467            ],
468            'attributes' => [],
469
470        ];
471
472        $this->assertEquals($expected, $output);
473
474    }
475
476    /**
477     * @depends testParseInnerTree
478     */
479    function testParseGetElementsNoElements() {
480
481        $input = <<<BLA
482<?xml version="1.0"?>
483<root xmlns="http://sabredav.org/ns">
484  <elem1>
485    hi
486  </elem1>
487</root>
488BLA;
489
490        $reader = new Reader();
491        $reader->elementMap = [
492            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
493
494                $innerTree = $reader->parseGetElements(['{http://sabredav.org/ns}elem1' => function(Reader $reader) {
495                    $reader->next();
496                    return "foobar";
497                }]);
498
499                return $innerTree;
500            }
501        ];
502        $reader->xml($input);
503
504        $output = $reader->parse();
505
506        $expected = [
507            'name'  => '{http://sabredav.org/ns}root',
508            'value' => [
509                [
510                    'name'       => '{http://sabredav.org/ns}elem1',
511                    'value'      => [],
512                    'attributes' => [],
513                ],
514            ],
515            'attributes' => [],
516
517        ];
518
519        $this->assertEquals($expected, $output);
520
521    }
522
523
524}
525