RouteTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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-2009 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. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. if (!defined('PHPUnit_MAIN_METHOD')) {
  27. define('PHPUnit_MAIN_METHOD', 'Zend_Controller_Router_RouteTest::main');
  28. }
  29. /** @see Zend_Controller_Request_Http */
  30. require_once 'Zend/Controller/Request/Http.php';
  31. /** @see Zend_Controller_Router_Route */
  32. require_once 'Zend/Controller/Router/Route.php';
  33. /** @see Zend_Translate */
  34. require_once 'Zend/Translate.php';
  35. /** @see Zend_Registry */
  36. require_once 'Zend/Registry.php';
  37. /** PHPUnit test case */
  38. require_once 'PHPUnit/Framework/TestCase.php';
  39. /**
  40. * @category Zend
  41. * @package Zend_Controller
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @group Zend_Controller
  46. * @group Zend_Controller_Router
  47. */
  48. class Zend_Controller_Router_RouteTest extends PHPUnit_Framework_TestCase
  49. {
  50. /**
  51. * Server backup
  52. *
  53. * @var array
  54. */
  55. protected $_server = array();
  56. /**
  57. * Setup test
  58. *
  59. * @return void
  60. */
  61. public function setUp()
  62. {
  63. // Backup server array
  64. $this->_server = $_SERVER;
  65. // Clean host env
  66. unset($_SERVER['HTTP_HOST'],
  67. $_SERVER['HTTPS'], $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT']);
  68. // Set translator
  69. $translator = new Zend_Translate('array', array('foo' => 'en_foo', 'bar' => 'en_bar'), 'en');
  70. $translator->addTranslation(array('foo' => 'de_foo', 'bar' => 'de_bar'), 'de');
  71. $translator->setLocale('en');
  72. Zend_Registry::set('Zend_Translate', $translator);
  73. }
  74. /**
  75. * Clean
  76. *
  77. * @return void
  78. */
  79. public function tearDown()
  80. {
  81. // Restore server array
  82. $_SERVER = $this->_server;
  83. // Remove translator and locale
  84. Zend_Registry::set('Zend_Translate', null);
  85. Zend_Registry::set('Zend_Locale', null);
  86. Zend_Controller_Router_Route::setDefaultTranslator(null);
  87. Zend_Controller_Router_Route::setDefaultLocale(null);
  88. }
  89. public function testStaticMatch()
  90. {
  91. $route = new Zend_Controller_Router_Route('users/all');
  92. $values = $route->match('users/all');
  93. $this->assertSame(array(), $values);
  94. }
  95. public function testStaticUTFMatch()
  96. {
  97. $route = new Zend_Controller_Router_Route('żółć');
  98. $values = $route->match('żółć');
  99. $this->assertSame(array(), $values);
  100. }
  101. public function testURLDecode()
  102. {
  103. $route = new Zend_Controller_Router_Route('żółć');
  104. $values = $route->match('%C5%BC%C3%B3%C5%82%C4%87');
  105. $this->assertSame(array(), $values);
  106. }
  107. public function testStaticPathShorterThanParts()
  108. {
  109. $route = new Zend_Controller_Router_Route('users/a/martel');
  110. $values = $route->match('users/a');
  111. $this->assertSame(false, $values);
  112. }
  113. public function testStaticPathLongerThanParts()
  114. {
  115. $route = new Zend_Controller_Router_Route('users/a');
  116. $values = $route->match('users/a/martel');
  117. $this->assertEquals(false, $values);
  118. }
  119. public function testStaticMatchWithDefaults()
  120. {
  121. $route = new Zend_Controller_Router_Route('users/all', array('controller' => 'ctrl'));
  122. $values = $route->match('users/all');
  123. $this->assertEquals('ctrl', $values['controller']);
  124. }
  125. public function testNotMatched()
  126. {
  127. $route = new Zend_Controller_Router_Route('users/all');
  128. $values = $route->match('users/martel');
  129. $this->assertEquals(false, $values);
  130. }
  131. public function testNotMatchedWithVariablesAndDefaults()
  132. {
  133. $route = new Zend_Controller_Router_Route(':controller/:action', array('controller' => 'index', 'action' => 'index'));
  134. $values = $route->match('archive/action/bogus');
  135. $this->assertEquals(false, $values);
  136. }
  137. public function testNotMatchedWithVariablesAndStatic()
  138. {
  139. $route = new Zend_Controller_Router_Route('archive/:year/:month');
  140. $values = $route->match('ctrl/act/2000');
  141. $this->assertEquals(false, $values);
  142. }
  143. public function testStaticMatchWithWildcard()
  144. {
  145. $route = new Zend_Controller_Router_Route('news/view/*', array('controller' => 'news', 'action' => 'view'));
  146. $values = $route->match('news/view/show/all/year/2000/empty');
  147. $this->assertEquals('news', $values['controller']);
  148. $this->assertEquals('view', $values['action']);
  149. $this->assertEquals('all', $values['show']);
  150. $this->assertEquals('2000', $values['year']);
  151. $this->assertEquals(null, $values['empty']);
  152. }
  153. public function testWildcardWithUTF()
  154. {
  155. $route = new Zend_Controller_Router_Route('news/*', array('controller' => 'news', 'action' => 'view'));
  156. $values = $route->match('news/klucz/wartość/wskaźnik/wartość');
  157. $this->assertEquals('news', $values['controller']);
  158. $this->assertEquals('view', $values['action']);
  159. $this->assertEquals('wartość', $values['klucz']);
  160. $this->assertEquals('wartość', $values['wskaźnik']);
  161. }
  162. public function testWildcardURLDecode()
  163. {
  164. $route = new Zend_Controller_Router_Route('news/*', array('controller' => 'news', 'action' => 'view'));
  165. $values = $route->match('news/wska%C5%BAnik/warto%C5%9B%C4%87');
  166. $this->assertEquals('news', $values['controller']);
  167. $this->assertEquals('view', $values['action']);
  168. $this->assertEquals('wartość', $values['wskaźnik']);
  169. }
  170. public function testVariableValues()
  171. {
  172. $route = new Zend_Controller_Router_Route(':controller/:action/:year');
  173. $values = $route->match('ctrl/act/2000');
  174. $this->assertEquals('ctrl', $values['controller']);
  175. $this->assertEquals('act', $values['action']);
  176. $this->assertEquals('2000', $values['year']);
  177. }
  178. public function testVariableUTFValues()
  179. {
  180. $route = new Zend_Controller_Router_Route('test/:param');
  181. $values = $route->match('test/aä');
  182. $this->assertEquals('aä', $values['param']);
  183. }
  184. public function testOneVariableValue()
  185. {
  186. $route = new Zend_Controller_Router_Route(':action', array('controller' => 'ctrl', 'action' => 'action'));
  187. $values = $route->match('act');
  188. $this->assertEquals('ctrl', $values['controller']);
  189. $this->assertEquals('act', $values['action']);
  190. }
  191. public function testVariablesWithDefault()
  192. {
  193. $route = new Zend_Controller_Router_Route(':controller/:action/:year', array('year' => '2006'));
  194. $values = $route->match('ctrl/act');
  195. $this->assertEquals('ctrl', $values['controller']);
  196. $this->assertEquals('act', $values['action']);
  197. $this->assertEquals('2006', $values['year']);
  198. }
  199. public function testVariablesWithNullDefault() // Kevin McArthur
  200. {
  201. $route = new Zend_Controller_Router_Route(':controller/:action/:year', array('year' => null));
  202. $values = $route->match('ctrl/act');
  203. $this->assertEquals('ctrl', $values['controller']);
  204. $this->assertEquals('act', $values['action']);
  205. $this->assertNull($values['year']);
  206. }
  207. public function testVariablesWithDefaultAndValue()
  208. {
  209. $route = new Zend_Controller_Router_Route(':controller/:action/:year', array('year' => '2006'));
  210. $values = $route->match('ctrl/act/2000');
  211. $this->assertEquals('ctrl', $values['controller']);
  212. $this->assertEquals('act', $values['action']);
  213. $this->assertEquals('2000', $values['year']);
  214. }
  215. public function testVariablesWithRequirementAndValue()
  216. {
  217. $route = new Zend_Controller_Router_Route(':controller/:action/:year', null, array('year' => '\d+'));
  218. $values = $route->match('ctrl/act/2000');
  219. $this->assertEquals('ctrl', $values['controller']);
  220. $this->assertEquals('act', $values['action']);
  221. $this->assertEquals('2000', $values['year']);
  222. }
  223. public function testVariablesWithRequirementAndIncorrectValue()
  224. {
  225. $route = new Zend_Controller_Router_Route(':controller/:action/:year', null, array('year' => '\d+'));
  226. $values = $route->match('ctrl/act/2000t');
  227. $this->assertEquals(false, $values);
  228. }
  229. public function testVariablesWithDefaultAndRequirement()
  230. {
  231. $route = new Zend_Controller_Router_Route(':controller/:action/:year', array('year' => '2006'), array('year' => '\d+'));
  232. $values = $route->match('ctrl/act/2000');
  233. $this->assertEquals('ctrl', $values['controller']);
  234. $this->assertEquals('act', $values['action']);
  235. $this->assertEquals('2000', $values['year']);
  236. }
  237. public function testVariablesWithDefaultAndRequirementAndIncorrectValue()
  238. {
  239. $route = new Zend_Controller_Router_Route(':controller/:action/:year', array('year' => '2006'), array('year' => '\d+'));
  240. $values = $route->match('ctrl/act/2000t');
  241. $this->assertEquals(false, $values);
  242. }
  243. public function testVariablesWithDefaultAndRequirementAndWithoutValue()
  244. {
  245. $route = new Zend_Controller_Router_Route(':controller/:action/:year', array('year' => '2006'), array('year' => '\d+'));
  246. $values = $route->match('ctrl/act');
  247. $this->assertEquals('ctrl', $values['controller']);
  248. $this->assertEquals('act', $values['action']);
  249. $this->assertEquals('2006', $values['year']);
  250. }
  251. public function testVariablesWithWildcardAndNumericKey()
  252. {
  253. $route = new Zend_Controller_Router_Route(':controller/:action/:next/*');
  254. $values = $route->match('c/a/next/2000/show/all/sort/name');
  255. $this->assertEquals('c', $values['controller']);
  256. $this->assertEquals('a', $values['action']);
  257. $this->assertEquals('next', $values['next']);
  258. $this->assertTrue(array_key_exists('2000', $values));
  259. }
  260. public function testRootRoute()
  261. {
  262. $route = new Zend_Controller_Router_Route('/');
  263. $values = $route->match('');
  264. $this->assertEquals(array(), $values);
  265. }
  266. public function testAssemble()
  267. {
  268. $route = new Zend_Controller_Router_Route('authors/:name');
  269. $url = $route->assemble(array('name' => 'martel'));
  270. $this->assertEquals('authors/martel', $url);
  271. }
  272. public function testAssembleWithoutValue()
  273. {
  274. $route = new Zend_Controller_Router_Route('authors/:name');
  275. try {
  276. $url = $route->assemble();
  277. } catch (Exception $e) {
  278. return true;
  279. }
  280. $this->fail();
  281. }
  282. public function testAssembleWithDefault()
  283. {
  284. $route = new Zend_Controller_Router_Route('authors/:name', array('name' => 'martel'));
  285. $url = $route->assemble();
  286. $this->assertEquals('authors', $url);
  287. }
  288. public function testAssembleWithDefaultAndValue()
  289. {
  290. $route = new Zend_Controller_Router_Route('authors/:name', array('name' => 'martel'));
  291. $url = $route->assemble(array('name' => 'mike'));
  292. $this->assertEquals('authors/mike', $url);
  293. }
  294. public function testAssembleWithWildcardMap()
  295. {
  296. $route = new Zend_Controller_Router_Route('authors/:name/*');
  297. $url = $route->assemble(array('name' => 'martel'));
  298. $this->assertEquals('authors/martel', $url);
  299. }
  300. public function testAssembleWithReset()
  301. {
  302. $route = new Zend_Controller_Router_Route('archive/:year/*', array('controller' => 'archive', 'action' => 'show'));
  303. $values = $route->match('archive/2006/show/all/sort/name');
  304. $url = $route->assemble(array('year' => '2005'), true);
  305. $this->assertEquals('archive/2005', $url);
  306. }
  307. public function testAssembleWithReset2()
  308. {
  309. $route = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'archive', 'action' => 'show'));
  310. $values = $route->match('users/list');
  311. $url = $route->assemble(array(), true);
  312. $this->assertEquals('', $url);
  313. }
  314. public function testAssembleWithReset3()
  315. {
  316. $route = new Zend_Controller_Router_Route('archive/:year/*', array('controller' => 'archive', 'action' => 'show', 'year' => 2005));
  317. $values = $route->match('archive/2006/show/all/sort/name');
  318. $url = $route->assemble(array(), true);
  319. $this->assertEquals('archive', $url);
  320. }
  321. public function testAssembleWithReset4()
  322. {
  323. $route = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'archive', 'action' => 'show'));
  324. $values = $route->match('users/list');
  325. $url = $route->assemble(array('action' => 'display'), true);
  326. $this->assertEquals('archive/display', $url);
  327. }
  328. public function testAssembleWithReset5()
  329. {
  330. $route = new Zend_Controller_Router_Route('*', array('controller' => 'index', 'action' => 'index'));
  331. $values = $route->match('key1/value1/key2/value2');
  332. $url = $route->assemble(array('key1' => 'newvalue'), true);
  333. $this->assertEquals('key1/newvalue', $url);
  334. }
  335. public function testAssembleWithWildcardAndAdditionalParameters()
  336. {
  337. $route = new Zend_Controller_Router_Route('authors/:name/*');
  338. $url = $route->assemble(array('name' => 'martel', 'var' => 'value'));
  339. $this->assertEquals('authors/martel/var/value', $url);
  340. }
  341. public function testAssembleWithUrlVariablesReuse()
  342. {
  343. $route = new Zend_Controller_Router_Route('archives/:year/:month');
  344. $values = $route->match('archives/2006/07');
  345. $this->assertType('array', $values);
  346. $url = $route->assemble(array('month' => '03'));
  347. $this->assertEquals('archives/2006/03', $url);
  348. }
  349. /**
  350. * @group ZF-7917
  351. */
  352. public function testAssembleWithGivenDataEqualsDefaults()
  353. {
  354. $route = new Zend_Controller_Router_Route('index/*', array(
  355. 'module' => 'default',
  356. 'controller' => 'index',
  357. 'action' => 'index'
  358. ));
  359. $this->assertEquals('index', $route->assemble(array(
  360. 'module' => 'default',
  361. 'controller' => 'index',
  362. 'action' => 'index'
  363. )));
  364. }
  365. public function testWildcardUrlVariablesOverwriting()
  366. {
  367. $route = new Zend_Controller_Router_Route('archives/:year/:month/*', array('controller' => 'archive'));
  368. $values = $route->match('archives/2006/07/controller/test/year/10000/sort/author');
  369. $this->assertType('array', $values);
  370. $this->assertEquals('archive', $values['controller']);
  371. $this->assertEquals('2006', $values['year']);
  372. $this->assertEquals('07', $values['month']);
  373. $this->assertEquals('author', $values['sort']);
  374. }
  375. public function testGetDefaults()
  376. {
  377. $route = new Zend_Controller_Router_Route('users/all',
  378. array('controller' => 'ctrl', 'action' => 'act'));
  379. $values = $route->getDefaults();
  380. $this->assertType('array', $values);
  381. $this->assertEquals('ctrl', $values['controller']);
  382. $this->assertEquals('act', $values['action']);
  383. }
  384. public function testGetDefault()
  385. {
  386. $route = new Zend_Controller_Router_Route('users/all',
  387. array('controller' => 'ctrl', 'action' => 'act'));
  388. $this->assertEquals('ctrl', $route->getDefault('controller'));
  389. $this->assertEquals(null, $route->getDefault('bogus'));
  390. }
  391. public function testGetInstance()
  392. {
  393. require_once 'Zend/Config.php';
  394. $routeConf = array(
  395. 'route' => 'users/all',
  396. 'defaults' => array(
  397. 'controller' => 'ctrl'
  398. )
  399. );
  400. $config = new Zend_Config($routeConf);
  401. $route = Zend_Controller_Router_Route::getInstance($config);
  402. $this->assertType('Zend_Controller_Router_Route', $route);
  403. $values = $route->match('users/all');
  404. $this->assertEquals('ctrl', $values['controller']);
  405. }
  406. public function testAssembleResetDefaults()
  407. {
  408. $route = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'index', 'action' => 'index'));
  409. $values = $route->match('news/view/id/3');
  410. $url = $route->assemble(array('controller' => null));
  411. $this->assertEquals('index/view/id/3', $url);
  412. $url = $route->assemble(array('action' => null));
  413. $this->assertEquals('news/index/id/3', $url);
  414. $url = $route->assemble(array('action' => null, 'id' => null));
  415. $this->assertEquals('news', $url);
  416. }
  417. public function testAssembleWithRemovedDefaults() // Test for ZF-1197
  418. {
  419. $route = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'index', 'action' => 'index'));
  420. $url = $route->assemble(array('id' => 3));
  421. $this->assertEquals('index/index/id/3', $url);
  422. $url = $route->assemble(array('action' => 'test'));
  423. $this->assertEquals('index/test', $url);
  424. $url = $route->assemble(array('action' => 'test', 'id' => 3));
  425. $this->assertEquals('index/test/id/3', $url);
  426. $url = $route->assemble(array('controller' => 'test'));
  427. $this->assertEquals('test', $url);
  428. $url = $route->assemble(array('controller' => 'test', 'action' => 'test'));
  429. $this->assertEquals('test/test', $url);
  430. $url = $route->assemble(array('controller' => 'test', 'id' => 3));
  431. $this->assertEquals('test/index/id/3', $url);
  432. $url = $route->assemble(array());
  433. $this->assertEquals('', $url);
  434. $route->match('ctrl');
  435. $url = $route->assemble(array('id' => 3));
  436. $this->assertEquals('ctrl/index/id/3', $url);
  437. $url = $route->assemble(array('action' => 'test'));
  438. $this->assertEquals('ctrl/test', $url);
  439. $url = $route->assemble();
  440. $this->assertEquals('ctrl', $url);
  441. $route->match('index');
  442. $url = $route->assemble();
  443. $this->assertEquals('', $url);
  444. }
  445. /**
  446. * Test guarding performance. Test may be failing on slow systems and shouldn't be failing on production.
  447. * This test is not critical in nature - it allows keeping changes performant.
  448. */
  449. /**
  450. * This test is commented out because performance testing should be done separately from unit
  451. * testing. It will be ported to a performance regression suite when such a suite is available.
  452. */
  453. // public function testRoutePerformance()
  454. // {
  455. // $count = 10000;
  456. // $expectedTime = 1;
  457. //
  458. // $info = "This test may be failing on slow systems and shouldn't be failing on production. Tests if " . ($count / 10) . " complicated routes can be matched in a tenth of a second. Actual test matches " . $count . " times to make the test more reliable.";
  459. //
  460. // $route = new Zend_Controller_Router_Route('archives/:year/:month/*', array('controller' => 'archive'));
  461. //
  462. // $time_start = microtime(true);
  463. //
  464. // for ($i = 1; $i <= $count; $i++) {
  465. // $values = $route->match('archives/2006/' . $i . '/controller/test/year/' . $i . '/sort/author');
  466. // }
  467. //
  468. // $time_end = microtime(true);
  469. // $time = $time_end - $time_start;
  470. //
  471. // $this->assertLessThan($expectedTime, $time, $info);
  472. // }
  473. public function testForZF2543()
  474. {
  475. $route = new Zend_Controller_Router_Route('families/:action/*', array('module' => 'default', 'controller' => 'categories', 'action' => 'index'));
  476. $this->assertEquals('families', $route->assemble());
  477. $values = $route->match('families/edit/id/4');
  478. $this->assertType('array', $values);
  479. $this->assertEquals('families/edit/id/4', $route->assemble());
  480. }
  481. public function testEncode()
  482. {
  483. $route = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'index', 'action' => 'index'));
  484. $url = $route->assemble(array('controller' => 'My Controller'), false, true);
  485. $this->assertEquals('My+Controller', $url);
  486. $url = $route->assemble(array('controller' => 'My Controller'), false, false);
  487. $this->assertEquals('My Controller', $url);
  488. $token = $route->match('en/foo/id/My Value');
  489. $url = $route->assemble(array(), false, true);
  490. $this->assertEquals('en/foo/id/My+Value', $url);
  491. $url = $route->assemble(array('id' => 'My Other Value'), false, true);
  492. $this->assertEquals('en/foo/id/My+Other+Value', $url);
  493. $route = new Zend_Controller_Router_Route(':controller/*', array('controller' => 'My Controller'));
  494. $url = $route->assemble(array('id' => 1), false, true);
  495. $this->assertEquals('My+Controller/id/1', $url);
  496. }
  497. public function testPartialMatch()
  498. {
  499. $route = new Zend_Controller_Router_Route(':lang/:temp', array('lang' => 'pl'), array('temp' => '\d+'));
  500. $values = $route->match('en/tmp/ctrl/action/id/1', true);
  501. $this->assertFalse($values);
  502. $route = new Zend_Controller_Router_Route(':lang/:temp', array('lang' => 'pl'));
  503. $values = $route->match('en/tmp/ctrl/action/id/1', true);
  504. $this->assertType('array', $values);
  505. $this->assertEquals('en', $values['lang']);
  506. $this->assertEquals('tmp', $values['temp']);
  507. $this->assertEquals('en/tmp', $route->getMatchedPath());
  508. }
  509. /**
  510. * Translated behaviour
  511. */
  512. public function testStaticTranslationAssemble()
  513. {
  514. $route = new Zend_Controller_Router_Route('foo/@foo');
  515. $url = $route->assemble();
  516. $this->assertEquals('foo/en_foo', $url);
  517. }
  518. public function testStaticTranslationMatch()
  519. {
  520. $route = new Zend_Controller_Router_Route('foo/@foo');
  521. $values = $route->match('foo/en_foo');
  522. $this->assertTrue(is_array($values));
  523. }
  524. public function testDynamicTranslationAssemble()
  525. {
  526. $route = new Zend_Controller_Router_Route('foo/:@myvar');
  527. $url = $route->assemble(array('myvar' => 'foo'));
  528. $this->assertEquals('foo/en_foo', $url);
  529. }
  530. public function testDynamicTranslationMatch()
  531. {
  532. $route = new Zend_Controller_Router_Route('foo/:@myvar');
  533. $values = $route->match('foo/en_foo');
  534. $this->assertEquals($values['myvar'], 'foo');
  535. }
  536. public function testTranslationMatchWrongLanguage()
  537. {
  538. $route = new Zend_Controller_Router_Route('foo/@foo');
  539. $values = $route->match('foo/de_foo');
  540. $this->assertFalse($values);
  541. }
  542. public function testTranslationAssembleLocaleInstanceOverride()
  543. {
  544. $route = new Zend_Controller_Router_Route('foo/@foo', null, null, null, 'de');
  545. $url = $route->assemble();
  546. $this->assertEquals('foo/de_foo', $url);
  547. }
  548. public function testTranslationAssembleLocaleParamOverride()
  549. {
  550. $route = new Zend_Controller_Router_Route('foo/@foo');
  551. $url = $route->assemble(array('@locale' => 'de'));
  552. $this->assertEquals('foo/de_foo', $url);
  553. }
  554. public function testTranslationAssembleLocaleStaticOverride()
  555. {
  556. Zend_Controller_Router_Route::setDefaultLocale('de');
  557. $route = new Zend_Controller_Router_Route('foo/@foo');
  558. $url = $route->assemble();
  559. $this->assertEquals('foo/de_foo', $url);
  560. }
  561. public function testTranslationAssembleLocaleRegistryOverride()
  562. {
  563. Zend_Registry::set('Zend_Locale', 'de');
  564. $route = new Zend_Controller_Router_Route('foo/@foo');
  565. $url = $route->assemble();
  566. $this->assertEquals('foo/de_foo', $url);
  567. }
  568. public function testTranslationAssembleTranslatorInstanceOverride()
  569. {
  570. $translator = new Zend_Translate('array', array('foo' => 'en_baz'), 'en');
  571. $route = new Zend_Controller_Router_Route('foo/@foo', null, null, $translator);
  572. $url = $route->assemble();
  573. $this->assertEquals('foo/en_baz', $url);
  574. }
  575. public function testTranslationAssembleTranslatorStaticOverride()
  576. {
  577. $translator = new Zend_Translate('array', array('foo' => 'en_baz'), 'en');
  578. Zend_Controller_Router_Route::setDefaultTranslator($translator);
  579. $route = new Zend_Controller_Router_Route('foo/@foo');
  580. $url = $route->assemble();
  581. $this->assertEquals('foo/en_baz', $url);
  582. }
  583. public function testTranslationAssembleTranslatorRegistryOverride()
  584. {
  585. $translator = new Zend_Translate('array', array('foo' => 'en_baz'), 'en');
  586. Zend_Registry::set('Zend_Translate', $translator);
  587. $route = new Zend_Controller_Router_Route('foo/@foo');
  588. $url = $route->assemble();
  589. $this->assertEquals('foo/en_baz', $url);
  590. }
  591. public function testTranslationAssembleTranslatorNotFound()
  592. {
  593. Zend_Registry::set('Zend_Translate', null);
  594. $route = new Zend_Controller_Router_Route('foo/@foo');
  595. try {
  596. $url = $route->assemble();
  597. $this->fail('Expected Zend_Controller_Router_Exception was not raised');
  598. } catch (Zend_Controller_Router_Exception $e) {
  599. $this->assertEquals('Could not find a translator', $e->getMessage());
  600. }
  601. }
  602. public function testEscapedSpecialCharsWithoutTranslation()
  603. {
  604. $route = new Zend_Controller_Router_Route('::foo/@@bar/:foo');
  605. $path = $route->assemble(array('foo' => 'bar'));
  606. $this->assertEquals($path, ':foo/@bar/bar');
  607. $values = $route->match(':foo/@bar/bar');
  608. $this->assertEquals($values['foo'], 'bar');
  609. }
  610. public function testEscapedSpecialCharsWithTranslation()
  611. {
  612. $route = new Zend_Controller_Router_Route('::foo/@@bar/:@myvar');
  613. $path = $route->assemble(array('myvar' => 'foo'));
  614. $this->assertEquals($path, ':foo/@bar/en_foo');
  615. $values = $route->match(':foo/@bar/en_foo');
  616. $this->assertEquals($values['myvar'], 'foo');
  617. }
  618. }
  619. if (PHPUnit_MAIN_METHOD == 'Zend_Controller_Router_RouteTests::main') {
  620. Zend_Controller_Router_RouteTests::main();
  621. }