RewriteTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. $token = $this->_router->route($request);
  204. $this->assertNull($token->getControllerName());
  205. $this->assertNull($token->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->assertType('Zend_Controller_Router_Exception', $e);
  238. }
  239. try {
  240. $route = $this->_router->getCurrentRouteName();
  241. $this->fail();
  242. } catch (Exception $e) {
  243. $this->assertType('Zend_Controller_Router_Exception', $e);
  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->assertType('Zend_Controller_Router_Route_Module', $route);
  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->assertType('Zend_Controller_Router_Route_Static', $this->_router->getRoute('news'));
  262. $this->assertType('Zend_Controller_Router_Route', $this->_router->getRoute('archive'));
  263. try {
  264. $this->_router->addConfig($config, 'database');
  265. } catch (Exception $e) {
  266. $this->assertType('Zend_Controller_Router_Exception', $e);
  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->assertType('Zend_Controller_Router_Route_Static', $this->_router->getRoute('news'));
  277. $this->assertType('Zend_Controller_Router_Route', $this->_router->getRoute('archive'));
  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->assertType('Zend_Controller_Router_Route_Static', $this->_router->getRoute('news'));
  286. $this->assertType('Zend_Controller_Router_Route', $this->_router->getRoute('archive'));
  287. }
  288. public function testRemoveDefaultRoutes()
  289. {
  290. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/ctrl/act');
  291. $this->_router->removeDefaultRoutes();
  292. $token = $this->_router->route($request);
  293. $routes = $this->_router->getRoutes();
  294. $this->assertEquals(0, count($routes));
  295. }
  296. public function testDefaultRouteMatchedWithModules()
  297. {
  298. Zend_Controller_Front::getInstance()->setControllerDirectory(array(
  299. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files',
  300. 'mod' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin',
  301. ));
  302. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/mod/ctrl/act');
  303. $token = $this->_router->route($request);
  304. $this->assertEquals('mod', $token->getModuleName());
  305. $this->assertEquals('ctrl', $token->getControllerName());
  306. $this->assertEquals('act', $token->getActionName());
  307. }
  308. public function testRouteCompatDefaults()
  309. {
  310. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/');
  311. $token = $this->_router->route($request);
  312. $this->assertEquals('default', $token->getModuleName());
  313. $this->assertEquals('defctrl', $token->getControllerName());
  314. $this->assertEquals('defact', $token->getActionName());
  315. }
  316. public function testDefaultRouteWithEmptyControllerAndAction()
  317. {
  318. Zend_Controller_Front::getInstance()->setControllerDirectory(array(
  319. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files',
  320. 'mod' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin',
  321. ));
  322. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/mod');
  323. $token = $this->_router->route($request);
  324. $this->assertEquals('mod', $token->getModuleName());
  325. $this->assertEquals('defctrl', $token->getControllerName());
  326. $this->assertEquals('defact', $token->getActionName());
  327. }
  328. public function testNumericallyIndexedReturnParams()
  329. {
  330. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/archive/2006');
  331. $this->_router->addRoute('test', new Zend_Controller_Router_Route_Mockup());
  332. $token = $this->_router->route($request);
  333. $this->assertEquals('index', $token->getControllerName());
  334. $this->assertEquals('index', $token->getActionName());
  335. $this->assertEquals('first_parameter_value', $token->getParam(0));
  336. }
  337. public function testUrlValuesHandling1() // See ZF-3212 and ZF-3219
  338. {
  339. $this->_router->addRoute('foo', new Zend_Controller_Router_Route(':lang/foo', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  340. $this->_router->addRoute('bar', new Zend_Controller_Router_Route(':lang/bar', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  341. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/nl/bar');
  342. $token = $this->_router->route($request);
  343. $this->assertEquals('nl/foo', $this->_router->getRoute('foo')->assemble());
  344. $this->assertEquals('nl/bar', $this->_router->getRoute('bar')->assemble());
  345. }
  346. public function testUrlValuesHandling2() // See ZF-3212 and ZF-3219
  347. {
  348. $this->_router->addRoute('foo', new Zend_Controller_Router_Route(':lang/foo', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  349. $this->_router->addRoute('bar', new Zend_Controller_Router_Route(':lang/bar', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  350. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/en/foo');
  351. $token = $this->_router->route($request);
  352. $this->assertEquals('en/foo', $this->_router->getRoute('foo')->assemble());
  353. $this->assertEquals('nl/bar', $this->_router->getRoute('bar')->assemble());
  354. }
  355. public function testUrlValuesHandling3() // See ZF-3212 and ZF-3219
  356. {
  357. $this->_router->addRoute('foo', new Zend_Controller_Router_Route(':lang/foo', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  358. $this->_router->addRoute('bar', new Zend_Controller_Router_Route(':lang/bar', array('lang' => 'nl', 'controller' => 'index', 'action' => 'index')));
  359. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/en/bar');
  360. $token = $this->_router->route($request);
  361. $this->assertEquals('nl/foo', $this->_router->getRoute('foo')->assemble());
  362. $this->assertEquals('en/bar', $this->_router->getRoute('bar')->assemble());
  363. }
  364. public function testRouteRequestInterface()
  365. {
  366. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/en/foo');
  367. $front = $this->_router->getFrontController()->setRequest($request);
  368. $this->_router->addRoute('req', new Zend_Controller_Router_Route_Interface_Mockup());
  369. $routeRequest = $this->_router->getRoute('req')->getRequest();
  370. $this->assertType('Zend_Controller_Request_Abstract', $request);
  371. $this->assertType('Zend_Controller_Request_Abstract', $routeRequest);
  372. $this->assertSame($request, $routeRequest);
  373. }
  374. public function testRoutingVersion2Routes()
  375. {
  376. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/en/bar');
  377. $request->setParam('path', 'v2test');
  378. $route = new Zend_Controller_RouterTest_RouteV2_Stub('not-important');
  379. $this->_router->addRoute('foo', $route);
  380. $token = $this->_router->route($request);
  381. $this->assertEquals('v2test', $token->getParam('path'));
  382. }
  383. public function testRoutingChainedRoutes()
  384. {
  385. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/foo/bar');
  386. $foo = new Zend_Controller_Router_Route('foo', array('foo' => true));
  387. $bar = new Zend_Controller_Router_Route('bar', array('bar' => true, 'controller' => 'foo', 'action' => 'bar'));
  388. $chain = new Zend_Controller_Router_Route_Chain();
  389. $chain->chain($foo)->chain($bar);
  390. $this->_router->addRoute('foo-bar', $chain);
  391. $token = $this->_router->route($request);
  392. $this->assertEquals('foo', $token->getControllerName());
  393. $this->assertEquals('bar', $token->getActionName());
  394. $this->assertEquals(true, $token->getParam('foo'));
  395. $this->assertEquals(true, $token->getParam('bar'));
  396. }
  397. public function testRouteWithHostnameChain()
  398. {
  399. $request = new Zend_Controller_Router_RewriteTest_Request('http://www.zend.com/bar');
  400. $foo = new Zend_Controller_Router_Route_Hostname('nope.zend.com', array('module' => 'nope-bla', 'bogus' => 'bogus'));
  401. $bar = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('module' => 'www-bla'));
  402. $bla = new Zend_Controller_Router_Route_Static('bar', array('controller' => 'foo', 'action' => 'bar'));
  403. $chainMatch = new Zend_Controller_Router_Route_Chain();
  404. $chainMatch->chain($bar)->chain($bla);
  405. $chainNoMatch = new Zend_Controller_Router_Route_Chain();
  406. $chainNoMatch->chain($foo)->chain($bla);
  407. $this->_router->addRoute('match', $chainMatch);
  408. $this->_router->addRoute('no-match', $chainNoMatch);
  409. $token = $this->_router->route($request);
  410. $this->assertEquals('www-bla', $token->getModuleName());
  411. $this->assertEquals('foo', $token->getControllerName());
  412. $this->assertEquals('bar', $token->getActionName());
  413. $this->assertNull($token->getParam('bogus'));
  414. }
  415. public function testAssemblingWithHostnameHttp()
  416. {
  417. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  418. $this->_router->addRoute('hostname-route', $route);
  419. $this->assertEquals('http://www.zend.com', $this->_router->assemble(array(), 'hostname-route'));
  420. }
  421. public function testAssemblingWithHostnameHttps()
  422. {
  423. $backupServer = $_SERVER;
  424. $_SERVER['HTTPS'] = 'on';
  425. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  426. $this->_router->addRoute('hostname-route', $route);
  427. $this->assertEquals('https://www.zend.com', $this->_router->assemble(array(), 'hostname-route'));
  428. $_SERVER = $backupServer;
  429. }
  430. public function testAssemblingWithHostnameThroughChainHttp()
  431. {
  432. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  433. $bar = new Zend_Controller_Router_Route_Static('bar');
  434. $chain = new Zend_Controller_Router_Route_Chain();
  435. $chain->chain($foo)->chain($bar);
  436. $this->_router->addRoute('foo-bar', $chain);
  437. $this->assertEquals('http://www.zend.com/bar', $this->_router->assemble(array(), 'foo-bar'));
  438. }
  439. public function testAssemblingWithHostnameWithChainHttp()
  440. {
  441. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  442. $bar = new Zend_Controller_Router_Route_Static('bar');
  443. $chain = $foo->chain($bar);
  444. $this->_router->addRoute('foo-bar', $chain);
  445. $this->assertEquals('http://www.zend.com/bar', $this->_router->assemble(array(), 'foo-bar'));
  446. }
  447. public function testAssemblingWithNonFirstHostname()
  448. {
  449. $this->markTestSkipped('Router features not ready');
  450. $foo = new Zend_Controller_Router_Route_Static('bar');
  451. $bar = new Zend_Controller_Router_Route_Hostname('www.zend.com');
  452. $foo->chain($bar);
  453. $this->_router->addRoute('foo-bar', $foo);
  454. $this->assertEquals('bar/www.zend.com', $this->_router->assemble(array(), 'foo-bar'));
  455. }
  456. /**
  457. * @see ZF-3922
  458. */
  459. public function testRouteShouldMatchEvenWithTrailingSlash()
  460. {
  461. $route = new Zend_Controller_Router_Route(
  462. 'blog/articles/:id',
  463. array(
  464. 'controller' => 'blog',
  465. 'action' => 'articles',
  466. 'id' => 0,
  467. ),
  468. array(
  469. 'id' => '[0-9]+',
  470. )
  471. );
  472. $this->_router->addRoute('article-id', $route);
  473. $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/blog/articles/2006/');
  474. $token = $this->_router->route($request);
  475. $this->assertSame('article-id', $this->_router->getCurrentRouteName());
  476. $this->assertEquals('2006', $token->getParam('id', false));
  477. }
  478. public function testGlobalParam()
  479. {
  480. $route = new Zend_Controller_Router_Route(
  481. ':lang/articles/:id',
  482. array(
  483. 'controller' => 'blog',
  484. 'action' => 'articles',
  485. 'id' => 0,
  486. )
  487. );
  488. $this->_router->addRoute('article-id', $route);
  489. $this->_router->setGlobalParam('lang', 'de');
  490. $url = $this->_router->assemble(array('id' => 1), 'article-id');
  491. $this->assertEquals('/de/articles/1', $url);
  492. }
  493. public function testGlobalParamOverride()
  494. {
  495. $route = new Zend_Controller_Router_Route(
  496. ':lang/articles/:id',
  497. array(
  498. 'controller' => 'blog',
  499. 'action' => 'articles',
  500. 'id' => 0,
  501. )
  502. );
  503. $this->_router->addRoute('article-id', $route);
  504. $this->_router->setGlobalParam('lang', 'de');
  505. $url = $this->_router->assemble(array('id' => 1, 'lang' => 'en'), 'article-id');
  506. $this->assertEquals('/en/articles/1', $url);
  507. }
  508. public function testChainNameSeparatorIsSetCorrectly() {
  509. $separators = array('_','unitTestSeparator','-');
  510. $results = array();
  511. foreach($separators as $separator) {
  512. $this->_router->setChainNameSeparator($separator);
  513. $results[] = $this->_router->getChainNameSeparator();
  514. }
  515. $this->assertEquals($separators, $results);
  516. }
  517. public function testChainNameSeparatorisUsedCorrectly() {
  518. $config = new Zend_Config(array('chains' => array(
  519. 'type'=>'Zend_Controller_Router_Route_Static',
  520. 'route'=>'foo',
  521. 'chains'=> array('bar'=>
  522. array('type'=>'Zend_Controller_Router_Route_Static',
  523. 'route'=>'bar',
  524. 'defaults'=>array(
  525. 'module'=>'module',
  526. 'controller'=>'controller',
  527. 'action'=>'action'))))));
  528. $this->_router->setChainNameSeparator('_separator_')
  529. ->addConfig($config);
  530. $url = $this->_router->assemble(array(),'chains_separator_bar');
  531. $this->assertEquals('/foo/bar',$url);
  532. }
  533. public function testRequestParamsUsedAsGlobalParam()
  534. {
  535. $route = new Zend_Controller_Router_Route(
  536. '/articles/:id',
  537. array(
  538. 'controller' => 'blog',
  539. 'action' => 'articles',
  540. )
  541. );
  542. $request = Zend_Controller_Front::getInstance()->getRequest();
  543. $request->setParam('id', 777);
  544. $this->_router->addRoute('article-id', $route);
  545. $this->_router->useRequestParametersAsGlobal(true);
  546. $this->_router->route($request);
  547. $url = $this->_router->assemble(array(), 'article-id');
  548. $this->assertEquals('/articles/777', $url);
  549. }
  550. }
  551. /**
  552. * Zend_Controller_Router_RewriteTest_Request - request object for router testing
  553. *
  554. * @uses Zend_Controller_Request_Interface
  555. */
  556. class Zend_Controller_Router_RewriteTest_Request extends Zend_Controller_Request_Http
  557. {
  558. protected $_host;
  559. protected $_port;
  560. public function __construct($uri = null)
  561. {
  562. if (null === $uri) {
  563. $uri = 'http://localhost/foo/bar/baz/2';
  564. }
  565. $uri = Zend_Uri_Http::fromString($uri);
  566. $this->_host = $uri->getHost();
  567. $this->_port = $uri->getPort();
  568. parent::__construct($uri);
  569. }
  570. public function getHttpHost() {
  571. $return = $this->_host;
  572. if ($this->_port) $return .= ':' . $this->_port;
  573. return $return;
  574. }
  575. }
  576. /**
  577. * Zend_Controller_RouterTest_Dispatcher
  578. */
  579. class Zend_Controller_Router_RewriteTest_Dispatcher extends Zend_Controller_Dispatcher_Standard
  580. {
  581. public function getDefaultControllerName()
  582. {
  583. return 'defctrl';
  584. }
  585. public function getDefaultAction()
  586. {
  587. return 'defact';
  588. }
  589. }
  590. /**
  591. * Zend_Controller_RouterTest_Request_Incorrect - request object for router testing
  592. *
  593. * @uses Zend_Controller_Request_Abstract
  594. */
  595. class Zend_Controller_Router_RewriteTest_Request_Incorrect extends Zend_Controller_Request_Abstract
  596. {
  597. }
  598. /**
  599. * Zend_Controller_RouterTest_RouteV2_Stub - request object for router testing
  600. *
  601. * @uses Zend_Controller_Request_Abstract
  602. */
  603. class Zend_Controller_RouterTest_RouteV2_Stub extends Zend_Controller_Router_Route_Abstract
  604. {
  605. public function match($request) {
  606. return array('path', $request->getParam('path'));
  607. }
  608. public static function getInstance(Zend_Config $config) {}
  609. public function assemble($data = array(), $reset = false, $encode = false) {}
  610. }
  611. class Zend_Controller_Router_Route_Mockup implements Zend_Controller_Router_Route_Interface
  612. {
  613. public function match($path, $partial = null)
  614. {
  615. return array(
  616. "controller" => "index",
  617. "action" => "index",
  618. 0 => "first_parameter_value"
  619. );
  620. }
  621. public static function getInstance(Zend_Config $config) {}
  622. public function assemble($data = array(), $reset = false, $encode = false) {}
  623. }
  624. class Zend_Controller_Router_Route_Interface_Mockup implements Zend_Controller_Router_Route_Interface
  625. {
  626. protected $_request;
  627. public function match($path, $partial = null) {}
  628. public static function getInstance(Zend_Config $config) {}
  629. public function assemble($data = array(), $reset = false, $encode = false) {}
  630. public function setRequest($request) {
  631. $this->_request = $request;
  632. }
  633. public function getRequest() {
  634. return $this->_request;
  635. }
  636. }
  637. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Router_RewriteTest::main") {
  638. Zend_Controller_Router_RewriteTest::main();
  639. }