1<?php
2
3namespace Sabre\Xml;
4
5class ServiceTest extends \PHPUnit_Framework_TestCase {
6
7    function testGetReader() {
8
9        $elems = [
10            '{http://sabre.io/ns}test' => 'Test!',
11        ];
12
13        $util = new Service();
14        $util->elementMap = $elems;
15
16        $reader = $util->getReader();
17        $this->assertInstanceOf('Sabre\\Xml\\Reader', $reader);
18        $this->assertEquals($elems, $reader->elementMap);
19
20    }
21
22    function testGetWriter() {
23
24        $ns = [
25            'http://sabre.io/ns' => 's',
26        ];
27
28        $util = new Service();
29        $util->namespaceMap = $ns;
30
31        $writer = $util->getWriter();
32        $this->assertInstanceOf('Sabre\\Xml\\Writer', $writer);
33        $this->assertEquals($ns, $writer->namespaceMap);
34
35    }
36
37    /**
38     * @depends testGetReader
39     */
40    function testParse() {
41
42        $xml = <<<XML
43<root xmlns="http://sabre.io/ns">
44  <child>value</child>
45</root>
46XML;
47        $util = new Service();
48        $result = $util->parse($xml, null, $rootElement);
49        $this->assertEquals('{http://sabre.io/ns}root', $rootElement);
50
51        $expected = [
52            [
53                'name'       => '{http://sabre.io/ns}child',
54                'value'      => 'value',
55                'attributes' => [],
56            ]
57        ];
58
59        $this->assertEquals(
60            $expected,
61            $result
62        );
63
64    }
65
66    /**
67     * @depends testGetReader
68     */
69    function testParseStream() {
70
71        $xml = <<<XML
72<root xmlns="http://sabre.io/ns">
73  <child>value</child>
74</root>
75XML;
76        $stream = fopen('php://memory', 'r+');
77        fwrite($stream, $xml);
78        rewind($stream);
79
80        $util = new Service();
81        $result = $util->parse($stream, null, $rootElement);
82        $this->assertEquals('{http://sabre.io/ns}root', $rootElement);
83
84        $expected = [
85            [
86                'name'       => '{http://sabre.io/ns}child',
87                'value'      => 'value',
88                'attributes' => [],
89            ]
90        ];
91
92        $this->assertEquals(
93            $expected,
94            $result
95        );
96
97    }
98
99    /**
100     * @depends testGetReader
101     */
102    function testExpect() {
103
104        $xml = <<<XML
105<root xmlns="http://sabre.io/ns">
106  <child>value</child>
107</root>
108XML;
109        $util = new Service();
110        $result = $util->expect('{http://sabre.io/ns}root', $xml);
111
112        $expected = [
113            [
114                'name'       => '{http://sabre.io/ns}child',
115                'value'      => 'value',
116                'attributes' => [],
117            ]
118        ];
119
120        $this->assertEquals(
121            $expected,
122            $result
123        );
124    }
125
126    /**
127     * @depends testGetReader
128     */
129    function testExpectStream() {
130
131        $xml = <<<XML
132<root xmlns="http://sabre.io/ns">
133  <child>value</child>
134</root>
135XML;
136
137        $stream = fopen('php://memory', 'r+');
138        fwrite($stream, $xml);
139        rewind($stream);
140
141        $util = new Service();
142        $result = $util->expect('{http://sabre.io/ns}root', $stream);
143
144        $expected = [
145            [
146                'name'       => '{http://sabre.io/ns}child',
147                'value'      => 'value',
148                'attributes' => [],
149            ]
150        ];
151
152        $this->assertEquals(
153            $expected,
154            $result
155        );
156    }
157
158    /**
159     * @depends testGetReader
160     * @expectedException \Sabre\Xml\ParseException
161     */
162    function testExpectWrong() {
163
164        $xml = <<<XML
165<root xmlns="http://sabre.io/ns">
166  <child>value</child>
167</root>
168XML;
169        $util = new Service();
170        $util->expect('{http://sabre.io/ns}error', $xml);
171
172    }
173
174    /**
175     * @depends testGetWriter
176     */
177    function testWrite() {
178
179        $util = new Service();
180        $util->namespaceMap = [
181            'http://sabre.io/ns' => 's',
182        ];
183        $result = $util->write('{http://sabre.io/ns}root', [
184            '{http://sabre.io/ns}child' => 'value',
185        ]);
186
187        $expected = <<<XML
188<?xml version="1.0"?>
189<s:root xmlns:s="http://sabre.io/ns">
190 <s:child>value</s:child>
191</s:root>
192
193XML;
194        $this->assertEquals(
195            $expected,
196            $result
197        );
198
199    }
200
201    function testMapValueObject() {
202
203        $input = <<<XML
204<?xml version="1.0"?>
205<order xmlns="http://sabredav.org/ns">
206 <id>1234</id>
207 <amount>99.99</amount>
208 <description>black friday deal</description>
209 <status>
210  <id>5</id>
211  <label>processed</label>
212 </status>
213</order>
214
215XML;
216
217        $ns = 'http://sabredav.org/ns';
218        $orderService = new \Sabre\Xml\Service();
219        $orderService->mapValueObject('{' . $ns . '}order', 'Sabre\Xml\Order');
220        $orderService->mapValueObject('{' . $ns . '}status', 'Sabre\Xml\OrderStatus');
221        $orderService->namespaceMap[$ns] = null;
222
223        $order = $orderService->parse($input);
224        $expected = new Order();
225        $expected->id = 1234;
226        $expected->amount = 99.99;
227        $expected->description = 'black friday deal';
228        $expected->status = new OrderStatus();
229        $expected->status->id = 5;
230        $expected->status->label = 'processed';
231
232        $this->assertEquals($expected, $order);
233
234        $writtenXml = $orderService->writeValueObject($order);
235        $this->assertEquals($input, $writtenXml);
236    }
237
238    function testMapValueObjectArrayProperty() {
239
240        $input = <<<XML
241<?xml version="1.0"?>
242<order xmlns="http://sabredav.org/ns">
243 <id>1234</id>
244 <amount>99.99</amount>
245 <description>black friday deal</description>
246 <status>
247  <id>5</id>
248  <label>processed</label>
249 </status>
250 <link>http://example.org/</link>
251 <link>http://example.com/</link>
252</order>
253
254XML;
255
256        $ns = 'http://sabredav.org/ns';
257        $orderService = new \Sabre\Xml\Service();
258        $orderService->mapValueObject('{' . $ns . '}order', 'Sabre\Xml\Order');
259        $orderService->mapValueObject('{' . $ns . '}status', 'Sabre\Xml\OrderStatus');
260        $orderService->namespaceMap[$ns] = null;
261
262        $order = $orderService->parse($input);
263        $expected = new Order();
264        $expected->id = 1234;
265        $expected->amount = 99.99;
266        $expected->description = 'black friday deal';
267        $expected->status = new OrderStatus();
268        $expected->status->id = 5;
269        $expected->status->label = 'processed';
270        $expected->link = ['http://example.org/', 'http://example.com/'];
271
272        $this->assertEquals($expected, $order);
273
274        $writtenXml = $orderService->writeValueObject($order);
275        $this->assertEquals($input, $writtenXml);
276    }
277
278    /**
279     * @expectedException \InvalidArgumentException
280     */
281    function testWriteVoNotFound() {
282
283        $service = new Service();
284        $service->writeValueObject(new \StdClass());
285
286    }
287
288    function testParseClarkNotation() {
289
290        $this->assertEquals([
291            'http://sabredav.org/ns',
292            'elem',
293        ], Service::parseClarkNotation('{http://sabredav.org/ns}elem'));
294
295    }
296
297    /**
298     * @expectedException \InvalidArgumentException
299     */
300    function testParseClarkNotationFail() {
301
302        Service::parseClarkNotation('http://sabredav.org/ns}elem');
303
304    }
305
306}
307
308/**
309 * asset for testMapValueObject()
310 * @internal
311 */
312class Order {
313    public $id;
314    public $amount;
315    public $description;
316    public $status;
317    public $empty;
318    public $link = [];
319}
320
321/**
322 * asset for testMapValueObject()
323 * @internal
324 */
325class OrderStatus {
326    public $id;
327    public $label;
328}
329