RouteTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_RESTfulModule_GET_user_index()
  219. {
  220. $request = $this->_buildRequest('GET', '/mod/user/index');
  221. $config = array('mod');
  222. $values = $this->_invokeRouteMatch($request, $config);
  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('index', $values['action']);
  228. }
  229. public function test_RESTfulModule_GET_user()
  230. {
  231. $request = $this->_buildRequest('GET', '/mod/user/1234');
  232. $config = array('mod');
  233. $values = $this->_invokeRouteMatch($request, $config);
  234. $this->assertType('array', $values);
  235. $this->assertTrue(isset($values['module']));
  236. $this->assertEquals('mod', $values['module']);
  237. $this->assertEquals('user', $values['controller']);
  238. $this->assertEquals('get', $values['action']);
  239. }
  240. public function test_RESTfulModule_POST_user()
  241. {
  242. $request = $this->_buildRequest('POST', '/mod/user');
  243. $config = array('mod');
  244. $values = $this->_invokeRouteMatch($request, $config);
  245. $this->assertType('array', $values);
  246. $this->assertTrue(isset($values['module']));
  247. $this->assertEquals('mod', $values['module']);
  248. $this->assertEquals('user', $values['controller']);
  249. $this->assertEquals('post', $values['action']);
  250. }
  251. public function test_RESTfulModule_POST_user_inNonRESTModule_returnsFalse()
  252. {
  253. $request = $this->_buildRequest('POST', '/default/user');
  254. $config = array('mod');
  255. $values = $this->_invokeRouteMatch($request, $config);
  256. $this->assertFalse($values);
  257. }
  258. public function test_RESTfulModule_PUT_user_byIdentifier()
  259. {
  260. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  261. $config = array('mod');
  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('put', $values['action']);
  268. $this->assertEquals('lcrouch', $values['id']);
  269. }
  270. public function test_RESTfulModule_DELETE_user_byIdentifier()
  271. {
  272. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  273. $config = array('mod');
  274. $values = $this->_invokeRouteMatch($request, $config);
  275. $this->assertType('array', $values);
  276. $this->assertTrue(isset($values['module']));
  277. $this->assertEquals('mod', $values['module']);
  278. $this->assertEquals('user', $values['controller']);
  279. $this->assertEquals('delete', $values['action']);
  280. $this->assertEquals('lcrouch', $values['id']);
  281. }
  282. public function test_RESTfulController_GET_user_index()
  283. {
  284. $request = $this->_buildRequest('GET', '/mod/user/index');
  285. $config = array('mod'=>array('user'));
  286. $values = $this->_invokeRouteMatch($request, $config);
  287. $this->assertType('array', $values);
  288. $this->assertTrue(isset($values['module']));
  289. $this->assertEquals('mod', $values['module']);
  290. $this->assertEquals('user', $values['controller']);
  291. $this->assertEquals('index', $values['action']);
  292. }
  293. public function test_RESTfulController_GET_default_controller_returns_false()
  294. {
  295. $request = $this->_buildRequest('GET', '/mod/index/index');
  296. $config = array('mod'=>array('user'));
  297. $values = $this->_invokeRouteMatch($request, $config);
  298. $this->assertFalse($values);
  299. }
  300. public function test_RESTfulController_GET_other_index_returns_false()
  301. {
  302. $request = $this->_buildRequest('GET', '/mod/project/index');
  303. $config = array('mod'=>array('user'));
  304. $values = $this->_invokeRouteMatch($request, $config);
  305. $this->assertFalse($values);
  306. }
  307. public function test_RESTfulController_GET_user()
  308. {
  309. $request = $this->_buildRequest('GET', '/mod/user/1234');
  310. $config = array('mod'=>array('user'));
  311. $values = $this->_invokeRouteMatch($request, $config);
  312. $this->assertType('array', $values);
  313. $this->assertTrue(isset($values['module']));
  314. $this->assertEquals('mod', $values['module']);
  315. $this->assertEquals('user', $values['controller']);
  316. $this->assertEquals('get', $values['action']);
  317. }
  318. public function test_RESTfulController_POST_user()
  319. {
  320. $request = $this->_buildRequest('POST', '/mod/user');
  321. $config = array('mod'=>array('user'));
  322. $values = $this->_invokeRouteMatch($request, $config);
  323. $this->assertType('array', $values);
  324. $this->assertTrue(isset($values['module']));
  325. $this->assertEquals('mod', $values['module']);
  326. $this->assertEquals('user', $values['controller']);
  327. $this->assertEquals('post', $values['action']);
  328. }
  329. public function test_RESTfulController_POST_user_inNonRESTModule_returnsFalse()
  330. {
  331. $request = $this->_buildRequest('POST', '/default/user');
  332. $config = array('mod'=>array('user'));
  333. $values = $this->_invokeRouteMatch($request, $config);
  334. $this->assertFalse($values);
  335. }
  336. public function test_RESTfulController_PUT_user_byIdentifier()
  337. {
  338. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  339. $config = array('mod'=>array('user'));
  340. $values = $this->_invokeRouteMatch($request, $config);
  341. $this->assertType('array', $values);
  342. $this->assertTrue(isset($values['module']));
  343. $this->assertEquals('mod', $values['module']);
  344. $this->assertEquals('user', $values['controller']);
  345. $this->assertEquals('put', $values['action']);
  346. $this->assertEquals('lcrouch', $values['id']);
  347. }
  348. public function test_RESTfulController_DELETE_user_byIdentifier()
  349. {
  350. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  351. $config = array('mod');
  352. $values = $this->_invokeRouteMatch($request, $config);
  353. $this->assertType('array', $values);
  354. $this->assertTrue(isset($values['module']));
  355. $this->assertEquals('mod', $values['module']);
  356. $this->assertEquals('user', $values['controller']);
  357. $this->assertEquals('delete', $values['action']);
  358. $this->assertEquals('lcrouch', $values['id']);
  359. }
  360. public function test_assemble_plain_ignores_action()
  361. {
  362. $route = new Zend_Rest_Route($this->_front, array(), array());
  363. $params = array('module'=>'mod', 'controller'=>'user', 'action'=>'get');
  364. $url = $route->assemble($params);
  365. $this->assertEquals('mod/user', $url);
  366. }
  367. public function test_assemble_id_after_controller()
  368. {
  369. $route = new Zend_Rest_Route($this->_front, array(), array());
  370. $params = array('module'=>'mod', 'controller'=>'user', 'id'=>'lcrouch');
  371. $url = $route->assemble($params);
  372. $this->assertEquals('mod/user/lcrouch', $url);
  373. }
  374. public function test_assemble_index_after_controller_with_params()
  375. {
  376. $route = new Zend_Rest_Route($this->_front, array(), array());
  377. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar');
  378. $url = $route->assemble($params);
  379. $this->assertEquals('mod/user/index/foo/bar', $url);
  380. }
  381. private function _buildRequest($method, $uri)
  382. {
  383. $request = new Zend_Controller_Request_HttpTestCase();
  384. $request->setMethod($method)->setRequestUri($uri);
  385. return $request;
  386. }
  387. private function _invokeRouteMatch($request, $config = array())
  388. {
  389. $this->_front->setRequest($request);
  390. $route = new Zend_Rest_Route($this->_front, array(), $config);
  391. $values = $route->match($request);
  392. return $values;
  393. }
  394. }
  395. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  396. if (PHPUnit_MAIN_METHOD == "Zend_Rest_RouteTest::main") {
  397. Zend_Rest_RouteTest::main();
  398. }