QueryLocationTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
  3. use GuzzleHttp\Command\Command;
  4. use GuzzleHttp\Command\Guzzle\Operation;
  5. use GuzzleHttp\Command\Guzzle\Parameter;
  6. use GuzzleHttp\Command\Guzzle\RequestLocation\QueryLocation;
  7. use GuzzleHttp\Psr7;
  8. use GuzzleHttp\Psr7\Request;
  9. /**
  10. * @covers \GuzzleHttp\Command\Guzzle\RequestLocation\QueryLocation
  11. * @covers \GuzzleHttp\Command\Guzzle\RequestLocation\AbstractLocation
  12. */
  13. class QueryLocationTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function queryProvider()
  16. {
  17. return [
  18. [['foo' => 'bar'], 'foo=bar'],
  19. [['foo' => [1, 2]], 'foo[0]=1&foo[1]=2'],
  20. [['foo' => ['bar' => 'baz', 'bim' => [4, 5]]], 'foo[bar]=baz&foo[bim][0]=4&foo[bim][1]=5']
  21. ];
  22. }
  23. /**
  24. * @group RequestLocation
  25. */
  26. public function testVisitsLocation()
  27. {
  28. $location = new QueryLocation();
  29. $command = new Command('foo', ['foo' => 'bar']);
  30. $request = new Request('POST', 'http://httbin.org');
  31. $param = new Parameter(['name' => 'foo']);
  32. $request = $location->visit($command, $request, $param);
  33. $this->assertEquals('foo=bar', urldecode($request->getUri()->getQuery()));
  34. }
  35. public function testVisitsMultipleLocations()
  36. {
  37. $request = new Request('POST', 'http://httbin.org');
  38. // First location
  39. $location = new QueryLocation();
  40. $command = new Command('foo', ['foo' => 'bar']);
  41. $param = new Parameter(['name' => 'foo']);
  42. $request = $location->visit($command, $request, $param);
  43. // Second location
  44. $location = new QueryLocation();
  45. $command = new Command('baz', ['baz' => [6, 7]]);
  46. $param = new Parameter(['name' => 'baz']);
  47. $request = $location->visit($command, $request, $param);
  48. $this->assertEquals('foo=bar&baz[0]=6&baz[1]=7', urldecode($request->getUri()->getQuery()));
  49. }
  50. /**
  51. * @group RequestLocation
  52. */
  53. public function testAddsAdditionalProperties()
  54. {
  55. $location = new QueryLocation();
  56. $command = new Command('foo', ['foo' => 'bar']);
  57. $command['add'] = 'props';
  58. $operation = new Operation([
  59. 'additionalParameters' => [
  60. 'location' => 'query'
  61. ]
  62. ]);
  63. $request = new Request('POST', 'http://httbin.org');
  64. $request = $location->after($command, $request, $operation);
  65. $this->assertEquals('props', Psr7\parse_query($request->getUri()->getQuery())['add']);
  66. }
  67. }