UriTemplateTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace GuzzleHttp\Tests;
  3. use GuzzleHttp\UriTemplate;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * @covers GuzzleHttp\UriTemplate
  7. */
  8. class UriTemplateTest extends TestCase
  9. {
  10. /**
  11. * @return array
  12. */
  13. public function templateProvider()
  14. {
  15. $params = array(
  16. 'var' => 'value',
  17. 'hello' => 'Hello World!',
  18. 'empty' => '',
  19. 'path' => '/foo/bar',
  20. 'x' => '1024',
  21. 'y' => '768',
  22. 'null' => null,
  23. 'list' => array('red', 'green', 'blue'),
  24. 'keys' => array(
  25. "semi" => ';',
  26. "dot" => '.',
  27. "comma" => ','
  28. ),
  29. 'empty_keys' => array(),
  30. );
  31. return array_map(function ($t) use ($params) {
  32. $t[] = $params;
  33. return $t;
  34. }, array(
  35. array('foo', 'foo'),
  36. array('{var}', 'value'),
  37. array('{hello}', 'Hello%20World%21'),
  38. array('{+var}', 'value'),
  39. array('{+hello}', 'Hello%20World!'),
  40. array('{+path}/here', '/foo/bar/here'),
  41. array('here?ref={+path}', 'here?ref=/foo/bar'),
  42. array('X{#var}', 'X#value'),
  43. array('X{#hello}', 'X#Hello%20World!'),
  44. array('map?{x,y}', 'map?1024,768'),
  45. array('{x,hello,y}', '1024,Hello%20World%21,768'),
  46. array('{+x,hello,y}', '1024,Hello%20World!,768'),
  47. array('{+path,x}/here', '/foo/bar,1024/here'),
  48. array('{#x,hello,y}', '#1024,Hello%20World!,768'),
  49. array('{#path,x}/here', '#/foo/bar,1024/here'),
  50. array('X{.var}', 'X.value'),
  51. array('X{.x,y}', 'X.1024.768'),
  52. array('{/var}', '/value'),
  53. array('{/var,x}/here', '/value/1024/here'),
  54. array('{;x,y}', ';x=1024;y=768'),
  55. array('{;x,y,empty}', ';x=1024;y=768;empty'),
  56. array('{?x,y}', '?x=1024&y=768'),
  57. array('{?x,y,empty}', '?x=1024&y=768&empty='),
  58. array('?fixed=yes{&x}', '?fixed=yes&x=1024'),
  59. array('{&x,y,empty}', '&x=1024&y=768&empty='),
  60. array('{var:3}', 'val'),
  61. array('{var:30}', 'value'),
  62. array('{list}', 'red,green,blue'),
  63. array('{list*}', 'red,green,blue'),
  64. array('{keys}', 'semi,%3B,dot,.,comma,%2C'),
  65. array('{keys*}', 'semi=%3B,dot=.,comma=%2C'),
  66. array('{+path:6}/here', '/foo/b/here'),
  67. array('{+list}', 'red,green,blue'),
  68. array('{+list*}', 'red,green,blue'),
  69. array('{+keys}', 'semi,;,dot,.,comma,,'),
  70. array('{+keys*}', 'semi=;,dot=.,comma=,'),
  71. array('{#path:6}/here', '#/foo/b/here'),
  72. array('{#list}', '#red,green,blue'),
  73. array('{#list*}', '#red,green,blue'),
  74. array('{#keys}', '#semi,;,dot,.,comma,,'),
  75. array('{#keys*}', '#semi=;,dot=.,comma=,'),
  76. array('X{.var:3}', 'X.val'),
  77. array('X{.list}', 'X.red,green,blue'),
  78. array('X{.list*}', 'X.red.green.blue'),
  79. array('X{.keys}', 'X.semi,%3B,dot,.,comma,%2C'),
  80. array('X{.keys*}', 'X.semi=%3B.dot=..comma=%2C'),
  81. array('{/var:1,var}', '/v/value'),
  82. array('{/list}', '/red,green,blue'),
  83. array('{/list*}', '/red/green/blue'),
  84. array('{/list*,path:4}', '/red/green/blue/%2Ffoo'),
  85. array('{/keys}', '/semi,%3B,dot,.,comma,%2C'),
  86. array('{/keys*}', '/semi=%3B/dot=./comma=%2C'),
  87. array('{;hello:5}', ';hello=Hello'),
  88. array('{;list}', ';list=red,green,blue'),
  89. array('{;list*}', ';list=red;list=green;list=blue'),
  90. array('{;keys}', ';keys=semi,%3B,dot,.,comma,%2C'),
  91. array('{;keys*}', ';semi=%3B;dot=.;comma=%2C'),
  92. array('{?var:3}', '?var=val'),
  93. array('{?list}', '?list=red,green,blue'),
  94. array('{?list*}', '?list=red&list=green&list=blue'),
  95. array('{?keys}', '?keys=semi,%3B,dot,.,comma,%2C'),
  96. array('{?keys*}', '?semi=%3B&dot=.&comma=%2C'),
  97. array('{&var:3}', '&var=val'),
  98. array('{&list}', '&list=red,green,blue'),
  99. array('{&list*}', '&list=red&list=green&list=blue'),
  100. array('{&keys}', '&keys=semi,%3B,dot,.,comma,%2C'),
  101. array('{&keys*}', '&semi=%3B&dot=.&comma=%2C'),
  102. array('{.null}', ''),
  103. array('{.null,var}', '.value'),
  104. array('X{.empty_keys*}', 'X'),
  105. array('X{.empty_keys}', 'X'),
  106. // Test that missing expansions are skipped
  107. array('test{&missing*}', 'test'),
  108. // Test that multiple expansions can be set
  109. array('http://{var}/{var:2}{?keys*}', 'http://value/va?semi=%3B&dot=.&comma=%2C'),
  110. // Test more complex query string stuff
  111. array('http://www.test.com{+path}{?var,keys*}', 'http://www.test.com/foo/bar?var=value&semi=%3B&dot=.&comma=%2C')
  112. ));
  113. }
  114. /**
  115. * @dataProvider templateProvider
  116. */
  117. public function testExpandsUriTemplates($template, $expansion, $params)
  118. {
  119. $uri = new UriTemplate();
  120. $this->assertSame($expansion, $uri->expand($template, $params));
  121. }
  122. public function expressionProvider()
  123. {
  124. return array(
  125. array(
  126. '{+var*}', array(
  127. 'operator' => '+',
  128. 'values' => array(
  129. array('modifier' => '*', 'value' => 'var')
  130. )
  131. ),
  132. ),
  133. array(
  134. '{?keys,var,val}', array(
  135. 'operator' => '?',
  136. 'values' => array(
  137. array('value' => 'keys', 'modifier' => ''),
  138. array('value' => 'var', 'modifier' => ''),
  139. array('value' => 'val', 'modifier' => '')
  140. )
  141. ),
  142. ),
  143. array(
  144. '{+x,hello,y}', array(
  145. 'operator' => '+',
  146. 'values' => array(
  147. array('value' => 'x', 'modifier' => ''),
  148. array('value' => 'hello', 'modifier' => ''),
  149. array('value' => 'y', 'modifier' => '')
  150. )
  151. )
  152. )
  153. );
  154. }
  155. /**
  156. * @dataProvider expressionProvider
  157. */
  158. public function testParsesExpressions($exp, $data)
  159. {
  160. $template = new UriTemplate();
  161. // Access the config object
  162. $class = new \ReflectionClass($template);
  163. $method = $class->getMethod('parseExpression');
  164. $method->setAccessible(true);
  165. $exp = substr($exp, 1, -1);
  166. $this->assertSame($data, $method->invokeArgs($template, array($exp)));
  167. }
  168. /**
  169. * @ticket https://github.com/guzzle/guzzle/issues/90
  170. */
  171. public function testAllowsNestedArrayExpansion()
  172. {
  173. $template = new UriTemplate();
  174. $result = $template->expand('http://example.com{+path}{/segments}{?query,data*,foo*}', array(
  175. 'path' => '/foo/bar',
  176. 'segments' => array('one', 'two'),
  177. 'query' => 'test',
  178. 'data' => array(
  179. 'more' => array('fun', 'ice cream')
  180. ),
  181. 'foo' => array(
  182. 'baz' => array(
  183. 'bar' => 'fizz',
  184. 'test' => 'buzz'
  185. ),
  186. 'bam' => 'boo'
  187. )
  188. ));
  189. $this->assertSame('http://example.com/foo/bar/one,two?query=test&more%5B0%5D=fun&more%5B1%5D=ice%20cream&baz%5Bbar%5D=fizz&baz%5Btest%5D=buzz&bam=boo', $result);
  190. }
  191. }