RouteTest.php 25 KB

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