RewriteTest.php 31 KB

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