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     * @expectedException \LogicException
197     */
198    function testMappedElementBadClass() {
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' => new \StdClass()
210        ];
211        $reader->xml($input);
212
213        $reader->parse();
214    }
215
216    /**
217     * @depends testMappedElement
218     */
219    function testMappedElementCallBack() {
220
221        $input = <<<BLA
222<?xml version="1.0"?>
223<root xmlns="http://sabredav.org/ns">
224  <elem1 />
225</root>
226BLA;
227
228        $reader = new Reader();
229        $reader->elementMap = [
230            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
231                $reader->next();
232                return 'foobar';
233            }
234        ];
235        $reader->xml($input);
236
237        $output = $reader->parse();
238
239        $expected = [
240            'name'  => '{http://sabredav.org/ns}root',
241            'value' => [
242                [
243                    'name'       => '{http://sabredav.org/ns}elem1',
244                    'value'      => 'foobar',
245                    'attributes' => [],
246                ],
247            ],
248            'attributes' => [],
249
250        ];
251
252        $this->assertEquals($expected, $output);
253
254    }
255
256    /**
257     * @depends testMappedElementCallBack
258     */
259    function testMappedElementCallBackNoNamespace() {
260
261        $input = <<<BLA
262<?xml version="1.0"?>
263<root>
264  <elem1 />
265</root>
266BLA;
267
268        $reader = new Reader();
269        $reader->elementMap = [
270            'elem1' => function(Reader $reader) {
271                $reader->next();
272                return 'foobar';
273            }
274        ];
275        $reader->xml($input);
276
277        $output = $reader->parse();
278
279        $expected = [
280            'name'  => '{}root',
281            'value' => [
282                [
283                    'name'       => '{}elem1',
284                    'value'      => 'foobar',
285                    'attributes' => [],
286                ],
287            ],
288            'attributes' => [],
289
290        ];
291
292        $this->assertEquals($expected, $output);
293
294    }
295
296    /**
297     * @depends testMappedElementCallBack
298     */
299    function testReadText() {
300
301        $input = <<<BLA
302<?xml version="1.0"?>
303<root xmlns="http://sabredav.org/ns">
304  <elem1>
305    <elem2>hello </elem2>
306    <elem2>world</elem2>
307  </elem1>
308</root>
309BLA;
310
311        $reader = new Reader();
312        $reader->elementMap = [
313            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
314                return $reader->readText();
315            }
316        ];
317        $reader->xml($input);
318
319        $output = $reader->parse();
320
321        $expected = [
322            'name'  => '{http://sabredav.org/ns}root',
323            'value' => [
324                [
325                    'name'       => '{http://sabredav.org/ns}elem1',
326                    'value'      => 'hello world',
327                    'attributes' => [],
328                ],
329            ],
330            'attributes' => [],
331
332        ];
333
334        $this->assertEquals($expected, $output);
335
336    }
337
338    function testParseProblem() {
339
340        $input = <<<BLA
341<?xml version="1.0"?>
342<root xmlns="http://sabredav.org/ns">
343BLA;
344
345        $reader = new Reader();
346        $reader->elementMap = [
347            '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Mock'
348        ];
349        $reader->xml($input);
350
351        try {
352            $output = $reader->parse();
353            $this->fail('We expected a ParseException to be thrown');
354        } catch (LibXMLException $e) {
355
356            $this->assertInternalType('array', $e->getErrors());
357
358        }
359
360    }
361
362    /**
363     * @expectedException \Sabre\Xml\ParseException
364     */
365    function testBrokenParserClass() {
366
367        $input = <<<BLA
368<?xml version="1.0"?>
369<root xmlns="http://sabredav.org/ns">
370<elem1 />
371</root>
372BLA;
373
374        $reader = new Reader();
375        $reader->elementMap = [
376            '{http://sabredav.org/ns}elem1' => 'Sabre\\Xml\\Element\\Eater'
377        ];
378        $reader->xml($input);
379        $reader->parse();
380
381
382    }
383
384    /**
385     * Test was added for Issue #10.
386     *
387     * @expectedException Sabre\Xml\LibXMLException
388     */
389    function testBrokenXml() {
390
391        $input = <<<BLA
392<test>
393<hello>
394</hello>
395</sffsdf>
396BLA;
397
398        $reader = new Reader();
399        $reader->xml($input);
400        $reader->parse();
401
402    }
403
404    /**
405     * Test was added for Issue #45.
406     *
407     * @expectedException Sabre\Xml\LibXMLException
408     */
409    function testBrokenXml2() {
410
411        $input = <<<XML
412<?xml version="1.0" encoding="UTF-8"?>
413<definitions>
414    <collaboration>
415        <participant id="sid-A33D08EB-A2DE-448F-86FE-A2B62E98818" name="Company" processRef="sid-A0A6A196-3C9A-4C69-88F6-7ED7DDFDD264">
416            <extensionElements>
417                <signavio:signavioMetaData metaKey="bgcolor" />
418                ""Administrative w">
419                <extensionElements>
420                    <signavio:signavioMetaData metaKey="bgcolor" metaValue=""/>
421                </extensionElements>
422                </lan
423XML;
424        $reader = new Reader();
425        $reader->xml($input);
426        $reader->parse();
427
428    }
429
430
431    /**
432     * @depends testMappedElement
433     */
434    function testParseInnerTree() {
435
436        $input = <<<BLA
437<?xml version="1.0"?>
438<root xmlns="http://sabredav.org/ns">
439  <elem1>
440     <elem1 />
441  </elem1>
442</root>
443BLA;
444
445        $reader = new Reader();
446        $reader->elementMap = [
447            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
448
449                $innerTree = $reader->parseInnerTree(['{http://sabredav.org/ns}elem1' => function(Reader $reader) {
450                    $reader->next();
451                    return "foobar";
452                }]);
453
454                return $innerTree;
455            }
456        ];
457        $reader->xml($input);
458
459        $output = $reader->parse();
460
461        $expected = [
462            'name'  => '{http://sabredav.org/ns}root',
463            'value' => [
464                [
465                    'name'  => '{http://sabredav.org/ns}elem1',
466                    'value' => [
467                        [
468                            'name'       => '{http://sabredav.org/ns}elem1',
469                            'value'      => 'foobar',
470                            'attributes' => [],
471                        ]
472                    ],
473                    'attributes' => [],
474                ],
475            ],
476            'attributes' => [],
477
478        ];
479
480        $this->assertEquals($expected, $output);
481
482    }
483
484    /**
485     * @depends testParseInnerTree
486     */
487    function testParseGetElements() {
488
489        $input = <<<BLA
490<?xml version="1.0"?>
491<root xmlns="http://sabredav.org/ns">
492  <elem1>
493     <elem1 />
494  </elem1>
495</root>
496BLA;
497
498        $reader = new Reader();
499        $reader->elementMap = [
500            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
501
502                $innerTree = $reader->parseGetElements(['{http://sabredav.org/ns}elem1' => function(Reader $reader) {
503                    $reader->next();
504                    return "foobar";
505                }]);
506
507                return $innerTree;
508            }
509        ];
510        $reader->xml($input);
511
512        $output = $reader->parse();
513
514        $expected = [
515            'name'  => '{http://sabredav.org/ns}root',
516            'value' => [
517                [
518                    'name'  => '{http://sabredav.org/ns}elem1',
519                    'value' => [
520                        [
521                            'name'       => '{http://sabredav.org/ns}elem1',
522                            'value'      => 'foobar',
523                            'attributes' => [],
524                        ]
525                    ],
526                    'attributes' => [],
527                ],
528            ],
529            'attributes' => [],
530
531        ];
532
533        $this->assertEquals($expected, $output);
534
535    }
536
537    /**
538     * @depends testParseInnerTree
539     */
540    function testParseGetElementsNoElements() {
541
542        $input = <<<BLA
543<?xml version="1.0"?>
544<root xmlns="http://sabredav.org/ns">
545  <elem1>
546    hi
547  </elem1>
548</root>
549BLA;
550
551        $reader = new Reader();
552        $reader->elementMap = [
553            '{http://sabredav.org/ns}elem1' => function(Reader $reader) {
554
555                $innerTree = $reader->parseGetElements(['{http://sabredav.org/ns}elem1' => function(Reader $reader) {
556                    $reader->next();
557                    return "foobar";
558                }]);
559
560                return $innerTree;
561            }
562        ];
563        $reader->xml($input);
564
565        $output = $reader->parse();
566
567        $expected = [
568            'name'  => '{http://sabredav.org/ns}root',
569            'value' => [
570                [
571                    'name'       => '{http://sabredav.org/ns}elem1',
572                    'value'      => [],
573                    'attributes' => [],
574                ],
575            ],
576            'attributes' => [],
577
578        ];
579
580        $this->assertEquals($expected, $output);
581
582    }
583
584
585}
586