ChainTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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_Route_ChainTest::main');
  24. }
  25. /** Zend_Config */
  26. require_once 'Zend/Config.php';
  27. /** Zend_Controller_Router_Rewrite */
  28. require_once 'Zend/Controller/Router/Rewrite.php';
  29. /** Zend_Controller_Dispatcher_Standard */
  30. require_once 'Zend/Controller/Dispatcher/Standard.php';
  31. /** Zend_Controller_Router_Route_Chain */
  32. require_once 'Zend/Controller/Router/Route/Chain.php';
  33. /** Zend_Controller_Router_Route */
  34. require_once 'Zend/Controller/Router/Route.php';
  35. /** Zend_Controller_Router_Route_Module */
  36. require_once 'Zend/Controller/Router/Route/Module.php';
  37. /** Zend_Controller_Router_Route_Static */
  38. require_once 'Zend/Controller/Router/Route/Static.php';
  39. /** Zend_Controller_Router_Route_Regex */
  40. require_once 'Zend/Controller/Router/Route/Regex.php';
  41. /** Zend_Controller_Router_Route_Hostname */
  42. require_once 'Zend/Controller/Router/Route/Hostname.php';
  43. /** Zend_Controller_Request_Http */
  44. require_once 'Zend/Controller/Request/Http.php';
  45. /** Zend_Uri_Http */
  46. require_once 'Zend/Uri/Http.php';
  47. /** Zend_Config */
  48. require_once 'Zend/Config.php';
  49. /**
  50. * @category Zend
  51. * @package Zend_Controller
  52. * @subpackage UnitTests
  53. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  54. * @license http://framework.zend.com/license/new-bsd New BSD License
  55. * @group Zend_Controller
  56. * @group Zend_Controller_Router
  57. */
  58. class Zend_Controller_Router_Route_ChainTest extends PHPUnit_Framework_TestCase
  59. {
  60. /**
  61. * Runs the test methods of this class.
  62. *
  63. * @access public
  64. * @static
  65. */
  66. public static function main()
  67. {
  68. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Router_Route_ChainTest");
  69. $result = PHPUnit_TextUI_TestRunner::run($suite);
  70. }
  71. public function testChaining()
  72. {
  73. $request = new Zend_Controller_Router_ChainTest_Request('http://localhost/foo/bar');
  74. $foo = new Zend_Controller_Router_Route('foo');
  75. $bar = new Zend_Controller_Router_Route('bar');
  76. $chain = $foo->chain($bar);
  77. $this->assertTrue($chain instanceof Zend_Controller_Router_Route_Chain);
  78. }
  79. public function testChainingMatch()
  80. {
  81. $chain = new Zend_Controller_Router_Route_Chain();
  82. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 1));
  83. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2));
  84. $chain->chain($foo)->chain($bar);
  85. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bla');
  86. $res = $chain->match($request);
  87. $this->assertFalse($res);
  88. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  89. $res = $chain->match($request);
  90. $this->assertEquals(1, $res['foo']);
  91. $this->assertEquals(2, $res['bar']);
  92. }
  93. /**
  94. * @group ZF-8812
  95. */
  96. public function testChainingMatchToDefaultValues()
  97. {
  98. $foo = new Zend_Controller_Router_Route(
  99. ':foo',
  100. array('foo' => 'bar'),
  101. array('foo' => '[a-z]{3}')
  102. );
  103. $bar = new Zend_Controller_Router_Route_Module(array(
  104. 'module' => 0,
  105. 'controller' => 1,
  106. 'action' => 2
  107. ));
  108. $chain = $foo->chain($bar);
  109. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/');
  110. $res = $chain->match($request);
  111. $this->assertTrue(is_array($res), 'Route did not match to default values.');
  112. }
  113. public function testChainingShortcutMatch()
  114. {
  115. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 1));
  116. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2, 'controller' => 'foo', 'action' => 'bar'));
  117. $chain = $foo->chain($bar);
  118. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  119. $res = $chain->match($request);
  120. $this->assertEquals(1, $res['foo']);
  121. $this->assertEquals(2, $res['bar']);
  122. }
  123. public function testChainingMatchFailure()
  124. {
  125. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 1));
  126. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2, 'controller' => 'foo', 'action' => 'bar'));
  127. $chain = $foo->chain($bar);
  128. $request = new Zend_Controller_Router_ChainTest_Request('http://nope.zend.com/bar');
  129. $res = $chain->match($request);
  130. $this->assertFalse($res);
  131. }
  132. public function testChainingVariableOverriding()
  133. {
  134. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 1, 'controller' => 'foo', 'module' => 'foo'));
  135. $bar = new Zend_Controller_Router_Route('bar', array('bar' => 2, 'controller' => 'bar', 'action' => 'bar'));
  136. $chain = $foo->chain($bar);
  137. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  138. $res = $chain->match($request);
  139. $this->assertEquals('foo', $res['module']);
  140. $this->assertEquals('bar', $res['controller']);
  141. $this->assertEquals('bar', $res['action']);
  142. }
  143. public function testChainingTooLongPath()
  144. {
  145. $foo = new Zend_Controller_Router_Route_Static('foo', array('foo' => 1));
  146. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2));
  147. $chain = $foo->chain($bar);
  148. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/bar/baz');
  149. $res = $chain->match($request);
  150. $this->assertFalse($res);
  151. }
  152. public function testChainingRegex()
  153. {
  154. $foo = new Zend_Controller_Router_Route_Regex('f..', array('foo' => 1));
  155. $bar = new Zend_Controller_Router_Route_Regex('b..', array('bar' => 2));
  156. $chain = $foo->chain($bar);
  157. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/bar');
  158. $res = $chain->match($request);
  159. $this->assertEquals(1, $res['foo']);
  160. $this->assertEquals(2, $res['bar']);
  161. }
  162. public function testChainingModule()
  163. {
  164. $foo = new Zend_Controller_Router_Route_Static('foo', array('foo' => 'bar'));
  165. $bar = new Zend_Controller_Router_Route_Module();
  166. $chain = $foo->chain($bar);
  167. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/bar/baz/var/val');
  168. $res = $chain->match($request);
  169. $this->assertEquals('bar', $res['foo']);
  170. $this->assertEquals('bar', $res['controller']);
  171. $this->assertEquals('baz', $res['action']);
  172. $this->assertEquals('val', $res['var']);
  173. }
  174. public function testVariableOmittingWhenPartial()
  175. {
  176. $foo = new Zend_Controller_Router_Route(':foo', array('foo' => 'foo'));
  177. $bar = new Zend_Controller_Router_Route(':bar', array('bar' => 'bar'));
  178. $chain = $foo->chain($bar);
  179. $path = $chain->assemble(array());
  180. $this->assertEquals('foo/', $path);
  181. }
  182. public function testVariableUnsettingRoute()
  183. {
  184. $foo = new Zend_Controller_Router_Route(':foo');
  185. $bar = new Zend_Controller_Router_Route_Module(array('controller' => 'ctrl', 'action' => 'act'));
  186. $chain = $foo->chain($bar);
  187. $path = $chain->assemble(array('foo' => 'bar', 'baz' => 'bat'));
  188. $this->assertEquals('bar/ctrl/act/baz/bat', $path);
  189. }
  190. public function testVariableUnsettingRegex()
  191. {
  192. $foo = new Zend_Controller_Router_Route_Regex('([^/]+)', array(), array(1 => 'foo'), '%s');
  193. $bar = new Zend_Controller_Router_Route_Module(array('controller' => 'ctrl', 'action' => 'act'));
  194. $chain = $foo->chain($bar);
  195. $path = $chain->assemble(array('foo' => 'bar', 'baz' => 'bat'));
  196. $this->assertEquals('bar/ctrl/act/baz/bat', $path);
  197. }
  198. public function testChainingSeparatorOverriding()
  199. {
  200. $foo = new Zend_Controller_Router_Route_Static('foo', array('foo' => 1));
  201. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2));
  202. $baz = new Zend_Controller_Router_Route_Static('baz', array('baz' => 3));
  203. $chain = $foo->chain($bar, '.');
  204. $res = $chain->match(new Zend_Controller_Router_ChainTest_Request('http://localhost/foo.bar'));
  205. $this->assertTrue(is_array($res));
  206. $res = $chain->match(new Zend_Controller_Router_ChainTest_Request('http://localhost/foo/bar'));
  207. $this->assertEquals(false, $res);
  208. $chain->chain($baz, ':');
  209. $res = $chain->match(new Zend_Controller_Router_ChainTest_Request('http://localhost/foo.bar:baz'));
  210. $this->assertTrue(is_array($res));
  211. }
  212. public function testI18nChaining()
  213. {
  214. $lang = new Zend_Controller_Router_Route(':lang', array('lang' => 'en'));
  215. $profile = new Zend_Controller_Router_Route('user/:id', array('controller' => 'foo', 'action' => 'bar'));
  216. $chain = $lang->chain($profile);
  217. $res = $chain->match(new Zend_Controller_Router_ChainTest_Request('http://localhost/en/user/1'));
  218. $this->assertEquals('en', $res['lang']);
  219. $this->assertEquals('1', $res['id']);
  220. }
  221. public function testChainingAssembleWithRoutePlaceholder()
  222. {
  223. $chain = new Zend_Controller_Router_Route_Chain();
  224. $foo = new Zend_Controller_Router_Route_Hostname(':account.zend.com');
  225. $bar = new Zend_Controller_Router_Route('bar/*');
  226. $chain->chain($foo)->chain($bar);
  227. $request = new Zend_Controller_Router_ChainTest_Request('http://foobar.zend.com/bar');
  228. $res = $chain->match($request);
  229. $this->assertTrue(is_array($res));
  230. $this->assertRegexp('#[^a-z0-9]?foobar\.zend\.com/bar/foo/bar#i', $chain->assemble(array('account' => 'foobar', 'foo' => 'bar')));
  231. }
  232. public function testChainingAssembleWithStatic()
  233. {
  234. $chain = new Zend_Controller_Router_Route_Chain();
  235. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 'foo'));
  236. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 'bar'));
  237. $chain->chain($foo)->chain($bar);
  238. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  239. $res = $chain->match($request);
  240. $this->assertTrue(is_array($res));
  241. $this->assertRegexp('#[^a-z0-9]?www\.zend\.com/bar$#i', $chain->assemble());
  242. }
  243. public function testChainingAssembleWithRegex()
  244. {
  245. $chain = new Zend_Controller_Router_Route_Chain();
  246. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 'foo'));
  247. $bar = new Zend_Controller_Router_Route_Regex('bar', array('bar' => 'bar'), array(), 'bar');
  248. $chain->chain($foo)->chain($bar);
  249. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  250. $res = $chain->match($request);
  251. $this->assertTrue(is_array($res));
  252. $this->assertRegexp('#[^a-z0-9]?www\.zend\.com/bar$#i', $chain->assemble());
  253. }
  254. public function testChainingReuse()
  255. {
  256. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 'foo'));
  257. $profile = new Zend_Controller_Router_Route('user/:id', array('controller' => 'prof'));
  258. $article = new Zend_Controller_Router_Route('article/:id', array('controller' => 'art', 'action' => 'art'));
  259. $profileChain = $foo->chain($profile);
  260. $articleChain = $foo->chain($article);
  261. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/user/1');
  262. $res = $profileChain->match($request);
  263. $this->assertTrue(is_array($res));
  264. $this->assertEquals('prof', $res['controller']);
  265. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/article/1');
  266. $res = $articleChain->match($request);
  267. $this->assertTrue(is_array($res));
  268. $this->assertEquals('art', $res['controller']);
  269. $this->assertEquals('art', $res['action']);
  270. }
  271. public function testConfigChaining()
  272. {
  273. $routes = array(
  274. /** Abstract routes */
  275. 'www' => array(
  276. 'type' => 'Zend_Controller_Router_Route_Hostname',
  277. 'route' => 'www.example.com',
  278. 'abstract' => true
  279. ),
  280. 'user' => array(
  281. 'type' => 'Zend_Controller_Router_Route_Hostname',
  282. 'route' => 'user.example.com',
  283. 'abstract' => true
  284. ),
  285. 'index' => array(
  286. 'type' => 'Zend_Controller_Router_Route_Static',
  287. 'route' => '',
  288. 'abstract' => true,
  289. 'defaults' => array(
  290. 'module' => 'default',
  291. 'controller' => 'index',
  292. 'action' => 'index'
  293. )
  294. ),
  295. 'imprint' => array(
  296. 'type' => 'Zend_Controller_Router_Route_Static',
  297. 'route' => 'imprint',
  298. 'abstract' => true,
  299. 'defaults' => array(
  300. 'module' => 'default',
  301. 'controller' => 'index',
  302. 'action' => 'imprint'
  303. )
  304. ),
  305. 'profile' => array(
  306. 'type' => 'Zend_Controller_Router_Route_Static',
  307. 'route' => 'profile',
  308. 'abstract' => true,
  309. 'defaults' => array(
  310. 'module' => 'user',
  311. 'controller' => 'profile',
  312. 'action' => 'index'
  313. )
  314. ),
  315. 'profile-edit' => array(
  316. 'type' => 'Zend_Controller_Router_Route_Static',
  317. 'route' => 'profile/edit',
  318. 'abstract' => true,
  319. 'defaults' => array(
  320. 'module' => 'user',
  321. 'controller' => 'profile',
  322. 'action' => 'edit'
  323. )
  324. ),
  325. /** Chains */
  326. 'www-index' => array(
  327. 'type' => 'Zend_Controller_Router_Route_Chain',
  328. 'chain' => 'www, index' // or array('www-subdomain', 'index'); / maybe both
  329. ),
  330. 'www-imprint' => array(
  331. 'type' => 'Zend_Controller_Router_Route_Chain',
  332. 'chain' => 'www, imprint'
  333. ),
  334. 'user-index' => array(
  335. 'type' => 'Zend_Controller_Router_Route_Chain',
  336. 'chain' => 'user, index'
  337. ),
  338. 'user-profile' => array(
  339. 'type' => 'Zend_Controller_Router_Route_Chain',
  340. 'chain' => 'user, profile'
  341. ),
  342. 'user-profile-edit' => array(
  343. 'type' => 'Zend_Controller_Router_Route_Chain',
  344. 'chain' => 'user, profile-edit'
  345. )
  346. );
  347. $router = new Zend_Controller_Router_Rewrite();
  348. $front = Zend_Controller_Front::getInstance();
  349. $front->resetInstance();
  350. $front->setDispatcher(new Zend_Controller_Router_ChainTest_Dispatcher());
  351. $front->setRequest(new Zend_Controller_Router_ChainTest_Request());
  352. $router->setFrontController($front);
  353. $router->addConfig(new Zend_Config($routes));
  354. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com/profile');
  355. $token = $router->route($request);
  356. $this->assertEquals('user', $token->getModuleName());
  357. $this->assertEquals('profile', $token->getControllerName());
  358. $this->assertEquals('index', $token->getActionName());
  359. $request = new Zend_Controller_Router_ChainTest_Request('http://foo.example.com/imprint');
  360. $token = $router->route($request);
  361. $this->assertEquals('default', $token->getModuleName());
  362. $this->assertEquals('imprint', $token->getControllerName());
  363. $this->assertEquals('defact', $token->getActionName());
  364. }
  365. public function testConfigChainingAlternative()
  366. {
  367. $routes = array(
  368. 'www' => array(
  369. 'type' => 'Zend_Controller_Router_Route_Hostname',
  370. 'route' => 'www.example.com',
  371. 'chains' => array(
  372. 'index' => array(
  373. 'type' => 'Zend_Controller_Router_Route_Static',
  374. 'route' => '',
  375. 'defaults' => array(
  376. 'module' => 'default',
  377. 'controller' => 'index',
  378. 'action' => 'index'
  379. )
  380. ),
  381. 'imprint' => array(
  382. 'type' => 'Zend_Controller_Router_Route_Static',
  383. 'route' => 'imprint',
  384. 'defaults' => array(
  385. 'module' => 'default',
  386. 'controller' => 'index',
  387. 'action' => 'imprint'
  388. )
  389. ),
  390. )
  391. ),
  392. 'user' => array(
  393. 'type' => 'Zend_Controller_Router_Route_Hostname',
  394. 'route' => 'user.example.com',
  395. 'chains' => array(
  396. 'profile' => array(
  397. 'type' => 'Zend_Controller_Router_Route_Static',
  398. 'route' => 'profile',
  399. 'defaults' => array(
  400. 'module' => 'user',
  401. 'controller' => 'profile',
  402. 'action' => 'index'
  403. ),
  404. 'chains' => array(
  405. 'standard' => array(
  406. 'type' => 'Zend_Controller_Router_Route_Static',
  407. 'route' => 'standard2',
  408. 'defaults' => array(
  409. 'mode' => 'standard'
  410. )
  411. ),
  412. 'detail' => array(
  413. 'type' => 'Zend_Controller_Router_Route_Static',
  414. 'route' => 'detail',
  415. 'defaults' => array(
  416. 'mode' => 'detail'
  417. )
  418. )
  419. )
  420. ),
  421. 'profile-edit' => array(
  422. 'type' => 'Zend_Controller_Router_Route_Static',
  423. 'route' => 'profile/edit',
  424. 'defaults' => array(
  425. 'module' => 'user',
  426. 'controller' => 'profile',
  427. 'action' => 'edit'
  428. )
  429. ),
  430. )
  431. ),
  432. );
  433. $router = $this->_getRouter();
  434. $router->addConfig(new Zend_Config($routes));
  435. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com/profile/edit');
  436. $token = $router->route($request);
  437. $this->assertEquals('user', $token->getModuleName());
  438. $this->assertEquals('profile', $token->getControllerName());
  439. $this->assertEquals('edit', $token->getActionName());
  440. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com/profile/detail');
  441. $token = $router->route($request);
  442. $this->assertEquals('user', $token->getModuleName());
  443. $this->assertEquals('profile', $token->getControllerName());
  444. $this->assertEquals('index', $token->getActionName());
  445. $this->assertEquals('detail', $token->getParam('mode'));
  446. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com/profile');
  447. $token = $router->route($request);
  448. }
  449. public function testConfigChainingMixed()
  450. {
  451. $routes = array(
  452. 'index' => array(
  453. 'type' => 'Zend_Controller_Router_Route_Static',
  454. 'route' => '',
  455. 'defaults' => array(
  456. 'module' => 'default',
  457. 'controller' => 'index',
  458. 'action' => 'index'
  459. )
  460. ),
  461. 'www' => array(
  462. 'type' => 'Zend_Controller_Router_Route_Hostname',
  463. 'route' => 'www.example.com',
  464. 'chains' => array(
  465. 'index',
  466. 'imprint' => array(
  467. 'type' => 'Zend_Controller_Router_Route_Static',
  468. 'route' => 'imprint',
  469. 'defaults' => array(
  470. 'module' => 'default',
  471. 'controller' => 'index',
  472. 'action' => 'imprint'
  473. )
  474. ),
  475. )
  476. ),
  477. 'user' => array(
  478. 'type' => 'Zend_Controller_Router_Route_Hostname',
  479. 'route' => 'user.example.com',
  480. 'chains' => array(
  481. 'index',
  482. 'profile' => array(
  483. 'type' => 'Zend_Controller_Router_Route_Static',
  484. 'route' => 'profile',
  485. 'defaults' => array(
  486. 'module' => 'user',
  487. 'controller' => 'profile',
  488. 'action' => 'index'
  489. )
  490. ),
  491. 'profile-edit' => array(
  492. 'type' => 'Zend_Controller_Router_Route_Static',
  493. 'route' => 'profile/edit',
  494. 'defaults' => array(
  495. 'module' => 'user',
  496. 'controller' => 'profile',
  497. 'action' => 'edit'
  498. )
  499. ),
  500. )
  501. ),
  502. );
  503. $router = $this->_getRouter();
  504. $router->addConfig(new Zend_Config($routes));
  505. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com');
  506. $token = $router->route($request);
  507. $this->assertEquals('default', $token->getModuleName());
  508. $this->assertEquals('index', $token->getControllerName());
  509. $this->assertEquals('index', $token->getActionName());
  510. $this->assertTrue($router->getRoute('user-profile') instanceof Zend_Controller_Router_Route_Chain);
  511. $this->assertTrue($router->getRoute('www-imprint') instanceof Zend_Controller_Router_Route_Chain);
  512. $this->assertTrue($router->getRoute('www-index') instanceof Zend_Controller_Router_Route_Chain);
  513. $this->assertTrue($router->getRoute('www-index') instanceof Zend_Controller_Router_Route_Chain);
  514. }
  515. public function testChainingWorksWithWildcardAndNoParameters()
  516. {
  517. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('module' => 'simple', 'controller' => 'bar', 'action' => 'bar'));
  518. $bar = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'index', 'action' => 'index'));
  519. $chain = $foo->chain($bar);
  520. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/bar/');
  521. $res = $chain->match($request);
  522. $this->assertEquals('simple', $res['module']);
  523. $this->assertEquals('foo', $res['controller']);
  524. $this->assertEquals('bar', $res['action']);
  525. }
  526. public function testChainingWorksWithWildcardAndOneParameter()
  527. {
  528. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('module' => 'simple', 'controller' => 'foo', 'action' => 'bar'));
  529. $bar = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'index', 'action' => 'index'));
  530. $chain = $foo->chain($bar);
  531. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/bar/id/12');
  532. $res = $chain->match($request);
  533. $this->assertEquals('simple', $res['module']);
  534. $this->assertEquals('foo', $res['controller']);
  535. $this->assertEquals('bar', $res['action']);
  536. }
  537. /**
  538. * @group ZF-7848
  539. */
  540. public function testChainingWithEmptyStaticRoutesMatchesCorrectly()
  541. {
  542. $adminRoute = new Zend_Controller_Router_Route('admin', array(
  543. 'module' => 'admin',
  544. 'controller' => 'index',
  545. 'action' => 'index',
  546. ));
  547. $indexRoute = new Zend_Controller_Router_Route_Static('', array(
  548. 'module' => 'admin',
  549. 'controller' => 'index',
  550. 'action' => 'index',
  551. ));
  552. $loginRoute = new Zend_Controller_Router_Route('login', array(
  553. 'module' => 'admin',
  554. 'controller' => 'login',
  555. 'action' => 'index',
  556. ));
  557. $emptyRoute = $adminRoute->chain($indexRoute);
  558. $nonEmptyRoute = $adminRoute->chain($loginRoute);
  559. $request = new Zend_Controller_Request_Http();
  560. $request->setPathInfo('/admin');
  561. $values = $emptyRoute->match($request);
  562. $this->assertEquals(array(
  563. 'module' => 'admin',
  564. 'controller' => 'index',
  565. 'action' => 'index',
  566. ), $values);
  567. $request->setPathInfo('/admin/');
  568. $values = $emptyRoute->match($request);
  569. $this->assertEquals(array(
  570. 'module' => 'admin',
  571. 'controller' => 'index',
  572. 'action' => 'index',
  573. ), $values);
  574. $request->setPathInfo('/admin/login');
  575. $values = $nonEmptyRoute->match($request);
  576. $this->assertEquals(array(
  577. 'module' => 'admin',
  578. 'controller' => 'login',
  579. 'action' => 'index',
  580. ), $values);
  581. }
  582. /**
  583. * @group ZF-7848
  584. */
  585. public function testChainingWithConfiguredEmptyStaticRoutesMatchesCorrectly()
  586. {
  587. $routes = array(
  588. 'admin' => array(
  589. 'route' => 'admin',
  590. 'defaults' => array(
  591. 'module' => 'admin',
  592. 'controller' => 'index',
  593. 'action' => 'index',
  594. ),
  595. 'chains' => array(
  596. 'index' => array(
  597. 'type' => 'Zend_Controller_Router_Route_Static',
  598. 'route' => '',
  599. 'defaults' => array(
  600. 'module' => 'admin',
  601. 'controller' => 'index',
  602. 'action' => 'index',
  603. ),
  604. ),
  605. 'login' => array(
  606. 'route' => 'login',
  607. 'defaults' => array(
  608. 'module' => 'admin',
  609. 'controller' => 'login',
  610. 'action' => 'index',
  611. ),
  612. ),
  613. ),
  614. ),
  615. );
  616. $config = new Zend_Config($routes);
  617. $rewrite = new Zend_Controller_Router_Rewrite();
  618. $rewrite->addConfig($config);
  619. $routes = $rewrite->getRoutes();
  620. $indexRoute = $rewrite->getRoute('admin-index');
  621. $loginRoute = $rewrite->getRoute('admin-login');
  622. $request = new Zend_Controller_Request_Http();
  623. $request->setPathInfo('/admin');
  624. $values = $indexRoute->match($request);
  625. $this->assertEquals(array(
  626. 'module' => 'admin',
  627. 'controller' => 'index',
  628. 'action' => 'index',
  629. ), $values);
  630. $request->setPathInfo('/admin/');
  631. $values = $indexRoute->match($request);
  632. $this->assertEquals(array(
  633. 'module' => 'admin',
  634. 'controller' => 'index',
  635. 'action' => 'index',
  636. ), $values);
  637. $request->setPathInfo('/admin/login');
  638. $values = $loginRoute->match($request);
  639. $this->assertEquals(array(
  640. 'module' => 'admin',
  641. 'controller' => 'login',
  642. 'action' => 'index',
  643. ), $values);
  644. }
  645. /**
  646. * @group ZF-7368
  647. */
  648. public function testChainingStaticDynamicMatchToDefaults()
  649. {
  650. $foo = new Zend_Controller_Router_Route_Static('foo');
  651. $bar = new Zend_Controller_Router_Route(':bar', array('bar' => 0));
  652. $chain = $foo->chain($bar);
  653. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo');
  654. $res = $chain->match($request);
  655. $this->assertTrue(is_array($res), 'Route did not match');
  656. $this->assertEquals(0, $res['bar']);
  657. }
  658. /**
  659. * @group ZF-7368
  660. */
  661. public function testChainingStaticDynamicMatchToParams()
  662. {
  663. $foo = new Zend_Controller_Router_Route_Static('foo');
  664. $bar = new Zend_Controller_Router_Route(':bar', array('bar' => 1));
  665. $chain = $foo->chain($bar);
  666. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/2');
  667. $res = $chain->match($request);
  668. $this->assertTrue(is_array($res), 'Route did not match');
  669. $this->assertEquals(2, $res['bar']);
  670. }
  671. protected function _getRouter()
  672. {
  673. $router = new Zend_Controller_Router_Rewrite();
  674. $front = Zend_Controller_Front::getInstance();
  675. $front->resetInstance();
  676. $front->setRequest(new Zend_Controller_Router_ChainTest_Request());
  677. $router->setFrontController($front);
  678. return $router;
  679. }
  680. }
  681. /**
  682. * Zend_Controller_Router_ChainTest_Request - request object for router testing
  683. *
  684. * @uses Zend_Controller_Request_Interface
  685. */
  686. class Zend_Controller_Router_ChainTest_Request extends Zend_Controller_Request_Http
  687. {
  688. protected $_host;
  689. protected $_port;
  690. public function __construct($uri = null)
  691. {
  692. if (null === $uri) {
  693. $uri = 'http://localhost/foo/bar/baz/2';
  694. }
  695. $uri = Zend_Uri_Http::fromString($uri);
  696. $this->_host = $uri->getHost();
  697. $this->_port = $uri->getPort();
  698. parent::__construct($uri);
  699. }
  700. public function getHttpHost() {
  701. $return = $this->_host;
  702. if ($this->_port) $return .= ':' . $this->_port;
  703. return $return;
  704. }
  705. }
  706. /**
  707. * Zend_Controller_Router_ChainTest_Dispatcher - dispatcher object for router testing
  708. */
  709. class Zend_Controller_Router_ChainTest_Dispatcher extends Zend_Controller_Dispatcher_Standard
  710. {
  711. public function getDefaultControllerName()
  712. {
  713. return 'defctrl';
  714. }
  715. public function getDefaultAction()
  716. {
  717. return 'defact';
  718. }
  719. }
  720. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Router_Route_ChainTest::main") {
  721. Zend_Controller_Router_Route_ChainTest::main();
  722. }