1<?php
2
3namespace Sabre\Xml\Element;
4
5use Sabre\Xml\Reader;
6use Sabre\Xml\Writer;
7
8class ElementsTest extends \PHPUnit_Framework_TestCase {
9
10    function testDeserialize() {
11
12        $input = <<<BLA
13<?xml version="1.0"?>
14<root xmlns="http://sabredav.org/ns">
15  <listThingy>
16    <elem1 />
17    <elem2 />
18    <elem3 />
19    <elem4 attr="val" />
20    <elem5>content</elem5>
21    <elem6><subnode /></elem6>
22  </listThingy>
23  <listThingy />
24  <otherThing>
25    <elem1 />
26    <elem2 />
27    <elem3 />
28  </otherThing>
29</root>
30BLA;
31
32        $reader = new Reader();
33        $reader->elementMap = [
34            '{http://sabredav.org/ns}listThingy' => 'Sabre\\Xml\\Element\\Elements',
35        ];
36        $reader->xml($input);
37
38        $output = $reader->parse();
39
40        $this->assertEquals([
41            'name'  => '{http://sabredav.org/ns}root',
42            'value' => [
43                [
44                    'name'  => '{http://sabredav.org/ns}listThingy',
45                    'value' => [
46                        '{http://sabredav.org/ns}elem1',
47                        '{http://sabredav.org/ns}elem2',
48                        '{http://sabredav.org/ns}elem3',
49                        '{http://sabredav.org/ns}elem4',
50                        '{http://sabredav.org/ns}elem5',
51                        '{http://sabredav.org/ns}elem6',
52                    ],
53                    'attributes' => [],
54                ],
55                [
56                    'name'       => '{http://sabredav.org/ns}listThingy',
57                    'value'      => [],
58                    'attributes' => [],
59                ],
60                [
61                    'name'  => '{http://sabredav.org/ns}otherThing',
62                    'value' => [
63                        [
64                            'name'       => '{http://sabredav.org/ns}elem1',
65                            'value'      => null,
66                            'attributes' => [],
67                        ],
68                        [
69                            'name'       => '{http://sabredav.org/ns}elem2',
70                            'value'      => null,
71                            'attributes' => [],
72                        ],
73                        [
74                            'name'       => '{http://sabredav.org/ns}elem3',
75                            'value'      => null,
76                            'attributes' => [],
77                        ],
78                    ],
79                    'attributes' => [],
80                ],
81            ],
82            'attributes' => [],
83        ], $output);
84
85    }
86
87    function testSerialize() {
88
89        $value = [
90            '{http://sabredav.org/ns}elem1',
91            '{http://sabredav.org/ns}elem2',
92            '{http://sabredav.org/ns}elem3',
93            '{http://sabredav.org/ns}elem4',
94            '{http://sabredav.org/ns}elem5',
95            '{http://sabredav.org/ns}elem6',
96        ];
97
98        $writer = new Writer();
99        $writer->namespaceMap = [
100            'http://sabredav.org/ns' => null
101        ];
102        $writer->openMemory();
103        $writer->startDocument('1.0');
104        $writer->setIndent(true);
105        $writer->write([
106            '{http://sabredav.org/ns}root' => new Elements($value),
107        ]);
108
109        $output = $writer->outputMemory();
110
111        $expected = <<<XML
112<?xml version="1.0"?>
113<root xmlns="http://sabredav.org/ns">
114 <elem1/>
115 <elem2/>
116 <elem3/>
117 <elem4/>
118 <elem5/>
119 <elem6/>
120</root>
121
122XML;
123
124        $this->assertEquals($expected, $output);
125
126
127    }
128
129}
130