RewriteTest.php 31 KB

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