XmlLocationTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Command\Command;
  5. use GuzzleHttp\Command\Guzzle\Description;
  6. use GuzzleHttp\Command\Guzzle\GuzzleClient;
  7. use GuzzleHttp\Command\Guzzle\Operation;
  8. use GuzzleHttp\Command\Guzzle\Parameter;
  9. use GuzzleHttp\Command\Guzzle\RequestLocation\XmlLocation;
  10. use GuzzleHttp\Handler\MockHandler;
  11. use GuzzleHttp\HandlerStack;
  12. use GuzzleHttp\Middleware;
  13. use GuzzleHttp\Psr7\Request;
  14. use GuzzleHttp\Psr7\Response;
  15. /**
  16. * @covers \GuzzleHttp\Command\Guzzle\RequestLocation\XmlLocation
  17. */
  18. class XmlLocationTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @group RequestLocation
  22. */
  23. public function testVisitsLocation()
  24. {
  25. $location = new XmlLocation();
  26. $command = new Command('foo', ['foo' => 'bar']);
  27. $command['bar'] = 'test';
  28. $request = new Request('POST', 'http://httbin.org');
  29. $param = new Parameter(['name' => 'foo']);
  30. $location->visit($command, $request, $param);
  31. $param = new Parameter(['name' => 'bar']);
  32. $location->visit($command, $request, $param);
  33. $operation = new Operation();
  34. $request = $location->after($command, $request, $operation);
  35. $xml = $request->getBody()->getContents();
  36. $this->assertEquals('<?xml version="1.0"?>' . "\n"
  37. . '<Request><foo>bar</foo><bar>test</bar></Request>' . "\n", $xml);
  38. $header = $request->getHeader('Content-Type');
  39. $this->assertArraySubset([0 => 'application/xml'], $header);
  40. }
  41. /**
  42. * @group RequestLocation
  43. */
  44. public function testCreatesBodyForEmptyDocument()
  45. {
  46. $location = new XmlLocation();
  47. $command = new Command('foo', ['foo' => 'bar']);
  48. $request = new Request('POST', 'http://httbin.org');
  49. $operation = new Operation([
  50. 'data' => ['xmlAllowEmpty' => true]
  51. ]);
  52. $request = $location->after($command, $request, $operation);
  53. $xml = $request->getBody()->getContents();
  54. $this->assertEquals('<?xml version="1.0"?>' . "\n"
  55. . '<Request/>' . "\n", $xml);
  56. $header = $request->getHeader('Content-Type');
  57. $this->assertArraySubset([0 => 'application/xml'], $header);
  58. }
  59. /**
  60. * @group RequestLocation
  61. */
  62. public function testAddsAdditionalParameters()
  63. {
  64. $location = new XmlLocation('xml', 'test');
  65. $command = new Command('foo', ['foo' => 'bar']);
  66. $request = new Request('POST', 'http://httbin.org');
  67. $param = new Parameter(['name' => 'foo']);
  68. $command['foo'] = 'bar';
  69. $location->visit($command, $request, $param);
  70. $operation = new Operation([
  71. 'additionalParameters' => [
  72. 'location' => 'xml'
  73. ]
  74. ]);
  75. $command['bam'] = 'boo';
  76. $request = $location->after($command, $request, $operation);
  77. $xml = $request->getBody()->getContents();
  78. $this->assertEquals('<?xml version="1.0"?>' . "\n"
  79. . '<Request><foo>bar</foo><foo>bar</foo><bam>boo</bam></Request>' . "\n", $xml);
  80. $header = $request->getHeader('Content-Type');
  81. $this->assertArraySubset([0 => 'test'], $header);
  82. }
  83. /**
  84. * @group RequestLocation
  85. */
  86. public function testAllowsXmlEncoding()
  87. {
  88. $location = new XmlLocation();
  89. $operation = new Operation([
  90. 'data' => ['xmlEncoding' => 'UTF-8']
  91. ]);
  92. $command = new Command('foo', ['foo' => 'bar']);
  93. $request = new Request('POST', 'http://httbin.org');
  94. $param = new Parameter(['name' => 'foo']);
  95. $command['foo'] = 'bar';
  96. $location->visit($command, $request, $param);
  97. $request = $location->after($command, $request, $operation);
  98. $xml = $request->getBody()->getContents();
  99. $this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  100. . '<Request><foo>bar</foo></Request>' . "\n", $xml);
  101. }
  102. public function xmlProvider()
  103. {
  104. return [
  105. [
  106. [
  107. 'data' => [
  108. 'xmlRoot' => [
  109. 'name' => 'test',
  110. 'namespaces' => 'http://foo.com'
  111. ]
  112. ],
  113. 'parameters' => [
  114. 'Foo' => [
  115. 'location' => 'xml',
  116. 'type' => 'string'
  117. ],
  118. 'Baz' => [
  119. 'location' => 'xml',
  120. 'type' => 'string'
  121. ]
  122. ]
  123. ],
  124. [
  125. 'Foo' => 'test',
  126. 'Baz' => 'bar'
  127. ],
  128. '<test xmlns="http://foo.com"><Foo>test</Foo><Baz>bar</Baz></test>'
  129. ],
  130. // Ensure that the content-type is not added
  131. [
  132. [
  133. 'parameters' => [
  134. 'Foo' => [
  135. 'location' => 'xml',
  136. 'type' => 'string'
  137. ]
  138. ]
  139. ],
  140. [],
  141. ''
  142. ],
  143. // Test with adding attributes and no namespace
  144. [
  145. [
  146. 'data' => [
  147. 'xmlRoot' => [
  148. 'name' => 'test'
  149. ]
  150. ],
  151. 'parameters' => [
  152. 'Foo' => [
  153. 'location' => 'xml',
  154. 'type' => 'string',
  155. 'data' => ['xmlAttribute' => true]
  156. ]
  157. ]
  158. ],
  159. [
  160. 'Foo' => 'test',
  161. 'Baz' => 'bar'
  162. ],
  163. '<test Foo="test"/>'
  164. ],
  165. // Test adding with an array
  166. [
  167. [
  168. 'parameters' => [
  169. 'Foo' => [
  170. 'location' => 'xml',
  171. 'type' => 'string'
  172. ],
  173. 'Baz' => [
  174. 'type' => 'array',
  175. 'location' => 'xml',
  176. 'items' => [
  177. 'type' => 'numeric',
  178. 'sentAs' => 'Bar'
  179. ]
  180. ]
  181. ]
  182. ],
  183. ['Foo' => 'test', 'Baz' => [1, 2]],
  184. '<Request><Foo>test</Foo><Baz><Bar>1</Bar><Bar>2</Bar></Baz></Request>'
  185. ],
  186. // Test adding an object
  187. [
  188. [
  189. 'parameters' => [
  190. 'Foo' => ['location' => 'xml', 'type' => 'string'],
  191. 'Baz' => [
  192. 'type' => 'object',
  193. 'location' => 'xml',
  194. 'properties' => [
  195. 'Bar' => ['type' => 'string'],
  196. 'Bam' => []
  197. ]
  198. ]
  199. ]
  200. ],
  201. [
  202. 'Foo' => 'test',
  203. 'Baz' => [
  204. 'Bar' => 'abc',
  205. 'Bam' => 'foo'
  206. ]
  207. ],
  208. '<Request><Foo>test</Foo><Baz><Bar>abc</Bar><Bam>foo</Bam></Baz></Request>'
  209. ],
  210. // Add an array that contains an object
  211. [
  212. [
  213. 'parameters' => [
  214. 'Baz' => [
  215. 'type' => 'array',
  216. 'location' => 'xml',
  217. 'items' => [
  218. 'type' => 'object',
  219. 'sentAs' => 'Bar',
  220. 'properties' => ['A' => [], 'B' => []]
  221. ]
  222. ]
  223. ]
  224. ],
  225. ['Baz' => [
  226. [
  227. 'A' => '1',
  228. 'B' => '2'
  229. ],
  230. [
  231. 'A' => '3',
  232. 'B' => '4'
  233. ]
  234. ]],
  235. '<Request><Baz><Bar><A>1</A><B>2</B></Bar><Bar><A>3</A><B>4</B></Bar></Baz></Request>'
  236. ],
  237. // Add an object of attributes
  238. [
  239. [
  240. 'parameters' => [
  241. 'Foo' => [
  242. 'location' => 'xml',
  243. 'type' => 'string'
  244. ],
  245. 'Baz' => [
  246. 'type' => 'object',
  247. 'location' => 'xml',
  248. 'properties' => [
  249. 'Bar' => [
  250. 'type' => 'string',
  251. 'data' => [
  252. 'xmlAttribute' => true
  253. ]
  254. ],
  255. 'Bam' => []
  256. ]
  257. ]
  258. ]
  259. ],
  260. [
  261. 'Foo' => 'test',
  262. 'Baz' => [
  263. 'Bar' => 'abc',
  264. 'Bam' => 'foo'
  265. ]
  266. ],
  267. '<Request><Foo>test</Foo><Baz Bar="abc"><Bam>foo</Bam></Baz></Request>'
  268. ],
  269. // Check order doesn't matter
  270. [
  271. [
  272. 'parameters' => [
  273. 'Foo' => [
  274. 'location' => 'xml',
  275. 'type' => 'string'
  276. ],
  277. 'Baz' => [
  278. 'type' => 'object',
  279. 'location' => 'xml',
  280. 'properties' => [
  281. 'Bar' => [
  282. 'type' => 'string',
  283. 'data' => [
  284. 'xmlAttribute' => true
  285. ]
  286. ],
  287. 'Bam' => []
  288. ]
  289. ]
  290. ]
  291. ],
  292. [
  293. 'Foo' => 'test',
  294. 'Baz' => [
  295. 'Bam' => 'foo',
  296. 'Bar' => 'abc'
  297. ]
  298. ],
  299. '<Request><Foo>test</Foo><Baz Bar="abc"><Bam>foo</Bam></Baz></Request>'
  300. ],
  301. // Add values with custom namespaces
  302. [
  303. [
  304. 'parameters' => [
  305. 'Foo' => [
  306. 'location' => 'xml',
  307. 'type' => 'string',
  308. 'data' => [
  309. 'xmlNamespace' => 'http://foo.com'
  310. ]
  311. ]
  312. ]
  313. ],
  314. ['Foo' => 'test'],
  315. '<Request><Foo xmlns="http://foo.com">test</Foo></Request>'
  316. ],
  317. // Add attributes with custom namespace prefix
  318. [
  319. [
  320. 'parameters' => [
  321. 'Wrap' => [
  322. 'type' => 'object',
  323. 'location' => 'xml',
  324. 'properties' => [
  325. 'Foo' => [
  326. 'type' => 'string',
  327. 'sentAs' => 'xsi:baz',
  328. 'data' => [
  329. 'xmlNamespace' => 'http://foo.com',
  330. 'xmlAttribute' => true
  331. ]
  332. ]
  333. ]
  334. ],
  335. ]
  336. ],
  337. ['Wrap' => [
  338. 'Foo' => 'test'
  339. ]],
  340. '<Request><Wrap xsi:baz="test" xmlns:xsi="http://foo.com"/></Request>'
  341. ],
  342. // Add nodes with custom namespace prefix
  343. [
  344. [
  345. 'parameters' => [
  346. 'Wrap' => [
  347. 'type' => 'object',
  348. 'location' => 'xml',
  349. 'properties' => [
  350. 'Foo' => [
  351. 'type' => 'string',
  352. 'sentAs' => 'xsi:Foo',
  353. 'data' => [
  354. 'xmlNamespace' => 'http://foobar.com'
  355. ]
  356. ]
  357. ]
  358. ],
  359. ]
  360. ],
  361. ['Wrap' => [
  362. 'Foo' => 'test'
  363. ]],
  364. '<Request><Wrap><xsi:Foo xmlns:xsi="http://foobar.com">test</xsi:Foo></Wrap></Request>'
  365. ],
  366. [
  367. [
  368. 'parameters' => [
  369. 'Foo' => [
  370. 'location' => 'xml',
  371. 'type' => 'string',
  372. 'data' => [
  373. 'xmlNamespace' => 'http://foo.com'
  374. ]
  375. ]
  376. ]
  377. ],
  378. ['Foo' => '<h1>This is a title</h1>'],
  379. '<Request><Foo xmlns="http://foo.com"><![CDATA[<h1>This is a title</h1>]]></Foo></Request>'
  380. ],
  381. // Flat array at top level
  382. [
  383. [
  384. 'parameters' => [
  385. 'Bars' => [
  386. 'type' => 'array',
  387. 'data' => ['xmlFlattened' => true],
  388. 'location' => 'xml',
  389. 'items' => [
  390. 'type' => 'object',
  391. 'sentAs' => 'Bar',
  392. 'properties' => [
  393. 'A' => [],
  394. 'B' => []
  395. ]
  396. ]
  397. ],
  398. 'Boos' => [
  399. 'type' => 'array',
  400. 'data' => ['xmlFlattened' => true],
  401. 'location' => 'xml',
  402. 'items' => [
  403. 'sentAs' => 'Boo',
  404. 'type' => 'string'
  405. ]
  406. ]
  407. ]
  408. ],
  409. [
  410. 'Bars' => [
  411. ['A' => '1', 'B' => '2'],
  412. ['A' => '3', 'B' => '4']
  413. ],
  414. 'Boos' => ['test', '123']
  415. ],
  416. '<Request><Bar><A>1</A><B>2</B></Bar><Bar><A>3</A><B>4</B></Bar><Boo>test</Boo><Boo>123</Boo></Request>'
  417. ],
  418. // Nested flat arrays
  419. [
  420. [
  421. 'parameters' => [
  422. 'Delete' => [
  423. 'type' => 'object',
  424. 'location' => 'xml',
  425. 'properties' => [
  426. 'Items' => [
  427. 'type' => 'array',
  428. 'data' => ['xmlFlattened' => true],
  429. 'items' => [
  430. 'type' => 'object',
  431. 'sentAs' => 'Item',
  432. 'properties' => [
  433. 'A' => [],
  434. 'B' => []
  435. ]
  436. ]
  437. ]
  438. ]
  439. ]
  440. ]
  441. ],
  442. [
  443. 'Delete' => [
  444. 'Items' => [
  445. ['A' => '1', 'B' => '2'],
  446. ['A' => '3', 'B' => '4']
  447. ]
  448. ]
  449. ],
  450. '<Request><Delete><Item><A>1</A><B>2</B></Item><Item><A>3</A><B>4</B></Item></Delete></Request>'
  451. ],
  452. // Test adding root node attributes after nodes
  453. [
  454. [
  455. 'data' => [
  456. 'xmlRoot' => [
  457. 'name' => 'test'
  458. ]
  459. ],
  460. 'parameters' => [
  461. 'Foo' => ['location' => 'xml', 'type' => 'string'],
  462. 'Baz' => ['location' => 'xml', 'type' => 'string', 'data' => ['xmlAttribute' => true]],
  463. ]
  464. ],
  465. ['Foo' => 'test', 'Baz' => 'bar'],
  466. '<test Baz="bar"><Foo>test</Foo></test>'
  467. ],
  468. ];
  469. }
  470. /**
  471. * @param array $operation
  472. * @param array $input
  473. * @param string $xml
  474. * @dataProvider xmlProvider
  475. * @group RequestLocation
  476. */
  477. public function testSerializesXml(array $operation, array $input, $xml)
  478. {
  479. $container = [];
  480. $history = Middleware::history($container);
  481. $mock = new MockHandler([new Response(200)]);
  482. $stack = new HandlerStack($mock);
  483. $stack->push($history);
  484. $operation['uri'] = 'http://httpbin.org';
  485. $client = new GuzzleClient(
  486. new Client(['handler' => $stack]),
  487. new Description([
  488. 'operations' => [
  489. 'foo' => $operation
  490. ]
  491. ])
  492. );
  493. $command = $client->getCommand('foo', $input);
  494. $client->execute($command);
  495. $this->assertCount(1, $container);
  496. foreach ($container as $transaction) {
  497. /** @var Request $request */
  498. $request = $transaction['request'];
  499. if (empty($input)) {
  500. if ($request->hasHeader('Content-Type')) {
  501. $this->assertArraySubset([0 => ''], $request->getHeader('Content-Type'));
  502. }
  503. } else {
  504. $this->assertArraySubset([0 => 'application/xml'], $request->getHeader('Content-Type'));
  505. }
  506. $body = str_replace(["\n", "<?xml version=\"1.0\"?>"], '', (string) $request->getBody());
  507. $this->assertEquals($xml, $body);
  508. }
  509. }
  510. }