RouteTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. * @category Zend
  16. * @package Zend_Rest
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Test helper */
  23. require_once dirname(__FILE__) . '/../../TestHelper.php';
  24. require_once "PHPUnit/Framework/TestCase.php";
  25. require_once "PHPUnit/Framework/TestSuite.php";
  26. /** Zend_Rest_Route */
  27. require_once 'Zend/Rest/Route.php';
  28. /** Zend_Controller_Front */
  29. require_once 'Zend/Controller/Front.php';
  30. /** Zend_Controller_Request_HttpTestCase */
  31. require_once 'Zend/Controller/Request/HttpTestCase.php';
  32. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  33. if (!defined("PHPUnit_MAIN_METHOD")) {
  34. define("PHPUnit_MAIN_METHOD", "Zend_Rest_RouteTest::main");
  35. }
  36. /**
  37. * @category Zend
  38. * @package Zend_Rest
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Rest
  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. /*
  96. * @group ZF-7437
  97. */
  98. public function test_RESTfulApp_GET_user_defaults()
  99. {
  100. $request = $this->_buildRequest('GET', '/user');
  101. $values = $this->_invokeRouteMatch($request);
  102. $this->assertType('array', $values);
  103. $this->assertTrue(isset($values['module']));
  104. $this->assertEquals('default', $values['module']);
  105. $this->assertEquals('user', $values['controller']);
  106. $this->assertEquals('index', $values['action']);
  107. }
  108. public function test_RESTfulApp_GET_user_index()
  109. {
  110. $request = $this->_buildRequest('GET', '/user/index');
  111. $values = $this->_invokeRouteMatch($request);
  112. $this->assertType('array', $values);
  113. $this->assertTrue(isset($values['module']));
  114. $this->assertEquals('default', $values['module']);
  115. $this->assertEquals('user', $values['controller']);
  116. $this->assertEquals('index', $values['action']);
  117. }
  118. public function test_RESTfulApp_GET_user_index_withParams()
  119. {
  120. $request = $this->_buildRequest('GET', '/user/index/changedSince/123456789/status/active');
  121. $values = $this->_invokeRouteMatch($request);
  122. $this->assertType('array', $values);
  123. $this->assertTrue(isset($values['module']));
  124. $this->assertEquals('default', $values['module']);
  125. $this->assertEquals('user', $values['controller']);
  126. $this->assertEquals('index', $values['action']);
  127. $this->assertEquals(123456789, $values['changedSince']);
  128. $this->assertEquals('active', $values['status']);
  129. }
  130. public function test_RESTfulApp_GET_project_byIdentifier()
  131. {
  132. $request = $this->_buildRequest('GET', '/project/zendframework');
  133. $values = $this->_invokeRouteMatch($request);
  134. $this->assertType('array', $values);
  135. $this->assertTrue(isset($values['module']));
  136. $this->assertEquals('default', $values['module']);
  137. $this->assertEquals('project', $values['controller']);
  138. $this->assertEquals('get', $values['action']);
  139. $this->assertEquals('zendframework', $values['id']);
  140. }
  141. public function test_RESTfulApp_GET_project_edit()
  142. {
  143. $request = $this->_buildRequest('GET', '/project/zendframework/edit');
  144. $values = $this->_invokeRouteMatch($request);
  145. $this->assertType('array', $values);
  146. $this->assertTrue(isset($values['module']));
  147. $this->assertEquals('default', $values['module']);
  148. $this->assertEquals('project', $values['controller']);
  149. $this->assertEquals('edit', $values['action']);
  150. $this->assertEquals('zendframework', $values['id']);
  151. }
  152. public function test_RESTfulApp_PUT_user_byIdentifier()
  153. {
  154. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  155. $values = $this->_invokeRouteMatch($request);
  156. $this->assertType('array', $values);
  157. $this->assertTrue(isset($values['module']));
  158. $this->assertEquals('mod', $values['module']);
  159. $this->assertEquals('user', $values['controller']);
  160. $this->assertEquals('put', $values['action']);
  161. $this->assertEquals('lcrouch', $values['id']);
  162. }
  163. public function test_RESTfulApp_POST_user()
  164. {
  165. $request = $this->_buildRequest('POST', '/mod/user');
  166. $values = $this->_invokeRouteMatch($request);
  167. $this->assertType('array', $values);
  168. $this->assertTrue(isset($values['module']));
  169. $this->assertEquals('mod', $values['module']);
  170. $this->assertEquals('user', $values['controller']);
  171. $this->assertEquals('post', $values['action']);
  172. }
  173. public function test_RESTfulApp_DELETE_user_byIdentifier()
  174. {
  175. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  176. $values = $this->_invokeRouteMatch($request);
  177. $this->assertType('array', $values);
  178. $this->assertTrue(isset($values['module']));
  179. $this->assertEquals('mod', $values['module']);
  180. $this->assertEquals('user', $values['controller']);
  181. $this->assertEquals('delete', $values['action']);
  182. $this->assertEquals('lcrouch', $values['id']);
  183. }
  184. public function test_RESTfulApp_POST_user_with_identifier_doesPUT()
  185. {
  186. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  187. $values = $this->_invokeRouteMatch($request);
  188. $this->assertType('array', $values);
  189. $this->assertTrue(isset($values['module']));
  190. $this->assertEquals('mod', $values['module']);
  191. $this->assertEquals('user', $values['controller']);
  192. $this->assertEquals('put', $values['action']);
  193. $this->assertEquals('lcrouch', $values['id']);
  194. }
  195. public function test_RESTfulApp_overload_POST_with_method_param_PUT()
  196. {
  197. $request = $this->_buildRequest('POST', '/mod/user');
  198. $request->setParam('_method', 'PUT');
  199. $values = $this->_invokeRouteMatch($request);
  200. $this->assertType('array', $values);
  201. $this->assertTrue(isset($values['module']));
  202. $this->assertEquals('mod', $values['module']);
  203. $this->assertEquals('user', $values['controller']);
  204. $this->assertEquals('put', $values['action']);
  205. }
  206. public function test_RESTfulApp_overload_POST_with_http_header_DELETE()
  207. {
  208. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  209. $request->setHeader('X-HTTP-Method-Override', 'DELETE');
  210. $values = $this->_invokeRouteMatch($request);
  211. $this->assertType('array', $values);
  212. $this->assertTrue(isset($values['module']));
  213. $this->assertEquals('mod', $values['module']);
  214. $this->assertEquals('user', $values['controller']);
  215. $this->assertEquals('delete', $values['action']);
  216. $this->assertEquals('lcrouch', $values['id']);
  217. }
  218. public function test_RESTfulApp_route_chaining()
  219. {
  220. $request = $this->_buildRequest('GET', '/api/user/lcrouch');
  221. $this->_front->setRequest($request);
  222. $router = $this->_front->getRouter();
  223. $router->removeDefaultRoutes();
  224. $nonRESTRoute = new Zend_Controller_Router_Route('api');
  225. $RESTRoute = new Zend_Rest_Route($this->_front);
  226. $router->addRoute("api", $nonRESTRoute->chain($RESTRoute));
  227. $routedRequest = $router->route($request);
  228. $this->assertEquals("default", $routedRequest->getParam("module"));
  229. $this->assertEquals("user", $routedRequest->getParam("controller"));
  230. $this->assertEquals("get", $routedRequest->getParam("action"));
  231. $this->assertEquals("lcrouch", $routedRequest->getParam("id"));
  232. }
  233. public function test_RESTfulModule_GET_user_index()
  234. {
  235. $request = $this->_buildRequest('GET', '/mod/user/index');
  236. $config = array('mod');
  237. $values = $this->_invokeRouteMatch($request, $config);
  238. $this->assertType('array', $values);
  239. $this->assertTrue(isset($values['module']));
  240. $this->assertEquals('mod', $values['module']);
  241. $this->assertEquals('user', $values['controller']);
  242. $this->assertEquals('index', $values['action']);
  243. }
  244. public function test_RESTfulModule_GET_user()
  245. {
  246. $request = $this->_buildRequest('GET', '/mod/user/1234');
  247. $config = array('mod');
  248. $values = $this->_invokeRouteMatch($request, $config);
  249. $this->assertType('array', $values);
  250. $this->assertTrue(isset($values['module']));
  251. $this->assertEquals('mod', $values['module']);
  252. $this->assertEquals('user', $values['controller']);
  253. $this->assertEquals('get', $values['action']);
  254. }
  255. public function test_RESTfulModule_POST_user()
  256. {
  257. $request = $this->_buildRequest('POST', '/mod/user');
  258. $config = array('mod');
  259. $values = $this->_invokeRouteMatch($request, $config);
  260. $this->assertType('array', $values);
  261. $this->assertTrue(isset($values['module']));
  262. $this->assertEquals('mod', $values['module']);
  263. $this->assertEquals('user', $values['controller']);
  264. $this->assertEquals('post', $values['action']);
  265. }
  266. public function test_RESTfulModule_POST_user_inNonRESTModule_returnsFalse()
  267. {
  268. $request = $this->_buildRequest('POST', '/default/user');
  269. $config = array('mod');
  270. $values = $this->_invokeRouteMatch($request, $config);
  271. $this->assertFalse($values);
  272. }
  273. public function test_RESTfulModule_PUT_user_byIdentifier()
  274. {
  275. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  276. $config = array('mod');
  277. $values = $this->_invokeRouteMatch($request, $config);
  278. $this->assertType('array', $values);
  279. $this->assertTrue(isset($values['module']));
  280. $this->assertEquals('mod', $values['module']);
  281. $this->assertEquals('user', $values['controller']);
  282. $this->assertEquals('put', $values['action']);
  283. $this->assertEquals('lcrouch', $values['id']);
  284. }
  285. public function test_RESTfulModule_DELETE_user_byIdentifier()
  286. {
  287. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  288. $config = array('mod');
  289. $values = $this->_invokeRouteMatch($request, $config);
  290. $this->assertType('array', $values);
  291. $this->assertTrue(isset($values['module']));
  292. $this->assertEquals('mod', $values['module']);
  293. $this->assertEquals('user', $values['controller']);
  294. $this->assertEquals('delete', $values['action']);
  295. $this->assertEquals('lcrouch', $values['id']);
  296. }
  297. public function test_RESTfulController_GET_user_index()
  298. {
  299. $request = $this->_buildRequest('GET', '/mod/user/index');
  300. $config = array('mod'=>array('user'));
  301. $values = $this->_invokeRouteMatch($request, $config);
  302. $this->assertType('array', $values);
  303. $this->assertTrue(isset($values['module']));
  304. $this->assertEquals('mod', $values['module']);
  305. $this->assertEquals('user', $values['controller']);
  306. $this->assertEquals('index', $values['action']);
  307. }
  308. public function test_RESTfulController_GET_default_controller_returns_false()
  309. {
  310. $request = $this->_buildRequest('GET', '/mod/index/index');
  311. $config = array('mod'=>array('user'));
  312. $values = $this->_invokeRouteMatch($request, $config);
  313. $this->assertFalse($values);
  314. }
  315. public function test_RESTfulController_GET_other_index_returns_false()
  316. {
  317. $request = $this->_buildRequest('GET', '/mod/project/index');
  318. $config = array('mod'=>array('user'));
  319. $values = $this->_invokeRouteMatch($request, $config);
  320. $this->assertFalse($values);
  321. }
  322. public function test_RESTfulController_GET_user()
  323. {
  324. $request = $this->_buildRequest('GET', '/mod/user/1234');
  325. $config = array('mod'=>array('user'));
  326. $values = $this->_invokeRouteMatch($request, $config);
  327. $this->assertType('array', $values);
  328. $this->assertTrue(isset($values['module']));
  329. $this->assertEquals('mod', $values['module']);
  330. $this->assertEquals('user', $values['controller']);
  331. $this->assertEquals('get', $values['action']);
  332. }
  333. public function test_RESTfulController_POST_user()
  334. {
  335. $request = $this->_buildRequest('POST', '/mod/user');
  336. $config = array('mod'=>array('user'));
  337. $values = $this->_invokeRouteMatch($request, $config);
  338. $this->assertType('array', $values);
  339. $this->assertTrue(isset($values['module']));
  340. $this->assertEquals('mod', $values['module']);
  341. $this->assertEquals('user', $values['controller']);
  342. $this->assertEquals('post', $values['action']);
  343. }
  344. public function test_RESTfulController_POST_user_inNonRESTModule_returnsFalse()
  345. {
  346. $request = $this->_buildRequest('POST', '/default/user');
  347. $config = array('mod'=>array('user'));
  348. $values = $this->_invokeRouteMatch($request, $config);
  349. $this->assertFalse($values);
  350. }
  351. public function test_RESTfulController_PUT_user_byIdentifier()
  352. {
  353. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  354. $config = array('mod'=>array('user'));
  355. $values = $this->_invokeRouteMatch($request, $config);
  356. $this->assertType('array', $values);
  357. $this->assertTrue(isset($values['module']));
  358. $this->assertEquals('mod', $values['module']);
  359. $this->assertEquals('user', $values['controller']);
  360. $this->assertEquals('put', $values['action']);
  361. $this->assertEquals('lcrouch', $values['id']);
  362. }
  363. public function test_RESTfulController_DELETE_user_byIdentifier()
  364. {
  365. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  366. $config = array('mod');
  367. $values = $this->_invokeRouteMatch($request, $config);
  368. $this->assertType('array', $values);
  369. $this->assertTrue(isset($values['module']));
  370. $this->assertEquals('mod', $values['module']);
  371. $this->assertEquals('user', $values['controller']);
  372. $this->assertEquals('delete', $values['action']);
  373. $this->assertEquals('lcrouch', $values['id']);
  374. }
  375. public function test_assemble_plain_ignores_action()
  376. {
  377. $route = new Zend_Rest_Route($this->_front, array(), array());
  378. $params = array('module'=>'mod', 'controller'=>'user', 'action'=>'get');
  379. $url = $route->assemble($params);
  380. $this->assertEquals('mod/user', $url);
  381. }
  382. public function test_assemble_id_after_controller()
  383. {
  384. $route = new Zend_Rest_Route($this->_front, array(), array());
  385. $params = array('module'=>'mod', 'controller'=>'user', 'id'=>'lcrouch');
  386. $url = $route->assemble($params);
  387. $this->assertEquals('mod/user/lcrouch', $url);
  388. }
  389. public function test_assemble_index_after_controller_with_params()
  390. {
  391. $route = new Zend_Rest_Route($this->_front, array(), array());
  392. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar');
  393. $url = $route->assemble($params);
  394. $this->assertEquals('mod/user/index/foo/bar', $url);
  395. }
  396. private function _buildRequest($method, $uri)
  397. {
  398. $request = new Zend_Controller_Request_HttpTestCase();
  399. $request->setMethod($method)->setRequestUri($uri);
  400. return $request;
  401. }
  402. private function _invokeRouteMatch($request, $config = array())
  403. {
  404. $this->_front->setRequest($request);
  405. $route = new Zend_Rest_Route($this->_front, array(), $config);
  406. $values = $route->match($request);
  407. return $values;
  408. }
  409. }
  410. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  411. if (PHPUnit_MAIN_METHOD == "Zend_Rest_RouteTest::main") {
  412. Zend_Rest_RouteTest::main();
  413. }