ChainTest.php 30 KB

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