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 testParseClarkNotation() {
202
203        $this->assertEquals([
204            'http://sabredav.org/ns',
205            'elem',
206        ], Service::parseClarkNotation('{http://sabredav.org/ns}elem'));
207
208    }
209
210    /**
211     * @expectedException \InvalidArgumentException
212     */
213    function testParseClarkNotationFail() {
214
215        Service::parseClarkNotation('http://sabredav.org/ns}elem');
216
217    }
218
219}
220