Open Monograph Press  3.3.0
Request/XmlVisitorTest.php
1 <?php
2 
4 
10 
15 {
16  public function xmlProvider()
17  {
18  return array(
19  array(
20  array(
21  'data' => array(
22  'xmlRoot' => array(
23  'name' => 'test',
24  'namespaces' => 'http://foo.com'
25  )
26  ),
27  'parameters' => array(
28  'Foo' => array('location' => 'xml', 'type' => 'string'),
29  'Baz' => array('location' => 'xml', 'type' => 'string')
30  )
31  ),
32  array('Foo' => 'test', 'Baz' => 'bar'),
33  '<test xmlns="http://foo.com"><Foo>test</Foo><Baz>bar</Baz></test>'
34  ),
35  // Ensure that the content-type is not added
36  array(array('parameters' => array('Foo' => array('location' => 'xml', 'type' => 'string'))), array(), ''),
37  // Test with adding attributes and no namespace
38  array(
39  array(
40  'data' => array(
41  'xmlRoot' => array(
42  'name' => 'test'
43  )
44  ),
45  'parameters' => array(
46  'Foo' => array('location' => 'xml', 'type' => 'string', 'data' => array('xmlAttribute' => true))
47  )
48  ),
49  array('Foo' => 'test', 'Baz' => 'bar'),
50  '<test Foo="test"/>'
51  ),
52  // Test adding with an array
53  array(
54  array(
55  'parameters' => array(
56  'Foo' => array('location' => 'xml', 'type' => 'string'),
57  'Baz' => array(
58  'type' => 'array',
59  'location' => 'xml',
60  'items' => array(
61  'type' => 'numeric',
62  'sentAs' => 'Bar'
63  )
64  )
65  )
66  ),
67  array('Foo' => 'test', 'Baz' => array(1, 2)),
68  '<Request><Foo>test</Foo><Baz><Bar>1</Bar><Bar>2</Bar></Baz></Request>'
69  ),
70  // Test adding an object
71  array(
72  array(
73  'parameters' => array(
74  'Foo' => array('location' => 'xml', 'type' => 'string'),
75  'Baz' => array(
76  'type' => 'object',
77  'location' => 'xml',
78  'properties' => array(
79  'Bar' => array('type' => 'string'),
80  'Bam' => array()
81  )
82  )
83  )
84  ),
85  array('Foo' => 'test', 'Baz' => array('Bar' => 'abc', 'Bam' => 'foo')),
86  '<Request><Foo>test</Foo><Baz><Bar>abc</Bar><Bam>foo</Bam></Baz></Request>'
87  ),
88  // Add an array that contains an object
89  array(
90  array(
91  'parameters' => array(
92  'Baz' => array(
93  'type' => 'array',
94  'location' => 'xml',
95  'items' => array(
96  'type' => 'object',
97  'sentAs' => 'Bar',
98  'properties' => array('A' => array(), 'B' => array())
99  )
100  )
101  )
102  ),
103  array('Baz' => array(
104  array('A' => '1', 'B' => '2'),
105  array('A' => '3', 'B' => '4')
106  )),
107  '<Request><Baz><Bar><A>1</A><B>2</B></Bar><Bar><A>3</A><B>4</B></Bar></Baz></Request>'
108  ),
109  // Add an object of attributes
110  array(
111  array(
112  'parameters' => array(
113  'Foo' => array('location' => 'xml', 'type' => 'string'),
114  'Baz' => array(
115  'type' => 'object',
116  'location' => 'xml',
117  'properties' => array(
118  'Bar' => array('type' => 'string', 'data' => array('xmlAttribute' => true)),
119  'Bam' => array()
120  )
121  )
122  )
123  ),
124  array('Foo' => 'test', 'Baz' => array('Bar' => 'abc', 'Bam' => 'foo')),
125  '<Request><Foo>test</Foo><Baz Bar="abc"><Bam>foo</Bam></Baz></Request>'
126  ),
127  // Check order doesn't matter
128  array(
129  array(
130  'parameters' => array(
131  'Foo' => array('location' => 'xml', 'type' => 'string'),
132  'Baz' => array(
133  'type' => 'object',
134  'location' => 'xml',
135  'properties' => array(
136  'Bar' => array('type' => 'string', 'data' => array('xmlAttribute' => true)),
137  'Bam' => array()
138  )
139  )
140  )
141  ),
142  array('Foo' => 'test', 'Baz' => array('Bam' => 'foo', 'Bar' => 'abc')),
143  '<Request><Foo>test</Foo><Baz Bar="abc"><Bam>foo</Bam></Baz></Request>'
144  ),
145  // Add values with custom namespaces
146  array(
147  array(
148  'parameters' => array(
149  'Foo' => array(
150  'location' => 'xml',
151  'type' => 'string',
152  'data' => array(
153  'xmlNamespace' => 'http://foo.com'
154  )
155  )
156  )
157  ),
158  array('Foo' => 'test'),
159  '<Request><Foo xmlns="http://foo.com">test</Foo></Request>'
160  ),
161  // Add attributes with custom namespace prefix
162  array(
163  array(
164  'parameters' => array(
165  'Wrap' => array(
166  'type' => 'object',
167  'location' => 'xml',
168  'properties' => array(
169  'Foo' => array(
170  'type' => 'string',
171  'sentAs' => 'xsi:baz',
172  'data' => array(
173  'xmlNamespace' => 'http://foo.com',
174  'xmlAttribute' => true
175  )
176  )
177  )
178  ),
179  )
180  ),
181  array('Wrap' => array(
182  'Foo' => 'test'
183  )),
184  '<Request><Wrap xsi:baz="test" xmlns:xsi="http://foo.com"/></Request>'
185  ),
186  // Add nodes with custom namespace prefix
187  array(
188  array(
189  'parameters' => array(
190  'Wrap' => array(
191  'type' => 'object',
192  'location' => 'xml',
193  'properties' => array(
194  'Foo' => array(
195  'type' => 'string',
196  'sentAs' => 'xsi:Foo',
197  'data' => array(
198  'xmlNamespace' => 'http://foobar.com'
199  )
200  )
201  )
202  ),
203  )
204  ),
205  array('Wrap' => array(
206  'Foo' => 'test'
207  )),
208  '<Request><Wrap><xsi:Foo xmlns:xsi="http://foobar.com">test</xsi:Foo></Wrap></Request>'
209  ),
210  array(
211  array(
212  'parameters' => array(
213  'Foo' => array(
214  'location' => 'xml',
215  'type' => 'string',
216  'data' => array(
217  'xmlNamespace' => 'http://foo.com'
218  )
219  )
220  )
221  ),
222  array('Foo' => '<h1>This is a title</h1>'),
223  '<Request><Foo xmlns="http://foo.com"><![CDATA[<h1>This is a title</h1>]]></Foo></Request>'
224  ),
225  // Flat array at top level
226  array(
227  array(
228  'parameters' => array(
229  'Bars' => array(
230  'type' => 'array',
231  'data' => array('xmlFlattened' => true),
232  'location' => 'xml',
233  'items' => array(
234  'type' => 'object',
235  'sentAs' => 'Bar',
236  'properties' => array(
237  'A' => array(),
238  'B' => array()
239  )
240  )
241  ),
242  'Boos' => array(
243  'type' => 'array',
244  'data' => array('xmlFlattened' => true),
245  'location' => 'xml',
246  'items' => array(
247  'sentAs' => 'Boo',
248  'type' => 'string'
249  )
250  )
251  )
252  ),
253  array(
254  'Bars' => array(
255  array('A' => '1', 'B' => '2'),
256  array('A' => '3', 'B' => '4')
257  ),
258  'Boos' => array('test', '123')
259  ),
260  '<Request><Bar><A>1</A><B>2</B></Bar><Bar><A>3</A><B>4</B></Bar><Boo>test</Boo><Boo>123</Boo></Request>'
261  ),
262  // Nested flat arrays
263  array(
264  array(
265  'parameters' => array(
266  'Delete' => array(
267  'type' => 'object',
268  'location' => 'xml',
269  'properties' => array(
270  'Items' => array(
271  'type' => 'array',
272  'data' => array('xmlFlattened' => true),
273  'items' => array(
274  'type' => 'object',
275  'sentAs' => 'Item',
276  'properties' => array(
277  'A' => array(),
278  'B' => array()
279  )
280  )
281  )
282  )
283  )
284  )
285  ),
286  array(
287  'Delete' => array(
288  'Items' => array(
289  array('A' => '1', 'B' => '2'),
290  array('A' => '3', 'B' => '4')
291  )
292  )
293  ),
294  '<Request><Delete><Item><A>1</A><B>2</B></Item><Item><A>3</A><B>4</B></Item></Delete></Request>'
295  )
296  );
297  }
298 
302  public function testSerializesXml(array $operation, array $input, $xml)
303  {
304  $operation = new Operation($operation);
305  $command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
306  ->setConstructorArgs(array($input, $operation))
307  ->getMockForAbstractClass();
308  $command->setClient(new Client('http://www.test.com/some/path.php'));
309  $request = $command->prepare();
310  if (!empty($input)) {
311  $this->assertEquals('application/xml', (string) $request->getHeader('Content-Type'));
312  } else {
313  $this->assertNull($request->getHeader('Content-Type'));
314  }
315  $body = str_replace(array("\n", "<?xml version=\"1.0\"?>"), '', (string) $request->getBody());
316  $this->assertEquals($xml, $body);
317  }
318 
320  {
321  $operation = new Operation(array(
322  'data' => array(
323  'xmlRoot' => array(
324  'name' => 'test',
325  'namespaces' => array(
326  'xsi' => 'http://foo.com'
327  )
328  )
329  ),
330  'parameters' => array(
331  'Foo' => array('location' => 'xml', 'type' => 'string'),
332  'Baz' => array('location' => 'xml', 'type' => 'string')
333  )
334  ));
335 
336  $command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
337  ->setConstructorArgs(array(array(
338  'Foo' => 'test',
339  'Baz' => 'bar'
340  ), $operation))
341  ->getMockForAbstractClass();
342 
343  $command->setClient(new Client());
344  $request = $command->prepare();
345  $this->assertEquals('application/xml', (string) $request->getHeader('Content-Type'));
346  $this->assertEquals(
347  '<?xml version="1.0"?>' . "\n"
348  . '<test xmlns:xsi="http://foo.com"><Foo>test</Foo><Baz>bar</Baz></test>' . "\n",
349  (string) $request->getBody()
350  );
351  }
352 
353  public function testCanChangeContentType()
354  {
355  $visitor = new XmlVisitor();
356  $visitor->setContentTypeHeader('application/foo');
357  $this->assertEquals('application/foo', $this->readAttribute($visitor, 'contentType'));
358  }
359 
361  {
362  $request = new EntityEnclosingRequest('POST', 'http://foo.com');
363  $visitor = new XmlVisitor();
364  $param = new Parameter(array(
365  'type' => 'object',
366  'location' => 'xml',
367  'name' => 'Out',
368  'properties' => array(
369  'Nodes' => array(
370  'required' => true,
371  'type' => 'array',
372  'min' => 1,
373  'items' => array('type' => 'string', 'sentAs' => 'Node')
374  )
375  )
376  ));
377 
378  $param->setParent(new Operation(array(
379  'data' => array(
380  'xmlRoot' => array(
381  'name' => 'Test',
382  'namespaces' => array(
383  'https://foo/'
384  )
385  )
386  )
387  )));
388 
389  $value = array('Nodes' => array('foo', 'baz'));
390  $this->assertTrue($this->validator->validate($param, $value));
391  $visitor->visit($this->command, $request, $param, $value);
392  $visitor->after($this->command, $request);
393 
394  $this->assertEquals(
395  "<?xml version=\"1.0\"?>\n"
396  . "<Test xmlns=\"https://foo/\"><Out><Nodes><Node>foo</Node><Node>baz</Node></Nodes></Out></Test>\n",
397  (string) $request->getBody()
398  );
399  }
400 
402  {
403  $operation = new Operation(array(
404  'data' => array(
405  'xmlRoot' => array(
406  'name' => 'Hi',
407  'namespaces' => array(
408  'xsi' => 'http://foo.com',
409  'foo' => 'http://foobar.com'
410  )
411  )
412  ),
413  'parameters' => array(
414  'Foo' => array('location' => 'xml', 'type' => 'string')
415  )
416  ));
417 
418  $command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
419  ->setConstructorArgs(array(array(
420  'Foo' => 'test'
421  ), $operation))
422  ->getMockForAbstractClass();
423 
424  $command->setClient(new Client());
425  $request = $command->prepare();
426  $this->assertEquals('application/xml', (string) $request->getHeader('Content-Type'));
427  $this->assertEquals(
428  '<?xml version="1.0"?>' . "\n"
429  . '<Hi xmlns:xsi="http://foo.com" xmlns:foo="http://foobar.com"><Foo>test</Foo></Hi>' . "\n",
430  (string) $request->getBody()
431  );
432  }
433 
434  public function testValuesAreFiltered()
435  {
436  $operation = new Operation(array(
437  'parameters' => array(
438  'Foo' => array(
439  'location' => 'xml',
440  'type' => 'string',
441  'filters' => array('strtoupper')
442  ),
443  'Bar' => array(
444  'location' => 'xml',
445  'type' => 'object',
446  'properties' => array(
447  'Baz' => array(
448  'filters' => array('strtoupper')
449  )
450  )
451  )
452  )
453  ));
454 
455  $command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
456  ->setConstructorArgs(array(array(
457  'Foo' => 'test',
458  'Bar' => array(
459  'Baz' => 'abc'
460  )
461  ), $operation))
462  ->getMockForAbstractClass();
463 
464  $command->setClient(new Client());
465  $request = $command->prepare();
466  $this->assertEquals(
467  '<?xml version="1.0"?>' . "\n"
468  . '<Request><Foo>TEST</Foo><Bar><Baz>ABC</Baz></Bar></Request>' . "\n",
469  (string) $request->getBody()
470  );
471  }
472 
473  public function testSkipsNullValues()
474  {
475  $operation = new Operation(array(
476  'parameters' => array(
477  'Foo' => array(
478  'location' => 'xml',
479  'type' => 'string'
480  ),
481  'Bar' => array(
482  'location' => 'xml',
483  'type' => 'object',
484  'properties' => array(
485  'Baz' => array(),
486  'Bam' => array(),
487  )
488  ),
489  'Arr' => array(
490  'type' => 'array',
491  'items' => array(
492  'type' => 'string'
493  )
494  )
495  )
496  ));
497 
498  $command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
499  ->setConstructorArgs(array(array(
500  'Foo' => null,
501  'Bar' => array(
502  'Bar' => null,
503  'Bam' => 'test'
504  ),
505  'Arr' => array(null)
506  ), $operation))
507  ->getMockForAbstractClass();
508 
509  $command->setClient(new Client());
510  $request = $command->prepare();
511  $this->assertEquals(
512  '<?xml version="1.0"?>' . "\n"
513  . '<Request><Bar><Bam>test</Bam></Bar></Request>' . "\n",
514  (string) $request->getBody()
515  );
516  }
517 
518  public function testAllowsXmlEncoding()
519  {
520  $operation = new Operation(array(
521  'data' => array(
522  'xmlEncoding' => 'UTF-8'
523  ),
524  'parameters' => array(
525  'Foo' => array('location' => 'xml')
526  )
527  ));
528  $command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
529  ->setConstructorArgs(array(array('Foo' => 'test'), $operation))
530  ->getMockForAbstractClass();
531  $command->setClient(new Client());
532  $request = $command->prepare();
533  $this->assertEquals(
534  '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
535  . '<Request><Foo>test</Foo></Request>' . "\n",
536  (string) $request->getBody()
537  );
538  }
539 
541  {
542  $operation = new Operation(array(
543  'httpMethod' => 'POST',
544  'data' => array('xmlAllowEmpty' => true),
545  'parameters' => array('Foo' => array('location' => 'xml'))
546  ));
547  $command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
548  ->setConstructorArgs(array(array(), $operation))
549  ->getMockForAbstractClass();
550  $command->setClient(new Client('http://foo.com'));
551  $request = $command->prepare();
552  $this->assertEquals(
553  '<?xml version="1.0"?>' . "\n"
554  . '<Request/>' . "\n",
555  (string) $request->getBody()
556  );
557  }
558 }
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testSerializesXml
testSerializesXml(array $operation, array $input, $xml)
Definition: Request/XmlVisitorTest.php:302
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testCanChangeContentType
testCanChangeContentType()
Definition: Request/XmlVisitorTest.php:353
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\xmlProvider
xmlProvider()
Definition: Request/XmlVisitorTest.php:16
Guzzle\Service\Description\Operation
Definition: Operation.php:10
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testSkipsNullValues
testSkipsNullValues()
Definition: Request/XmlVisitorTest.php:473
Guzzle\Service\Client
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Service/Client.php:25
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testAllowsXmlEncoding
testAllowsXmlEncoding()
Definition: Request/XmlVisitorTest.php:518
Guzzle\Tests\Service\Command\LocationVisitor\Request
Definition: AbstractVisitorTestCase.php:3
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testAllowsSendingXmlPayloadIfNoXmlParamsWereSet
testAllowsSendingXmlPayloadIfNoXmlParamsWereSet()
Definition: Request/XmlVisitorTest.php:540
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest
Definition: Request/XmlVisitorTest.php:14
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testCanAddMultipleNamespacesToRoot
testCanAddMultipleNamespacesToRoot()
Definition: Request/XmlVisitorTest.php:401
Guzzle\Tests\Service\Command\LocationVisitor\Request\AbstractVisitorTestCase\$param
$param
Definition: AbstractVisitorTestCase.php:17
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testCanAddArrayOfSimpleTypes
testCanAddArrayOfSimpleTypes()
Definition: Request/XmlVisitorTest.php:360
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testValuesAreFiltered
testValuesAreFiltered()
Definition: Request/XmlVisitorTest.php:434
Guzzle\Http\Message\EntityEnclosingRequest
Definition: EntityEnclosingRequest.php:14
Guzzle\Tests\Service\Command\LocationVisitor\Request\AbstractVisitorTestCase\$command
$command
Definition: AbstractVisitorTestCase.php:15
Guzzle\Service\Command\LocationVisitor\Request\XmlVisitor
Definition: Request/XmlVisitor.php:13
Guzzle\Tests\Service\Command\LocationVisitor\Request\AbstractVisitorTestCase
Definition: AbstractVisitorTestCase.php:13
Guzzle\Tests\Service\Command\LocationVisitor\Request\XmlVisitorTest\testAddsContentTypeAndTopLevelValues
testAddsContentTypeAndTopLevelValues()
Definition: Request/XmlVisitorTest.php:319
Guzzle\Tests\Service\Command\LocationVisitor\Request\AbstractVisitorTestCase\$request
$request
Definition: AbstractVisitorTestCase.php:16