RouteTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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-2010 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. /** Zend_Rest_Route */
  23. require_once 'Zend/Rest/Route.php';
  24. /** Zend_Controller_Front */
  25. require_once 'Zend/Controller/Front.php';
  26. /** Zend_Controller_Request_HttpTestCase */
  27. require_once 'Zend/Controller/Request/HttpTestCase.php';
  28. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  29. if (!defined("PHPUnit_MAIN_METHOD")) {
  30. define("PHPUnit_MAIN_METHOD", "Zend_Rest_RouteTest::main");
  31. }
  32. /**
  33. * @category Zend
  34. * @package Zend_Rest
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Rest
  39. */
  40. class Zend_Rest_RouteTest extends PHPUnit_Framework_TestCase
  41. {
  42. protected $_front;
  43. protected $_request;
  44. protected $_dispatcher;
  45. /**
  46. * Runs the test methods of this class.
  47. *
  48. * @access public
  49. * @static
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Rest_RouteTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. public function setUp()
  57. {
  58. $this->_front = Zend_Controller_Front::getInstance();
  59. $this->_front->resetInstance();
  60. $this->_front->setParam('noErrorHandler', true)
  61. ->setParam('noViewRenderer', true);
  62. $this->_dispatcher = $this->_front->getDispatcher();
  63. $this->_dispatcher->setControllerDirectory(array(
  64. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR .
  65. '..' . DIRECTORY_SEPARATOR .
  66. 'Controller' . DIRECTORY_SEPARATOR .
  67. '_files',
  68. 'mod' => dirname(__FILE__) . DIRECTORY_SEPARATOR .
  69. '..' . DIRECTORY_SEPARATOR .
  70. 'Controller' . DIRECTORY_SEPARATOR .
  71. '_files' . DIRECTORY_SEPARATOR .
  72. 'Admin',
  73. ));
  74. }
  75. public function test_getVersion()
  76. {
  77. $route = new Zend_Rest_Route($this->_front);
  78. $this->assertEquals(2, $route->getVersion());
  79. }
  80. public function test_getInstance_fromINIConfig()
  81. {
  82. require_once('Zend/Config/Ini.php');
  83. $config = new Zend_Config_Ini(dirname(__FILE__) . '/../Controller/_files/routes.ini', 'testing');
  84. require_once('Zend/Controller/Router/Rewrite.php');
  85. $router = new Zend_Controller_Router_Rewrite();
  86. $router->addConfig($config, 'routes');
  87. $route = $router->getRoute('rest');
  88. $this->assertType('Zend_Rest_Route', $route);
  89. $this->assertEquals('object', $route->getDefault('controller'));
  90. $request = $this->_buildRequest('GET', '/mod/project');
  91. $values = $this->_invokeRouteMatch($request, array(), $route);
  92. $this->assertEquals('mod', $values['module']);
  93. $this->assertEquals('project', $values['controller']);
  94. $this->assertEquals('index', $values['action']);
  95. $request = $this->_buildRequest('POST', '/mod/user');
  96. $values = $this->_invokeRouteMatch($request, array(), $route);
  97. $this->assertEquals('mod', $values['module']);
  98. $this->assertEquals('user', $values['controller']);
  99. $this->assertEquals('post', $values['action']);
  100. $request = $this->_buildRequest('GET', '/other');
  101. $values = $this->_invokeRouteMatch($request, array(), $route);
  102. $this->assertFalse($values);
  103. }
  104. public function test_RESTfulApp_defaults()
  105. {
  106. $request = $this->_buildRequest('GET', '/');
  107. $values = $this->_invokeRouteMatch($request);
  108. $this->assertType('array', $values);
  109. $this->assertTrue(isset($values['module']));
  110. $this->assertEquals('default', $values['module']);
  111. $this->assertEquals('index', $values['controller']);
  112. $this->assertEquals('index', $values['action']);
  113. }
  114. /*
  115. * @group ZF-7437
  116. */
  117. public function test_RESTfulApp_GET_user_defaults()
  118. {
  119. $request = $this->_buildRequest('GET', '/user');
  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('user', $values['controller']);
  125. $this->assertEquals('index', $values['action']);
  126. }
  127. public function test_RESTfulApp_GET_user_index()
  128. {
  129. $request = $this->_buildRequest('GET', '/user/index');
  130. $values = $this->_invokeRouteMatch($request);
  131. $this->assertType('array', $values);
  132. $this->assertTrue(isset($values['module']));
  133. $this->assertEquals('default', $values['module']);
  134. $this->assertEquals('user', $values['controller']);
  135. $this->assertEquals('index', $values['action']);
  136. }
  137. public function test_RESTfulApp_GET_user_index_withParams()
  138. {
  139. $request = $this->_buildRequest('GET', '/user/index/changedSince/123456789/status/active');
  140. $values = $this->_invokeRouteMatch($request);
  141. $this->assertType('array', $values);
  142. $this->assertTrue(isset($values['module']));
  143. $this->assertEquals('default', $values['module']);
  144. $this->assertEquals('user', $values['controller']);
  145. $this->assertEquals('index', $values['action']);
  146. $this->assertEquals(123456789, $values['changedSince']);
  147. $this->assertEquals('active', $values['status']);
  148. }
  149. public function test_RESTfulApp_GET_user_index_withQueryParams()
  150. {
  151. $request = $this->_buildRequest('GET', '/user/?changedSince=123456789&status=active');
  152. $values = $this->_invokeRouteMatch($request);
  153. $this->assertType('array', $values);
  154. $this->assertTrue(isset($values['module']));
  155. $this->assertEquals('default', $values['module']);
  156. $this->assertEquals('user', $values['controller']);
  157. $this->assertEquals('index', $values['action']);
  158. $this->assertEquals(123456789, $values['changedSince']);
  159. $this->assertEquals('active', $values['status']);
  160. }
  161. public function test_RESTfulApp_GET_project_byIdentifier()
  162. {
  163. $request = $this->_buildRequest('GET', '/project/zendframework');
  164. $values = $this->_invokeRouteMatch($request);
  165. $this->assertType('array', $values);
  166. $this->assertTrue(isset($values['module']));
  167. $this->assertEquals('default', $values['module']);
  168. $this->assertEquals('project', $values['controller']);
  169. $this->assertEquals('get', $values['action']);
  170. $this->assertEquals('zendframework', $values['id']);
  171. }
  172. public function test_RESTfulApp_GET_project_byIdQueryParam()
  173. {
  174. $request = $this->_buildRequest('GET', '/project/?id=zendframework');
  175. $values = $this->_invokeRouteMatch($request);
  176. $this->assertType('array', $values);
  177. $this->assertTrue(isset($values['module']));
  178. $this->assertEquals('default', $values['module']);
  179. $this->assertEquals('project', $values['controller']);
  180. $this->assertEquals('get', $values['action']);
  181. $this->assertEquals('zendframework', $values['id']);
  182. }
  183. public function test_RESTfulApp_GET_project_byIdentifier_urlencoded()
  184. {
  185. $request = $this->_buildRequest('GET', '/project/zend+framework');
  186. $values = $this->_invokeRouteMatch($request);
  187. $this->assertType('array', $values);
  188. $this->assertTrue(isset($values['module']));
  189. $this->assertEquals('default', $values['module']);
  190. $this->assertEquals('project', $values['controller']);
  191. $this->assertEquals('get', $values['action']);
  192. $this->assertEquals('zend framework', $values['id']);
  193. }
  194. public function test_RESTfulApp_HEAD_project_byIdentifier()
  195. {
  196. $request = $this->_buildRequest('HEAD', '/project/lcrouch');
  197. $values = $this->_invokeRouteMatch($request);
  198. $this->assertType('array', $values);
  199. $this->assertTrue(isset($values['module']));
  200. $this->assertEquals('default', $values['module']);
  201. $this->assertEquals('project', $values['controller']);
  202. $this->assertEquals('head', $values['action']);
  203. }
  204. public function test_RESTfulApp_GET_project_edit()
  205. {
  206. $request = $this->_buildRequest('GET', '/project/zendframework/edit');
  207. $values = $this->_invokeRouteMatch($request);
  208. $this->assertType('array', $values);
  209. $this->assertTrue(isset($values['module']));
  210. $this->assertEquals('default', $values['module']);
  211. $this->assertEquals('project', $values['controller']);
  212. $this->assertEquals('edit', $values['action']);
  213. $this->assertEquals('zendframework', $values['id']);
  214. }
  215. public function test_RESTfulApp_PUT_user_byIdentifier()
  216. {
  217. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  218. $values = $this->_invokeRouteMatch($request);
  219. $this->assertType('array', $values);
  220. $this->assertTrue(isset($values['module']));
  221. $this->assertEquals('mod', $values['module']);
  222. $this->assertEquals('user', $values['controller']);
  223. $this->assertEquals('put', $values['action']);
  224. $this->assertEquals('lcrouch', $values['id']);
  225. }
  226. public function test_RESTfulApp_POST_user()
  227. {
  228. $request = $this->_buildRequest('POST', '/mod/user');
  229. $values = $this->_invokeRouteMatch($request);
  230. $this->assertType('array', $values);
  231. $this->assertTrue(isset($values['module']));
  232. $this->assertEquals('mod', $values['module']);
  233. $this->assertEquals('user', $values['controller']);
  234. $this->assertEquals('post', $values['action']);
  235. }
  236. public function test_RESTfulApp_DELETE_user_byIdentifier()
  237. {
  238. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  239. $values = $this->_invokeRouteMatch($request);
  240. $this->assertType('array', $values);
  241. $this->assertTrue(isset($values['module']));
  242. $this->assertEquals('mod', $values['module']);
  243. $this->assertEquals('user', $values['controller']);
  244. $this->assertEquals('delete', $values['action']);
  245. $this->assertEquals('lcrouch', $values['id']);
  246. }
  247. public function test_RESTfulApp_POST_user_with_identifier_doesPUT()
  248. {
  249. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  250. $values = $this->_invokeRouteMatch($request);
  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('put', $values['action']);
  256. $this->assertEquals('lcrouch', $values['id']);
  257. }
  258. public function test_RESTfulApp_overload_POST_with_method_param_PUT()
  259. {
  260. $request = $this->_buildRequest('POST', '/mod/user');
  261. $request->setParam('_method', 'PUT');
  262. $values = $this->_invokeRouteMatch($request);
  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. }
  269. public function test_RESTfulApp_overload_POST_with_http_header_DELETE()
  270. {
  271. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  272. $request->setHeader('X-HTTP-Method-Override', 'DELETE');
  273. $values = $this->_invokeRouteMatch($request);
  274. $this->assertType('array', $values);
  275. $this->assertTrue(isset($values['module']));
  276. $this->assertEquals('mod', $values['module']);
  277. $this->assertEquals('user', $values['controller']);
  278. $this->assertEquals('delete', $values['action']);
  279. $this->assertEquals('lcrouch', $values['id']);
  280. }
  281. public function test_RESTfulApp_route_chaining()
  282. {
  283. $request = $this->_buildRequest('GET', '/api/user/lcrouch');
  284. $this->_front->setRequest($request);
  285. $router = $this->_front->getRouter();
  286. $router->removeDefaultRoutes();
  287. $nonRESTRoute = new Zend_Controller_Router_Route('api');
  288. $RESTRoute = new Zend_Rest_Route($this->_front);
  289. $router->addRoute("api", $nonRESTRoute->chain($RESTRoute));
  290. $routedRequest = $router->route($request);
  291. $this->assertEquals("default", $routedRequest->getParam("module"));
  292. $this->assertEquals("user", $routedRequest->getParam("controller"));
  293. $this->assertEquals("get", $routedRequest->getParam("action"));
  294. $this->assertEquals("lcrouch", $routedRequest->getParam("id"));
  295. }
  296. public function test_RESTfulModule_GET_user_index()
  297. {
  298. $request = $this->_buildRequest('GET', '/mod/user/index');
  299. $config = array('mod');
  300. $values = $this->_invokeRouteMatch($request, $config);
  301. $this->assertType('array', $values);
  302. $this->assertTrue(isset($values['module']));
  303. $this->assertEquals('mod', $values['module']);
  304. $this->assertEquals('user', $values['controller']);
  305. $this->assertEquals('index', $values['action']);
  306. }
  307. public function test_RESTfulModule_GET_user()
  308. {
  309. $request = $this->_buildRequest('GET', '/mod/user/1234');
  310. $config = array('mod');
  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_RESTfulModule_POST_user()
  319. {
  320. $request = $this->_buildRequest('POST', '/mod/user');
  321. $config = array('mod');
  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_RESTfulModule_POST_user_inNonRESTModule_returnsFalse()
  330. {
  331. $request = $this->_buildRequest('POST', '/default/user');
  332. $config = array('mod');
  333. $values = $this->_invokeRouteMatch($request, $config);
  334. $this->assertFalse($values);
  335. }
  336. public function test_RESTfulModule_PUT_user_byIdentifier()
  337. {
  338. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  339. $config = array('mod');
  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_RESTfulModule_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_RESTfulController_GET_user_index()
  361. {
  362. $request = $this->_buildRequest('GET', '/mod/user/index');
  363. $config = array('mod'=>array('user'));
  364. $values = $this->_invokeRouteMatch($request, $config);
  365. $this->assertType('array', $values);
  366. $this->assertTrue(isset($values['module']));
  367. $this->assertEquals('mod', $values['module']);
  368. $this->assertEquals('user', $values['controller']);
  369. $this->assertEquals('index', $values['action']);
  370. }
  371. public function test_RESTfulController_GET_default_controller_returns_false()
  372. {
  373. $request = $this->_buildRequest('GET', '/mod/index/index');
  374. $config = array('mod'=>array('user'));
  375. $values = $this->_invokeRouteMatch($request, $config);
  376. $this->assertFalse($values);
  377. }
  378. public function test_RESTfulController_GET_other_index_returns_false()
  379. {
  380. $request = $this->_buildRequest('GET', '/mod/project/index');
  381. $config = array('mod'=>array('user'));
  382. $values = $this->_invokeRouteMatch($request, $config);
  383. $this->assertFalse($values);
  384. }
  385. public function test_RESTfulController_GET_user()
  386. {
  387. $request = $this->_buildRequest('GET', '/mod/user/1234');
  388. $config = array('mod'=>array('user'));
  389. $values = $this->_invokeRouteMatch($request, $config);
  390. $this->assertType('array', $values);
  391. $this->assertTrue(isset($values['module']));
  392. $this->assertEquals('mod', $values['module']);
  393. $this->assertEquals('user', $values['controller']);
  394. $this->assertEquals('get', $values['action']);
  395. }
  396. public function test_RESTfulController_POST_user()
  397. {
  398. $request = $this->_buildRequest('POST', '/mod/user');
  399. $config = array('mod'=>array('user'));
  400. $values = $this->_invokeRouteMatch($request, $config);
  401. $this->assertType('array', $values);
  402. $this->assertTrue(isset($values['module']));
  403. $this->assertEquals('mod', $values['module']);
  404. $this->assertEquals('user', $values['controller']);
  405. $this->assertEquals('post', $values['action']);
  406. }
  407. public function test_RESTfulController_POST_user_inNonRESTModule_returnsFalse()
  408. {
  409. $request = $this->_buildRequest('POST', '/default/user');
  410. $config = array('mod'=>array('user'));
  411. $values = $this->_invokeRouteMatch($request, $config);
  412. $this->assertFalse($values);
  413. }
  414. public function test_postToNonRESTfulDefaultController_moduleHasAnotherRESTfulController_defaultControllerInURL_returnsFalse()
  415. {
  416. $request = $this->_buildRequest('POST', '/mod/index');
  417. $config = array('mod'=>array('user'));
  418. $values = $this->_invokeRouteMatch($request, $config);
  419. $this->assertFalse($values);
  420. }
  421. public function test_postToNonRESTfulDefaultController_moduleHasAnotherRESTfulController_noDefaultControllerInURL_returnsFalse()
  422. {
  423. $request = $this->_buildRequest('POST', '/mod');
  424. $config = array('mod'=>array('user'));
  425. $values = $this->_invokeRouteMatch($request, $config);
  426. $this->assertFalse($values);
  427. }
  428. public function test_RESTfulController_PUT_user_byIdentifier()
  429. {
  430. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  431. $config = array('mod'=>array('user'));
  432. $values = $this->_invokeRouteMatch($request, $config);
  433. $this->assertType('array', $values);
  434. $this->assertTrue(isset($values['module']));
  435. $this->assertEquals('mod', $values['module']);
  436. $this->assertEquals('user', $values['controller']);
  437. $this->assertEquals('put', $values['action']);
  438. $this->assertEquals('lcrouch', $values['id']);
  439. }
  440. public function test_RESTfulController_DELETE_user_byIdentifier()
  441. {
  442. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  443. $config = array('mod');
  444. $values = $this->_invokeRouteMatch($request, $config);
  445. $this->assertType('array', $values);
  446. $this->assertTrue(isset($values['module']));
  447. $this->assertEquals('mod', $values['module']);
  448. $this->assertEquals('user', $values['controller']);
  449. $this->assertEquals('delete', $values['action']);
  450. $this->assertEquals('lcrouch', $values['id']);
  451. }
  452. public function test_assemble_plain_ignores_action()
  453. {
  454. $route = new Zend_Rest_Route($this->_front, array(), array());
  455. $params = array('module'=>'mod', 'controller'=>'user', 'action'=>'get');
  456. $url = $route->assemble($params);
  457. $this->assertEquals('mod/user', $url);
  458. }
  459. public function test_assemble_id_after_controller()
  460. {
  461. $route = new Zend_Rest_Route($this->_front, array(), array());
  462. $params = array('module'=>'mod', 'controller'=>'user', 'id'=>'lcrouch');
  463. $url = $route->assemble($params);
  464. $this->assertEquals('mod/user/lcrouch', $url);
  465. }
  466. public function test_assemble_index_after_controller_with_params()
  467. {
  468. $route = new Zend_Rest_Route($this->_front, array(), array());
  469. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar');
  470. $url = $route->assemble($params);
  471. $this->assertEquals('mod/user/index/foo/bar', $url);
  472. }
  473. public function test_assemble_encode_param_values()
  474. {
  475. $route = new Zend_Rest_Route($this->_front, array(), array());
  476. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar is n!ice');
  477. $url = $route->assemble($params);
  478. $this->assertEquals('mod/user/index/foo/bar+is+n%21ice', $url);
  479. }
  480. public function test_assemble_does_NOT_encode_param_values()
  481. {
  482. $route = new Zend_Rest_Route($this->_front, array(), array());
  483. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar is n!ice');
  484. $url = $route->assemble($params, false, false);
  485. $this->assertEquals('mod/user/index/foo/bar is n!ice', $url);
  486. }
  487. /**
  488. * @group ZF-9823
  489. */
  490. public function test_assemble_edit_with_module_appends_action_after_id()
  491. {
  492. $route = new Zend_Rest_Route($this->_front, array(), array());
  493. $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'edit', 'id'=>1);
  494. $url = $route->assemble($params);
  495. $this->assertEquals('mod/users/1/edit', $url);
  496. }
  497. /**
  498. * @group ZF-9823
  499. */
  500. public function test_assemble_edit_without_module_appends_action_after_id()
  501. {
  502. $route = new Zend_Rest_Route($this->_front, array(), array());
  503. $params = array('controller'=>'users', 'action'=>'edit', 'id'=>1);
  504. $url = $route->assemble($params);
  505. $this->assertEquals('users/1/edit', $url);
  506. }
  507. /**
  508. * @group ZF-9823
  509. */
  510. public function test_assemble_new_with_module_appends_action()
  511. {
  512. $route = new Zend_Rest_Route($this->_front, array(), array());
  513. $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'new');
  514. $url = $route->assemble($params);
  515. $this->assertEquals('mod/users/new', $url);
  516. }
  517. /**
  518. * @group ZF-9823
  519. */
  520. public function test_assemble_new_without_module_appends_action()
  521. {
  522. $route = new Zend_Rest_Route($this->_front, array(), array());
  523. $params = array('controller'=>'users', 'action'=>'new');
  524. $url = $route->assemble($params);
  525. $this->assertEquals('users/new', $url);
  526. }
  527. /**
  528. * @group ZF-9823
  529. */
  530. public function test_assemble_random_action_with_module_removes_action()
  531. {
  532. $route = new Zend_Rest_Route($this->_front, array(), array());
  533. $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'newbar');
  534. $url = $route->assemble($params);
  535. $this->assertNotEquals('mod/users/newbar', $url);
  536. }
  537. /**
  538. * @group ZF-9823
  539. */
  540. public function test_assemble_random_action_without_module_removes_action()
  541. {
  542. $route = new Zend_Rest_Route($this->_front, array(), array());
  543. $params = array('controller'=>'users', 'action'=>'newbar');
  544. $url = $route->assemble($params);
  545. $this->assertNotEquals('users/newbar', $url);
  546. }
  547. /**
  548. * @group ZF-9823
  549. */
  550. public function test_assemble_with_module_honors_index_parameter_with_resource_id_and_extra_parameters()
  551. {
  552. $route = new Zend_Rest_Route($this->_front, array(), array());
  553. $params = array('module'=>'mod', 'controller'=>'users', 'id' => 1, 'extra'=>'parameter', 'another' => 'parameter', 'index' => true);
  554. $url = $route->assemble($params, false, false);
  555. $this->assertEquals('mod/users/index/1/extra/parameter/another/parameter', $url);
  556. }
  557. /**
  558. * @group ZF-9823
  559. */
  560. public function test_assemble_without_module_honors_index_parameter_with_resource_id_and_extra_parameters()
  561. {
  562. $route = new Zend_Rest_Route($this->_front, array(), array());
  563. $params = array('controller'=>'users', 'id' => 1, 'extra'=>'parameter', 'another' => 'parameter', 'index' => true);
  564. $url = $route->assemble($params, false, false);
  565. $this->assertEquals('users/index/1/extra/parameter/another/parameter', $url);
  566. }
  567. private function _buildRequest($method, $uri)
  568. {
  569. $request = new Zend_Controller_Request_HttpTestCase();
  570. $request->setMethod($method)->setRequestUri($uri);
  571. return $request;
  572. }
  573. private function _invokeRouteMatch($request, $config = array(), $route = null)
  574. {
  575. $this->_front->setRequest($request);
  576. if ($route == null)
  577. $route = new Zend_Rest_Route($this->_front, array(), $config);
  578. $values = $route->match($request);
  579. return $values;
  580. }
  581. }
  582. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  583. if (PHPUnit_MAIN_METHOD == "Zend_Rest_RouteTest::main") {
  584. Zend_Rest_RouteTest::main();
  585. }