RouteTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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_PUT_user_byIdentifier()
  142. {
  143. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  144. $values = $this->_invokeRouteMatch($request);
  145. $this->assertType('array', $values);
  146. $this->assertTrue(isset($values['module']));
  147. $this->assertEquals('mod', $values['module']);
  148. $this->assertEquals('user', $values['controller']);
  149. $this->assertEquals('put', $values['action']);
  150. $this->assertEquals('lcrouch', $values['id']);
  151. }
  152. public function test_RESTfulApp_POST_user()
  153. {
  154. $request = $this->_buildRequest('POST', '/mod/user');
  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('post', $values['action']);
  161. }
  162. public function test_RESTfulApp_DELETE_user_byIdentifier()
  163. {
  164. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  165. $values = $this->_invokeRouteMatch($request);
  166. $this->assertType('array', $values);
  167. $this->assertTrue(isset($values['module']));
  168. $this->assertEquals('mod', $values['module']);
  169. $this->assertEquals('user', $values['controller']);
  170. $this->assertEquals('delete', $values['action']);
  171. $this->assertEquals('lcrouch', $values['id']);
  172. }
  173. public function test_RESTfulApp_POST_user_with_identifier_doesPUT()
  174. {
  175. $request = $this->_buildRequest('POST', '/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('put', $values['action']);
  182. $this->assertEquals('lcrouch', $values['id']);
  183. }
  184. public function test_RESTfulApp_overload_POST_with_method_param_PUT()
  185. {
  186. $request = $this->_buildRequest('POST', '/mod/user');
  187. $request->setParam('_method', 'PUT');
  188. $values = $this->_invokeRouteMatch($request);
  189. $this->assertType('array', $values);
  190. $this->assertTrue(isset($values['module']));
  191. $this->assertEquals('mod', $values['module']);
  192. $this->assertEquals('user', $values['controller']);
  193. $this->assertEquals('put', $values['action']);
  194. }
  195. public function test_RESTfulApp_overload_POST_with_http_header_DELETE()
  196. {
  197. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  198. $request->setHeader('X-HTTP-Method-Override', 'DELETE');
  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('delete', $values['action']);
  205. $this->assertEquals('lcrouch', $values['id']);
  206. }
  207. public function test_RESTfulModule_GET_user_index()
  208. {
  209. $request = $this->_buildRequest('GET', '/mod/user/index');
  210. $config = array('mod');
  211. $values = $this->_invokeRouteMatch($request, $config);
  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('index', $values['action']);
  217. }
  218. public function test_RESTfulModule_GET_user()
  219. {
  220. $request = $this->_buildRequest('GET', '/mod/user/1234');
  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('get', $values['action']);
  228. }
  229. public function test_RESTfulModule_POST_user()
  230. {
  231. $request = $this->_buildRequest('POST', '/mod/user');
  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('post', $values['action']);
  239. }
  240. public function test_RESTfulModule_POST_user_inNonRESTModule_returnsFalse()
  241. {
  242. $request = $this->_buildRequest('POST', '/default/user');
  243. $config = array('mod');
  244. $values = $this->_invokeRouteMatch($request, $config);
  245. $this->assertFalse($values);
  246. }
  247. public function test_RESTfulModule_PUT_user_byIdentifier()
  248. {
  249. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  250. $config = array('mod');
  251. $values = $this->_invokeRouteMatch($request, $config);
  252. $this->assertType('array', $values);
  253. $this->assertTrue(isset($values['module']));
  254. $this->assertEquals('mod', $values['module']);
  255. $this->assertEquals('user', $values['controller']);
  256. $this->assertEquals('put', $values['action']);
  257. $this->assertEquals('lcrouch', $values['id']);
  258. }
  259. public function test_RESTfulModule_DELETE_user_byIdentifier()
  260. {
  261. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  262. $config = array('mod');
  263. $values = $this->_invokeRouteMatch($request, $config);
  264. $this->assertType('array', $values);
  265. $this->assertTrue(isset($values['module']));
  266. $this->assertEquals('mod', $values['module']);
  267. $this->assertEquals('user', $values['controller']);
  268. $this->assertEquals('delete', $values['action']);
  269. $this->assertEquals('lcrouch', $values['id']);
  270. }
  271. public function test_RESTfulController_GET_user_index()
  272. {
  273. $request = $this->_buildRequest('GET', '/mod/user/index');
  274. $config = array('mod'=>array('user'));
  275. $values = $this->_invokeRouteMatch($request, $config);
  276. $this->assertType('array', $values);
  277. $this->assertTrue(isset($values['module']));
  278. $this->assertEquals('mod', $values['module']);
  279. $this->assertEquals('user', $values['controller']);
  280. $this->assertEquals('index', $values['action']);
  281. }
  282. public function test_RESTfulController_GET_other_index_returns_false()
  283. {
  284. $request = $this->_buildRequest('GET', '/mod/project/index');
  285. $config = array('mod'=>array('user'));
  286. $values = $this->_invokeRouteMatch($request, $config);
  287. $this->assertFalse($values);
  288. }
  289. public function test_RESTfulController_GET_user()
  290. {
  291. $request = $this->_buildRequest('GET', '/mod/user/1234');
  292. $config = array('mod'=>array('user'));
  293. $values = $this->_invokeRouteMatch($request, $config);
  294. $this->assertType('array', $values);
  295. $this->assertTrue(isset($values['module']));
  296. $this->assertEquals('mod', $values['module']);
  297. $this->assertEquals('user', $values['controller']);
  298. $this->assertEquals('get', $values['action']);
  299. }
  300. public function test_RESTfulController_POST_user()
  301. {
  302. $request = $this->_buildRequest('POST', '/mod/user');
  303. $config = array('mod'=>array('user'));
  304. $values = $this->_invokeRouteMatch($request, $config);
  305. $this->assertType('array', $values);
  306. $this->assertTrue(isset($values['module']));
  307. $this->assertEquals('mod', $values['module']);
  308. $this->assertEquals('user', $values['controller']);
  309. $this->assertEquals('post', $values['action']);
  310. }
  311. public function test_RESTfulController_POST_user_inNonRESTModule_returnsFalse()
  312. {
  313. $request = $this->_buildRequest('POST', '/default/user');
  314. $config = array('mod'=>array('user'));
  315. $values = $this->_invokeRouteMatch($request, $config);
  316. $this->assertFalse($values);
  317. }
  318. public function test_RESTfulController_PUT_user_byIdentifier()
  319. {
  320. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  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('put', $values['action']);
  328. $this->assertEquals('lcrouch', $values['id']);
  329. }
  330. public function test_RESTfulController_DELETE_user_byIdentifier()
  331. {
  332. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  333. $config = array('mod');
  334. $values = $this->_invokeRouteMatch($request, $config);
  335. $this->assertType('array', $values);
  336. $this->assertTrue(isset($values['module']));
  337. $this->assertEquals('mod', $values['module']);
  338. $this->assertEquals('user', $values['controller']);
  339. $this->assertEquals('delete', $values['action']);
  340. $this->assertEquals('lcrouch', $values['id']);
  341. }
  342. public function test_assemble_plain_ignores_action()
  343. {
  344. $route = new Zend_Rest_Route($this->_front, array(), array());
  345. $params = array('module'=>'mod', 'controller'=>'user', 'action'=>'get');
  346. $url = $route->assemble($params);
  347. $this->assertEquals('mod/user', $url);
  348. }
  349. public function test_assemble_id_after_controller()
  350. {
  351. $route = new Zend_Rest_Route($this->_front, array(), array());
  352. $params = array('module'=>'mod', 'controller'=>'user', 'id'=>'lcrouch');
  353. $url = $route->assemble($params);
  354. $this->assertEquals('mod/user/lcrouch', $url);
  355. }
  356. public function test_assemble_index_after_controller_with_params()
  357. {
  358. $route = new Zend_Rest_Route($this->_front, array(), array());
  359. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar');
  360. $url = $route->assemble($params);
  361. $this->assertEquals('mod/user/index/foo/bar', $url);
  362. }
  363. private function _buildRequest($method, $uri)
  364. {
  365. $request = new Zend_Controller_Request_HttpTestCase();
  366. $request->setMethod($method)->setRequestUri($uri);
  367. return $request;
  368. }
  369. private function _invokeRouteMatch($request, $config = array())
  370. {
  371. $this->_front->setRequest($request);
  372. $route = new Zend_Rest_Route($this->_front, array(), $config);
  373. $values = $route->match($request);
  374. return $values;
  375. }
  376. }
  377. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  378. if (PHPUnit_MAIN_METHOD == "Zend_Rest_RouteTest::main") {
  379. Zend_Rest_RouteTest::main();
  380. }