UrlGeneratorTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 testUrlWithExtraParametersFromGlobals()
  84. {
  85. $routes = $this->getRoutes('test', new Route('/testing'));
  86. $generator = $this->getGenerator($routes);
  87. $context = new RequestContext('/app.php');
  88. $context->setParameter('bar', 'bar');
  89. $generator->setContext($context);
  90. $url = $generator->generate('test', array('foo' => 'bar'));
  91. $this->assertEquals('/app.php/testing?foo=bar', $url);
  92. }
  93. public function testUrlWithGlobalParameter()
  94. {
  95. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  96. $generator = $this->getGenerator($routes);
  97. $context = new RequestContext('/app.php');
  98. $context->setParameter('foo', 'bar');
  99. $generator->setContext($context);
  100. $url = $generator->generate('test', array());
  101. $this->assertEquals('/app.php/testing/bar', $url);
  102. }
  103. /**
  104. * @expectedException Symfony\Component\Routing\Exception\RouteNotFoundException
  105. */
  106. public function testGenerateWithoutRoutes()
  107. {
  108. $routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
  109. $this->getGenerator($routes)->generate('test', array(), true);
  110. }
  111. /**
  112. * @expectedException Symfony\Component\Routing\Exception\MissingMandatoryParametersException
  113. */
  114. public function testGenerateForRouteWithoutMandatoryParameter()
  115. {
  116. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  117. $this->getGenerator($routes)->generate('test', array(), true);
  118. }
  119. /**
  120. * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
  121. */
  122. public function testGenerateForRouteWithInvalidOptionalParameter()
  123. {
  124. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
  125. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  126. }
  127. /**
  128. * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
  129. */
  130. public function testGenerateForRouteWithInvalidManditoryParameter()
  131. {
  132. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
  133. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  134. }
  135. public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
  136. {
  137. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
  138. $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test'));
  139. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
  140. $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  141. }
  142. public function testSchemeRequirementForcesAbsoluteUrl()
  143. {
  144. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
  145. $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
  146. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
  147. $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  148. }
  149. public function testNoTrailingSlashForMultipleOptionalParameters()
  150. {
  151. $routes = $this->getRoutes('test', new Route('/category/{slug1}/{slug2}/{slug3}', array('slug2' => null, 'slug3' => null)));
  152. $this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', array('slug1' => 'foo')));
  153. }
  154. protected function getGenerator(RouteCollection $routes, array $parameters = array())
  155. {
  156. $context = new RequestContext('/app.php');
  157. foreach ($parameters as $key => $value) {
  158. $method = 'set'.$key;
  159. $context->$method($value);
  160. }
  161. $generator = new UrlGenerator($routes, $context);
  162. return $generator;
  163. }
  164. protected function getRoutes($name, Route $route)
  165. {
  166. $routes = new RouteCollection();
  167. $routes->add($name, $route);
  168. return $routes;
  169. }
  170. }