ChainTest.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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-2015 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-2015 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. public function testMultipleChainsWithVersion2Routes()
  672. {
  673. $foo = new Zend_Controller_Router_Route_SubclassTest('foo');
  674. $bar = new Zend_Controller_Router_Route_SubclassTest('bar', array('baz' => 'no'));
  675. $chain = $foo->chain($bar);
  676. $foo2 = new Zend_Controller_Router_Route_SubclassTest('foo');
  677. $baz = new Zend_Controller_Router_Route_SubclassTest('baz', array('baz' => 'baz'));
  678. $chain2 = $foo2->chain($baz);
  679. $rewrite = new Zend_Controller_Router_Rewrite();
  680. $rewrite->addRoute('chain2', $chain2); // First In Last Out, we want this to be matched against second
  681. $rewrite->addRoute('chain1', $chain);
  682. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/baz');
  683. $res = $rewrite->route($request);
  684. $this->assertEquals('baz', $res->getParam('baz'), 'Route did not match');
  685. $this->assertEquals('chain2', $rewrite->getCurrentRouteName(), 'Routing did not match expected route');
  686. }
  687. /**
  688. * @throws Zend_Controller_Router_Exception
  689. */
  690. public function testMultipleChainsResettingPathInfoInSegmentBlock()
  691. {
  692. $foo = new Zend_Controller_Router_Route_SubclassTest('notfoo');
  693. $bar = new Zend_Controller_Router_Route_SubclassTest('bar', array('baz' => 'no'));
  694. $chain = $foo->chain($bar);
  695. $static = new Zend_Controller_Router_Route_SimpleSubclassTest('/foo', array('foo' => 'foo'));
  696. $rewrite = new Zend_Controller_Router_Rewrite();
  697. $rewrite->addRoute('static', $static); // First In Last Out, we want this to be matched against second
  698. $rewrite->addRoute('chain', $chain);
  699. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo');
  700. $res = $rewrite->route($request);
  701. $this->assertEquals('foo', $res->getParam('foo'), 'Route did not match');
  702. $this->assertEquals('static', $rewrite->getCurrentRouteName(), 'Routing did not match expected route');
  703. }
  704. /**
  705. * @group ZF-11443
  706. */
  707. public function testGetDefault()
  708. {
  709. // Create chained route
  710. $chain = new Zend_Controller_Router_Route_Chain();
  711. $foo = new Zend_Controller_Router_Route_Hostname(
  712. 'www.example.com', array('foo' => 'foo')
  713. );
  714. $bar = new Zend_Controller_Router_Route_Regex(
  715. 'bar', array('bar' => 'bar'), array(), 'bar'
  716. );
  717. $baz = new Zend_Controller_Router_Route_Static(
  718. 'baz', array('baz' => 'baz')
  719. );
  720. $chain->chain($foo)->chain($bar)->chain($baz);
  721. // Test
  722. $this->assertSame('foo', $chain->getDefault('foo'));
  723. $this->assertSame('bar', $chain->getDefault('bar'));
  724. $this->assertSame('baz', $chain->getDefault('baz'));
  725. }
  726. /**
  727. * @group ZF-11443
  728. */
  729. public function testGetDefaultPriority()
  730. {
  731. // Create chained route
  732. $chain = new Zend_Controller_Router_Route_Chain();
  733. $foo = new Zend_Controller_Router_Route_Hostname(
  734. 'www.example.com', array('priority' => 1)
  735. );
  736. $bar = new Zend_Controller_Router_Route_Regex(
  737. 'bar', array('priority' => 2), array(), 'bar'
  738. );
  739. $baz = new Zend_Controller_Router_Route_Static(
  740. 'baz', array('priority' => 3)
  741. );
  742. $chain->chain($foo)->chain($bar)->chain($baz);
  743. // Test
  744. $this->assertSame(3, $chain->getDefault('priority'));
  745. }
  746. /**
  747. * @group ZF-11443
  748. */
  749. public function testGetDefaults()
  750. {
  751. // Create chained route
  752. $chain = new Zend_Controller_Router_Route_Chain();
  753. $foo = new Zend_Controller_Router_Route_Hostname(
  754. 'www.example.com', array('foo' => 'foo')
  755. );
  756. $bar = new Zend_Controller_Router_Route_Regex(
  757. 'bar', array('bar' => 'bar'), array(), 'bar'
  758. );
  759. $baz = new Zend_Controller_Router_Route_Static(
  760. 'baz', array('baz' => 'baz')
  761. );
  762. $chain->chain($foo)->chain($bar)->chain($baz);
  763. // Get defaults
  764. $values = $chain->getDefaults();
  765. // Test
  766. $this->assertTrue(is_array($values));
  767. $this->assertSame('foo', $values['foo']);
  768. $this->assertSame('bar', $values['bar']);
  769. $this->assertSame('baz', $values['baz']);
  770. }
  771. /**
  772. * @group ZF-11443
  773. */
  774. public function testGetDefaultsPriority()
  775. {
  776. // Create chained route
  777. $chain = new Zend_Controller_Router_Route_Chain();
  778. $foo = new Zend_Controller_Router_Route_Hostname(
  779. 'www.zend.com', array('priority' => 1)
  780. );
  781. $bar = new Zend_Controller_Router_Route_Regex(
  782. 'bar', array('priority' => 2), array(), 'bar'
  783. );
  784. $baz = new Zend_Controller_Router_Route_Static(
  785. 'baz', array('priority' => 3)
  786. );
  787. $chain->chain($foo)->chain($bar)->chain($baz);
  788. // Get defaults
  789. $values = $chain->getDefaults();
  790. // Test
  791. $this->assertSame(3, $values['priority']);
  792. }
  793. protected function _getRouter()
  794. {
  795. $router = new Zend_Controller_Router_Rewrite();
  796. $front = Zend_Controller_Front::getInstance();
  797. $front->resetInstance();
  798. $front->setRequest(new Zend_Controller_Router_ChainTest_Request());
  799. $router->setFrontController($front);
  800. return $router;
  801. }
  802. }
  803. class Zend_Controller_Router_Route_SimpleSubclassTest extends Zend_Controller_Router_Route_Abstract
  804. {
  805. /**
  806. * @var string
  807. */
  808. protected $path;
  809. /**
  810. * @var array
  811. */
  812. protected $params = array();
  813. public function __construct($path, $params)
  814. {
  815. $this->path = $path;
  816. $this->params = $params;
  817. }
  818. public function match($path, $partial = false)
  819. {
  820. $path = $path->getPathInfo();
  821. if ($path == $this->path) {
  822. $this->setMatchedPath($this->path);
  823. return $this->params;
  824. }
  825. return false;
  826. }
  827. public function getVersion()
  828. {
  829. return 2;
  830. }
  831. public function assemble($data = array(), $reset = false, $encode = false)
  832. {}
  833. public static function getInstance(Zend_Config $config)
  834. {}
  835. }
  836. class Zend_Controller_Router_Route_SubclassTest extends Zend_Controller_Router_Route_Static
  837. {
  838. public function match($path, $partial = false)
  839. {
  840. $path = $path->getPathInfo();
  841. $match = parent::match($path, $partial); // TODO: Change the autogenerated stub
  842. if (is_array($match)) {
  843. $this->setMatchedPath($this->_route);
  844. }
  845. return $match;
  846. }
  847. public function getVersion()
  848. {
  849. return 2;
  850. }
  851. public function assemble($data = array(), $reset = false, $encode = false)
  852. {
  853. // TODO: Implement assemble() method.
  854. }
  855. public static function getInstance(Zend_Config $config)
  856. {
  857. // TODO: Implement getInstance() method.
  858. }
  859. }
  860. /**
  861. * Zend_Controller_Router_ChainTest_Request - request object for router testing
  862. *
  863. * @uses Zend_Controller_Request_Interface
  864. */
  865. class Zend_Controller_Router_ChainTest_Request extends Zend_Controller_Request_Http
  866. {
  867. protected $_host;
  868. protected $_port;
  869. public function __construct($uri = null)
  870. {
  871. if (null === $uri) {
  872. $uri = 'http://localhost/foo/bar/baz/2';
  873. }
  874. $uri = Zend_Uri_Http::fromString($uri);
  875. $this->_host = $uri->getHost();
  876. $this->_port = $uri->getPort();
  877. parent::__construct($uri);
  878. }
  879. public function getHttpHost() {
  880. $return = $this->_host;
  881. if ($this->_port) $return .= ':' . $this->_port;
  882. return $return;
  883. }
  884. }
  885. /**
  886. * Zend_Controller_Router_ChainTest_Dispatcher - dispatcher object for router testing
  887. */
  888. class Zend_Controller_Router_ChainTest_Dispatcher extends Zend_Controller_Dispatcher_Standard
  889. {
  890. public function getDefaultControllerName()
  891. {
  892. return 'defctrl';
  893. }
  894. public function getDefaultAction()
  895. {
  896. return 'defact';
  897. }
  898. }
  899. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Router_Route_ChainTest::main") {
  900. Zend_Controller_Router_Route_ChainTest::main();
  901. }