/core/src/test/php/net/xp_framework/unittest/webservices/rest/srv/RestRouteTest.class.php

https://github.com/ghiata/xp-framework · PHP · 362 lines · 168 code · 35 blank · 159 comment · 0 complexity · 98b8d1ecc1ac5af4dfd4118338f0d6d6 MD5 · raw file

  1. <?php namespace net\xp_framework\unittest\webservices\rest\srv;
  2. use unittest\TestCase;
  3. use webservices\rest\srv\RestRoute;
  4. /**
  5. * Test default router
  6. *
  7. * @see xp://webservices.rest.srv.RestRoute
  8. */
  9. class RestRouteTest extends TestCase {
  10. protected $handler= null;
  11. protected $target= null;
  12. /**
  13. * Setup
  14. *
  15. */
  16. public function setUp() {
  17. $this->handler= $this->getClass();
  18. $this->target= $this->handler->getMethod('fixtureTarget');
  19. }
  20. /**
  21. * Target method
  22. */
  23. #[@webservice]
  24. public function fixtureTarget() {
  25. // Intentionally empty
  26. }
  27. /**
  28. * Test getVerb()
  29. *
  30. */
  31. #[@test]
  32. public function verb() {
  33. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, null, null);
  34. $this->assertEquals('GET', $r->getVerb());
  35. }
  36. /**
  37. * Test getVerb()
  38. *
  39. */
  40. #[@test]
  41. public function verb_is_uppercased() {
  42. $r= new RestRoute('Get', '/resource', $this->handler, $this->target, null, null);
  43. $this->assertEquals('GET', $r->getVerb());
  44. }
  45. /**
  46. * Test getPath()
  47. *
  48. */
  49. #[@test]
  50. public function path() {
  51. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, null, null);
  52. $this->assertEquals('/resource', $r->getPath());
  53. }
  54. /**
  55. * Test getHandler()
  56. *
  57. */
  58. #[@test]
  59. public function handler() {
  60. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, null, null);
  61. $this->assertEquals($this->handler, $r->getHandler());
  62. }
  63. /**
  64. * Test getTarget()
  65. *
  66. */
  67. #[@test]
  68. public function target() {
  69. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, null, null);
  70. $this->assertEquals($this->target, $r->getTarget());
  71. }
  72. /**
  73. * Test getAccepts()
  74. *
  75. */
  76. #[@test]
  77. public function accepts() {
  78. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, array('text/json'), null);
  79. $this->assertEquals(array('text/json'), $r->getAccepts());
  80. }
  81. /**
  82. * Test getAccepts()
  83. *
  84. */
  85. #[@test]
  86. public function accepts_default() {
  87. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, null, null);
  88. $this->assertEquals(array('text/json'), $r->getAccepts((array)'text/json'));
  89. }
  90. /**
  91. * Test getProduces()
  92. *
  93. */
  94. #[@test]
  95. public function produces() {
  96. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, null, array('text/json'));
  97. $this->assertEquals(array('text/json'), $r->getProduces());
  98. }
  99. /**
  100. * Test getProduces()
  101. *
  102. */
  103. #[@test]
  104. public function produces_default() {
  105. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, null, null);
  106. $this->assertEquals(array('text/json'), $r->getProduces((array)'text/json'));
  107. }
  108. /**
  109. * Test getPattern()
  110. *
  111. */
  112. #[@test]
  113. public function pattern() {
  114. $r= new RestRoute('GET', '/resource', $this->handler, $this->target, null, null);
  115. $this->assertEquals('#^/resource$#', $r->getPattern());
  116. }
  117. /**
  118. * Test getPattern()
  119. *
  120. */
  121. #[@test]
  122. public function pattern_with_placeholder() {
  123. $r= new RestRoute('GET', '/resource/{id}', $this->handler, $this->target, null, null);
  124. $this->assertEquals('#^/resource/(?P<id>[^/]+)$#', $r->getPattern());
  125. }
  126. /**
  127. * Test getPattern()
  128. *
  129. */
  130. #[@test]
  131. public function pattern_with_two_placeholders() {
  132. $r= new RestRoute('GET', '/resource/{id}/{sub}', $this->handler, $this->target, null, null);
  133. $this->assertEquals('#^/resource/(?P<id>[^/]+)/(?P<sub>[^/]+)$#', $r->getPattern());
  134. }
  135. /**
  136. * Test toString()
  137. *
  138. */
  139. #[@test]
  140. public function string_representation() {
  141. $r= new RestRoute('GET', '/resource/{id}/{sub}', $this->handler, $this->target, null, null);
  142. $this->assertEquals(
  143. 'webservices.rest.srv.RestRoute(GET /resource/{id}/{sub} -> var net.xp_framework.unittest.webservices.rest.srv.RestRouteTest::fixtureTarget())',
  144. $r->toString()
  145. );
  146. }
  147. /**
  148. * Test toString()
  149. *
  150. */
  151. #[@test]
  152. public function string_representation_with_produces() {
  153. $r= new RestRoute('GET', '/resource/{id}/{sub}', $this->handler, $this->target, null, array('text/json'));
  154. $this->assertEquals(
  155. 'webservices.rest.srv.RestRoute(GET /resource/{id}/{sub} -> var net.xp_framework.unittest.webservices.rest.srv.RestRouteTest::fixtureTarget() @ text/json)',
  156. $r->toString()
  157. );
  158. }
  159. /**
  160. * Test toString()
  161. *
  162. */
  163. #[@test]
  164. public function string_representation_with_accepts_and_produces() {
  165. $r= new RestRoute('GET', '/resource/{id}/{sub}', $this->handler, $this->target, array('text/xml'), array('text/json'));
  166. $this->assertEquals(
  167. 'webservices.rest.srv.RestRoute(GET /resource/{id}/{sub} @ text/xml -> var net.xp_framework.unittest.webservices.rest.srv.RestRouteTest::fixtureTarget() @ text/json)',
  168. $r->toString()
  169. );
  170. }
  171. /**
  172. * Test toString()
  173. *
  174. */
  175. #[@test]
  176. public function string_representation_with_param() {
  177. $r= new RestRoute('GET', '/resource/{id}', $this->handler, $this->target, null, null);
  178. $r->addParam('id', new \webservices\rest\srv\RestParamSource('id', \webservices\rest\srv\ParamReader::forName('path')));
  179. $this->assertEquals(
  180. 'webservices.rest.srv.RestRoute(GET /resource/{id} -> var net.xp_framework.unittest.webservices.rest.srv.RestRouteTest::fixtureTarget(@$id: path(\'id\')))',
  181. $r->toString()
  182. );
  183. }
  184. /**
  185. * Test toString()
  186. *
  187. */
  188. #[@test]
  189. public function string_representation_with_params() {
  190. $r= new RestRoute('GET', '/resource/{id}/{sub}', $this->handler, $this->target, null, null);
  191. $r->addParam('id', new \webservices\rest\srv\RestParamSource('id', \webservices\rest\srv\ParamReader::forName('path')));
  192. $r->addParam('sub', new \webservices\rest\srv\RestParamSource('sub', \webservices\rest\srv\ParamReader::forName('path')));
  193. $this->assertEquals(
  194. 'webservices.rest.srv.RestRoute(GET /resource/{id}/{sub} -> var net.xp_framework.unittest.webservices.rest.srv.RestRouteTest::fixtureTarget(@$id: path(\'id\'), @$sub: path(\'sub\')))',
  195. $r->toString()
  196. );
  197. }
  198. /**
  199. * Test appliesTo()
  200. *
  201. */
  202. #[@test]
  203. public function applies_to_matches_resource_and_subresource() {
  204. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  205. $this->assertEquals(
  206. array(0 => '/binford/6100/chainsaw', 'id' => '6100', 1 => '6100', 'name' => 'chainsaw', 2 => 'chainsaw'),
  207. $r->appliesTo('/binford/6100/chainsaw')
  208. );
  209. }
  210. /**
  211. * Test appliesTo()
  212. *
  213. */
  214. #[@test]
  215. public function applies_to_matches_resource_and_subresource_with_star() {
  216. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  217. $this->assertEquals(
  218. array(0 => '/binford/61/*', 'id' => '61', 1 => '61', 'name' => '*', 2 => '*'),
  219. $r->appliesTo('/binford/61/*')
  220. );
  221. }
  222. /**
  223. * Test appliesTo()
  224. *
  225. */
  226. #[@test]
  227. public function applies_to_matches_resource_and_subresource_with_dot() {
  228. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  229. $this->assertEquals(
  230. array(0 => '/binford/61/.', 'id' => '61', 1 => '61', 'name' => '.', 2 => '.'),
  231. $r->appliesTo('/binford/61/.')
  232. );
  233. }
  234. /**
  235. * Test appliesTo()
  236. *
  237. */
  238. #[@test]
  239. public function applies_to_matches_resource_and_subresource_with_urlencoded() {
  240. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  241. $this->assertEquals(
  242. array(0 => '/binford/61/%40', 'id' => '61', 1 => '61', 'name' => '%40', 2 => '%40'),
  243. $r->appliesTo('/binford/61/%40')
  244. );
  245. }
  246. /**
  247. * Test appliesTo()
  248. *
  249. */
  250. #[@test]
  251. public function applies_to_matches_resource_with_dash() {
  252. $r= new RestRoute('GET', '/binford/{id}-{name}', null, null, null, null);
  253. $this->assertEquals(
  254. array(0 => '/binford/610-scissors', 'id' => '610', 1 => '610', 'name' => 'scissors', 2 => 'scissors'),
  255. $r->appliesTo('/binford/610-scissors')
  256. );
  257. }
  258. /**
  259. * Test appliesTo()
  260. *
  261. */
  262. #[@test]
  263. public function applies_to_matches_resource_with_prefix() {
  264. $r= new RestRoute('GET', '/binford/power{name}', null, null, null, null);
  265. $this->assertEquals(
  266. array(0 => '/binford/powercar', 'name' => 'car', 1 => 'car'),
  267. $r->appliesTo('/binford/powercar')
  268. );
  269. }
  270. /**
  271. * Test appliesTo()
  272. *
  273. */
  274. #[@test]
  275. public function applies_to_matches_resource_with_postfix() {
  276. $r= new RestRoute('GET', '/binford/{name}power', null, null, null, null);
  277. $this->assertEquals(
  278. array(0 => '/binford/morepower', 'name' => 'more', 1 => 'more'),
  279. $r->appliesTo('/binford/morepower')
  280. );
  281. }
  282. /**
  283. * Test appliesTo()
  284. *
  285. */
  286. #[@test]
  287. public function applies_to_does_not_match_empty_path_segment() {
  288. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  289. $this->assertEquals(null, $r->appliesTo('/binford//chainsaw'));
  290. }
  291. /**
  292. * Test appliesTo()
  293. *
  294. */
  295. #[@test]
  296. public function applies_to_does_not_match_base() {
  297. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  298. $this->assertNull($r->appliesTo('/binford'));
  299. }
  300. /**
  301. * Test appliesTo()
  302. *
  303. */
  304. #[@test]
  305. public function applies_to_does_not_match_partial() {
  306. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  307. $this->assertNull($r->appliesTo('/binford/6100'));
  308. }
  309. /**
  310. * Test appliesTo()
  311. *
  312. */
  313. #[@test]
  314. public function applies_to_does_not_match_partial_with_trailing_slash() {
  315. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  316. $this->assertNull($r->appliesTo('/binford/6100/'));
  317. }
  318. /**
  319. * Test appliesTo()
  320. *
  321. */
  322. #[@test]
  323. public function applies_to_does_not_match_partial_with_trailing_slashes() {
  324. $r= new RestRoute('GET', '/binford/{id}/{name}', null, null, null, null);
  325. $this->assertNull($r->appliesTo('/binford/6100//'));
  326. }
  327. }