UrlGeneratorTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  72. * @expectedException \InvalidArgumentException
  73. */
  74. public function testGenerateWithoutRoutes()
  75. {
  76. $routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
  77. $this->getGenerator($routes)->generate('test', array(), true);
  78. }
  79. /**
  80. * @expectedException \InvalidArgumentException
  81. */
  82. public function testGenerateForRouteWithoutManditoryParameter()
  83. {
  84. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  85. $this->getGenerator($routes)->generate('test', array(), true);
  86. }
  87. /**
  88. * @expectedException \InvalidArgumentException
  89. */
  90. public function testGenerateForRouteWithInvalidOptionalParameter()
  91. {
  92. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
  93. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  94. }
  95. /**
  96. * @expectedException \InvalidArgumentException
  97. */
  98. public function testGenerateForRouteWithInvalidManditoryParameter()
  99. {
  100. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
  101. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  102. }
  103. public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
  104. {
  105. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
  106. $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test'));
  107. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
  108. $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  109. }
  110. public function testSchemeRequirementForcesAbsoluteUrl()
  111. {
  112. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
  113. $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
  114. $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
  115. $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
  116. }
  117. protected function getGenerator(RouteCollection $routes, array $parameters = array())
  118. {
  119. $context = new RequestContext('/app.php');
  120. foreach ($parameters as $key => $value) {
  121. $method = 'set'.$key;
  122. $context->$method($value);
  123. }
  124. $generator = new UrlGenerator($routes, $context);
  125. return $generator;
  126. }
  127. protected function getRoutes($name, Route $route)
  128. {
  129. $routes = new RouteCollection();
  130. $routes->add($name, $route);
  131. return $routes;
  132. }
  133. }