RouteTest.php 26 KB

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