UrlGeneratorTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 testRelativeUrlWithExtraParameters()
  66. {
  67. $routes = $this->getRoutes('test', new Route('/testing'));
  68. $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
  69. $this->assertEquals('/app.php/testing?foo=bar', $url);
  70. }
  71. public function testAbsoluteUrlWithExtraParameters()
  72. {
  73. $routes = $this->getRoutes('test', new Route('/testing'));
  74. $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  75. $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
  76. }
  77. public function testUrlWithExtraParametersFromGlobals()
  78. {
  79. $routes = $this->getRoutes('test', new Route('/testing'));
  80. $generator = $this->getGenerator($routes);
  81. $context = new RequestContext('/app.php');
  82. $context->setParameter('bar', 'bar');
  83. $generator->setContext($context);
  84. $url = $generator->generate('test', array('foo' => 'bar'));
  85. $this->assertEquals('/app.php/testing?foo=bar', $url);
  86. }
  87. public function testUrlWithGlobalParameter()
  88. {
  89. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  90. $generator = $this->getGenerator($routes);
  91. $context = new RequestContext('/app.php');
  92. $context->setParameter('foo', 'bar');
  93. $generator->setContext($context);
  94. $url = $generator->generate('test', array());
  95. $this->assertEquals('/app.php/testing/bar', $url);
  96. }
  97. /**
  98. * @expectedException \InvalidArgumentException
  99. */
  100. public function testGenerateWithoutRoutes()
  101. {
  102. $routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
  103. $this->getGenerator($routes)->generate('test', array(), true);
  104. }
  105. /**
  106. * @expectedException \InvalidArgumentException
  107. */
  108. public function testGenerateForRouteWithoutManditoryParameter()
  109. {
  110. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  111. $this->getGenerator($routes)->generate('test', array(), true);
  112. }
  113. /**
  114. * @expectedException \InvalidArgumentException
  115. */
  116. public function testGenerateForRouteWithInvalidOptionalParameter()
  117. {
  118. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
  119. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  120. }
  121. /**
  122. * @expectedException \InvalidArgumentException
  123. */
  124. public function testGenerateForRouteWithInvalidManditoryParameter()
  125. {
  126. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
  127. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  128. }
  129. public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
  130. {
  131. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
  132. $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test'));
  133. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
  134. $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  135. }
  136. public function testSchemeRequirementForcesAbsoluteUrl()
  137. {
  138. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
  139. $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
  140. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
  141. $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  142. }
  143. protected function getGenerator(RouteCollection $routes, array $parameters = array())
  144. {
  145. $context = new RequestContext('/app.php');
  146. foreach ($parameters as $key => $value) {
  147. $method = 'set'.$key;
  148. $context->$method($value);
  149. }
  150. $generator = new UrlGenerator($routes, $context);
  151. return $generator;
  152. }
  153. protected function getRoutes($name, Route $route)
  154. {
  155. $routes = new RouteCollection();
  156. $routes->add($name, $route);
  157. return $routes;
  158. }
  159. }