RewriteTest.php 28 KB

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