ChainTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Controller
  5. * @subpackage UnitTests
  6. */
  7. if (!defined('PHPUnit_MAIN_METHOD')) {
  8. define('PHPUnit_MAIN_METHOD', 'Zend_Controller_Router_Route_ChainTest::main');
  9. }
  10. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  11. /** Zend_Config */
  12. require_once 'Zend/Config.php';
  13. /** Zend_Controller_Router_Rewrite */
  14. require_once 'Zend/Controller/Router/Rewrite.php';
  15. /** Zend_Controller_Router_Route_Chain */
  16. require_once 'Zend/Controller/Router/Route/Chain.php';
  17. /** Zend_Controller_Router_Route */
  18. require_once 'Zend/Controller/Router/Route.php';
  19. /** Zend_Controller_Router_Route_Module */
  20. require_once 'Zend/Controller/Router/Route/Module.php';
  21. /** Zend_Controller_Router_Route_Static */
  22. require_once 'Zend/Controller/Router/Route/Static.php';
  23. /** Zend_Controller_Router_Route_Regex */
  24. require_once 'Zend/Controller/Router/Route/Regex.php';
  25. /** Zend_Controller_Router_Route_Hostname */
  26. require_once 'Zend/Controller/Router/Route/Hostname.php';
  27. /** Zend_Controller_Request_Http */
  28. require_once 'Zend/Controller/Request/Http.php';
  29. /** Zend_Uri_Http */
  30. require_once 'Zend/Uri/Http.php';
  31. /** Zend_Config */
  32. require_once 'Zend/Config.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Controller
  36. * @subpackage UnitTests
  37. */
  38. class Zend_Controller_Router_Route_ChainTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Runs the test methods of this class.
  42. *
  43. * @access public
  44. * @static
  45. */
  46. public static function main()
  47. {
  48. require_once "PHPUnit/TextUI/TestRunner.php";
  49. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Router_Route_ChainTest");
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. public function testChaining()
  53. {
  54. $request = new Zend_Controller_Router_ChainTest_Request('http://localhost/foo/bar');
  55. $foo = new Zend_Controller_Router_Route('foo');
  56. $bar = new Zend_Controller_Router_Route('bar');
  57. $chain = $foo->chain($bar);
  58. $this->assertType('Zend_Controller_Router_Route_Chain', $chain);
  59. }
  60. public function testChainingMatch()
  61. {
  62. $chain = new Zend_Controller_Router_Route_Chain();
  63. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 1));
  64. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2));
  65. $chain->chain($foo)->chain($bar);
  66. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bla');
  67. $res = $chain->match($request);
  68. $this->assertFalse($res);
  69. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  70. $res = $chain->match($request);
  71. $this->assertEquals(1, $res['foo']);
  72. $this->assertEquals(2, $res['bar']);
  73. }
  74. public function testChainingShortcutMatch()
  75. {
  76. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 1));
  77. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2, 'controller' => 'foo', 'action' => 'bar'));
  78. $chain = $foo->chain($bar);
  79. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  80. $res = $chain->match($request);
  81. $this->assertEquals(1, $res['foo']);
  82. $this->assertEquals(2, $res['bar']);
  83. }
  84. public function testChainingMatchFailure()
  85. {
  86. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 1));
  87. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2, 'controller' => 'foo', 'action' => 'bar'));
  88. $chain = $foo->chain($bar);
  89. $request = new Zend_Controller_Router_ChainTest_Request('http://nope.zend.com/bar');
  90. $res = $chain->match($request);
  91. $this->assertFalse($res);
  92. }
  93. public function testChainingVariableOverriding()
  94. {
  95. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 1, 'controller' => 'foo', 'module' => 'foo'));
  96. $bar = new Zend_Controller_Router_Route('bar', array('bar' => 2, 'controller' => 'bar', 'action' => 'bar'));
  97. $chain = $foo->chain($bar);
  98. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  99. $res = $chain->match($request);
  100. $this->assertEquals('foo', $res['module']);
  101. $this->assertEquals('bar', $res['controller']);
  102. $this->assertEquals('bar', $res['action']);
  103. }
  104. public function testChainingTooLongPath()
  105. {
  106. $foo = new Zend_Controller_Router_Route_Static('foo', array('foo' => 1));
  107. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2));
  108. $chain = $foo->chain($bar);
  109. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/bar/baz');
  110. $res = $chain->match($request);
  111. $this->assertFalse($res);
  112. }
  113. public function testChainingRegex()
  114. {
  115. $foo = new Zend_Controller_Router_Route_Regex('f..', array('foo' => 1));
  116. $bar = new Zend_Controller_Router_Route_Regex('b..', array('bar' => 2));
  117. $chain = $foo->chain($bar);
  118. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/bar');
  119. $res = $chain->match($request);
  120. $this->assertEquals(1, $res['foo']);
  121. $this->assertEquals(2, $res['bar']);
  122. }
  123. public function testChainingModule()
  124. {
  125. $foo = new Zend_Controller_Router_Route_Static('foo', array('foo' => 'bar'));
  126. $bar = new Zend_Controller_Router_Route_Module();
  127. $chain = $foo->chain($bar);
  128. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo/bar/baz/var/val');
  129. $res = $chain->match($request);
  130. $this->assertEquals('bar', $res['foo']);
  131. $this->assertEquals('bar', $res['controller']);
  132. $this->assertEquals('baz', $res['action']);
  133. $this->assertEquals('val', $res['var']);
  134. }
  135. public function testVariableUnsettingRoute()
  136. {
  137. $foo = new Zend_Controller_Router_Route(':foo');
  138. $bar = new Zend_Controller_Router_Route_Module(array('controller' => 'ctrl', 'action' => 'act'));
  139. $chain = $foo->chain($bar);
  140. $path = $chain->assemble(array('foo' => 'bar', 'baz' => 'bat'));
  141. $this->assertEquals('bar/ctrl/act/baz/bat', $path);
  142. }
  143. public function testVariableUnsettingRegex()
  144. {
  145. $foo = new Zend_Controller_Router_Route_Regex('([^/]+)', array(), array(1 => 'foo'), '%s');
  146. $bar = new Zend_Controller_Router_Route_Module(array('controller' => 'ctrl', 'action' => 'act'));
  147. $chain = $foo->chain($bar);
  148. $path = $chain->assemble(array('foo' => 'bar', 'baz' => 'bat'));
  149. $this->assertEquals('bar/ctrl/act/baz/bat', $path);
  150. }
  151. public function testChainingSeparatorOverriding()
  152. {
  153. $foo = new Zend_Controller_Router_Route_Static('foo', array('foo' => 1));
  154. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 2));
  155. $baz = new Zend_Controller_Router_Route_Static('baz', array('baz' => 3));
  156. $chain = $foo->chain($bar, '.');
  157. $res = $chain->match(new Zend_Controller_Router_ChainTest_Request('http://localhost/foo.bar'));
  158. $this->assertType('array', $res);
  159. $res = $chain->match(new Zend_Controller_Router_ChainTest_Request('http://localhost/foo/bar'));
  160. $this->assertEquals(false, $res);
  161. $chain->chain($baz, ':');
  162. $res = $chain->match(new Zend_Controller_Router_ChainTest_Request('http://localhost/foo.bar:baz'));
  163. $this->assertType('array', $res);
  164. }
  165. public function testI18nChaining()
  166. {
  167. $lang = new Zend_Controller_Router_Route(':lang', array('lang' => 'en'));
  168. $profile = new Zend_Controller_Router_Route('user/:id', array('controller' => 'foo', 'action' => 'bar'));
  169. $chain = $lang->chain($profile);
  170. $res = $chain->match(new Zend_Controller_Router_ChainTest_Request('http://localhost/en/user/1'));
  171. $this->assertEquals('en', $res['lang']);
  172. $this->assertEquals('1', $res['id']);
  173. }
  174. public function testChainingAssembleWithRoutePlaceholder()
  175. {
  176. $chain = new Zend_Controller_Router_Route_Chain();
  177. $foo = new Zend_Controller_Router_Route_Hostname(':account.zend.com');
  178. $bar = new Zend_Controller_Router_Route('bar/*');
  179. $chain->chain($foo)->chain($bar);
  180. $request = new Zend_Controller_Router_ChainTest_Request('http://foobar.zend.com/bar');
  181. $res = $chain->match($request);
  182. $this->assertType('array', $res);
  183. $this->assertRegexp('#[^a-z0-9]?foobar\.zend\.com/bar/foo/bar#i', $chain->assemble(array('account' => 'foobar', 'foo' => 'bar')));
  184. }
  185. public function testChainingAssembleWithStatic()
  186. {
  187. $chain = new Zend_Controller_Router_Route_Chain();
  188. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 'foo'));
  189. $bar = new Zend_Controller_Router_Route_Static('bar', array('bar' => 'bar'));
  190. $chain->chain($foo)->chain($bar);
  191. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  192. $res = $chain->match($request);
  193. $this->assertType('array', $res);
  194. $this->assertRegexp('#[^a-z0-9]?www\.zend\.com/bar$#i', $chain->assemble());
  195. }
  196. public function testChainingAssembleWithRegex()
  197. {
  198. $chain = new Zend_Controller_Router_Route_Chain();
  199. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 'foo'));
  200. $bar = new Zend_Controller_Router_Route_Regex('bar', array('bar' => 'bar'), array(), 'bar');
  201. $chain->chain($foo)->chain($bar);
  202. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/bar');
  203. $res = $chain->match($request);
  204. $this->assertType('array', $res);
  205. $this->assertRegexp('#[^a-z0-9]?www\.zend\.com/bar$#i', $chain->assemble());
  206. }
  207. public function testChainingReuse()
  208. {
  209. $foo = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('foo' => 'foo'));
  210. $profile = new Zend_Controller_Router_Route('user/:id', array('controller' => 'prof'));
  211. $article = new Zend_Controller_Router_Route('article/:id', array('controller' => 'art', 'action' => 'art'));
  212. $profileChain = $foo->chain($profile);
  213. $articleChain = $foo->chain($article);
  214. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/user/1');
  215. $res = $profileChain->match($request);
  216. $this->assertType('array', $res);
  217. $this->assertEquals('prof', $res['controller']);
  218. $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/article/1');
  219. $res = $articleChain->match($request);
  220. $this->assertType('array', $res);
  221. $this->assertEquals('art', $res['controller']);
  222. $this->assertEquals('art', $res['action']);
  223. }
  224. public function testConfigChaining()
  225. {
  226. $routes = array(
  227. /** Abstract routes */
  228. 'www' => array(
  229. 'type' => 'Zend_Controller_Router_Route_Hostname',
  230. 'route' => 'www.example.com',
  231. 'abstract' => true
  232. ),
  233. 'user' => array(
  234. 'type' => 'Zend_Controller_Router_Route_Hostname',
  235. 'route' => 'user.example.com',
  236. 'abstract' => true
  237. ),
  238. 'index' => array(
  239. 'type' => 'Zend_Controller_Router_Route_Static',
  240. 'route' => '',
  241. 'abstract' => true,
  242. 'defaults' => array(
  243. 'module' => 'default',
  244. 'controller' => 'index',
  245. 'action' => 'index'
  246. )
  247. ),
  248. 'imprint' => array(
  249. 'type' => 'Zend_Controller_Router_Route_Static',
  250. 'route' => 'imprint',
  251. 'abstract' => true,
  252. 'defaults' => array(
  253. 'module' => 'default',
  254. 'controller' => 'index',
  255. 'action' => 'imprint'
  256. )
  257. ),
  258. 'profile' => array(
  259. 'type' => 'Zend_Controller_Router_Route_Static',
  260. 'route' => 'profile',
  261. 'abstract' => true,
  262. 'defaults' => array(
  263. 'module' => 'user',
  264. 'controller' => 'profile',
  265. 'action' => 'index'
  266. )
  267. ),
  268. 'profile-edit' => array(
  269. 'type' => 'Zend_Controller_Router_Route_Static',
  270. 'route' => 'profile/edit',
  271. 'abstract' => true,
  272. 'defaults' => array(
  273. 'module' => 'user',
  274. 'controller' => 'profile',
  275. 'action' => 'edit'
  276. )
  277. ),
  278. /** Chains */
  279. 'www-index' => array(
  280. 'type' => 'Zend_Controller_Router_Route_Chain',
  281. 'chain' => 'www, index' // or array('www-subdomain', 'index'); / maybe both
  282. ),
  283. 'www-imprint' => array(
  284. 'type' => 'Zend_Controller_Router_Route_Chain',
  285. 'chain' => 'www, imprint'
  286. ),
  287. 'user-index' => array(
  288. 'type' => 'Zend_Controller_Router_Route_Chain',
  289. 'chain' => 'user, index'
  290. ),
  291. 'user-profile' => array(
  292. 'type' => 'Zend_Controller_Router_Route_Chain',
  293. 'chain' => 'user, profile'
  294. ),
  295. 'user-profile-edit' => array(
  296. 'type' => 'Zend_Controller_Router_Route_Chain',
  297. 'chain' => 'user, profile-edit'
  298. )
  299. );
  300. $router = new Zend_Controller_Router_Rewrite();
  301. $front = Zend_Controller_Front::getInstance();
  302. $front->resetInstance();
  303. $front->setDispatcher(new Zend_Controller_Router_RewriteTest_Dispatcher());
  304. $front->setRequest(new Zend_Controller_Router_RewriteTest_Request());
  305. $router->setFrontController($front);
  306. $router->addConfig(new Zend_Config($routes));
  307. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com/profile');
  308. $token = $router->route($request);
  309. $this->assertEquals('user', $token->getModuleName());
  310. $this->assertEquals('profile', $token->getControllerName());
  311. $this->assertEquals('index', $token->getActionName());
  312. $request = new Zend_Controller_Router_ChainTest_Request('http://foo.example.com/imprint');
  313. $token = $router->route($request);
  314. $this->assertEquals('default', $token->getModuleName());
  315. $this->assertEquals('imprint', $token->getControllerName());
  316. $this->assertEquals('defact', $token->getActionName());
  317. }
  318. public function testConfigChainingAlternative()
  319. {
  320. $routes = array(
  321. 'www' => array(
  322. 'type' => 'Zend_Controller_Router_Route_Hostname',
  323. 'route' => 'www.example.com',
  324. 'chains' => array(
  325. 'index' => array(
  326. 'type' => 'Zend_Controller_Router_Route_Static',
  327. 'route' => '',
  328. 'defaults' => array(
  329. 'module' => 'default',
  330. 'controller' => 'index',
  331. 'action' => 'index'
  332. )
  333. ),
  334. 'imprint' => array(
  335. 'type' => 'Zend_Controller_Router_Route_Static',
  336. 'route' => 'imprint',
  337. 'defaults' => array(
  338. 'module' => 'default',
  339. 'controller' => 'index',
  340. 'action' => 'imprint'
  341. )
  342. ),
  343. )
  344. ),
  345. 'user' => array(
  346. 'type' => 'Zend_Controller_Router_Route_Hostname',
  347. 'route' => 'user.example.com',
  348. 'chains' => array(
  349. 'profile' => array(
  350. 'type' => 'Zend_Controller_Router_Route_Static',
  351. 'route' => 'profile',
  352. 'defaults' => array(
  353. 'module' => 'user',
  354. 'controller' => 'profile',
  355. 'action' => 'index'
  356. ),
  357. 'chains' => array(
  358. 'standard' => array(
  359. 'type' => 'Zend_Controller_Router_Route_Static',
  360. 'route' => 'standard2',
  361. 'defaults' => array(
  362. 'mode' => 'standard'
  363. )
  364. ),
  365. 'detail' => array(
  366. 'type' => 'Zend_Controller_Router_Route_Static',
  367. 'route' => 'detail',
  368. 'defaults' => array(
  369. 'mode' => 'detail'
  370. )
  371. )
  372. )
  373. ),
  374. 'profile-edit' => array(
  375. 'type' => 'Zend_Controller_Router_Route_Static',
  376. 'route' => 'profile/edit',
  377. 'defaults' => array(
  378. 'module' => 'user',
  379. 'controller' => 'profile',
  380. 'action' => 'edit'
  381. )
  382. ),
  383. )
  384. ),
  385. );
  386. $router = $this->_getRouter();
  387. $router->addConfig(new Zend_Config($routes));
  388. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com/profile/edit');
  389. $token = $router->route($request);
  390. $this->assertEquals('user', $token->getModuleName());
  391. $this->assertEquals('profile', $token->getControllerName());
  392. $this->assertEquals('edit', $token->getActionName());
  393. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com/profile/detail');
  394. $token = $router->route($request);
  395. $this->assertEquals('user', $token->getModuleName());
  396. $this->assertEquals('profile', $token->getControllerName());
  397. $this->assertEquals('index', $token->getActionName());
  398. $this->assertEquals('detail', $token->getParam('mode'));
  399. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com/profile');
  400. $token = $router->route($request);
  401. }
  402. public function testConfigChainingMixed()
  403. {
  404. $routes = array(
  405. 'index' => array(
  406. 'type' => 'Zend_Controller_Router_Route_Static',
  407. 'route' => '',
  408. 'defaults' => array(
  409. 'module' => 'default',
  410. 'controller' => 'index',
  411. 'action' => 'index'
  412. )
  413. ),
  414. 'www' => array(
  415. 'type' => 'Zend_Controller_Router_Route_Hostname',
  416. 'route' => 'www.example.com',
  417. 'chains' => array(
  418. 'index',
  419. 'imprint' => array(
  420. 'type' => 'Zend_Controller_Router_Route_Static',
  421. 'route' => 'imprint',
  422. 'defaults' => array(
  423. 'module' => 'default',
  424. 'controller' => 'index',
  425. 'action' => 'imprint'
  426. )
  427. ),
  428. )
  429. ),
  430. 'user' => array(
  431. 'type' => 'Zend_Controller_Router_Route_Hostname',
  432. 'route' => 'user.example.com',
  433. 'chains' => array(
  434. 'index',
  435. 'profile' => array(
  436. 'type' => 'Zend_Controller_Router_Route_Static',
  437. 'route' => 'profile',
  438. 'defaults' => array(
  439. 'module' => 'user',
  440. 'controller' => 'profile',
  441. 'action' => 'index'
  442. )
  443. ),
  444. 'profile-edit' => array(
  445. 'type' => 'Zend_Controller_Router_Route_Static',
  446. 'route' => 'profile/edit',
  447. 'defaults' => array(
  448. 'module' => 'user',
  449. 'controller' => 'profile',
  450. 'action' => 'edit'
  451. )
  452. ),
  453. )
  454. ),
  455. );
  456. $router = $this->_getRouter();
  457. $router->addConfig(new Zend_Config($routes));
  458. $request = new Zend_Controller_Router_ChainTest_Request('http://user.example.com');
  459. $token = $router->route($request);
  460. $this->assertEquals('default', $token->getModuleName());
  461. $this->assertEquals('index', $token->getControllerName());
  462. $this->assertEquals('index', $token->getActionName());
  463. $this->assertType('Zend_Controller_Router_Route_Chain', $router->getRoute('user-profile'));
  464. $this->assertType('Zend_Controller_Router_Route_Chain', $router->getRoute('www-imprint'));
  465. $this->assertType('Zend_Controller_Router_Route_Chain', $router->getRoute('www-index'));
  466. $this->assertType('Zend_Controller_Router_Route_Chain', $router->getRoute('www-index'));
  467. }
  468. protected function _getRouter()
  469. {
  470. $router = new Zend_Controller_Router_Rewrite();
  471. $front = Zend_Controller_Front::getInstance();
  472. $front->resetInstance();
  473. $front->setRequest(new Zend_Controller_Router_ChainTest_Request());
  474. $router->setFrontController($front);
  475. return $router;
  476. }
  477. }
  478. /**
  479. * Zend_Controller_Router_ChainTest_Request - request object for router testing
  480. *
  481. * @uses Zend_Controller_Request_Interface
  482. */
  483. class Zend_Controller_Router_ChainTest_Request extends Zend_Controller_Request_Http
  484. {
  485. protected $_host;
  486. protected $_port;
  487. public function __construct($uri = null)
  488. {
  489. if (null === $uri) {
  490. $uri = 'http://localhost/foo/bar/baz/2';
  491. }
  492. $uri = Zend_Uri_Http::fromString($uri);
  493. $this->_host = $uri->getHost();
  494. $this->_port = $uri->getPort();
  495. parent::__construct($uri);
  496. }
  497. public function getHttpHost() {
  498. $return = $this->_host;
  499. if ($this->_port) $return .= ':' . $this->_port;
  500. return $return;
  501. }
  502. }
  503. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Router_Route_ChainTest::main") {
  504. Zend_Controller_Router_Route_ChainTest::main();
  505. }