RewriteTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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_Controller
  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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Controller_Router_RewriteTest::main');
  24. }
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /** Zend_Controller_Router_Rewrite */
  27. require_once 'Zend/Controller/Router/Rewrite.php';
  28. /** Zend_Controller_Dispatcher_Standard */
  29. require_once 'Zend/Controller/Dispatcher/Standard.php';
  30. /** Zend_Controller_Front */
  31. require_once 'Zend/Controller/Front.php';
  32. /** Zend_Controller_Request_Http */
  33. require_once 'Zend/Controller/Request/Http.php';
  34. /** Zend_Controller_Router_Route */
  35. require_once 'Zend/Controller/Router/Route.php';
  36. /** Zend_Controller_Router_Route_Chain */
  37. require_once 'Zend/Controller/Router/Route/Chain.php';
  38. /** Zend_Controller_Router_Route_Hostname */
  39. require_once 'Zend/Controller/Router/Route/Hostname.php';
  40. /** Zend_Uri_Http */
  41. require_once 'Zend/Uri/Http.php';
  42. /**
  43. * @category Zend
  44. * @package Zend_Controller
  45. * @subpackage UnitTests
  46. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. * @group Zend_Controller
  49. * @group Zend_Controller_Router
  50. */
  51. class Zend_Controller_Router_RewriteTest extends PHPUnit_Framework_TestCase
  52. {
  53. protected $_router;
  54. /**
  55. * Runs the test methods of this class.
  56. *
  57. * @access public
  58. * @static
  59. */
  60. public static function main()
  61. {
  62. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Router_RewriteTest");
  63. $result = PHPUnit_TextUI_TestRunner::run($suite);
  64. }
  65. public function setUp() {
  66. $this->_router = new Zend_Controller_Router_Rewrite();
  67. $front = Zend_Controller_Front::getInstance();
  68. $front->resetInstance();
  69. $front->setDispatcher(new Zend_Controller_Router_RewriteTest_Dispatcher());
  70. $front->setRequest(new Zend_Controller_Router_RewriteTest_Request());
  71. $this->_router->setFrontController($front);
  72. }
  73. public function tearDown() {
  74. unset($this->_router);
  75. }
  76. public function testAddRoute()
  77. {
  78. $this->_router->addRoute('archive', new Zend_Controller_Router_Route('archive/:year', array('year' => '2006', 'controller' => 'archive', 'action' => 'show'), array('year' => '\d+')));
  79. $routes = $this->_router->getRoutes();
  80. $this->assertEquals(1, count($routes));
  81. $this->assertType('Zend_Controller_Router_Route', $routes['archive']);
  82. $this->_router->addRoute('register', new Zend_Controller_Router_Route('register/:action', array('controller' => 'profile', 'action' => 'register')));
  83. $routes = $this->_router->getRoutes();
  84. $this->assertEquals(2, count($routes));
  85. $this->assertType('Zend_Controller_Router_Route', $routes['register']);
  86. }
  87. public function testAddRoutes()
  88. {
  89. $routes = array(
  90. 'archive' => new Zend_Controller_Router_Route('archive/:year', array('year' => '2006', 'controller' => 'archive', 'action' => 'show'), array('year' => '\d+')),
  91. 'register' => new Zend_Controller_Router_Route('register/:action', array('controller' => 'profile', 'action' => 'register'))
  92. );
  93. $this->_router->addRoutes($routes);
  94. $values = $this->_router->getRoutes();
  95. $this->assertEquals(2, count($values));
  96. $this->assertType('Zend_Controller_Router_Route', $values['archive']);
  97. $this->assertType('Zend_Controller_Router_Route', $values['register']);
  98. }
  99. public function testHasRoute()
  100. {
  101. $this->_router->addRoute('archive', new Zend_Controller_Router_Route('archive/:year', array('year' => '2006', 'controller' => 'archive', 'action' => 'show'), array('year' => '\d+')));
  102. $this->assertEquals(true, $this->_router->hasRoute('archive'));
  103. $this->assertEquals(false, $this->_router->hasRoute('bogus'));
  104. }
  105. public function testGetRoute()
  106. {
  107. $archive = new Zend_Controller_Router_Route('archive/:year', array('year' => '2006', 'controller' => 'archive', 'action' => 'show'), array('year' => '\d+'));
  108. $this->_router->addRoute('archive', $archive);
  109. $route = $this->_router->getRoute('archive');
  110. $this->assertType('Zend_Controller_Router_Route', $route);
  111. $this->assertSame($route, $archive);
  112. }
  113. public function testRemoveRoute()
  114. {
  115. $this->_router->addRoute('archive', new Zend_Controller_Router_Route('archive/:year', array('year' => '2006', 'controller' => 'archive', 'action' => 'show'), array('year' => '\d+')));
  116. $route = $this->_router->getRoute('archive');
  117. $this->_router->removeRoute('archive');
  118. $routes = $this->_router->getRoutes();
  119. $this->assertEquals(0, count($routes));
  120. try {
  121. $route = $this->_router->removeRoute('archive');
  122. } catch (Zend_Controller_Router_Exception $e) {
  123. $this->assertType('Zend_Controller_Router_Exception', $e);
  124. return true;
  125. }
  126. $this->fail();
  127. }
  128. public function testGetNonExistentRoute()
  129. {
  130. try {
  131. $route = $this->_router->getRoute('bogus');
  132. } catch (Zend_Controller_Router_Exception $e) {
  133. $this->assertType('Zend_Controller_Router_Exception', $e);
  134. return true;
  135. }
  136. $this->fail();
  137. }
  138. public function testRoute()
  139. {
  140. $request = new Zend_Controller_Router_RewriteTest_Request();
  141. $token = $this->_router->route($request);
  142. $this->assertType('Zend_Controller_Request_Http', $token);
  143. }
  144. public function testRouteWithIncorrectRequest()
  145. {
  146. $request = new Zend_Controller_Router_RewriteTest_Request_Incorrect();
  147. try {
  148. $token = $this->_router->route($request);
  149. $this->fail('Should throw an Exception');
  150. } catch (Exception $e) {
  151. $this->assertType('Zend_Controller_Router_Exception', $e);
  152. }
  153. }
  154. public function testDefaultRoute()
  155. {
  156. $request = new Zend_Controller_Router_RewriteTest_Request();
  157. $token = $this->_router->route($request);
  158. $routes = $this->_router->getRoutes();
  159. $this->assertType('Zend_Controller_Router_Route_Module', $routes['default']);
  160. }
  161. public function testDefaultRouteWithEmptyAction()
  162. {
  163. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/ctrl');
  164. $token = $this->_router->route($request);
  165. $this->assertEquals('ctrl', $token->getControllerName());
  166. $this->assertEquals('defact', $token->getActionName());
  167. }
  168. public function testEmptyRoute()
  169. {
  170. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/');
  171. $this->_router->removeDefaultRoutes();
  172. $this->_router->addRoute('empty', new Zend_Controller_Router_Route('', array('controller' => 'ctrl', 'action' => 'act')));
  173. $token = $this->_router->route($request);
  174. $this->assertEquals('ctrl', $token->getControllerName());
  175. $this->assertEquals('act', $token->getActionName());
  176. }
  177. public function testEmptyPath()
  178. {
  179. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/');
  180. $this->_router->removeDefaultRoutes();
  181. $this->_router->addRoute('catch-all', new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'ctrl', 'action' => 'act')));
  182. $token = $this->_router->route($request);
  183. $this->assertEquals('ctrl', $token->getControllerName());
  184. $this->assertEquals('act', $token->getActionName());
  185. }
  186. public function testEmptyPathWithWildcardRoute()
  187. {
  188. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/');
  189. $this->_router->removeDefaultRoutes();
  190. $this->_router->addRoute('catch-all', new Zend_Controller_Router_Route('*', array('controller' => 'ctrl', 'action' => 'act')));
  191. $token = $this->_router->route($request);
  192. $this->assertEquals('ctrl', $token->getControllerName());
  193. $this->assertEquals('act', $token->getActionName());
  194. }
  195. public function testRouteNotMatched()
  196. {
  197. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/archive/action/bogus');
  198. $this->_router->addRoute('default', new Zend_Controller_Router_Route(':controller/:action'));
  199. try {
  200. $token = $this->_router->route($request);
  201. $this->fail('An expected Zend_Controller_Router_Exception was not raised');
  202. } catch (Zend_Controller_Router_Exception $expected) {
  203. $this->assertEquals('No route matched the request', $expected->getMessage());
  204. }
  205. $this->assertNull($request->getControllerName());
  206. $this->assertNull($request->getActionName());
  207. }
  208. public function testDefaultRouteMatched()
  209. {
  210. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/ctrl/act');
  211. $token = $this->_router->route($request);
  212. $this->assertEquals('ctrl', $token->getControllerName());
  213. $this->assertEquals('act', $token->getActionName());
  214. }
  215. public function testDefaultRouteMatchedWithControllerOnly()
  216. {
  217. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/ctrl');
  218. $token = $this->_router->route($request);
  219. $this->assertEquals('ctrl', $token->getControllerName());
  220. $this->assertEquals('defact', $token->getActionName());
  221. }
  222. public function testFirstRouteMatched()
  223. {
  224. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/archive/2006');
  225. $this->_router->addRoute('archive', new Zend_Controller_Router_Route('archive/:year', array('year' => '2006', 'controller' => 'archive', 'action' => 'show'), array('year' => '\d+')));
  226. $this->_router->addRoute('register', new Zend_Controller_Router_Route('register/:action', array('controller' => 'profile', 'action' => 'register')));
  227. $token = $this->_router->route($request);
  228. $this->assertEquals('archive', $token->getControllerName());
  229. $this->assertEquals('show', $token->getActionName());
  230. }
  231. public function testGetCurrentRoute()
  232. {
  233. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/ctrl/act');
  234. try {
  235. $route = $this->_router->getCurrentRoute();
  236. $this->fail();
  237. } catch (Exception $e) {
  238. $this->assertType('Zend_Controller_Router_Exception', $e);
  239. }
  240. try {
  241. $route = $this->_router->getCurrentRouteName();
  242. $this->fail();
  243. } catch (Exception $e) {
  244. $this->assertType('Zend_Controller_Router_Exception', $e);
  245. }
  246. $token = $this->_router->route($request);
  247. try {
  248. $route = $this->_router->getCurrentRoute();
  249. $name = $this->_router->getCurrentRouteName();
  250. } catch (Exception $e) {
  251. $this->fail('Current route is not set');
  252. }
  253. $this->assertEquals('default', $name);
  254. $this->assertType('Zend_Controller_Router_Route_Module', $route);
  255. }
  256. public function testAddConfig()
  257. {
  258. require_once 'Zend/Config/Ini.php';
  259. $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'routes.ini';
  260. $config = new Zend_Config_Ini($file, 'testing');
  261. $this->_router->addConfig($config, 'routes');
  262. $this->assertType('Zend_Controller_Router_Route_Static', $this->_router->getRoute('news'));
  263. $this->assertType('Zend_Controller_Router_Route', $this->_router->getRoute('archive'));
  264. try {
  265. $this->_router->addConfig($config, 'database');
  266. } catch (Exception $e) {
  267. $this->assertType('Zend_Controller_Router_Exception', $e);
  268. return true;
  269. }
  270. }
  271. public function testAddConfigWithoutSection()
  272. {
  273. require_once 'Zend/Config/Ini.php';
  274. $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'routes.ini';
  275. $config = new Zend_Config_Ini($file, 'testing');
  276. $this->_router->addConfig($config->routes);
  277. $this->assertType('Zend_Controller_Router_Route_Static', $this->_router->getRoute('news'));
  278. $this->assertType('Zend_Controller_Router_Route', $this->_router->getRoute('archive'));
  279. }
  280. public function testAddConfigWithRootNode()
  281. {
  282. require_once 'Zend/Config/Ini.php';
  283. $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'routes-root.ini';
  284. $config = new Zend_Config_Ini($file, 'routes');
  285. $this->_router->addConfig($config);
  286. $this->assertType('Zend_Controller_Router_Route_Static', $this->_router->getRoute('news'));
  287. $this->assertType('Zend_Controller_Router_Route', $this->_router->getRoute('archive'));
  288. }
  289. public function testRemoveDefaultRoutes()
  290. {
  291. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/ctrl/act');
  292. $this->_router->removeDefaultRoutes();
  293. try {
  294. $token = $this->_router->route($request);
  295. $this->fail('An expected Zend_Controller_Router_Exception was not raised');
  296. } catch (Zend_Controller_Router_Exception $expected) {
  297. $this->assertEquals('No route matched the request', $expected->getMessage());
  298. }
  299. $routes = $this->_router->getRoutes();
  300. $this->assertEquals(0, count($routes));
  301. }
  302. public function testDefaultRouteMatchedWithModules()
  303. {
  304. Zend_Controller_Front::getInstance()->setControllerDirectory(array(
  305. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files',
  306. 'mod' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin',
  307. ));
  308. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/mod/ctrl/act');
  309. $token = $this->_router->route($request);
  310. $this->assertEquals('mod', $token->getModuleName());
  311. $this->assertEquals('ctrl', $token->getControllerName());
  312. $this->assertEquals('act', $token->getActionName());
  313. }
  314. public function testRouteCompatDefaults()
  315. {
  316. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/');
  317. $token = $this->_router->route($request);
  318. $this->assertEquals('default', $token->getModuleName());
  319. $this->assertEquals('defctrl', $token->getControllerName());
  320. $this->assertEquals('defact', $token->getActionName());
  321. }
  322. public function testDefaultRouteWithEmptyControllerAndAction()
  323. {
  324. Zend_Controller_Front::getInstance()->setControllerDirectory(array(
  325. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files',
  326. 'mod' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin',
  327. ));
  328. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/mod');
  329. $token = $this->_router->route($request);
  330. $this->assertEquals('mod', $token->getModuleName());
  331. $this->assertEquals('defctrl', $token->getControllerName());
  332. $this->assertEquals('defact', $token->getActionName());
  333. }
  334. public function testNumericallyIndexedReturnParams()
  335. {
  336. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/archive/2006');
  337. $this->_router->addRoute('test', new Zend_Controller_Router_Route_Mockup());
  338. $token = $this->_router->route($request);
  339. $this->assertEquals('index', $token->getControllerName());
  340. $this->assertEquals('index', $token->getActionName());
  341. $this->assertEquals('first_parameter_value', $token->getParam(0));
  342. }
  343. public function testUrlValuesHandling1() // See ZF-3212 and ZF-3219
  344. {
  345. $this->_router->addRoute('foo', new Zend_Controller_Router_Route(':lang/foo', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  346. $this->_router->addRoute('bar', new Zend_Controller_Router_Route(':lang/bar', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  347. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/nl/bar');
  348. $token = $this->_router->route($request);
  349. $this->assertEquals('nl/foo', $this->_router->getRoute('foo')->assemble());
  350. $this->assertEquals('nl/bar', $this->_router->getRoute('bar')->assemble());
  351. }
  352. public function testUrlValuesHandling2() // See ZF-3212 and ZF-3219
  353. {
  354. $this->_router->addRoute('foo', new Zend_Controller_Router_Route(':lang/foo', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  355. $this->_router->addRoute('bar', new Zend_Controller_Router_Route(':lang/bar', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  356. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/en/foo');
  357. $token = $this->_router->route($request);
  358. $this->assertEquals('en/foo', $this->_router->getRoute('foo')->assemble());
  359. $this->assertEquals('nl/bar', $this->_router->getRoute('bar')->assemble());
  360. }
  361. public function testUrlValuesHandling3() // See ZF-3212 and ZF-3219
  362. {
  363. $this->_router->addRoute('foo', new Zend_Controller_Router_Route(':lang/foo', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  364. $this->_router->addRoute('bar', new Zend_Controller_Router_Route(':lang/bar', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  365. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/en/bar');
  366. $token = $this->_router->route($request);
  367. $this->assertEquals('nl/foo', $this->_router->getRoute('foo')->assemble());
  368. $this->assertEquals('en/bar', $this->_router->getRoute('bar')->assemble());
  369. }
  370. public function testRouteRequestInterface()
  371. {
  372. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/en/foo');
  373. $front = $this->_router->getFrontController()->setRequest($request);
  374. $this->_router->addRoute('req', new Zend_Controller_Router_Route_Interface_Mockup());
  375. $routeRequest = $this->_router->getRoute('req')->getRequest();
  376. $this->assertType('Zend_Controller_Request_Abstract', $request);
  377. $this->assertType('Zend_Controller_Request_Abstract', $routeRequest);
  378. $this->assertSame($request, $routeRequest);
  379. }
  380. public function testRoutingVersion2Routes()
  381. {
  382. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/en/bar');
  383. $request->setParam('path', 'v2test');
  384. $route = new Zend_Controller_RouterTest_RouteV2_Stub('not-important');
  385. $this->_router->addRoute('foo', $route);
  386. $token = $this->_router->route($request);
  387. $this->assertEquals('v2test', $token->getParam('path'));
  388. }
  389. public function testRoutingChainedRoutes()
  390. {
  391. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/foo/bar');
  392. $foo = new Zend_Controller_Router_Route('foo', array('foo' => true));
  393. $bar = new Zend_Controller_Router_Route('bar', array('bar' => true, 'controller' => 'foo', 'action' => 'bar'));
  394. $chain = new Zend_Controller_Router_Route_Chain();
  395. $chain->chain($foo)->chain($bar);
  396. $this->_router->addRoute('foo-bar', $chain);
  397. $token = $this->_router->route($request);
  398. $this->assertEquals('foo', $token->getControllerName());
  399. $this->assertEquals('bar', $token->getActionName());
  400. $this->assertEquals(true, $token->getParam('foo'));
  401. $this->assertEquals(true, $token->getParam('bar'));
  402. }
  403. public function testRouteWithHostnameChain()
  404. {
  405. $request = new Zend_Controller_Router_RewriteTest_Request('http://www.zend.com/bar');
  406. $foo = new Zend_Controller_Router_Route_Hostname('nope.zend.com', array('module' => 'nope-bla', 'bogus' => 'bogus'));
  407. $bar = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('module' => 'www-bla'));
  408. $bla = new Zend_Controller_Router_Route_Static('bar', array('controller' => 'foo', 'action' => 'bar'));
  409. $chainMatch = new Zend_Controller_Router_Route_Chain();
  410. $chainMatch->chain($bar)->chain($bla);
  411. $chainNoMatch = new Zend_Controller_Router_Route_Chain();
  412. $chainNoMatch->chain($foo)->chain($bla);
  413. $this->_router->addRoute('match', $chainMatch);
  414. $this->_router->addRoute('no-match', $chainNoMatch);
  415. $token = $this->_router->route($request);
  416. $this->assertEquals('www-bla', $token->getModuleName());
  417. $this->assertEquals('foo', $token->getControllerName());
  418. $this->assertEquals('bar', $token->getActionName());
  419. $this->assertNull($token->getParam('bogus'));
  420. }
  421. public function testAssemblingWithHostnameHttp()
  422. {
  423. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  424. $this->_router->addRoute('hostname-route', $route);
  425. $this->assertEquals('http://www.zend.com', $this->_router->assemble(array(), 'hostname-route'));
  426. }
  427. public function testAssemblingWithHostnameHttps()
  428. {
  429. $backupServer = $_SERVER;
  430. $_SERVER['HTTPS'] = 'on';
  431. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  432. $this->_router->addRoute('hostname-route', $route);
  433. $this->assertEquals('https://www.zend.com', $this->_router->assemble(array(), 'hostname-route'));
  434. $_SERVER = $backupServer;
  435. }
  436. public function testAssemblingWithHostnameThroughChainHttp()
  437. {
  438. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  439. $bar = new Zend_Controller_Router_Route_Static('bar');
  440. $chain = new Zend_Controller_Router_Route_Chain();
  441. $chain->chain($foo)->chain($bar);
  442. $this->_router->addRoute('foo-bar', $chain);
  443. $this->assertEquals('http://www.zend.com/bar', $this->_router->assemble(array(), 'foo-bar'));
  444. }
  445. public function testAssemblingWithHostnameWithChainHttp()
  446. {
  447. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  448. $bar = new Zend_Controller_Router_Route_Static('bar');
  449. $chain = $foo->chain($bar);
  450. $this->_router->addRoute('foo-bar', $chain);
  451. $this->assertEquals('http://www.zend.com/bar', $this->_router->assemble(array(), 'foo-bar'));
  452. }
  453. public function testAssemblingWithNonFirstHostname()
  454. {
  455. $this->markTestSkipped('Router features not ready');
  456. $foo = new Zend_Controller_Router_Route_Static('bar');
  457. $bar = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  458. $foo->chain($bar);
  459. $this->_router->addRoute('foo-bar', $foo);
  460. $this->assertEquals('bar/www.zend.com', $this->_router->assemble(array(), 'foo-bar'));
  461. }
  462. /**
  463. * @see ZF-3922
  464. */
  465. public function testRouteShouldMatchEvenWithTrailingSlash()
  466. {
  467. $route = new Zend_Controller_Router_Route(
  468. 'blog/articles/:id',
  469. array(
  470. 'controller' => 'blog',
  471. 'action' => 'articles',
  472. 'id' => 0,
  473. ),
  474. array(
  475. 'id' => '[0-9]+',
  476. )
  477. );
  478. $this->_router->addRoute('article-id', $route);
  479. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/blog/articles/2006/');
  480. $token = $this->_router->route($request);
  481. $this->assertSame('article-id', $this->_router->getCurrentRouteName());
  482. $this->assertEquals('2006', $token->getParam('id', false));
  483. }
  484. public function testGlobalParam()
  485. {
  486. $route = new Zend_Controller_Router_Route(
  487. ':lang/articles/:id',
  488. array(
  489. 'controller' => 'blog',
  490. 'action' => 'articles',
  491. 'id' => 0,
  492. )
  493. );
  494. $this->_router->addRoute('article-id', $route);
  495. $this->_router->setGlobalParam('lang', 'de');
  496. $url = $this->_router->assemble(array('id' => 1), 'article-id');
  497. $this->assertEquals('/de/articles/1', $url);
  498. }
  499. public function testGlobalParamOverride()
  500. {
  501. $route = new Zend_Controller_Router_Route(
  502. ':lang/articles/:id',
  503. array(
  504. 'controller' => 'blog',
  505. 'action' => 'articles',
  506. 'id' => 0,
  507. )
  508. );
  509. $this->_router->addRoute('article-id', $route);
  510. $this->_router->setGlobalParam('lang', 'de');
  511. $url = $this->_router->assemble(array('id' => 1, 'lang' => 'en'), 'article-id');
  512. $this->assertEquals('/en/articles/1', $url);
  513. }
  514. public function testChainNameSeparatorIsSetCorrectly() {
  515. $separators = array('_','unitTestSeparator','-');
  516. $results = array();
  517. foreach($separators as $separator) {
  518. $this->_router->setChainNameSeparator($separator);
  519. $results[] = $this->_router->getChainNameSeparator();
  520. }
  521. $this->assertEquals($separators, $results);
  522. }
  523. public function testChainNameSeparatorisUsedCorrectly() {
  524. $config = new Zend_Config(array('chains' => array(
  525. 'type'=>'Zend_Controller_Router_Route_Static',
  526. 'route'=>'foo',
  527. 'chains'=> array('bar'=>
  528. array('type'=>'Zend_Controller_Router_Route_Static',
  529. 'route'=>'bar',
  530. 'defaults'=>array(
  531. 'module'=>'module',
  532. 'controller'=>'controller',
  533. 'action'=>'action'))))));
  534. $this->_router->setChainNameSeparator('_separator_')
  535. ->addConfig($config);
  536. $url = $this->_router->assemble(array(),'chains_separator_bar');
  537. $this->assertEquals('/foo/bar',$url);
  538. }
  539. public function testRequestParamsUsedAsGlobalParam()
  540. {
  541. $route = new Zend_Controller_Router_Route(
  542. '/articles/:id',
  543. array(
  544. 'controller' => 'blog',
  545. 'action' => 'articles',
  546. )
  547. );
  548. $request = Zend_Controller_Front::getInstance()->getRequest();
  549. $request->setParam('id', 777);
  550. $this->_router->addRoute('article-id', $route);
  551. $this->_router->useRequestParametersAsGlobal(true);
  552. $this->_router->route($request);
  553. $url = $this->_router->assemble(array(), 'article-id');
  554. $this->assertEquals('/articles/777', $url);
  555. }
  556. /**
  557. * Test that it is possible to generate a URL with a numerical key
  558. *
  559. * @since 2010-06-11
  560. * @group ZF-8914
  561. * @covers Zend_Controller_Router_Rewrite::assemble
  562. */
  563. public function testCanGenerateNumericKeyUri()
  564. {
  565. $this->_router->addRoute(
  566. 'default',
  567. new Zend_Controller_Router_Route(
  568. ':controller/:action/*',
  569. array('controller' => 'index', 'action' => 'index')
  570. )
  571. );
  572. $params = array(
  573. 'controller' => 'index',
  574. 'action' => 'index',
  575. '2' => 'foo',
  576. 'page' => 'bar',
  577. );
  578. $this->assertEquals(
  579. '/index/index/2/foo/page/bar',
  580. $this->_router->assemble($params)
  581. );
  582. }
  583. }
  584. /**
  585. * Zend_Controller_Router_RewriteTest_Request - request object for router testing
  586. *
  587. * @uses Zend_Controller_Request_Interface
  588. */
  589. class Zend_Controller_Router_RewriteTest_Request extends Zend_Controller_Request_Http
  590. {
  591. protected $_host;
  592. protected $_port;
  593. public function __construct($uri = null)
  594. {
  595. if (null === $uri) {
  596. $uri = 'http://localhost/foo/bar/baz/2';
  597. }
  598. $uri = Zend_Uri_Http::fromString($uri);
  599. $this->_host = $uri->getHost();
  600. $this->_port = $uri->getPort();
  601. parent::__construct($uri);
  602. }
  603. public function getHttpHost() {
  604. $return = $this->_host;
  605. if ($this->_port) $return .= ':' . $this->_port;
  606. return $return;
  607. }
  608. }
  609. /**
  610. * Zend_Controller_RouterTest_Dispatcher
  611. */
  612. class Zend_Controller_Router_RewriteTest_Dispatcher extends Zend_Controller_Dispatcher_Standard
  613. {
  614. public function getDefaultControllerName()
  615. {
  616. return 'defctrl';
  617. }
  618. public function getDefaultAction()
  619. {
  620. return 'defact';
  621. }
  622. }
  623. /**
  624. * Zend_Controller_RouterTest_Request_Incorrect - request object for router testing
  625. *
  626. * @uses Zend_Controller_Request_Abstract
  627. */
  628. class Zend_Controller_Router_RewriteTest_Request_Incorrect extends Zend_Controller_Request_Abstract
  629. {
  630. }
  631. /**
  632. * Zend_Controller_RouterTest_RouteV2_Stub - request object for router testing
  633. *
  634. * @uses Zend_Controller_Request_Abstract
  635. */
  636. class Zend_Controller_RouterTest_RouteV2_Stub extends Zend_Controller_Router_Route_Abstract
  637. {
  638. public function match($request) {
  639. return array('path', $request->getParam('path'));
  640. }
  641. public static function getInstance(Zend_Config $config) {}
  642. public function assemble($data = array(), $reset = false, $encode = false) {}
  643. }
  644. class Zend_Controller_Router_Route_Mockup implements Zend_Controller_Router_Route_Interface
  645. {
  646. public function match($path, $partial = null)
  647. {
  648. return array(
  649. "controller" => "index",
  650. "action" => "index",
  651. 0 => "first_parameter_value"
  652. );
  653. }
  654. public static function getInstance(Zend_Config $config) {}
  655. public function assemble($data = array(), $reset = false, $encode = false) {}
  656. }
  657. class Zend_Controller_Router_Route_Interface_Mockup implements Zend_Controller_Router_Route_Interface
  658. {
  659. protected $_request;
  660. public function match($path, $partial = null) {}
  661. public static function getInstance(Zend_Config $config) {}
  662. public function assemble($data = array(), $reset = false, $encode = false) {}
  663. public function setRequest($request) {
  664. $this->_request = $request;
  665. }
  666. public function getRequest() {
  667. return $this->_request;
  668. }
  669. }
  670. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Router_RewriteTest::main") {
  671. Zend_Controller_Router_RewriteTest::main();
  672. }