1<?php
2
3namespace Sabre\Xml\Element;
4
5use Sabre\Xml\Reader;
6use Sabre\Xml\Writer;
7
8class KeyValueTest 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  <struct>
16    <elem1 />
17    <elem2>hi</elem2>
18    <elem3>
19       <elem4>foo</elem4>
20       <elem5>foo &amp; bar</elem5>
21    </elem3>
22    <elem6>Hi<!-- ignore me -->there</elem6>
23  </struct>
24  <struct />
25  <otherThing>
26    <elem1 />
27  </otherThing>
28</root>
29BLA;
30
31        $reader = new Reader();
32        $reader->elementMap = [
33            '{http://sabredav.org/ns}struct' => 'Sabre\\Xml\\Element\\KeyValue',
34        ];
35        $reader->xml($input);
36
37        $output = $reader->parse();
38
39        $this->assertEquals([
40            'name'  => '{http://sabredav.org/ns}root',
41            'value' => [
42                [
43                    'name'  => '{http://sabredav.org/ns}struct',
44                    'value' => [
45                        '{http://sabredav.org/ns}elem1' => null,
46                        '{http://sabredav.org/ns}elem2' => 'hi',
47                        '{http://sabredav.org/ns}elem3' => [
48                            [
49                                'name'       => '{http://sabredav.org/ns}elem4',
50                                'value'      => 'foo',
51                                'attributes' => [],
52                            ],
53                            [
54                                'name'       => '{http://sabredav.org/ns}elem5',
55                                'value'      => 'foo & bar',
56                                'attributes' => [],
57                            ],
58                        ],
59                        '{http://sabredav.org/ns}elem6' => 'Hithere',
60                    ],
61                    'attributes' => [],
62                ],
63                [
64                    'name'       => '{http://sabredav.org/ns}struct',
65                    'value'      => [],
66                    'attributes' => [],
67                ],
68                [
69                    'name'  => '{http://sabredav.org/ns}otherThing',
70                    'value' => [
71                        [
72                            'name'       => '{http://sabredav.org/ns}elem1',
73                            'value'      => null,
74                            'attributes' => [],
75                        ],
76                    ],
77                    'attributes' => [],
78                ],
79            ],
80            'attributes' => [],
81        ], $output);
82
83    }
84
85    /**
86     * This test was added to find out why an element gets eaten by the
87     * SabreDAV MKCOL parser.
88     */
89    function testElementEater() {
90
91        $input = <<<BLA
92<?xml version="1.0"?>
93<mkcol xmlns="DAV:">
94  <set>
95    <prop>
96        <resourcetype><collection /></resourcetype>
97        <displayname>bla</displayname>
98    </prop>
99  </set>
100</mkcol>
101BLA;
102
103        $reader = new Reader();
104        $reader->elementMap = [
105            '{DAV:}set'          => 'Sabre\\Xml\\Element\\KeyValue',
106            '{DAV:}prop'         => 'Sabre\\Xml\\Element\\KeyValue',
107            '{DAV:}resourcetype' => 'Sabre\\Xml\\Element\\Elements',
108        ];
109        $reader->xml($input);
110
111        $expected = [
112            'name'  => '{DAV:}mkcol',
113            'value' => [
114                [
115                    'name'  => '{DAV:}set',
116                    'value' => [
117                        '{DAV:}prop' => [
118                            '{DAV:}resourcetype' => [
119                                '{DAV:}collection',
120                            ],
121                            '{DAV:}displayname' => 'bla',
122                        ],
123                    ],
124                    'attributes' => [],
125                ],
126            ],
127            'attributes' => [],
128        ];
129
130        $this->assertEquals($expected, $reader->parse());
131
132    }
133
134
135    function testSerialize() {
136
137        $value = [
138            '{http://sabredav.org/ns}elem1' => null,
139            '{http://sabredav.org/ns}elem2' => 'textValue',
140            '{http://sabredav.org/ns}elem3' => [
141                '{http://sabredav.org/ns}elem4' => 'text2',
142                '{http://sabredav.org/ns}elem5' => null,
143            ],
144            '{http://sabredav.org/ns}elem6' => 'text3',
145        ];
146
147        $writer = new Writer();
148        $writer->namespaceMap = [
149            'http://sabredav.org/ns' => null
150        ];
151        $writer->openMemory();
152        $writer->startDocument('1.0');
153        $writer->setIndent(true);
154        $writer->write([
155            '{http://sabredav.org/ns}root' => new KeyValue($value),
156        ]);
157
158        $output = $writer->outputMemory();
159
160        $expected = <<<XML
161<?xml version="1.0"?>
162<root xmlns="http://sabredav.org/ns">
163 <elem1/>
164 <elem2>textValue</elem2>
165 <elem3>
166  <elem4>text2</elem4>
167  <elem5/>
168 </elem3>
169 <elem6>text3</elem6>
170</root>
171
172XML;
173
174        $this->assertEquals($expected, $output);
175
176    }
177
178    /**
179     * I discovered that when there's no whitespace between elements, elements
180     * can get skipped.
181     */
182    function testElementSkipProblem() {
183
184        $input = <<<BLA
185<?xml version="1.0" encoding="utf-8"?>
186<root xmlns="http://sabredav.org/ns">
187<elem3>val3</elem3><elem4>val4</elem4><elem5>val5</elem5></root>
188BLA;
189
190        $reader = new Reader();
191        $reader->elementMap = [
192            '{http://sabredav.org/ns}root' => 'Sabre\\Xml\\Element\\KeyValue',
193        ];
194        $reader->xml($input);
195
196        $output = $reader->parse();
197
198        $this->assertEquals([
199            'name'  => '{http://sabredav.org/ns}root',
200            'value' => [
201                '{http://sabredav.org/ns}elem3' => 'val3',
202                '{http://sabredav.org/ns}elem4' => 'val4',
203                '{http://sabredav.org/ns}elem5' => 'val5',
204            ],
205            'attributes' => [],
206        ], $output);
207
208    }
209
210}
211