PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/UrlHandler_Test.php

http://github.com/ethna/ethna
PHP | 341 lines | 240 code | 48 blank | 53 comment | 2 complexity | 12b3c5a4f56a68ff4dc14e2b67f93721 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * UrlHandler_Test.php
  4. */
  5. /**
  6. * Ethna_UrlHandler??????????
  7. *
  8. * @access public
  9. */
  10. class Ethna_UrlHandler_Test extends Ethna_UnitTestBase
  11. {
  12. var $url_handler;
  13. function setUp()
  14. {
  15. $this->url_handler = new Ethna_UrlHandler_TestClass($this);
  16. }
  17. // {{{ $_simple_map
  18. var $_simple_map = array(
  19. 'entrypoint' => array(
  20. 'test_foo_bar' => array(
  21. 'path' => 'foo/bar',
  22. 'path_regexp' => false,
  23. 'path_ext' => false,
  24. ),
  25. ),
  26. );
  27. // }}}
  28. // {{{ $_complex_map
  29. var $_complex_map = array(
  30. 'entrypoint' => array(
  31. 'test_foo_bar' => array(
  32. 'path' => 'foo/bar',
  33. 'path_regexp' => array(
  34. 'dummy_index1' => '|foo/bar/([^/]*)$|',
  35. 'dummy_index2' => '|foo/bar/([^/]*)/([^/]*)$|',
  36. ),
  37. 'path_ext' => array(
  38. 'dummy_index1' => array(
  39. 'param1' => array(),
  40. ),
  41. 'dummy_index2' => array(
  42. 'param1' => array(),
  43. 'param2' => array(),
  44. ),
  45. ),
  46. ),
  47. ),
  48. );
  49. // }}}
  50. // {{{ $_prefix_suffix_map
  51. var $_prefix_suffix_map = array(
  52. 'entrypoint' => array(
  53. 'test_foo_bar' => array(
  54. 'path' => 'foo/bar',
  55. 'path_regexp' => '|foo/bar/(.*)$|',
  56. 'path_ext' => array(
  57. 'param1' => array(
  58. 'url_prefix' => 'URL-',
  59. 'url_suffix' => '-URL',
  60. 'form_prefix' => 'FORM-',
  61. 'form_suffix' => '-FORM',
  62. ),
  63. ),
  64. ),
  65. ),
  66. );
  67. // }}}
  68. // {{{ test_requestToAction_simple
  69. function test_requestToAction_simple()
  70. {
  71. // pathinfo ?? action ??
  72. $http_vars = array(
  73. '__url_handler__' => 'entrypoint', // not empty
  74. '__url_info__' => '/foo/bar', // null or not empty
  75. 'param3' => 'value3',
  76. );
  77. $this->url_handler->setActionMap($this->_simple_map);
  78. $injected = $this->url_handler->requestToAction($http_vars);
  79. // action ?????
  80. $diff = array_diff($injected, $http_vars);
  81. $this->assertEqual(count($diff), 1);
  82. $this->assertEqual($diff['action_test_foo_bar'], true);
  83. // action ??????????????????
  84. $diff = array_diff($http_vars, $injected);
  85. $this->assertEqual(count($diff), 0);
  86. // action ?????
  87. $this->url_handler->setActionMap($this->_complex_map);
  88. $injected = $this->url_handler->requestToAction($http_vars);
  89. $diff = array_diff($injected, $http_vars);
  90. $this->assertEqual(count($diff), 1);
  91. $this->assertEqual($diff['action_test_foo_bar'], true);
  92. }
  93. // }}}
  94. // {{{ test_requestToAction_nopathinfo
  95. function test_requestToAction_nopathinfo()
  96. {
  97. // pathinfo ??
  98. $http_vars = array(
  99. '__url_handler__' => 'entrypoint',
  100. '__url_info__' => null,
  101. );
  102. $this->url_handler->setActionMap($this->_complex_map);
  103. $injected = $this->url_handler->requestToAction($http_vars);
  104. // ????
  105. $diff = array_diff($injected, $http_vars);
  106. $this->assertEqual(count($diff), 0);
  107. }
  108. // }}}
  109. // {{{ test_requestToAction_withparams1
  110. function test_requestToAction_withparams1()
  111. {
  112. // pathinfo ?? action ???????????
  113. $http_vars = array(
  114. '__url_handler__' => 'entrypoint',
  115. '__url_info__' => '/foo/bar/aaa',
  116. );
  117. // ???? action_map ???: ?????? array() ???
  118. $this->url_handler->setActionMap($this->_simple_map);
  119. $injected = $this->url_handler->requestToAction($http_vars);
  120. $this->assertEqual(count($injected), 0);
  121. // action ?????? param1 ?????
  122. $this->url_handler->setActionMap($this->_complex_map);
  123. $injected = $this->url_handler->requestToAction($http_vars);
  124. $diff = array_diff($injected, $http_vars);
  125. $this->assertEqual(count($diff), 2);
  126. $this->assertEqual($diff['action_test_foo_bar'], true);
  127. $this->assertEqual($diff['param1'], 'aaa');
  128. }
  129. // }}}
  130. // {{{ test_requestToAction_withparams2
  131. function test_requestToAction_withparams2()
  132. {
  133. // pathinfo ?? action ??????????????
  134. $http_vars = array(
  135. '__url_handler__' => 'entrypoint',
  136. '__url_info__' => '/foo/bar/aaa/bbb',
  137. );
  138. $this->url_handler->setActionMap($this->_complex_map);
  139. $injected = $this->url_handler->requestToAction($http_vars);
  140. $diff = array_diff($injected, $http_vars);
  141. $this->assertEqual(count($diff), 3);
  142. $this->assertEqual($diff['action_test_foo_bar'], true);
  143. $this->assertEqual($diff['param1'], 'aaa');
  144. $this->assertEqual($diff['param2'], 'bbb');
  145. }
  146. // }}}
  147. // {{{ test_requestToAction_withparams3
  148. function test_requestToAction_withparams3()
  149. {
  150. // ??????????????????
  151. $http_vars = array(
  152. '__url_handler__' => 'entrypoint',
  153. '__url_info__' => '/foo/bar/aaa/bbb/ccc',
  154. );
  155. $this->url_handler->setActionMap($this->_complex_map);
  156. $injected = $this->url_handler->requestToAction($http_vars);
  157. $this->assertEqual(count($injected), 0);
  158. }
  159. // }}}
  160. // {{{ test_requestToAction_misc
  161. function test_requestToAction_misc()
  162. {
  163. // ??? pathinfo ?????
  164. $http_vars = array(
  165. '__url_handler__' => 'entrypoint',
  166. );
  167. $this->url_handler->setActionMap($this->_complex_map);
  168. // ??? slash ?????????
  169. $http_vars['__url_info__'] = '///foo///bar///value1///';
  170. $injected = $this->url_handler->requestToAction($http_vars);
  171. $diff = array_diff($injected, $http_vars);
  172. $this->assertEqual($diff['action_test_foo_bar'], true);
  173. $this->assertEqual($diff['param1'], 'value1');
  174. $this->assertFalse(isset($diff['param2']));
  175. // path ? '/./' ???
  176. $http_vars['__url_info__'] = '/foo/bar/./value1';
  177. $injected = $this->url_handler->requestToAction($http_vars);
  178. $diff = array_diff($injected, $http_vars);
  179. $this->assertEqual($diff['action_test_foo_bar'], true);
  180. $this->assertEqual($diff['param1'], '.');
  181. $this->assertEqual($diff['param2'], 'value1');
  182. // path ? '/../' ???
  183. $http_vars['__url_info__'] = '/foo/bar/../baz';
  184. $injected = $this->url_handler->requestToAction($http_vars);
  185. $diff = array_diff($injected, $http_vars);
  186. $this->assertEqual($diff['action_test_foo_bar'], true);
  187. $this->assertEqual($diff['param1'], '..');
  188. $this->assertEqual($diff['param2'], 'baz');
  189. // ???????
  190. $http_vars['__url_info__'] = '/foo/bar/' . str_repeat('a', 10000);
  191. $injected = $this->url_handler->requestToAction($http_vars);
  192. $diff = array_diff($injected, $http_vars);
  193. $this->assertEqual($diff['action_test_foo_bar'], true);
  194. $this->assertTrue(isset($diff['param1']));
  195. $this->assertFalse(isset($diff['param2']));
  196. }
  197. // }}}
  198. // {{{ test_requestToAction_prefix_suffix
  199. function test_requestToAction_prefix_suffix()
  200. {
  201. $http_vars = array(
  202. '__url_handler__' => 'entrypoint',
  203. '__url_info__' => '/foo/bar/URL-value1-URL',
  204. 'param3' => 'value3',
  205. );
  206. $this->url_handler->setActionMap($this->_prefix_suffix_map);
  207. $injected = $this->url_handler->requestToAction($http_vars);
  208. $diff = array_diff($injected, $http_vars);
  209. $this->assertEqual($diff['action_test_foo_bar'], true);
  210. $this->assertEqual($diff['param1'], 'FORM-value1-FORM');
  211. }
  212. // }}}
  213. // {{{ test_actionToRequest_simple
  214. function test_actionToRequest_simple()
  215. {
  216. $action = 'test_foo_bar';
  217. $param = array(
  218. 'hoge' => 'fuga',
  219. );
  220. $this->url_handler->setActionMap($this->_simple_map);
  221. $ret = $this->url_handler->actionToRequest($action, $param);
  222. $this->assertFalse(is_null($ret));
  223. list($path, $path_key) = $ret;
  224. // action "test_foo_bar" ??????? "entrypoint" ? "/foo/bar"
  225. $this->assertEqual($path, 'entrypoint/foo/bar');
  226. $this->assertTrue($path_key == array());
  227. }
  228. // }}}
  229. // {{{ test_actionToRequest_unknownaction
  230. function test_actionToRequest_unknownaction()
  231. {
  232. $action = 'test_unknown_action';
  233. $param = array(
  234. 'hoge' => 'fuga',
  235. );
  236. $this->url_handler->setActionMap($this->_simple_map);
  237. $ret = $this->url_handler->actionToRequest($action, $param);
  238. $this->assertTrue(is_null($ret));
  239. }
  240. // }}}
  241. // {{{ test_actionToRequest_param1
  242. function test_actionToRequest_param1()
  243. {
  244. $action = 'test_foo_bar';
  245. $param = array(
  246. 'hoge' => 'fuga',
  247. 'param1' => 'value1',
  248. );
  249. $this->url_handler->setActionMap($this->_complex_map);
  250. list($path, $path_key) = $this->url_handler->actionToRequest($action, $param);
  251. $this->assertEqual($path, 'entrypoint/foo/bar/value1');
  252. $this->assertTrue($path_key == array('param1'));
  253. }
  254. // }}}
  255. // {{{ test_actionToRequest_param2
  256. function test_actionToRequest_param2()
  257. {
  258. $action = 'test_foo_bar';
  259. $param = array(
  260. 'hoge' => 'fuga',
  261. 'param1' => 'value1',
  262. 'param2' => 'value2',
  263. );
  264. $this->url_handler->setActionMap($this->_complex_map);
  265. list($path, $path_key) = $this->url_handler->actionToRequest($action, $param);
  266. $this->assertEqual($path, 'entrypoint/foo/bar/value1/value2');
  267. $this->assertEqual($path_key, array('param1', 'param2'));
  268. }
  269. // }}}
  270. }
  271. class Ethna_UrlHandler_TestClass extends Ethna_UrlHandler
  272. {
  273. function _getPath_Entrypoint($action, $params)
  274. {
  275. return array('/entrypoint', array());
  276. }
  277. function _normalizerequest_Test($http_vars)
  278. {
  279. return $http_vars;
  280. }
  281. function _input_filter_Test($input)
  282. {
  283. return strtolower($input);
  284. }
  285. function _output_filter_Test($output)
  286. {
  287. return strtoupper($output);
  288. }
  289. public function setActionMap($am)
  290. {
  291. $this->action_map = $am;
  292. }
  293. }