RouteTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. /** 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-2010 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_user_index_withQueryParams()
  155. {
  156. $request = $this->_buildRequest('GET', '/user/?changedSince=123456789&status=active');
  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('user', $values['controller']);
  162. $this->assertEquals('index', $values['action']);
  163. $this->assertEquals(123456789, $values['changedSince']);
  164. $this->assertEquals('active', $values['status']);
  165. }
  166. public function test_RESTfulApp_GET_project_byIdentifier()
  167. {
  168. $request = $this->_buildRequest('GET', '/project/zendframework');
  169. $values = $this->_invokeRouteMatch($request);
  170. $this->assertType('array', $values);
  171. $this->assertTrue(isset($values['module']));
  172. $this->assertEquals('default', $values['module']);
  173. $this->assertEquals('project', $values['controller']);
  174. $this->assertEquals('get', $values['action']);
  175. $this->assertEquals('zendframework', $values['id']);
  176. }
  177. public function test_RESTfulApp_GET_project_byIdQueryParam()
  178. {
  179. $request = $this->_buildRequest('GET', '/project/?id=zendframework');
  180. $values = $this->_invokeRouteMatch($request);
  181. $this->assertType('array', $values);
  182. $this->assertTrue(isset($values['module']));
  183. $this->assertEquals('default', $values['module']);
  184. $this->assertEquals('project', $values['controller']);
  185. $this->assertEquals('get', $values['action']);
  186. $this->assertEquals('zendframework', $values['id']);
  187. }
  188. public function test_RESTfulApp_GET_project_byIdentifier_urlencoded()
  189. {
  190. $request = $this->_buildRequest('GET', '/project/zend+framework');
  191. $values = $this->_invokeRouteMatch($request);
  192. $this->assertType('array', $values);
  193. $this->assertTrue(isset($values['module']));
  194. $this->assertEquals('default', $values['module']);
  195. $this->assertEquals('project', $values['controller']);
  196. $this->assertEquals('get', $values['action']);
  197. $this->assertEquals('zend framework', $values['id']);
  198. }
  199. public function test_RESTfulApp_HEAD_project_byIdentifier()
  200. {
  201. $request = $this->_buildRequest('HEAD', '/project/lcrouch');
  202. $values = $this->_invokeRouteMatch($request);
  203. $this->assertType('array', $values);
  204. $this->assertTrue(isset($values['module']));
  205. $this->assertEquals('default', $values['module']);
  206. $this->assertEquals('project', $values['controller']);
  207. $this->assertEquals('head', $values['action']);
  208. }
  209. public function test_RESTfulApp_GET_project_edit()
  210. {
  211. $request = $this->_buildRequest('GET', '/project/zendframework/edit');
  212. $values = $this->_invokeRouteMatch($request);
  213. $this->assertType('array', $values);
  214. $this->assertTrue(isset($values['module']));
  215. $this->assertEquals('default', $values['module']);
  216. $this->assertEquals('project', $values['controller']);
  217. $this->assertEquals('edit', $values['action']);
  218. $this->assertEquals('zendframework', $values['id']);
  219. }
  220. public function test_RESTfulApp_PUT_user_byIdentifier()
  221. {
  222. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  223. $values = $this->_invokeRouteMatch($request);
  224. $this->assertType('array', $values);
  225. $this->assertTrue(isset($values['module']));
  226. $this->assertEquals('mod', $values['module']);
  227. $this->assertEquals('user', $values['controller']);
  228. $this->assertEquals('put', $values['action']);
  229. $this->assertEquals('lcrouch', $values['id']);
  230. }
  231. public function test_RESTfulApp_POST_user()
  232. {
  233. $request = $this->_buildRequest('POST', '/mod/user');
  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('post', $values['action']);
  240. }
  241. public function test_RESTfulApp_DELETE_user_byIdentifier()
  242. {
  243. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  244. $values = $this->_invokeRouteMatch($request);
  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('delete', $values['action']);
  250. $this->assertEquals('lcrouch', $values['id']);
  251. }
  252. public function test_RESTfulApp_POST_user_with_identifier_doesPUT()
  253. {
  254. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  255. $values = $this->_invokeRouteMatch($request);
  256. $this->assertType('array', $values);
  257. $this->assertTrue(isset($values['module']));
  258. $this->assertEquals('mod', $values['module']);
  259. $this->assertEquals('user', $values['controller']);
  260. $this->assertEquals('put', $values['action']);
  261. $this->assertEquals('lcrouch', $values['id']);
  262. }
  263. public function test_RESTfulApp_overload_POST_with_method_param_PUT()
  264. {
  265. $request = $this->_buildRequest('POST', '/mod/user');
  266. $request->setParam('_method', 'PUT');
  267. $values = $this->_invokeRouteMatch($request);
  268. $this->assertType('array', $values);
  269. $this->assertTrue(isset($values['module']));
  270. $this->assertEquals('mod', $values['module']);
  271. $this->assertEquals('user', $values['controller']);
  272. $this->assertEquals('put', $values['action']);
  273. }
  274. public function test_RESTfulApp_overload_POST_with_http_header_DELETE()
  275. {
  276. $request = $this->_buildRequest('POST', '/mod/user/lcrouch');
  277. $request->setHeader('X-HTTP-Method-Override', 'DELETE');
  278. $values = $this->_invokeRouteMatch($request);
  279. $this->assertType('array', $values);
  280. $this->assertTrue(isset($values['module']));
  281. $this->assertEquals('mod', $values['module']);
  282. $this->assertEquals('user', $values['controller']);
  283. $this->assertEquals('delete', $values['action']);
  284. $this->assertEquals('lcrouch', $values['id']);
  285. }
  286. public function test_RESTfulApp_route_chaining()
  287. {
  288. $request = $this->_buildRequest('GET', '/api/user/lcrouch');
  289. $this->_front->setRequest($request);
  290. $router = $this->_front->getRouter();
  291. $router->removeDefaultRoutes();
  292. $nonRESTRoute = new Zend_Controller_Router_Route('api');
  293. $RESTRoute = new Zend_Rest_Route($this->_front);
  294. $router->addRoute("api", $nonRESTRoute->chain($RESTRoute));
  295. $routedRequest = $router->route($request);
  296. $this->assertEquals("default", $routedRequest->getParam("module"));
  297. $this->assertEquals("user", $routedRequest->getParam("controller"));
  298. $this->assertEquals("get", $routedRequest->getParam("action"));
  299. $this->assertEquals("lcrouch", $routedRequest->getParam("id"));
  300. }
  301. public function test_RESTfulModule_GET_user_index()
  302. {
  303. $request = $this->_buildRequest('GET', '/mod/user/index');
  304. $config = array('mod');
  305. $values = $this->_invokeRouteMatch($request, $config);
  306. $this->assertType('array', $values);
  307. $this->assertTrue(isset($values['module']));
  308. $this->assertEquals('mod', $values['module']);
  309. $this->assertEquals('user', $values['controller']);
  310. $this->assertEquals('index', $values['action']);
  311. }
  312. public function test_RESTfulModule_GET_user()
  313. {
  314. $request = $this->_buildRequest('GET', '/mod/user/1234');
  315. $config = array('mod');
  316. $values = $this->_invokeRouteMatch($request, $config);
  317. $this->assertType('array', $values);
  318. $this->assertTrue(isset($values['module']));
  319. $this->assertEquals('mod', $values['module']);
  320. $this->assertEquals('user', $values['controller']);
  321. $this->assertEquals('get', $values['action']);
  322. }
  323. public function test_RESTfulModule_POST_user()
  324. {
  325. $request = $this->_buildRequest('POST', '/mod/user');
  326. $config = array('mod');
  327. $values = $this->_invokeRouteMatch($request, $config);
  328. $this->assertType('array', $values);
  329. $this->assertTrue(isset($values['module']));
  330. $this->assertEquals('mod', $values['module']);
  331. $this->assertEquals('user', $values['controller']);
  332. $this->assertEquals('post', $values['action']);
  333. }
  334. public function test_RESTfulModule_POST_user_inNonRESTModule_returnsFalse()
  335. {
  336. $request = $this->_buildRequest('POST', '/default/user');
  337. $config = array('mod');
  338. $values = $this->_invokeRouteMatch($request, $config);
  339. $this->assertFalse($values);
  340. }
  341. public function test_RESTfulModule_PUT_user_byIdentifier()
  342. {
  343. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  344. $config = array('mod');
  345. $values = $this->_invokeRouteMatch($request, $config);
  346. $this->assertType('array', $values);
  347. $this->assertTrue(isset($values['module']));
  348. $this->assertEquals('mod', $values['module']);
  349. $this->assertEquals('user', $values['controller']);
  350. $this->assertEquals('put', $values['action']);
  351. $this->assertEquals('lcrouch', $values['id']);
  352. }
  353. public function test_RESTfulModule_DELETE_user_byIdentifier()
  354. {
  355. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  356. $config = array('mod');
  357. $values = $this->_invokeRouteMatch($request, $config);
  358. $this->assertType('array', $values);
  359. $this->assertTrue(isset($values['module']));
  360. $this->assertEquals('mod', $values['module']);
  361. $this->assertEquals('user', $values['controller']);
  362. $this->assertEquals('delete', $values['action']);
  363. $this->assertEquals('lcrouch', $values['id']);
  364. }
  365. public function test_RESTfulController_GET_user_index()
  366. {
  367. $request = $this->_buildRequest('GET', '/mod/user/index');
  368. $config = array('mod'=>array('user'));
  369. $values = $this->_invokeRouteMatch($request, $config);
  370. $this->assertType('array', $values);
  371. $this->assertTrue(isset($values['module']));
  372. $this->assertEquals('mod', $values['module']);
  373. $this->assertEquals('user', $values['controller']);
  374. $this->assertEquals('index', $values['action']);
  375. }
  376. public function test_RESTfulController_GET_default_controller_returns_false()
  377. {
  378. $request = $this->_buildRequest('GET', '/mod/index/index');
  379. $config = array('mod'=>array('user'));
  380. $values = $this->_invokeRouteMatch($request, $config);
  381. $this->assertFalse($values);
  382. }
  383. public function test_RESTfulController_GET_other_index_returns_false()
  384. {
  385. $request = $this->_buildRequest('GET', '/mod/project/index');
  386. $config = array('mod'=>array('user'));
  387. $values = $this->_invokeRouteMatch($request, $config);
  388. $this->assertFalse($values);
  389. }
  390. public function test_RESTfulController_GET_user()
  391. {
  392. $request = $this->_buildRequest('GET', '/mod/user/1234');
  393. $config = array('mod'=>array('user'));
  394. $values = $this->_invokeRouteMatch($request, $config);
  395. $this->assertType('array', $values);
  396. $this->assertTrue(isset($values['module']));
  397. $this->assertEquals('mod', $values['module']);
  398. $this->assertEquals('user', $values['controller']);
  399. $this->assertEquals('get', $values['action']);
  400. }
  401. public function test_RESTfulController_POST_user()
  402. {
  403. $request = $this->_buildRequest('POST', '/mod/user');
  404. $config = array('mod'=>array('user'));
  405. $values = $this->_invokeRouteMatch($request, $config);
  406. $this->assertType('array', $values);
  407. $this->assertTrue(isset($values['module']));
  408. $this->assertEquals('mod', $values['module']);
  409. $this->assertEquals('user', $values['controller']);
  410. $this->assertEquals('post', $values['action']);
  411. }
  412. public function test_RESTfulController_POST_user_inNonRESTModule_returnsFalse()
  413. {
  414. $request = $this->_buildRequest('POST', '/default/user');
  415. $config = array('mod'=>array('user'));
  416. $values = $this->_invokeRouteMatch($request, $config);
  417. $this->assertFalse($values);
  418. }
  419. public function test_postToNonRESTfulDefaultController_moduleHasAnotherRESTfulController_defaultControllerInURL_returnsFalse()
  420. {
  421. $request = $this->_buildRequest('POST', '/mod/index');
  422. $config = array('mod'=>array('user'));
  423. $values = $this->_invokeRouteMatch($request, $config);
  424. $this->assertFalse($values);
  425. }
  426. public function test_postToNonRESTfulDefaultController_moduleHasAnotherRESTfulController_noDefaultControllerInURL_returnsFalse()
  427. {
  428. $request = $this->_buildRequest('POST', '/mod');
  429. $config = array('mod'=>array('user'));
  430. $values = $this->_invokeRouteMatch($request, $config);
  431. $this->assertFalse($values);
  432. }
  433. public function test_RESTfulController_PUT_user_byIdentifier()
  434. {
  435. $request = $this->_buildRequest('PUT', '/mod/user/lcrouch');
  436. $config = array('mod'=>array('user'));
  437. $values = $this->_invokeRouteMatch($request, $config);
  438. $this->assertType('array', $values);
  439. $this->assertTrue(isset($values['module']));
  440. $this->assertEquals('mod', $values['module']);
  441. $this->assertEquals('user', $values['controller']);
  442. $this->assertEquals('put', $values['action']);
  443. $this->assertEquals('lcrouch', $values['id']);
  444. }
  445. public function test_RESTfulController_DELETE_user_byIdentifier()
  446. {
  447. $request = $this->_buildRequest('DELETE', '/mod/user/lcrouch');
  448. $config = array('mod');
  449. $values = $this->_invokeRouteMatch($request, $config);
  450. $this->assertType('array', $values);
  451. $this->assertTrue(isset($values['module']));
  452. $this->assertEquals('mod', $values['module']);
  453. $this->assertEquals('user', $values['controller']);
  454. $this->assertEquals('delete', $values['action']);
  455. $this->assertEquals('lcrouch', $values['id']);
  456. }
  457. public function test_assemble_plain_ignores_action()
  458. {
  459. $route = new Zend_Rest_Route($this->_front, array(), array());
  460. $params = array('module'=>'mod', 'controller'=>'user', 'action'=>'get');
  461. $url = $route->assemble($params);
  462. $this->assertEquals('mod/user', $url);
  463. }
  464. public function test_assemble_id_after_controller()
  465. {
  466. $route = new Zend_Rest_Route($this->_front, array(), array());
  467. $params = array('module'=>'mod', 'controller'=>'user', 'id'=>'lcrouch');
  468. $url = $route->assemble($params);
  469. $this->assertEquals('mod/user/lcrouch', $url);
  470. }
  471. public function test_assemble_index_after_controller_with_params()
  472. {
  473. $route = new Zend_Rest_Route($this->_front, array(), array());
  474. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar');
  475. $url = $route->assemble($params);
  476. $this->assertEquals('mod/user/index/foo/bar', $url);
  477. }
  478. public function test_assemble_encode_param_values()
  479. {
  480. $route = new Zend_Rest_Route($this->_front, array(), array());
  481. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar is n!ice');
  482. $url = $route->assemble($params);
  483. $this->assertEquals('mod/user/index/foo/bar+is+n%21ice', $url);
  484. }
  485. public function test_assemble_does_NOT_encode_param_values()
  486. {
  487. $route = new Zend_Rest_Route($this->_front, array(), array());
  488. $params = array('module'=>'mod', 'controller'=>'user', 'index'=>true, 'foo'=>'bar is n!ice');
  489. $url = $route->assemble($params, false, false);
  490. $this->assertEquals('mod/user/index/foo/bar is n!ice', $url);
  491. }
  492. /**
  493. * @group ZF-9823
  494. */
  495. public function test_assemble_edit_with_module_appends_action_after_id()
  496. {
  497. $route = new Zend_Rest_Route($this->_front, array(), array());
  498. $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'edit', 'id'=>1);
  499. $url = $route->assemble($params);
  500. $this->assertEquals('mod/users/1/edit', $url);
  501. }
  502. /**
  503. * @group ZF-9823
  504. */
  505. public function test_assemble_edit_without_module_appends_action_after_id()
  506. {
  507. $route = new Zend_Rest_Route($this->_front, array(), array());
  508. $params = array('controller'=>'users', 'action'=>'edit', 'id'=>1);
  509. $url = $route->assemble($params);
  510. $this->assertEquals('users/1/edit', $url);
  511. }
  512. /**
  513. * @group ZF-9823
  514. */
  515. public function test_assemble_new_with_module_appends_action()
  516. {
  517. $route = new Zend_Rest_Route($this->_front, array(), array());
  518. $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'new');
  519. $url = $route->assemble($params);
  520. $this->assertEquals('mod/users/new', $url);
  521. }
  522. /**
  523. * @group ZF-9823
  524. */
  525. public function test_assemble_new_without_module_appends_action()
  526. {
  527. $route = new Zend_Rest_Route($this->_front, array(), array());
  528. $params = array('controller'=>'users', 'action'=>'new');
  529. $url = $route->assemble($params);
  530. $this->assertEquals('users/new', $url);
  531. }
  532. /**
  533. * @group ZF-9823
  534. */
  535. public function test_assemble_random_action_with_module_removes_action()
  536. {
  537. $route = new Zend_Rest_Route($this->_front, array(), array());
  538. $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'newbar');
  539. $url = $route->assemble($params);
  540. $this->assertNotEquals('mod/users/newbar', $url);
  541. }
  542. /**
  543. * @group ZF-9823
  544. */
  545. public function test_assemble_random_action_without_module_removes_action()
  546. {
  547. $route = new Zend_Rest_Route($this->_front, array(), array());
  548. $params = array('controller'=>'users', 'action'=>'newbar');
  549. $url = $route->assemble($params);
  550. $this->assertNotEquals('users/newbar', $url);
  551. }
  552. /**
  553. * @group ZF-9823
  554. */
  555. public function test_assemble_with_module_honors_index_parameter_with_resource_id_and_extra_parameters()
  556. {
  557. $route = new Zend_Rest_Route($this->_front, array(), array());
  558. $params = array('module'=>'mod', 'controller'=>'users', 'id' => 1, 'extra'=>'parameter', 'another' => 'parameter', 'index' => true);
  559. $url = $route->assemble($params, false, false);
  560. $this->assertEquals('mod/users/index/1/extra/parameter/another/parameter', $url);
  561. }
  562. /**
  563. * @group ZF-9823
  564. */
  565. public function test_assemble_without_module_honors_index_parameter_with_resource_id_and_extra_parameters()
  566. {
  567. $route = new Zend_Rest_Route($this->_front, array(), array());
  568. $params = array('controller'=>'users', 'id' => 1, 'extra'=>'parameter', 'another' => 'parameter', 'index' => true);
  569. $url = $route->assemble($params, false, false);
  570. $this->assertEquals('users/index/1/extra/parameter/another/parameter', $url);
  571. }
  572. private function _buildRequest($method, $uri)
  573. {
  574. $request = new Zend_Controller_Request_HttpTestCase();
  575. $request->setMethod($method)->setRequestUri($uri);
  576. return $request;
  577. }
  578. private function _invokeRouteMatch($request, $config = array(), $route = null)
  579. {
  580. $this->_front->setRequest($request);
  581. if ($route == null)
  582. $route = new Zend_Rest_Route($this->_front, array(), $config);
  583. $values = $route->match($request);
  584. return $values;
  585. }
  586. }
  587. // Call Zend_Rest_RouteTest::main() if this source file is executed directly.
  588. if (PHPUnit_MAIN_METHOD == "Zend_Rest_RouteTest::main") {
  589. Zend_Rest_RouteTest::main();
  590. }