RouteTest.php 26 KB

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