UrlGeneratorTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Routing\Generator;
  11. use Symfony\Component\Routing\RouteCollection;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Routing\Generator\UrlGenerator;
  14. use Symfony\Component\Routing\RequestContext;
  15. class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testAbsoluteUrlWithPort80()
  18. {
  19. $routes = $this->getRoutes('test', new Route('/testing'));
  20. $url = $this->getGenerator($routes)->generate('test', array(), true);
  21. $this->assertEquals('http://localhost/app.php/testing', $url);
  22. }
  23. public function testAbsoluteSecureUrlWithPort443()
  24. {
  25. $routes = $this->getRoutes('test', new Route('/testing'));
  26. $url = $this->getGenerator($routes, array('scheme' => 'https'))->generate('test', array(), true);
  27. $this->assertEquals('https://localhost/app.php/testing', $url);
  28. }
  29. public function testAbsoluteUrlWithNonStandardPort()
  30. {
  31. $routes = $this->getRoutes('test', new Route('/testing'));
  32. $url = $this->getGenerator($routes, array('httpPort' => 8080))->generate('test', array(), true);
  33. $this->assertEquals('http://localhost:8080/app.php/testing', $url);
  34. }
  35. public function testAbsoluteSecureUrlWithNonStandardPort()
  36. {
  37. $routes = $this->getRoutes('test', new Route('/testing'));
  38. $url = $this->getGenerator($routes, array('httpsPort' => 8080, 'scheme' => 'https'))->generate('test', array(), true);
  39. $this->assertEquals('https://localhost:8080/app.php/testing', $url);
  40. }
  41. public function testRelativeUrlWithoutParameters()
  42. {
  43. $routes = $this->getRoutes('test', new Route('/testing'));
  44. $url = $this->getGenerator($routes)->generate('test', array(), false);
  45. $this->assertEquals('/app.php/testing', $url);
  46. }
  47. public function testRelativeUrlWithParameter()
  48. {
  49. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  50. $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
  51. $this->assertEquals('/app.php/testing/bar', $url);
  52. }
  53. public function testRelativeUrlWithNullParameter()
  54. {
  55. $routes = $this->getRoutes('test', new Route('/testing.{format}', array('format' => null)));
  56. $url = $this->getGenerator($routes)->generate('test', array(), false);
  57. $this->assertEquals('/app.php/testing', $url);
  58. }
  59. public function testRelativeUrlWithNullParameterButNotOptional()
  60. {
  61. $routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', array('foo' => null)));
  62. $url = $this->getGenerator($routes)->generate('test', array(), false);
  63. $this->assertEquals('/app.php/testing//bar', $url);
  64. }
  65. public function testRelativeUrlWithOptionalZeroParameter()
  66. {
  67. $routes = $this->getRoutes('test', new Route('/testing/{page}'));
  68. $url = $this->getGenerator($routes)->generate('test', array('page' => 0), false);
  69. $this->assertEquals('/app.php/testing/0', $url);
  70. }
  71. public function testRelativeUrlWithExtraParameters()
  72. {
  73. $routes = $this->getRoutes('test', new Route('/testing'));
  74. $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
  75. $this->assertEquals('/app.php/testing?foo=bar', $url);
  76. }
  77. public function testAbsoluteUrlWithExtraParameters()
  78. {
  79. $routes = $this->getRoutes('test', new Route('/testing'));
  80. $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  81. $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
  82. }
  83. public function testUrlWithNullExtraParameters()
  84. {
  85. $routes = $this->getRoutes('test', new Route('/testing'));
  86. $url = $this->getGenerator($routes)->generate('test', array('foo' => null), true);
  87. $this->assertEquals('http://localhost/app.php/testing', $url);
  88. }
  89. public function testUrlWithExtraParametersFromGlobals()
  90. {
  91. $routes = $this->getRoutes('test', new Route('/testing'));
  92. $generator = $this->getGenerator($routes);
  93. $context = new RequestContext('/app.php');
  94. $context->setParameter('bar', 'bar');
  95. $generator->setContext($context);
  96. $url = $generator->generate('test', array('foo' => 'bar'));
  97. $this->assertEquals('/app.php/testing?foo=bar', $url);
  98. }
  99. public function testUrlWithGlobalParameter()
  100. {
  101. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  102. $generator = $this->getGenerator($routes);
  103. $context = new RequestContext('/app.php');
  104. $context->setParameter('foo', 'bar');
  105. $generator->setContext($context);
  106. $url = $generator->generate('test', array());
  107. $this->assertEquals('/app.php/testing/bar', $url);
  108. }
  109. /**
  110. * @expectedException Symfony\Component\Routing\Exception\RouteNotFoundException
  111. */
  112. public function testGenerateWithoutRoutes()
  113. {
  114. $routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
  115. $this->getGenerator($routes)->generate('test', array(), true);
  116. }
  117. /**
  118. * @expectedException Symfony\Component\Routing\Exception\MissingMandatoryParametersException
  119. */
  120. public function testGenerateForRouteWithoutMandatoryParameter()
  121. {
  122. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  123. $this->getGenerator($routes)->generate('test', array(), true);
  124. }
  125. /**
  126. * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
  127. */
  128. public function testGenerateForRouteWithInvalidOptionalParameter()
  129. {
  130. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
  131. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  132. }
  133. /**
  134. * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
  135. */
  136. public function testGenerateForRouteWithInvalidManditoryParameter()
  137. {
  138. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
  139. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  140. }
  141. public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
  142. {
  143. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
  144. $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test'));
  145. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
  146. $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  147. }
  148. public function testSchemeRequirementForcesAbsoluteUrl()
  149. {
  150. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
  151. $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
  152. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
  153. $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  154. }
  155. public function testNoTrailingSlashForMultipleOptionalParameters()
  156. {
  157. $routes = $this->getRoutes('test', new Route('/category/{slug1}/{slug2}/{slug3}', array('slug2' => null, 'slug3' => null)));
  158. $this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', array('slug1' => 'foo')));
  159. }
  160. public function testWithAnIntegerAsADefaultValue()
  161. {
  162. $routes = $this->getRoutes('test', new Route('/{default}', array('default' => 0)));
  163. $this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
  164. }
  165. protected function getGenerator(RouteCollection $routes, array $parameters = array())
  166. {
  167. $context = new RequestContext('/app.php');
  168. foreach ($parameters as $key => $value) {
  169. $method = 'set'.$key;
  170. $context->$method($value);
  171. }
  172. $generator = new UrlGenerator($routes, $context);
  173. return $generator;
  174. }
  175. protected function getRoutes($name, Route $route)
  176. {
  177. $routes = new RouteCollection();
  178. $routes->add($name, $route);
  179. return $routes;
  180. }
  181. }