1<?php
2
3namespace Sabre\DAV\Xml\Element;
4
5use Sabre\DAV\Xml\XmlTest;
6use Sabre\DAV\Xml\Property\Complex;
7use Sabre\DAV\Xml\Property\Href;
8
9class PropTest extends XmlTest {
10
11    function testDeserializeSimple() {
12
13        $input = <<<XML
14<?xml version="1.0"?>
15<root xmlns="DAV:">
16    <foo>bar</foo>
17</root>
18XML;
19
20        $expected = [
21            '{DAV:}foo' => 'bar',
22        ];
23
24        $this->assertDecodeProp($input, $expected);
25
26    }
27    function testDeserializeEmpty() {
28
29        $input = <<<XML
30<?xml version="1.0"?>
31<root xmlns="DAV:" />
32XML;
33
34        $expected = [
35        ];
36
37        $this->assertDecodeProp($input, $expected);
38
39    }
40    function testDeserializeComplex() {
41
42        $input = <<<XML
43<?xml version="1.0"?>
44<root xmlns="DAV:">
45    <foo><no>yes</no></foo>
46</root>
47XML;
48
49        $expected = [
50            '{DAV:}foo' => new Complex('<no xmlns="DAV:">yes</no>')
51        ];
52
53        $this->assertDecodeProp($input, $expected);
54
55    }
56    function testDeserializeCustom() {
57
58        $input = <<<XML
59<?xml version="1.0"?>
60<root xmlns="DAV:">
61    <foo><href>/hello</href></foo>
62</root>
63XML;
64
65        $expected = [
66            '{DAV:}foo' => new Href('/hello', false)
67        ];
68
69        $elementMap = [
70            '{DAV:}foo' => 'Sabre\DAV\Xml\Property\Href'
71        ];
72
73        $this->assertDecodeProp($input, $expected, $elementMap);
74
75    }
76    function testDeserializeCustomCallback() {
77
78        $input = <<<XML
79<?xml version="1.0"?>
80<root xmlns="DAV:">
81    <foo>blabla</foo>
82</root>
83XML;
84
85        $expected = [
86            '{DAV:}foo' => 'zim',
87        ];
88
89        $elementMap = [
90            '{DAV:}foo' => function($reader) {
91                $reader->next();
92                return 'zim';
93            }
94        ];
95
96        $this->assertDecodeProp($input, $expected, $elementMap);
97
98    }
99
100    /**
101     * @expectedException \LogicException
102     */
103    function testDeserializeCustomBad() {
104
105        $input = <<<XML
106<?xml version="1.0"?>
107<root xmlns="DAV:">
108    <foo>blabla</foo>
109</root>
110XML;
111
112        $expected = [];
113
114        $elementMap = [
115            '{DAV:}foo' => 'idk?',
116        ];
117
118        $this->assertDecodeProp($input, $expected, $elementMap);
119
120    }
121
122    /**
123     * @expectedException \LogicException
124     */
125    function testDeserializeCustomBadObj() {
126
127        $input = <<<XML
128<?xml version="1.0"?>
129<root xmlns="DAV:">
130    <foo>blabla</foo>
131</root>
132XML;
133
134        $expected = [];
135
136        $elementMap = [
137            '{DAV:}foo' => new \StdClass(),
138        ];
139
140        $this->assertDecodeProp($input, $expected, $elementMap);
141
142    }
143
144    function assertDecodeProp($input, array $expected, array $elementMap = []) {
145
146        $elementMap['{DAV:}root'] = 'Sabre\DAV\Xml\Element\Prop';
147
148        $result = $this->parse($input, $elementMap);
149        $this->assertInternalType('array', $result);
150        $this->assertEquals($expected, $result['value']);
151
152    }
153
154}
155