RouteTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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_getInstance_fromINIConfig()
  86. {
  87. require_once('Zend/Config/Ini.php');
  88. $config = new Zend_Config_Ini(dirname(__FILE__) . '/../Controller/_files/routes.ini', 'testing');
  89. require_once('Zend/Controller/Router/Rewrite.php');
  90. $router = new Zend_Controller_Router_Rewrite();
  91. $router->addConfig($config, 'routes');
  92. $route = $router->getRoute('rest');
  93. $this->assertType('Zend_Rest_Route', $route);
  94. $this->assertEquals('object', $route->getDefault('controller'));
  95. $request = $this->_buildRequest('GET', '/mod/project');
  96. $values = $this->_invokeRouteMatch($request, array(), $route);
  97. $this->assertEquals('mod', $values['module']);
  98. $this->assertEquals('project', $values['controller']);
  99. $this->assertEquals('index', $values['action']);
  100. $request = $this->_buildRequest('POST', '/mod/user');
  101. $values = $this->_invokeRouteMatch($request, array(), $route);
  102. $this->assertEquals('mod', $values['module']);
  103. $this->assertEquals('user', $values['controller']);
  104. $this->assertEquals('post', $values['action']);
  105. $request = $this->_buildRequest('GET', '/other');
  106. $values = $this->_invokeRouteMatch($request, array(), $route);
  107. $this->assertFalse($values);
  108. }
  109. public function test_RESTfulApp_defaults()
  110. {
  111. $request = $this->_buildRequest('GET', '/');
  112. $values = $this->_invokeRouteMatch($request);
  113. $this->assertType('array', $values);
  114. $this->assertTrue(isset($values['module']));
  115. $this->assertEquals('default', $values['module']);
  116. $this->assertEquals('index', $values['controller']);
  117. $this->assertEquals('index', $values['action']);
  118. }
  119. /*
  120. * @group ZF-7437
  121. */
  122. public function test_RESTfulApp_GET_user_defaults()
  123. {
  124. $request = $this->_buildRequest('GET', '/user');
  125. $values = $this->_invokeRouteMatch($request);
  126. $this->assertType('array', $values);
  127. $this->assertTrue(isset($values['module']));
  128. $this->assertEquals('default', $values['module']);
  129. $this->assertEquals('user', $values['controller']);
  130. $this->assertEquals('index', $values['action']);
  131. }
  132. public function test_RESTfulApp_GET_user_index()
  133. {
  134. $request = $this->_buildRequest('GET', '/user/index');
  135. $values = $this->_invokeRouteMatch($request);
  136. $this->assertType('array', $values);
  137. $this->assertTrue(isset($values['module']));
  138. $this->assertEquals('default', $values['module']);
  139. $this->assertEquals('user', $values['controller']);
  140. $this->assertEquals('index', $values['action']);
  141. }
  142. public function test_RESTfulApp_GET_user_index_withParams()
  143. {
  144. $request = $this->_buildRequest('GET', '/user/index/changedSince/123456789/status/active');
  145. $values = $this->_invokeRouteMatch($request);
  146. $this->assertType('array', $values);
  147. $this->assertTrue(isset($values['module']));
  148. $this->assertEquals('default', $values['module']);
  149. $this->assertEquals('user', $values['controller']);
  150. $this->assertEquals('index', $values['action']);
  151. $this->assertEquals(123456789, $values['changedSince']);
  152. $this->assertEquals('active', $values['status']);
  153. }
  154. public function test_RESTfulApp_GET_project_byIdentifier()
  155. {
  156. $request = $this->_buildRequest('GET', '/project/zendframework');
  157. $values = $this->_invokeRouteMatch($request);
  158. $this->assertType('array', $values);
  159. $this->assertTrue(isset($values['module']));
  160. $this->assertEquals('default', $values['module']);
  161. $this->assertEquals('project', $values['controller']);
  162. $this->assertEquals('get', $values['action']);
  163. $this->assertEquals('zendframework', $values['id']);
  164. }
  165. public function test_RESTfulApp_GET_project_byIdentifier_urlencoded()
  166. {
  167. $request = $this->_buildRequest('GET', '/project/zend+framework');
  168. $values = $this->_invokeRouteMatch($request);
  169. $this->assertType('array', $values);
  170. $this->assertTrue(isset($values['module']));
  171. $this->assertEquals('default', $values['module']);
  172. $this->assertEquals('project', $values['controller']);
  173. $this->assertEquals('get', $values['action']);
  174. $this->assertEquals('zend framework', $values['id']);
  175. }
  176. public function test_RESTfulApp_GET_project_edit()
  177. {
  178. $request = $this->_buildRequest('GET', '/project/zendframework/edit');
  179. $values = $this->_invokeRouteMatch($request);
  180. $this->assertType('array', $values);
  181. $this->assertTrue(isset($values['module']));
  182. $this->assertEquals('default', $values['module']);
  183. $this->assertEquals('project', $values['controller']);
  184. $this->assertEquals('edit', $values['action']);
  185. $this->assertEquals('zendframework', $values['id']);
  186. }
  187. public function test_RESTfulApp_PUT_user_byIdentifier()
  188. {
  189. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  190. $values = $this->_invokeRouteMatch($request);
  191. $this->assertType('array', $values);
  192. $this->assertTrue(isset($values['module']));
  193. $this->assertEquals('mod', $values['module']);
  194. $this->assertEquals('user', $values['controller']);
  195. $this->assertEquals('put', $values['action']);
  196. $this->assertEquals('lcrouch', $values['id']);
  197. }
  198. public function test_RESTfulApp_POST_user()
  199. {
  200. $request = $this->_buildRequest('POST', '/mod/user');
  201. $values = $this->_invokeRouteMatch($request);
  202. $this->assertType('array', $values);
  203. $this->assertTrue(isset($values['module']));
  204. $this->assertEquals('mod', $values['module']);
  205. $this->assertEquals('user', $values['controller']);
  206. $this->assertEquals('post', $values['action']);
  207. }
  208. public function test_RESTfulApp_DELETE_user_byIdentifier()
  209. {
  210. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  211. $values = $this->_invokeRouteMatch($request);
  212. $this->assertType('array', $values);
  213. $this->assertTrue(isset($values['module']));
  214. $this->assertEquals('mod', $values['module']);
  215. $this->assertEquals('user', $values['controller']);
  216. $this->assertEquals('delete', $values['action']);
  217. $this->assertEquals('lcrouch', $values['id']);
  218. }
  219. public function test_RESTfulApp_POST_user_with_identifier_doesPUT()
  220. {
  221. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  222. $values = $this->_invokeRouteMatch($request);
  223. $this->assertType('array', $values);
  224. $this->assertTrue(isset($values['module']));
  225. $this->assertEquals('mod', $values['module']);
  226. $this->assertEquals('user', $values['controller']);
  227. $this->assertEquals('put', $values['action']);
  228. $this->assertEquals('lcrouch', $values['id']);
  229. }
  230. public function test_RESTfulApp_overload_POST_with_method_param_PUT()
  231. {
  232. $request = $this->_buildRequest('POST', '/mod/user');
  233. $request->setParam('_method', 'PUT');
  234. $values = $this->_invokeRouteMatch($request);
  235. $this->assertType('array', $values);
  236. $this->assertTrue(isset($values['module']));
  237. $this->assertEquals('mod', $values['module']);
  238. $this->assertEquals('user', $values['controller']);
  239. $this->assertEquals('put', $values['action']);
  240. }
  241. public function test_RESTfulApp_overload_POST_with_http_header_DELETE()
  242. {
  243. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  244. $request->setHeader('X-HTTP-Method-Override', 'DELETE');
  245. $values = $this->_invokeRouteMatch($request);
  246. $this->assertType('array', $values);
  247. $this->assertTrue(isset($values['module']));
  248. $this->assertEquals('mod', $values['module']);
  249. $this->assertEquals('user', $values['controller']);
  250. $this->assertEquals('delete', $values['action']);
  251. $this->assertEquals('lcrouch', $values['id']);
  252. }
  253. public function test_RESTfulApp_route_chaining()
  254. {
  255. $request = $this->_buildRequest('GET', '/api/user/lcrouch');
  256. $this->_front->setRequest($request);
  257. $router = $this->_front->getRouter();
  258. $router->removeDefaultRoutes();
  259. $nonRESTRoute = new Zend_Controller_Router_Route('api');
  260. $RESTRoute = new Zend_Rest_Route($this->_front);
  261. $router->addRoute("api", $nonRESTRoute->chain($RESTRoute));
  262. $routedRequest = $router->route($request);
  263. $this->assertEquals("default", $routedRequest->getParam("module"));
  264. $this->assertEquals("user", $routedRequest->getParam("controller"));
  265. $this->assertEquals("get", $routedRequest->getParam("action"));
  266. $this->assertEquals("lcrouch", $routedRequest->getParam("id"));
  267. }
  268. public function test_RESTfulModule_GET_user_index()
  269. {
  270. $request = $this->_buildRequest('GET', '/mod/user/index');
  271. $config = array('mod');
  272. $values = $this->_invokeRouteMatch($request, $config);
  273. $this->assertType('array', $values);
  274. $this->assertTrue(isset($values['module']));
  275. $this->assertEquals('mod', $values['module']);
  276. $this->assertEquals('user', $values['controller']);
  277. $this->assertEquals('index', $values['action']);
  278. }
  279. public function test_RESTfulModule_GET_user()
  280. {
  281. $request = $this->_buildRequest('GET', '/mod/user/1234');
  282. $config = array('mod');
  283. $values = $this->_invokeRouteMatch($request, $config);
  284. $this->assertType('array', $values);
  285. $this->assertTrue(isset($values['module']));
  286. $this->assertEquals('mod', $values['module']);
  287. $this->assertEquals('user', $values['controller']);
  288. $this->assertEquals('get', $values['action']);
  289. }
  290. public function test_RESTfulModule_POST_user()
  291. {
  292. $request = $this->_buildRequest('POST', '/mod/user');
  293. $config = array('mod');
  294. $values = $this->_invokeRouteMatch($request, $config);
  295. $this->assertType('array', $values);
  296. $this->assertTrue(isset($values['module']));
  297. $this->assertEquals('mod', $values['module']);
  298. $this->assertEquals('user', $values['controller']);
  299. $this->assertEquals('post', $values['action']);
  300. }
  301. public function test_RESTfulModule_POST_user_inNonRESTModule_returnsFalse()
  302. {
  303. $request = $this->_buildRequest('POST', '/default/user');
  304. $config = array('mod');
  305. $values = $this->_invokeRouteMatch($request, $config);
  306. $this->assertFalse($values);
  307. }
  308. public function test_RESTfulModule_PUT_user_byIdentifier()
  309. {
  310. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  311. $config = array('mod');
  312. $values = $this->_invokeRouteMatch($request, $config);
  313. $this->assertType('array', $values);
  314. $this->assertTrue(isset($values['module']));
  315. $this->assertEquals('mod', $values['module']);
  316. $this->assertEquals('user', $values['controller']);
  317. $this->assertEquals('put', $values['action']);
  318. $this->assertEquals('lcrouch', $values['id']);
  319. }
  320. public function test_RESTfulModule_DELETE_user_byIdentifier()
  321. {
  322. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  323. $config = array('mod');
  324. $values = $this->_invokeRouteMatch($request, $config);
  325. $this->assertType('array', $values);
  326. $this->assertTrue(isset($values['module']));
  327. $this->assertEquals('mod', $values['module']);
  328. $this->assertEquals('user', $values['controller']);
  329. $this->assertEquals('delete', $values['action']);
  330. $this->assertEquals('lcrouch', $values['id']);
  331. }
  332. public function test_RESTfulController_GET_user_index()
  333. {
  334. $request = $this->_buildRequest('GET', '/mod/user/index');
  335. $config = array('mod'=>array('user'));
  336. $values = $this->_invokeRouteMatch($request, $config);
  337. $this->assertType('array', $values);
  338. $this->assertTrue(isset($values['module']));
  339. $this->assertEquals('mod', $values['module']);
  340. $this->assertEquals('user', $values['controller']);
  341. $this->assertEquals('index', $values['action']);
  342. }
  343. public function test_RESTfulController_GET_default_controller_returns_false()
  344. {
  345. $request = $this->_buildRequest('GET', '/mod/index/index');
  346. $config = array('mod'=>array('user'));
  347. $values = $this->_invokeRouteMatch($request, $config);
  348. $this->assertFalse($values);
  349. }
  350. public function test_RESTfulController_GET_other_index_returns_false()
  351. {
  352. $request = $this->_buildRequest('GET', '/mod/project/index');
  353. $config = array('mod'=>array('user'));
  354. $values = $this->_invokeRouteMatch($request, $config);
  355. $this->assertFalse($values);
  356. }
  357. public function test_RESTfulController_GET_user()
  358. {
  359. $request = $this->_buildRequest('GET', '/mod/user/1234');
  360. $config = array('mod'=>array('user'));
  361. $values = $this->_invokeRouteMatch($request, $config);
  362. $this->assertType('array', $values);
  363. $this->assertTrue(isset($values['module']));
  364. $this->assertEquals('mod', $values['module']);
  365. $this->assertEquals('user', $values['controller']);
  366. $this->assertEquals('get', $values['action']);
  367. }
  368. public function test_RESTfulController_POST_user()
  369. {
  370. $request = $this->_buildRequest('POST', '/mod/user');
  371. $config = array('mod'=>array('user'));
  372. $values = $this->_invokeRouteMatch($request, $config);
  373. $this->assertType('array', $values);
  374. $this->assertTrue(isset($values['module']));
  375. $this->assertEquals('mod', $values['module']);
  376. $this->assertEquals('user', $values['controller']);
  377. $this->assertEquals('post', $values['action']);
  378. }
  379. public function test_RESTfulController_POST_user_inNonRESTModule_returnsFalse()
  380. {
  381. $request = $this->_buildRequest('POST', '/default/user');
  382. $config = array('mod'=>array('user'));
  383. $values = $this->_invokeRouteMatch($request, $config);
  384. $this->assertFalse($values);
  385. }
  386. public function test_postToNonRESTfulDefaultController_moduleHasAnotherRESTfulController_defaultControllerInURL_returnsFalse()
  387. {
  388. $request = $this->_buildRequest('POST', '/mod/index');
  389. $config = array('mod'=>array('user'));
  390. $values = $this->_invokeRouteMatch($request, $config);
  391. $this->assertFalse($values);
  392. }
  393. public function test_postToNonRESTfulDefaultController_moduleHasAnotherRESTfulController_noDefaultControllerInURL_returnsFalse()
  394. {
  395. $request = $this->_buildRequest('POST', '/mod');
  396. $config = array('mod'=>array('user'));
  397. $values = $this->_invokeRouteMatch($request, $config);
  398. $this->assertFalse($values);
  399. }
  400. public function test_RESTfulController_PUT_user_byIdentifier()
  401. {
  402. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  403. $config = array('mod'=>array('user'));
  404. $values = $this->_invokeRouteMatch($request, $config);
  405. $this->assertType('array', $values);
  406. $this->assertTrue(isset($values['module']));
  407. $this->assertEquals('mod', $values['module']);
  408. $this->assertEquals('user', $values['controller']);
  409. $this->assertEquals('put', $values['action']);
  410. $this->assertEquals('lcrouch', $values['id']);
  411. }
  412. public function test_RESTfulController_DELETE_user_byIdentifier()
  413. {
  414. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  415. $config = array('mod');
  416. $values = $this->_invokeRouteMatch($request, $config);
  417. $this->assertType('array', $values);
  418. $this->assertTrue(isset($values['module']));
  419. $this->assertEquals('mod', $values['module']);
  420. $this->assertEquals('user', $values['controller']);
  421. $this->assertEquals('delete', $values['action']);
  422. $this->assertEquals('lcrouch', $values['id']);
  423. }
  424. public function test_assemble_plain_ignores_action()
  425. {
  426. $route = new Zend_Rest_Route($this->_front, array(), array());
  427. $params = array('module'=>'mod', 'controller'=>'user', 'action'=>'get');
  428. $url = $route->assemble($params);
  429. $this->assertEquals('mod/user', $url);
  430. }
  431. public function test_assemble_id_after_controller()
  432. {
  433. $route = new Zend_Rest_Route($this->_front, array(), array());
  434. $params = array('module'=>'mod', 'controller'=>'user', 'id'=>'lcrouch');
  435. $url = $route->assemble($params);
  436. $this->assertEquals('mod/user/lcrouch', $url);
  437. }
  438. public function test_assemble_index_after_controller_with_params()
  439. {
  440. $route = new Zend_Rest_Route($this->_front, array(), array());
  441. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar');
  442. $url = $route->assemble($params);
  443. $this->assertEquals('mod/user/index/foo/bar', $url);
  444. }
  445. public function test_assemble_encode_param_values()
  446. {
  447. $route = new Zend_Rest_Route($this->_front, array(), array());
  448. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar is n!ice');
  449. $url = $route->assemble($params);
  450. $this->assertEquals('mod/user/index/foo/bar+is+n%21ice', $url);
  451. }
  452. public function test_assemble_does_NOT_encode_param_values()
  453. {
  454. $route = new Zend_Rest_Route($this->_front, array(), array());
  455. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar is n!ice');
  456. $url = $route->assemble($params, false, false);
  457. $this->assertEquals('mod/user/index/foo/bar is n!ice', $url);
  458. }
  459. private function _buildRequest($method, $uri)
  460. {
  461. $request = new Zend_Controller_Request_HttpTestCase();
  462. $request->setMethod($method)->setRequestUri($uri);
  463. return $request;
  464. }
  465. private function _invokeRouteMatch($request, $config = array(), $route = null)
  466. {
  467. $this->_front->setRequest($request);
  468. if ($route == null)
  469. $route = new Zend_Rest_Route($this->_front, array(), $config);
  470. $values = $route->match($request);
  471. return $values;
  472. }
  473. }
  474. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  475. if (PHPUnit_MAIN_METHOD == "Zend_Rest_RouteTest::main") {
  476. Zend_Rest_RouteTest::main();
  477. }