Open Journal Systems  3.3.0
Response/XmlVisitorTest.php
1 <?php
2 
4 
8 
13 {
14  public function testBeforeMethodParsesXml()
15  {
16  $visitor = new Visitor();
17  $command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')
18  ->setMethods(array('getResponse'))
19  ->getMockForAbstractClass();
20  $command->expects($this->once())
21  ->method('getResponse')
22  ->will($this->returnValue(new Response(200, null, '<foo><Bar>test</Bar></foo>')));
23  $result = array();
24  $visitor->before($command, $result);
25  $this->assertEquals(array('Bar' => 'test'), $result);
26  }
27 
29  {
30  $this->markTestSkipped("Response/XmlVisitor cannot accept 'xmlns' in response, see #368 (http://git.io/USa1mA).");
31 
32  $visitor = new Visitor();
33  $command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')
34  ->setMethods(array('getResponse'))
35  ->getMockForAbstractClass();
36  $command->expects($this->once())
37  ->method('getResponse')
38  ->will($this->returnValue(new Response(200, null, '<foo xmlns="urn:foo"><bar:Bar xmlns:bar="urn:bar">test</bar:Bar></foo>')));
39  $result = array();
40  $visitor->before($command, $result);
41  $this->assertEquals(array('Bar' => 'test'), $result);
42  }
43 
45  {
46  $visitor = new Visitor();
47  $command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')
48  ->setMethods(array('getResponse'))
49  ->getMockForAbstractClass();
50  $command->expects($this->once())
51  ->method('getResponse')
52  ->will($this->returnValue(new Response(200, null, '<foo><Items><Bar>test</Bar></Items></foo>')));
53  $result = array();
54  $visitor->before($command, $result);
55  $this->assertEquals(array('Items' => array('Bar' => 'test')), $result);
56  }
57 
59  {
60  $visitor = new Visitor();
61  $param = new Parameter(array(
62  'location' => 'xml',
63  'name' => 'foo',
64  'sentAs' => 'Bar'
65  ));
66  $value = array('Bar' => 'test');
67  $visitor->visit($this->command, $this->response, $param, $value);
68  $this->assertArrayHasKey('foo', $value);
69  $this->assertEquals('test', $value['foo']);
70  }
71 
73  {
74  $visitor = new Visitor();
75  $param = new Parameter(array(
76  'location' => 'xml',
77  'name' => 'foo',
78  'sentAs' => 'Foo',
79  'type' => 'array',
80  'items' => array(
81  'type' => 'object',
82  'properties' => array(
83  'Bar' => array('type' => 'string'),
84  'Baz' => array('type' => 'string'),
85  'Bam' => array('type' => 'string')
86  )
87  )
88  ));
89 
90  $xml = new \SimpleXMLElement('<Test><Foo><Bar>1</Bar><Baz>2</Baz></Foo></Test>');
91  $value = json_decode(json_encode($xml), true);
92  $visitor->visit($this->command, $this->response, $param, $value);
93  $this->assertEquals(array(
94  'foo' => array(
95  array (
96  'Bar' => '1',
97  'Baz' => '2'
98  )
99  )
100  ), $value);
101  }
102 
104  {
105  $visitor = new Visitor();
106  $param = new Parameter(array(
107  'location' => 'xml',
108  'name' => 'foo',
109  'type' => 'array',
110  'items' => array('type' => 'string')
111  ));
112 
113  $value = array('foo' => array('bar', 'baz'));
114  $visitor->visit($this->command, $this->response, $param, $value);
115  $this->assertEquals(array('foo' => array('bar', 'baz')), $value);
116 
117  $value = array('foo' => 'bar');
118  $visitor->visit($this->command, $this->response, $param, $value);
119  $this->assertEquals(array('foo' => array('bar')), $value);
120  }
121 
122  public function xmlDataProvider()
123  {
124  $param = new Parameter(array(
125  'location' => 'xml',
126  'name' => 'Items',
127  'type' => 'array',
128  'items' => array(
129  'type' => 'object',
130  'name' => 'Item',
131  'properties' => array(
132  'Bar' => array('type' => 'string'),
133  'Baz' => array('type' => 'string')
134  )
135  )
136  ));
137 
138  return array(
139  array($param, '<Test><Items><Item><Bar>1</Bar></Item><Item><Bar>2</Bar></Item></Items></Test>', array(
140  'Items' => array(
141  array('Bar' => 1),
142  array('Bar' => 2)
143  )
144  )),
145  array($param, '<Test><Items><Item><Bar>1</Bar></Item></Items></Test>', array(
146  'Items' => array(
147  array('Bar' => 1)
148  )
149  )),
150  array($param, '<Test><Items /></Test>', array(
151  'Items' => array()
152  ))
153  );
154  }
155 
159  public function testEnsuresWrappedArraysAreInCorrectLocations($param, $xml, $result)
160  {
161  $visitor = new Visitor();
162  $xml = new \SimpleXMLElement($xml);
163  $value = json_decode(json_encode($xml), true);
164  $visitor->visit($this->command, $this->response, $param, $value);
165  $this->assertEquals($result, $value);
166  }
167 
168  public function testCanRenameValues()
169  {
170  $visitor = new Visitor();
171  $param = new Parameter(array(
172  'name' => 'TerminatingInstances',
173  'type' => 'array',
174  'location' => 'xml',
175  'sentAs' => 'instancesSet',
176  'items' => array(
177  'name' => 'item',
178  'type' => 'object',
179  'sentAs' => 'item',
180  'properties' => array(
181  'InstanceId' => array(
182  'type' => 'string',
183  'sentAs' => 'instanceId',
184  ),
185  'CurrentState' => array(
186  'type' => 'object',
187  'sentAs' => 'currentState',
188  'properties' => array(
189  'Code' => array(
190  'type' => 'numeric',
191  'sentAs' => 'code',
192  ),
193  'Name' => array(
194  'type' => 'string',
195  'sentAs' => 'name',
196  ),
197  ),
198  ),
199  'PreviousState' => array(
200  'type' => 'object',
201  'sentAs' => 'previousState',
202  'properties' => array(
203  'Code' => array(
204  'type' => 'numeric',
205  'sentAs' => 'code',
206  ),
207  'Name' => array(
208  'type' => 'string',
209  'sentAs' => 'name',
210  ),
211  ),
212  ),
213  ),
214  )
215  ));
216 
217  $value = array(
218  'instancesSet' => array (
219  'item' => array (
220  'instanceId' => 'i-3ea74257',
221  'currentState' => array(
222  'code' => '32',
223  'name' => 'shutting-down',
224  ),
225  'previousState' => array(
226  'code' => '16',
227  'name' => 'running',
228  ),
229  ),
230  )
231  );
232 
233  $visitor->visit($this->command, $this->response, $param, $value);
234 
235  $this->assertEquals(array(
236  'TerminatingInstances' => array(
237  array(
238  'InstanceId' => 'i-3ea74257',
239  'CurrentState' => array(
240  'Code' => '32',
241  'Name' => 'shutting-down',
242  ),
243  'PreviousState' => array(
244  'Code' => '16',
245  'Name' => 'running',
246  )
247  )
248  )
249  ), $value);
250  }
251 
252  public function testCanRenameAttributes()
253  {
254  $visitor = new Visitor();
255  $param = new Parameter(array(
256  'name' => 'RunningQueues',
257  'type' => 'array',
258  'location' => 'xml',
259  'items' => array(
260  'type' => 'object',
261  'sentAs' => 'item',
262  'properties' => array(
263  'QueueId' => array(
264  'type' => 'string',
265  'sentAs' => 'queue_id',
266  'data' => array(
267  'xmlAttribute' => true,
268  ),
269  ),
270  'CurrentState' => array(
271  'type' => 'object',
272  'properties' => array(
273  'Code' => array(
274  'type' => 'numeric',
275  'sentAs' => 'code',
276  'data' => array(
277  'xmlAttribute' => true,
278  ),
279  ),
280  'Name' => array(
281  'sentAs' => 'name',
282  'data' => array(
283  'xmlAttribute' => true,
284  ),
285  ),
286  ),
287  ),
288  'PreviousState' => array(
289  'type' => 'object',
290  'properties' => array(
291  'Code' => array(
292  'type' => 'numeric',
293  'sentAs' => 'code',
294  'data' => array(
295  'xmlAttribute' => true,
296  ),
297  ),
298  'Name' => array(
299  'sentAs' => 'name',
300  'data' => array(
301  'xmlAttribute' => true,
302  ),
303  ),
304  ),
305  ),
306  ),
307  )
308  ));
309 
310  $xml = '<wrap><RunningQueues><item queue_id="q-3ea74257"><CurrentState code="32" name="processing" /><PreviousState code="16" name="wait" /></item></RunningQueues></wrap>';
311  $value = json_decode(json_encode(new \SimpleXMLElement($xml)), true);
312  $visitor->visit($this->command, $this->response, $param, $value);
313 
314  $this->assertEquals(array(
315  'RunningQueues' => array(
316  array(
317  'QueueId' => 'q-3ea74257',
318  'CurrentState' => array(
319  'Code' => '32',
320  'Name' => 'processing',
321  ),
322  'PreviousState' => array(
323  'Code' => '16',
324  'Name' => 'wait',
325  ),
326  ),
327  )
328  ), $value);
329  }
330 
332  {
333  $visitor = new Visitor();
334  $param = new Parameter(array(
335  'name' => 'Foo',
336  'type' => 'array',
337  'location' => 'xml',
338  'items' => array(
339  'type' => 'object',
340  'properties' => array(
341  'Baz' => array('type' => 'array'),
342  'Bar' => array(
343  'type' => 'object',
344  'properties' => array(
345  'Baz' => array('type' => 'array'),
346  )
347  )
348  )
349  )
350  ));
351 
352  $value = array();
353  $visitor->visit($this->command, $this->response, $param, $value);
354 
355  $value = array(
356  'Foo' => array(
357  'Bar' => array()
358  )
359  );
360  $visitor->visit($this->command, $this->response, $param, $value);
361  $this->assertEquals(array(
362  'Foo' => array(
363  array(
364  'Bar' => array()
365  )
366  )
367  ), $value);
368  }
369 
375  {
376  $visitor = new Visitor();
377  $param = new Parameter(array(
378  'name' => 'foo',
379  'type' => 'object',
380  'additionalProperties' => false,
381  'properties' => array(
382  'bar' => array(
383  'type' => 'string',
384  'name' => 'bar',
385  ),
386  ),
387  ));
388  $this->value = array('foo' => array('bar' => 15, 'unknown' => 'Unknown'));
389  $visitor->visit($this->command, $this->response, $param, $this->value);
390  $this->assertEquals(array('foo' => array('bar' => 15)), $this->value);
391  }
392 
398  {
399  $visitor = new Visitor();
400  $param = new Parameter(array(
401  'name' => 'foo',
402  'type' => 'object',
403  'additionalProperties' => false,
404  'properties' => array(
405  'bar' => array(
406  'name' => 'bar',
407  'sentAs' => 'baz',
408  ),
409  ),
410  ));
411  $this->value = array('foo' => array('baz' => 15, 'unknown' => 'Unknown'));
412  $visitor->visit($this->command, $this->response, $param, $this->value);
413  $this->assertEquals(array('foo' => array('bar' => 15)), $this->value);
414  }
415 
417  {
418  $visitor = new Visitor();
419  $param = new Parameter(array(
420  'name' => 'foo',
421  'type' => 'object',
422  'properties' => array(
423  'bar' => array('type' => 'string')
424  ),
425  ));
426  $xml = '<wrapper><foo><bar /></foo></wrapper>';
427  $value = json_decode(json_encode(new \SimpleXMLElement($xml)), true);
428  $visitor->visit($this->command, $this->response, $param, $value);
429  $this->assertEquals(array('foo' => array('bar' => '')), $value);
430  }
431 }
Guzzle\Tests\Service\Command\LocationVisitor\Response\AbstractResponseVisitorTest
Definition: AbstractResponseVisitorTest.php:8
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\xmlDataProvider
xmlDataProvider()
Definition: Response/XmlVisitorTest.php:122
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testAddsEmptyArraysWhenValueIsMissing
testAddsEmptyArraysWhenValueIsMissing()
Definition: Response/XmlVisitorTest.php:331
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testEnsuresRepeatedArraysAreInCorrectLocations
testEnsuresRepeatedArraysAreInCorrectLocations()
Definition: Response/XmlVisitorTest.php:72
Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor
Definition: Response/XmlVisitor.php:12
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testDiscardingUnknownProperties
testDiscardingUnknownProperties()
Definition: Response/XmlVisitorTest.php:374
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest
Definition: Response/XmlVisitorTest.php:12
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testCanRenameAttributes
testCanRenameAttributes()
Definition: Response/XmlVisitorTest.php:252
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testBeforeMethodParsesXmlWithNamespace
testBeforeMethodParsesXmlWithNamespace()
Definition: Response/XmlVisitorTest.php:28
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testBeforeMethodParsesNestedXml
testBeforeMethodParsesNestedXml()
Definition: Response/XmlVisitorTest.php:44
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testEnsuresWrappedArraysAreInCorrectLocations
testEnsuresWrappedArraysAreInCorrectLocations($param, $xml, $result)
Definition: Response/XmlVisitorTest.php:159
Guzzle\Tests\Service\Command\LocationVisitor\Response
Definition: AbstractResponseVisitorTest.php:3
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testProperlyHandlesEmptyStringValues
testProperlyHandlesEmptyStringValues()
Definition: Response/XmlVisitorTest.php:416
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testDiscardingUnknownPropertiesWithAliasing
testDiscardingUnknownPropertiesWithAliasing()
Definition: Response/XmlVisitorTest.php:397
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testBeforeMethodParsesXml
testBeforeMethodParsesXml()
Definition: Response/XmlVisitorTest.php:14
Guzzle\Tests\Service\Command\LocationVisitor\Response\AbstractResponseVisitorTest\$command
$command
Definition: AbstractResponseVisitorTest.php:20
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testEnsuresFlatArraysAreFlat
testEnsuresFlatArraysAreFlat()
Definition: Response/XmlVisitorTest.php:103
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testCanRenameValues
testCanRenameValues()
Definition: Response/XmlVisitorTest.php:168
Guzzle\Tests\Service\Command\LocationVisitor\Response\XmlVisitorTest\testCanExtractAndRenameTopLevelXmlValues
testCanExtractAndRenameTopLevelXmlValues()
Definition: Response/XmlVisitorTest.php:58
Guzzle\Tests\Service\Command\LocationVisitor\Response\AbstractResponseVisitorTest\$value
$value
Definition: AbstractResponseVisitorTest.php:26