UrlGeneratorTest.php 6.8 KB

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