QueryTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Tests\Psr7;
  4. use GuzzleHttp\Psr7;
  5. use PHPUnit\Framework\TestCase;
  6. class QueryTest extends TestCase
  7. {
  8. public function parseQueryProvider()
  9. {
  10. return [
  11. // Does not need to parse when the string is empty
  12. ['', []],
  13. // Can parse mult-values items
  14. ['q=a&q=b', ['q' => ['a', 'b']]],
  15. // Can parse multi-valued items that use numeric indices
  16. ['q[0]=a&q[1]=b', ['q[0]' => 'a', 'q[1]' => 'b']],
  17. // Can parse duplicates and does not include numeric indices
  18. ['q[]=a&q[]=b', ['q[]' => ['a', 'b']]],
  19. // Ensures that the value of "q" is an array even though one value
  20. ['q[]=a', ['q[]' => 'a']],
  21. // Does not modify "." to "_" like PHP's parse_str()
  22. ['q.a=a&q.b=b', ['q.a' => 'a', 'q.b' => 'b']],
  23. // Can decode %20 to " "
  24. ['q%20a=a%20b', ['q a' => 'a b']],
  25. // Can parse funky strings with no values by assigning each to null
  26. ['q&a', ['q' => null, 'a' => null]],
  27. // Does not strip trailing equal signs
  28. ['data=abc=', ['data' => 'abc=']],
  29. // Can store duplicates without affecting other values
  30. ['foo=a&foo=b&?µ=c', ['foo' => ['a', 'b'], '?µ' => 'c']],
  31. // Sets value to null when no "=" is present
  32. ['foo', ['foo' => null]],
  33. // Preserves "0" keys.
  34. ['0', ['0' => null]],
  35. // Sets the value to an empty string when "=" is present
  36. ['0=', ['0' => '']],
  37. // Preserves falsey keys
  38. ['var=0', ['var' => '0']],
  39. ['a[b][c]=1&a[b][c]=2', ['a[b][c]' => ['1', '2']]],
  40. ['a[b]=c&a[d]=e', ['a[b]' => 'c', 'a[d]' => 'e']],
  41. // Ensure it doesn't leave things behind with repeated values
  42. // Can parse mult-values items
  43. ['q=a&q=b&q=c', ['q' => ['a', 'b', 'c']]],
  44. // Keeps first null when parsing mult-values
  45. ['q&q=&q=a', ['q' => [null, '', 'a']]],
  46. ];
  47. }
  48. /**
  49. * @dataProvider parseQueryProvider
  50. */
  51. public function testParsesQueries($input, $output): void
  52. {
  53. $result = Psr7\Query::parse($input);
  54. self::assertSame($output, $result);
  55. }
  56. public function testDoesNotDecode(): void
  57. {
  58. $str = 'foo%20=bar';
  59. $data = Psr7\Query::parse($str, false);
  60. self::assertSame(['foo%20' => 'bar'], $data);
  61. }
  62. /**
  63. * @dataProvider parseQueryProvider
  64. */
  65. public function testParsesAndBuildsQueries($input): void
  66. {
  67. $result = Psr7\Query::parse($input, false);
  68. self::assertSame($input, Psr7\Query::build($result, false));
  69. }
  70. public function testEncodesWithRfc1738(): void
  71. {
  72. $str = Psr7\Query::build(['foo bar' => 'baz+'], PHP_QUERY_RFC1738);
  73. self::assertSame('foo+bar=baz%2B', $str);
  74. }
  75. public function testEncodesWithRfc3986(): void
  76. {
  77. $str = Psr7\Query::build(['foo bar' => 'baz+'], PHP_QUERY_RFC3986);
  78. self::assertSame('foo%20bar=baz%2B', $str);
  79. }
  80. public function testDoesNotEncode(): void
  81. {
  82. $str = Psr7\Query::build(['foo bar' => 'baz+'], false);
  83. self::assertSame('foo bar=baz+', $str);
  84. }
  85. public function testCanControlDecodingType(): void
  86. {
  87. $result = Psr7\Query::parse('var=foo+bar', PHP_QUERY_RFC3986);
  88. self::assertSame('foo+bar', $result['var']);
  89. $result = Psr7\Query::parse('var=foo+bar', PHP_QUERY_RFC1738);
  90. self::assertSame('foo bar', $result['var']);
  91. }
  92. public function testBuildBooleans(): void
  93. {
  94. $data = [
  95. 'true' => true,
  96. 'false' => false,
  97. ];
  98. self::assertEquals(http_build_query($data), Psr7\Query::build($data));
  99. $data = [
  100. 'foo' => [true, 'true'],
  101. 'bar' => [false, 'false'],
  102. ];
  103. self::assertEquals('foo=1&foo=true&bar=0&bar=false', Psr7\Query::build($data, PHP_QUERY_RFC1738));
  104. }
  105. }