PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/google/apiclient/tests/Google/Utils/UriTemplateTest.php

https://gitlab.com/ndhoang93/ytb
PHP | 296 lines | 219 code | 21 blank | 56 comment | 0 complexity | 00266c7f6e9c5c94ae2668a5027c8d86 MD5 | raw file
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class Google_Utils_UriTemplateTest extends BaseTest
  21. {
  22. public function testLevelOne()
  23. {
  24. $var = "value";
  25. $hello = "Hello World!";
  26. $urit = new Google_Utils_UriTemplate();
  27. $this->assertEquals(
  28. "value",
  29. $urit->parse("{var}", array("var" => $var))
  30. );
  31. $this->assertEquals(
  32. "Hello%20World%21",
  33. $urit->parse("{hello}", array("hello" => $hello))
  34. );
  35. }
  36. public function testLevelTwo()
  37. {
  38. $var = "value";
  39. $hello = "Hello World!";
  40. $path = "/foo/bar";
  41. $urit = new Google_Utils_UriTemplate();
  42. $this->assertEquals(
  43. "value",
  44. $urit->parse("{+var}", array("var" => $var))
  45. );
  46. $this->assertEquals(
  47. "Hello%20World!",
  48. $urit->parse("{+hello}", array("hello" => $hello))
  49. );
  50. $this->assertEquals(
  51. "/foo/bar/here",
  52. $urit->parse("{+path}/here", array("path" => $path))
  53. );
  54. $this->assertEquals(
  55. "here?ref=/foo/bar",
  56. $urit->parse("here?ref={+path}", array("path" => $path))
  57. );
  58. $this->assertEquals(
  59. "X#value",
  60. $urit->parse("X{#var}", array("var" => $var))
  61. );
  62. $this->assertEquals(
  63. "X#Hello%20World!",
  64. $urit->parse("X{#hello}", array("hello" => $hello))
  65. );
  66. }
  67. public function testLevelThree()
  68. {
  69. $var = "value";
  70. $hello = "Hello World!";
  71. $empty = '';
  72. $path = "/foo/bar";
  73. $x = "1024";
  74. $y = "768";
  75. $urit = new Google_Utils_UriTemplate();
  76. $this->assertEquals(
  77. "map?1024,768",
  78. $urit->parse("map?{x,y}", array("x" => $x, "y" => $y))
  79. );
  80. $this->assertEquals(
  81. "1024,Hello%20World%21,768",
  82. $urit->parse("{x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello))
  83. );
  84. $this->assertEquals(
  85. "1024,Hello%20World!,768",
  86. $urit->parse("{+x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello))
  87. );
  88. $this->assertEquals(
  89. "/foo/bar,1024/here",
  90. $urit->parse("{+path,x}/here", array("x" => $x, "path" => $path))
  91. );
  92. $this->assertEquals(
  93. "#1024,Hello%20World!,768",
  94. $urit->parse("{#x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello))
  95. );
  96. $this->assertEquals(
  97. "#/foo/bar,1024/here",
  98. $urit->parse("{#path,x}/here", array("x" => $x, "path" => $path))
  99. );
  100. $this->assertEquals(
  101. "X.value",
  102. $urit->parse("X{.var}", array("var" => $var))
  103. );
  104. $this->assertEquals(
  105. "X.1024.768",
  106. $urit->parse("X{.x,y}", array("x" => $x, "y" => $y))
  107. );
  108. $this->assertEquals(
  109. "X.value",
  110. $urit->parse("X{.var}", array("var" => $var))
  111. );
  112. $this->assertEquals(
  113. "X.1024.768",
  114. $urit->parse("X{.x,y}", array("x" => $x, "y" => $y))
  115. );
  116. $this->assertEquals(
  117. "/value",
  118. $urit->parse("{/var}", array("var" => $var))
  119. );
  120. $this->assertEquals(
  121. "/value/1024/here",
  122. $urit->parse("{/var,x}/here", array("x" => $x, "var" => $var))
  123. );
  124. $this->assertEquals(
  125. ";x=1024;y=768",
  126. $urit->parse("{;x,y}", array("x" => $x, "y" => $y))
  127. );
  128. $this->assertEquals(
  129. ";x=1024;y=768;empty",
  130. $urit->parse("{;x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty))
  131. );
  132. $this->assertEquals(
  133. "?x=1024&y=768",
  134. $urit->parse("{?x,y}", array("x" => $x, "y" => $y))
  135. );
  136. $this->assertEquals(
  137. "?x=1024&y=768&empty=",
  138. $urit->parse("{?x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty))
  139. );
  140. $this->assertEquals(
  141. "?fixed=yes&x=1024",
  142. $urit->parse("?fixed=yes{&x}", array("x" => $x, "y" => $y))
  143. );
  144. $this->assertEquals(
  145. "&x=1024&y=768&empty=",
  146. $urit->parse("{&x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty))
  147. );
  148. }
  149. public function testLevelFour()
  150. {
  151. $values = array(
  152. 'var' => "value",
  153. 'hello' => "Hello World!",
  154. 'path' => "/foo/bar",
  155. 'list' => array("red", "green", "blue"),
  156. 'keys' => array("semi" => ";", "dot" => ".", "comma" => ","),
  157. );
  158. $tests = array(
  159. "{var:3}" => "val",
  160. "{var:30}" => "value",
  161. "{list}" => "red,green,blue",
  162. "{list*}" => "red,green,blue",
  163. "{keys}" => "semi,%3B,dot,.,comma,%2C",
  164. "{keys*}" => "semi=%3B,dot=.,comma=%2C",
  165. "{+path:6}/here" => "/foo/b/here",
  166. "{+list}" => "red,green,blue",
  167. "{+list*}" => "red,green,blue",
  168. "{+keys}" => "semi,;,dot,.,comma,,",
  169. "{+keys*}" => "semi=;,dot=.,comma=,",
  170. "{#path:6}/here" => "#/foo/b/here",
  171. "{#list}" => "#red,green,blue",
  172. "{#list*}" => "#red,green,blue",
  173. "{#keys}" => "#semi,;,dot,.,comma,,",
  174. "{#keys*}" => "#semi=;,dot=.,comma=,",
  175. "X{.var:3}" => "X.val",
  176. "X{.list}" => "X.red,green,blue",
  177. "X{.list*}" => "X.red.green.blue",
  178. "X{.keys}" => "X.semi,%3B,dot,.,comma,%2C",
  179. "X{.keys*}" => "X.semi=%3B.dot=..comma=%2C",
  180. "{/var:1,var}" => "/v/value",
  181. "{/list}" => "/red,green,blue",
  182. "{/list*}" => "/red/green/blue",
  183. "{/list*,path:4}" => "/red/green/blue/%2Ffoo",
  184. "{/keys}" => "/semi,%3B,dot,.,comma,%2C",
  185. "{/keys*}" => "/semi=%3B/dot=./comma=%2C",
  186. "{;hello:5}" => ";hello=Hello",
  187. "{;list}" => ";list=red,green,blue",
  188. "{;list*}" => ";list=red;list=green;list=blue",
  189. "{;keys}" => ";keys=semi,%3B,dot,.,comma,%2C",
  190. "{;keys*}" => ";semi=%3B;dot=.;comma=%2C",
  191. "{?var:3}" => "?var=val",
  192. "{?list}" => "?list=red,green,blue",
  193. "{?list*}" => "?list=red&list=green&list=blue",
  194. "{?keys}" => "?keys=semi,%3B,dot,.,comma,%2C",
  195. "{?keys*}" => "?semi=%3B&dot=.&comma=%2C",
  196. "{&var:3}" => "&var=val",
  197. "{&list}" => "&list=red,green,blue",
  198. "{&list*}" => "&list=red&list=green&list=blue",
  199. "{&keys}" => "&keys=semi,%3B,dot,.,comma,%2C",
  200. "{&keys*}" => "&semi=%3B&dot=.&comma=%2C",
  201. "find{?list*}" => "find?list=red&list=green&list=blue",
  202. "www{.list*}" => "www.red.green.blue"
  203. );
  204. $urit = new Google_Utils_UriTemplate();
  205. foreach ($tests as $input => $output) {
  206. $this->assertEquals($output, $urit->parse($input, $values), $input . " failed");
  207. }
  208. }
  209. public function testMultipleAnnotations()
  210. {
  211. $var = "value";
  212. $hello = "Hello World!";
  213. $urit = new Google_Utils_UriTemplate();
  214. $this->assertEquals(
  215. "http://www.google.com/Hello%20World!?var=value",
  216. $urit->parse(
  217. "http://www.google.com/{+hello}{?var}",
  218. array("var" => $var, "hello" => $hello)
  219. )
  220. );
  221. $params = array(
  222. "playerId" => "me",
  223. "leaderboardId" => "CgkIhcG1jYEbEAIQAw",
  224. "timeSpan" => "ALL_TIME",
  225. "other" => "irrelevant"
  226. );
  227. $this->assertEquals(
  228. "players/me/leaderboards/CgkIhcG1jYEbEAIQAw/scores/ALL_TIME",
  229. $urit->parse(
  230. "players/{playerId}/leaderboards/{leaderboardId}/scores/{timeSpan}",
  231. $params
  232. )
  233. );
  234. }
  235. /**
  236. * This test test against the JSON files defined in
  237. * https://github.com/uri-templates/uritemplate-test
  238. *
  239. * We don't ship these tests with it, so they'll just silently
  240. * skip unless provided - this is mainly for use when
  241. * making specific URI template changes and wanting
  242. * to do a full regression check.
  243. */
  244. public function testAgainstStandardTests()
  245. {
  246. $location = "../../uritemplate-test/*.json";
  247. $urit = new Google_Utils_UriTemplate();
  248. foreach (glob($location) as $file) {
  249. $test = json_decode(file_get_contents($file), true);
  250. foreach ($test as $title => $testsets) {
  251. foreach ($testsets['testcases'] as $cases) {
  252. $input = $cases[0];
  253. $output = $cases[1];
  254. if ($output == false) {
  255. continue; // skipping negative tests for now
  256. } else if (is_array($output)) {
  257. $response = $urit->parse($input, $testsets['variables']);
  258. $this->assertContains(
  259. $response,
  260. $output,
  261. $input . " failed from " . $title
  262. );
  263. } else {
  264. $this->assertEquals(
  265. $output,
  266. $urit->parse($input, $testsets['variables']),
  267. $input . " failed."
  268. );
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }