ChainTest.php 30 KB

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