RouteTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @package Zend_Rest
  16. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. * @version $Id$
  19. */
  20. /** Test helper */
  21. require_once dirname(__FILE__) . '/../../TestHelper.php';
  22. require_once "PHPUnit/Framework/TestCase.php";
  23. require_once "PHPUnit/Framework/TestSuite.php";
  24. /**
  25. * @category Zend
  26. * @package Zend_Rest
  27. * @subpackage UnitTests
  28. */
  29. /** Zend_Rest_Route */
  30. require_once 'Zend/Rest/Route.php';
  31. /** Zend_Controller_Front */
  32. require_once 'Zend/Controller/Front.php';
  33. /** Zend_Controller_Request_HttpTestCase */
  34. require_once 'Zend/Controller/Request/HttpTestCase.php';
  35. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  36. if (!defined("PHPUnit_MAIN_METHOD")) {
  37. define("PHPUnit_MAIN_METHOD", "Zend_Rest_RouteTest::main");
  38. }
  39. /**
  40. * @category Zend
  41. * @package Zend_Rest
  42. * @subpackage UnitTests
  43. */
  44. class Zend_Rest_RouteTest extends PHPUnit_Framework_TestCase
  45. {
  46. protected $_front;
  47. protected $_request;
  48. protected $_dispatcher;
  49. /**
  50. * Runs the test methods of this class.
  51. *
  52. * @access public
  53. * @static
  54. */
  55. public static function main()
  56. {
  57. require_once "PHPUnit/TextUI/TestRunner.php";
  58. $suite = new PHPUnit_Framework_TestSuite("Zend_Rest_RouteTest");
  59. $result = PHPUnit_TextUI_TestRunner::run($suite);
  60. }
  61. public function setUp()
  62. {
  63. $this->_front = Zend_Controller_Front::getInstance();
  64. $this->_front->resetInstance();
  65. $this->_front->setParam('noErrorHandler', true)
  66. ->setParam('noViewRenderer', true);
  67. $this->_dispatcher = $this->_front->getDispatcher();
  68. $this->_dispatcher->setControllerDirectory(array(
  69. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR .
  70. '..' . DIRECTORY_SEPARATOR .
  71. 'Controller' . DIRECTORY_SEPARATOR .
  72. '_files',
  73. 'mod' => dirname(__FILE__) . DIRECTORY_SEPARATOR .
  74. '..' . DIRECTORY_SEPARATOR .
  75. 'Controller' . DIRECTORY_SEPARATOR .
  76. '_files' . DIRECTORY_SEPARATOR .
  77. 'Admin',
  78. ));
  79. }
  80. public function test_getVersion()
  81. {
  82. $route = new Zend_Rest_Route($this->_front);
  83. $this->assertEquals(2, $route->getVersion());
  84. }
  85. public function test_RESTfulApp_defaults()
  86. {
  87. $request = $this->_buildRequest('GET', '/');
  88. $values = $this->_invokeRouteMatch($request);
  89. $this->assertType('array', $values);
  90. $this->assertTrue(isset($values['module']));
  91. $this->assertEquals('default', $values['module']);
  92. $this->assertEquals('index', $values['controller']);
  93. $this->assertEquals('index', $values['action']);
  94. }
  95. public function test_RESTfulApp_GET_user_index()
  96. {
  97. $request = $this->_buildRequest('GET', '/user/index');
  98. $values = $this->_invokeRouteMatch($request);
  99. $this->assertType('array', $values);
  100. $this->assertTrue(isset($values['module']));
  101. $this->assertEquals('default', $values['module']);
  102. $this->assertEquals('user', $values['controller']);
  103. $this->assertEquals('index', $values['action']);
  104. }
  105. public function test_RESTfulApp_GET_user_index_withParams()
  106. {
  107. $request = $this->_buildRequest('GET', '/user/index/changedSince/123456789/status/active');
  108. $values = $this->_invokeRouteMatch($request);
  109. $this->assertType('array', $values);
  110. $this->assertTrue(isset($values['module']));
  111. $this->assertEquals('default', $values['module']);
  112. $this->assertEquals('user', $values['controller']);
  113. $this->assertEquals('index', $values['action']);
  114. $this->assertEquals(123456789, $values['changedSince']);
  115. $this->assertEquals('active', $values['status']);
  116. }
  117. public function test_RESTfulApp_GET_project_byIdentifier()
  118. {
  119. $request = $this->_buildRequest('GET', '/project/zendframework');
  120. $values = $this->_invokeRouteMatch($request);
  121. $this->assertType('array', $values);
  122. $this->assertTrue(isset($values['module']));
  123. $this->assertEquals('default', $values['module']);
  124. $this->assertEquals('project', $values['controller']);
  125. $this->assertEquals('get', $values['action']);
  126. $this->assertEquals('zendframework', $values['id']);
  127. }
  128. public function test_RESTfulApp_PUT_user_byIdentifier()
  129. {
  130. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  131. $values = $this->_invokeRouteMatch($request);
  132. $this->assertType('array', $values);
  133. $this->assertTrue(isset($values['module']));
  134. $this->assertEquals('mod', $values['module']);
  135. $this->assertEquals('user', $values['controller']);
  136. $this->assertEquals('put', $values['action']);
  137. $this->assertEquals('lcrouch', $values['id']);
  138. }
  139. public function test_RESTfulApp_POST_user()
  140. {
  141. $request = $this->_buildRequest('POST', '/mod/user');
  142. $values = $this->_invokeRouteMatch($request);
  143. $this->assertType('array', $values);
  144. $this->assertTrue(isset($values['module']));
  145. $this->assertEquals('mod', $values['module']);
  146. $this->assertEquals('user', $values['controller']);
  147. $this->assertEquals('post', $values['action']);
  148. }
  149. public function test_RESTfulApp_DELETE_user_byIdentifier()
  150. {
  151. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  152. $values = $this->_invokeRouteMatch($request);
  153. $this->assertType('array', $values);
  154. $this->assertTrue(isset($values['module']));
  155. $this->assertEquals('mod', $values['module']);
  156. $this->assertEquals('user', $values['controller']);
  157. $this->assertEquals('delete', $values['action']);
  158. $this->assertEquals('lcrouch', $values['id']);
  159. }
  160. public function test_RESTfulApp_POST_user_with_identifier_doesPUT()
  161. {
  162. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  163. $values = $this->_invokeRouteMatch($request);
  164. $this->assertType('array', $values);
  165. $this->assertTrue(isset($values['module']));
  166. $this->assertEquals('mod', $values['module']);
  167. $this->assertEquals('user', $values['controller']);
  168. $this->assertEquals('put', $values['action']);
  169. $this->assertEquals('lcrouch', $values['id']);
  170. }
  171. public function test_RESTfulApp_overload_POST_with_method_param_PUT()
  172. {
  173. $request = $this->_buildRequest('POST', '/mod/user');
  174. $request->setParam('_method', 'PUT');
  175. $values = $this->_invokeRouteMatch($request);
  176. $this->assertType('array', $values);
  177. $this->assertTrue(isset($values['module']));
  178. $this->assertEquals('mod', $values['module']);
  179. $this->assertEquals('user', $values['controller']);
  180. $this->assertEquals('put', $values['action']);
  181. }
  182. public function test_RESTfulApp_overload_POST_with_http_header_DELETE()
  183. {
  184. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  185. $request->setHeader('X-HTTP-Method-Override', 'DELETE');
  186. $values = $this->_invokeRouteMatch($request);
  187. $this->assertType('array', $values);
  188. $this->assertTrue(isset($values['module']));
  189. $this->assertEquals('mod', $values['module']);
  190. $this->assertEquals('user', $values['controller']);
  191. $this->assertEquals('delete', $values['action']);
  192. $this->assertEquals('lcrouch', $values['id']);
  193. }
  194. public function test_RESTfulModule_GET_user_index()
  195. {
  196. $request = $this->_buildRequest('GET', '/mod/user/index');
  197. $config = array('mod');
  198. $values = $this->_invokeRouteMatch($request, $config);
  199. $this->assertType('array', $values);
  200. $this->assertTrue(isset($values['module']));
  201. $this->assertEquals('mod', $values['module']);
  202. $this->assertEquals('user', $values['controller']);
  203. $this->assertEquals('index', $values['action']);
  204. }
  205. public function test_RESTfulModule_GET_user()
  206. {
  207. $request = $this->_buildRequest('GET', '/mod/user/1234');
  208. $config = array('mod');
  209. $values = $this->_invokeRouteMatch($request, $config);
  210. $this->assertType('array', $values);
  211. $this->assertTrue(isset($values['module']));
  212. $this->assertEquals('mod', $values['module']);
  213. $this->assertEquals('user', $values['controller']);
  214. $this->assertEquals('get', $values['action']);
  215. }
  216. public function test_RESTfulModule_POST_user()
  217. {
  218. $request = $this->_buildRequest('POST', '/mod/user');
  219. $config = array('mod');
  220. $values = $this->_invokeRouteMatch($request, $config);
  221. $this->assertType('array', $values);
  222. $this->assertTrue(isset($values['module']));
  223. $this->assertEquals('mod', $values['module']);
  224. $this->assertEquals('user', $values['controller']);
  225. $this->assertEquals('post', $values['action']);
  226. }
  227. public function test_RESTfulModule_POST_user_inNonRESTModule_returnsFalse()
  228. {
  229. $request = $this->_buildRequest('POST', '/default/user');
  230. $config = array('mod');
  231. $values = $this->_invokeRouteMatch($request, $config);
  232. $this->assertFalse($values);
  233. }
  234. public function test_RESTfulModule_PUT_user_byIdentifier()
  235. {
  236. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  237. $config = array('mod');
  238. $values = $this->_invokeRouteMatch($request, $config);
  239. $this->assertType('array', $values);
  240. $this->assertTrue(isset($values['module']));
  241. $this->assertEquals('mod', $values['module']);
  242. $this->assertEquals('user', $values['controller']);
  243. $this->assertEquals('put', $values['action']);
  244. $this->assertEquals('lcrouch', $values['id']);
  245. }
  246. public function test_RESTfulModule_DELETE_user_byIdentifier()
  247. {
  248. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  249. $config = array('mod');
  250. $values = $this->_invokeRouteMatch($request, $config);
  251. $this->assertType('array', $values);
  252. $this->assertTrue(isset($values['module']));
  253. $this->assertEquals('mod', $values['module']);
  254. $this->assertEquals('user', $values['controller']);
  255. $this->assertEquals('delete', $values['action']);
  256. $this->assertEquals('lcrouch', $values['id']);
  257. }
  258. public function test_RESTfulController_GET_user_index()
  259. {
  260. $request = $this->_buildRequest('GET', '/mod/user/index');
  261. $config = array('mod'=>array('user'));
  262. $values = $this->_invokeRouteMatch($request, $config);
  263. $this->assertType('array', $values);
  264. $this->assertTrue(isset($values['module']));
  265. $this->assertEquals('mod', $values['module']);
  266. $this->assertEquals('user', $values['controller']);
  267. $this->assertEquals('index', $values['action']);
  268. }
  269. public function test_RESTfulController_GET_other_index_returns_false()
  270. {
  271. $request = $this->_buildRequest('GET', '/mod/project/index');
  272. $config = array('mod'=>array('user'));
  273. $values = $this->_invokeRouteMatch($request, $config);
  274. $this->assertFalse($values);
  275. }
  276. public function test_RESTfulController_GET_user()
  277. {
  278. $request = $this->_buildRequest('GET', '/mod/user/1234');
  279. $config = array('mod'=>array('user'));
  280. $values = $this->_invokeRouteMatch($request, $config);
  281. $this->assertType('array', $values);
  282. $this->assertTrue(isset($values['module']));
  283. $this->assertEquals('mod', $values['module']);
  284. $this->assertEquals('user', $values['controller']);
  285. $this->assertEquals('get', $values['action']);
  286. }
  287. public function test_RESTfulController_POST_user()
  288. {
  289. $request = $this->_buildRequest('POST', '/mod/user');
  290. $config = array('mod'=>array('user'));
  291. $values = $this->_invokeRouteMatch($request, $config);
  292. $this->assertType('array', $values);
  293. $this->assertTrue(isset($values['module']));
  294. $this->assertEquals('mod', $values['module']);
  295. $this->assertEquals('user', $values['controller']);
  296. $this->assertEquals('post', $values['action']);
  297. }
  298. public function test_RESTfulController_POST_user_inNonRESTModule_returnsFalse()
  299. {
  300. $request = $this->_buildRequest('POST', '/default/user');
  301. $config = array('mod'=>array('user'));
  302. $values = $this->_invokeRouteMatch($request, $config);
  303. $this->assertFalse($values);
  304. }
  305. public function test_RESTfulController_PUT_user_byIdentifier()
  306. {
  307. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  308. $config = array('mod'=>array('user'));
  309. $values = $this->_invokeRouteMatch($request, $config);
  310. $this->assertType('array', $values);
  311. $this->assertTrue(isset($values['module']));
  312. $this->assertEquals('mod', $values['module']);
  313. $this->assertEquals('user', $values['controller']);
  314. $this->assertEquals('put', $values['action']);
  315. $this->assertEquals('lcrouch', $values['id']);
  316. }
  317. public function test_RESTfulController_DELETE_user_byIdentifier()
  318. {
  319. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  320. $config = array('mod');
  321. $values = $this->_invokeRouteMatch($request, $config);
  322. $this->assertType('array', $values);
  323. $this->assertTrue(isset($values['module']));
  324. $this->assertEquals('mod', $values['module']);
  325. $this->assertEquals('user', $values['controller']);
  326. $this->assertEquals('delete', $values['action']);
  327. $this->assertEquals('lcrouch', $values['id']);
  328. }
  329. public function test_assemble_plain_ignores_action()
  330. {
  331. $route = new Zend_Rest_Route($this->_front, array(), array());
  332. $params = array('module'=>'mod', 'controller'=>'user', 'action'=>'get');
  333. $url = $route->assemble($params);
  334. $this->assertEquals('mod/user', $url);
  335. }
  336. public function test_assemble_id_after_controller()
  337. {
  338. $route = new Zend_Rest_Route($this->_front, array(), array());
  339. $params = array('module'=>'mod', 'controller'=>'user', 'id'=>'lcrouch');
  340. $url = $route->assemble($params);
  341. $this->assertEquals('mod/user/lcrouch', $url);
  342. }
  343. public function test_assemble_index_after_controller_with_params()
  344. {
  345. $route = new Zend_Rest_Route($this->_front, array(), array());
  346. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar');
  347. $url = $route->assemble($params);
  348. $this->assertEquals('mod/user/index/foo/bar', $url);
  349. }
  350. private function _buildRequest($method, $uri)
  351. {
  352. $request = new Zend_Controller_Request_HttpTestCase();
  353. $request->setMethod($method)->setRequestUri($uri);
  354. return $request;
  355. }
  356. private function _invokeRouteMatch($request, $config = array())
  357. {
  358. $this->_front->setRequest($request);
  359. $route = new Zend_Rest_Route($this->_front, array(), $config);
  360. $values = $route->match($request);
  361. return $values;
  362. }
  363. }
  364. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  365. if (PHPUnit_MAIN_METHOD == "Zend_Rest_RouteTest::main") {
  366. Zend_Rest_RouteTest::main();
  367. }