ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\Xml;
4 
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 
40  function testParse() {
41 
42  $xml = <<<XML
43 <root xmlns="http://sabre.io/ns">
44  <child>value</child>
45 </root>
46 XML;
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 
69  function testParseStream() {
70 
71  $xml = <<<XML
72 <root xmlns="http://sabre.io/ns">
73  <child>value</child>
74 </root>
75 XML;
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 
102  function testExpect() {
103 
104  $xml = <<<XML
105 <root xmlns="http://sabre.io/ns">
106  <child>value</child>
107 </root>
108 XML;
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 
129  function testExpectStream() {
130 
131  $xml = <<<XML
132 <root xmlns="http://sabre.io/ns">
133  <child>value</child>
134 </root>
135 XML;
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 
162  function testExpectWrong() {
163 
164  $xml = <<<XML
165 <root xmlns="http://sabre.io/ns">
166  <child>value</child>
167 </root>
168 XML;
169  $util = new Service();
170  $util->expect('{http://sabre.io/ns}error', $xml);
171 
172  }
173 
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 
193 XML;
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 
215 XML;
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 
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 
254 XML;
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;
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);
274  $writtenXml = $orderService->writeValueObject($order);
275  $this->assertEquals($input, $writtenXml);
276  }
277 
281  function testWriteVoNotFound() {
282 
283  $service = new Service();
284  $service->writeValueObject(new \StdClass());
285 
286  }
287 
289 
290  $this->assertEquals([
291  'http://sabredav.org/ns',
292  'elem',
293  ], Service::parseClarkNotation('{http://sabredav.org/ns}elem'));
294 
295  }
296 
301 
302  Service::parseClarkNotation('http://sabredav.org/ns}elem');
303 
304  }
305 
306 }
307 
312 class Order {
313  public $id;
314  public $amount;
315  public $description;
316  public $status;
317  public $empty;
318  public $link = [];
319 }
320 
325 class OrderStatus {
326  public $id;
327  public $label;
328 }
testParseStream()
testGetReader
Definition: ServiceTest.php:69
$result
testWrite()
testGetWriter
testParse()
testGetReader
Definition: ServiceTest.php:40
if(!array_key_exists('StateId', $_REQUEST)) $id
$stream
PHP stream implementation.
static http()
Fetches the global http state from ILIAS.
testExpect()
testGetReader
static parseClarkNotation($str)
Parses a clark-notation string, and returns the namespace and element name components.
Definition: Service.php:274
asset for testMapValueObject()
XML parsing and writing service.
Definition: Service.php:16
asset for testMapValueObject()
testExpectStream()
testGetReader
testExpectWrong()
testGetReader