/standard/tags/release-1.5.0PR/tests/Zend/Controller/Router/Route/RegexTest.php

https://github.com/bhaumik25/zend-framework · PHP · 414 lines · 300 code · 96 blank · 18 comment · 0 complexity · 70229c2ed369dd52e66979b2be18acfd MD5 · raw file

  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Controller
  5. * @subpackage UnitTests
  6. */
  7. /** Zend_Controller_Router_Route_Regex */
  8. require_once 'Zend/Controller/Router/Route/Regex.php';
  9. /** PHPUnit test case */
  10. require_once 'PHPUnit/Framework/TestCase.php';
  11. /**
  12. * @category Zend
  13. * @package Zend_Controller
  14. * @subpackage UnitTests
  15. */
  16. class Zend_Controller_Router_Route_RegexTest extends PHPUnit_Framework_TestCase
  17. {
  18. public function testStaticMatch()
  19. {
  20. $route = new Zend_Controller_Router_Route_Regex('users/all');
  21. $values = $route->match('users/all');
  22. $this->assertSame(array(), $values);
  23. }
  24. public function testStaticUTFMatch()
  25. {
  26. $route = new Zend_Controller_Router_Route_Regex('żółć');
  27. $values = $route->match('żółć');
  28. $this->assertSame(array(), $values);
  29. }
  30. public function testURLDecode()
  31. {
  32. $route = new Zend_Controller_Router_Route_Regex('żółć');
  33. $values = $route->match('%C5%BC%C3%B3%C5%82%C4%87');
  34. $this->assertSame(array(), $values);
  35. }
  36. public function testStaticNoMatch()
  37. {
  38. $route = new Zend_Controller_Router_Route_Regex('users/a/martel');
  39. $values = $route->match('users/a');
  40. $this->assertSame(false, $values);
  41. }
  42. public function testStaticMatchWithDefaults()
  43. {
  44. $route = new Zend_Controller_Router_Route_Regex('users/all', array('controller' => 'ctrl'));
  45. $values = $route->match('users/all');
  46. $this->assertSame(1, count($values));
  47. $this->assertSame('ctrl', $values['controller']);
  48. }
  49. public function testRootRoute()
  50. {
  51. $route = new Zend_Controller_Router_Route_Regex('');
  52. $values = $route->match('/');
  53. $this->assertSame(array(), $values);
  54. }
  55. public function testVariableMatch()
  56. {
  57. $route = new Zend_Controller_Router_Route_Regex('users/(.+)');
  58. $values = $route->match('users/martel');
  59. $this->assertSame(1, count($values));
  60. $this->assertSame('martel', $values[1]);
  61. }
  62. public function testDoubleMatch()
  63. {
  64. $route = new Zend_Controller_Router_Route_Regex('users/(user_(\d+).html)');
  65. $values = $route->match('users/user_1354.html');
  66. $this->assertSame(2, count($values));
  67. $this->assertSame('user_1354.html', $values[1]);
  68. $this->assertSame('1354', $values[2]);
  69. }
  70. public function testNegativeMatch()
  71. {
  72. $route = new Zend_Controller_Router_Route_Regex('((?!admin|moderator).+)',
  73. array('module' => 'index', 'controller' => 'index'),
  74. array(1 => 'action')
  75. );
  76. $values = $route->match('users');
  77. $this->assertSame(3, count($values));
  78. $this->assertSame('index', $values['module']);
  79. $this->assertSame('index', $values['controller']);
  80. $this->assertSame('users', $values['action']);
  81. }
  82. public function testNumericDefault()
  83. {
  84. $route = new Zend_Controller_Router_Route_Regex('users/?(.+)?', array(1 => 'martel'));
  85. $values = $route->match('users');
  86. $this->assertSame(1, count($values));
  87. $this->assertSame('martel', $values[1]);
  88. }
  89. public function testVariableMatchWithNumericDefault()
  90. {
  91. $route = new Zend_Controller_Router_Route_Regex('users/?(.+)?', array(1 => 'martel'));
  92. $values = $route->match('users/vicki');
  93. $this->assertSame(1, count($values));
  94. $this->assertSame('vicki', $values[1]);
  95. }
  96. public function testNamedVariableMatch()
  97. {
  98. $route = new Zend_Controller_Router_Route_Regex('users/(?P<username>.+)');
  99. $values = $route->match('users/martel');
  100. $this->assertSame(1, count($values));
  101. $this->assertSame('martel', $values[1]);
  102. }
  103. public function testMappedVariableMatch()
  104. {
  105. $route = new Zend_Controller_Router_Route_Regex('users/(.+)', null, array(1 => 'username'));
  106. $values = $route->match('users/martel');
  107. $this->assertSame(1, count($values));
  108. $this->assertSame('martel', $values['username']);
  109. }
  110. public function testMappedVariableWithDefault()
  111. {
  112. $route = new Zend_Controller_Router_Route_Regex('users(?:/(.+))?', array('username' => 'martel'), array(1 => 'username'));
  113. $values = $route->match('users');
  114. $this->assertSame(1, count($values));
  115. $this->assertSame('martel', $values['username']);
  116. }
  117. public function testMappedVariableWithNamedSubpattern()
  118. {
  119. $route = new Zend_Controller_Router_Route_Regex('users/(?P<name>.+)', null, array(1 => 'username'));
  120. $values = $route->match('users/martel');
  121. $this->assertSame(1, count($values));
  122. $this->assertSame('martel', $values['username']);
  123. }
  124. public function testOptionalVar()
  125. {
  126. $route = new Zend_Controller_Router_Route_Regex('users/(\w+)/?(?:p/(\d+))?', null, array(1 => 'username', 2 => 'page'));
  127. $values = $route->match('users/martel/p/1');
  128. $this->assertSame(2, count($values));
  129. $this->assertSame('martel', $values['username']);
  130. $this->assertSame('1', $values['page']);
  131. }
  132. public function testEmptyOptionalVar()
  133. {
  134. $route = new Zend_Controller_Router_Route_Regex('users/(\w+)/?(?:p/(\d+))?', null, array(1 => 'username', 2 => 'page'));
  135. $values = $route->match('users/martel');
  136. $this->assertSame(1, count($values));
  137. $this->assertSame('martel', $values['username']);
  138. }
  139. public function testMixedMap()
  140. {
  141. $route = new Zend_Controller_Router_Route_Regex('users/(\w+)/?(?:p/(\d+))?', null, array(1 => 'username'));
  142. $values = $route->match('users/martel/p/1');
  143. $this->assertSame(2, count($values));
  144. $this->assertSame('martel', $values['username']);
  145. $this->assertSame('1', $values[2]);
  146. }
  147. public function testNumericDefaultWithMap()
  148. {
  149. $route = new Zend_Controller_Router_Route_Regex('users/?(.+)?', array(1 => 'martel'), array(1 => 'username'));
  150. $values = $route->match('users');
  151. $this->assertSame(1, count($values));
  152. $this->assertSame('martel', $values['username']);
  153. }
  154. public function testMixedMapWithDefault()
  155. {
  156. $route = new Zend_Controller_Router_Route_Regex('users/(\w+)/?(?:p/(\d+))?', array(2 => '1'), array(1 => 'username'));
  157. $values = $route->match('users/martel/p/10');
  158. $this->assertSame(2, count($values));
  159. $this->assertSame('martel', $values['username']);
  160. $this->assertSame('10', $values[2]);
  161. }
  162. public function testMixedMapWithDefaults2()
  163. {
  164. $route = new Zend_Controller_Router_Route_Regex('users/?(\w+)?/?(?:p/(\d+))?', array(2 => '1', 'username' => 'martel'), array(1 => 'username'));
  165. $values = $route->match('users');
  166. $this->assertSame(2, count($values));
  167. $this->assertSame('martel', $values['username']);
  168. $this->assertSame('1', $values[2]);
  169. }
  170. public function testOptionalVarWithMapAndDefault()
  171. {
  172. $route = new Zend_Controller_Router_Route_Regex('users/(\w+)/?(?:p/(\d+))?', array('page' => '1', 'username' => 'martel'), array(1 => 'username', 2 => 'page'));
  173. $values = $route->match('users/martel');
  174. $this->assertSame(2, count($values));
  175. $this->assertSame('martel', $values['username']);
  176. $this->assertSame('1', $values['page']);
  177. }
  178. public function testOptionalVarWithMapAndNumericDefault()
  179. {
  180. $route = new Zend_Controller_Router_Route_Regex('users/(\w+)/?(?:p/(\d+))?', array(2 => '1'), array(2 => 'page'));
  181. $values = $route->match('users/martel');
  182. $this->assertSame(2, count($values));
  183. $this->assertSame('martel', $values[1]);
  184. $this->assertSame('1', $values['page']);
  185. }
  186. public function testMappedAndNumericDefault()
  187. {
  188. $route = new Zend_Controller_Router_Route_Regex('users/?(\w+)?', array(1 => 'martel', 'username' => 'vicki'), array(1 => 'username'));
  189. $values = $route->match('users');
  190. // Matches both defaults but the one defined last is used
  191. $this->assertSame(1, count($values));
  192. $this->assertSame('vicki', $values['username']);
  193. }
  194. public function testAssemble()
  195. {
  196. $route = new Zend_Controller_Router_Route_Regex('users/(.+)', null, array(1 => 'username'), 'users/%s');
  197. $values = $route->match('users/martel');
  198. $url = $route->assemble();
  199. $this->assertSame('users/martel', $url);
  200. }
  201. public function testAssembleWithDefault()
  202. {
  203. $route = new Zend_Controller_Router_Route_Regex('users/?(.+)?', array(1 => 'martel'), null, 'users/%s');
  204. $values = $route->match('users');
  205. $url = $route->assemble();
  206. $this->assertSame('users/martel', $url);
  207. }
  208. public function testAssembleWithMappedDefault()
  209. {
  210. $route = new Zend_Controller_Router_Route_Regex('users/?(.+)?', array('username' => 'martel'), array(1 => 'username'), 'users/%s');
  211. $values = $route->match('users');
  212. $url = $route->assemble();
  213. $this->assertSame('users/martel', $url);
  214. }
  215. public function testAssembleWithData()
  216. {
  217. $route = new Zend_Controller_Router_Route_Regex('users/(.+)', null, null, 'users/%s');
  218. $values = $route->match('users/martel');
  219. $url = $route->assemble(array(1 => 'vicki'));
  220. $this->assertSame('users/vicki', $url);
  221. }
  222. public function testAssembleWithMappedVariable()
  223. {
  224. $route = new Zend_Controller_Router_Route_Regex('users/(.+)', null, array(1 => 'username'), 'users/%s');
  225. $values = $route->match('users/martel');
  226. $url = $route->assemble(array('username' => 'vicki'));
  227. $this->assertSame('users/vicki', $url);
  228. }
  229. public function testAssembleWithMappedVariableAndNumericKey()
  230. {
  231. $route = new Zend_Controller_Router_Route_Regex('users/(.+)', null, array(1 => 'username'), 'users/%s');
  232. $values = $route->match('users/martel');
  233. $url = $route->assemble(array(1 => 'vicki'));
  234. $this->assertSame('users/vicki', $url);
  235. }
  236. public function testAssembleWithoutMatch()
  237. {
  238. $route = new Zend_Controller_Router_Route_Regex('users/(.+)', null, null, 'users/%s');
  239. try {
  240. $url = $route->assemble();
  241. $this->fail();
  242. } catch (Exception $e) {}
  243. }
  244. public function testAssembleWithDefaultWithoutMatch()
  245. {
  246. $route = new Zend_Controller_Router_Route_Regex('users/?(.+)?', array(1 => 'martel'), null, 'users/%s');
  247. $url = $route->assemble();
  248. $this->assertSame('users/martel', $url);
  249. }
  250. public function testAssembleWithMappedDefaultWithoutMatch()
  251. {
  252. $route = new Zend_Controller_Router_Route_Regex('users/?(.+)?', array('username' => 'martel'), array(1 => 'username'), 'users/%s');
  253. $url = $route->assemble();
  254. $this->assertSame('users/martel', $url);
  255. }
  256. public function testAssembleWithDataWithoutMatch()
  257. {
  258. $route = new Zend_Controller_Router_Route_Regex('users/(.+)', null, null, 'users/%s');
  259. $url = $route->assemble(array(1 => 'vicki'));
  260. $this->assertSame('users/vicki', $url);
  261. }
  262. public function testAssembleWithMappedVariableWithoutMatch()
  263. {
  264. $route = new Zend_Controller_Router_Route_Regex('users/(.+)', null, array(1 => 'username'), 'users/%s');
  265. $url = $route->assemble(array('username' => 'vicki'));
  266. $this->assertSame('users/vicki', $url);
  267. }
  268. public function testAssembleZF1332()
  269. {
  270. $route = new Zend_Controller_Router_Route_Regex(
  271. '(.+)\.([0-9]+)-([0-9]+)\.html',
  272. array('module' => 'default', 'controller' => 'content.item', 'action' => 'forward'),
  273. array(1 => 'name', 2 => 'id', 3 => 'class'),
  274. '%s.%s-%s.html'
  275. );
  276. $route->match('uml-explained-composition.72-3.html');
  277. $url = $route->assemble();
  278. $this->assertSame('uml-explained-composition.72-3.html', $url);
  279. $url = $route->assemble(array('name' => 'post_name', 'id' => '12', 'class' => 5));
  280. $this->assertSame('post_name.12-5.html', $url);
  281. }
  282. public function testGetInstance()
  283. {
  284. require_once 'Zend/Config.php';
  285. $routeConf = array(
  286. 'route' => 'forum/(\d+)',
  287. 'reverse' => 'forum/%d',
  288. 'defaults' => array(
  289. 'controller' => 'ctrl'
  290. )
  291. );
  292. /* numeric Zend_Config indexes don't work at the moment
  293. 'map' => array(
  294. '1' => 'forum_id'
  295. )
  296. */
  297. $config = new Zend_Config($routeConf);
  298. $route = Zend_Controller_Router_Route_Regex::getInstance($config);
  299. $this->assertType('Zend_Controller_Router_Route_Regex', $route);
  300. $values = $route->match('forum/1');
  301. $this->assertSame('ctrl', $values['controller']);
  302. }
  303. public function testAssembleZF2301()
  304. {
  305. $route = new Zend_Controller_Router_Route_Regex(
  306. "itemlist(?:/(\d+))?",
  307. array('page' => 1), // Defaults
  308. array(1 => 'page'), // Parameter map
  309. 'itemlist/%d'
  310. );
  311. $values = $route->match('/itemlist/2');
  312. $this->assertEquals(array('page' => 2), $values);
  313. $url = $route->assemble();
  314. $this->assertEquals('itemlist/2', $url);
  315. $url = $route->assemble(array('page' => 2));
  316. $this->assertEquals('itemlist/2', $url);
  317. }
  318. }