UrlGeneratorTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testAbsoluteUrlWithPort80()
  17. {
  18. $routes = $this->getRoutes('test', new Route('/testing'));
  19. $url = $this->getGenerator($routes)->generate('test', array(), true);
  20. $this->assertEquals('http://localhost/app.php/testing', $url);
  21. }
  22. public function testAbsoluteSecureUrlWithPort443()
  23. {
  24. $routes = $this->getRoutes('test', new Route('/testing'));
  25. $url = $this->getGenerator($routes, array('port' => 443, 'is_secure' => true))->generate('test', array(), true);
  26. $this->assertEquals('https://localhost/app.php/testing', $url);
  27. }
  28. public function testAbsoluteUrlWithNonStandardPort()
  29. {
  30. $routes = $this->getRoutes('test', new Route('/testing'));
  31. $url = $this->getGenerator($routes, array('port' => 8080))->generate('test', array(), true);
  32. $this->assertEquals('http://localhost:8080/app.php/testing', $url);
  33. }
  34. public function testAbsoluteSecureUrlWithNonStandardPort()
  35. {
  36. $routes = $this->getRoutes('test', new Route('/testing'));
  37. $url = $this->getGenerator($routes, array('port' => 8080, 'is_secure' => true))->generate('test', array(), true);
  38. $this->assertEquals('https://localhost:8080/app.php/testing', $url);
  39. }
  40. public function testRelativeUrlWithoutParameters()
  41. {
  42. $routes = $this->getRoutes('test', new Route('/testing'));
  43. $url = $this->getGenerator($routes)->generate('test', array(), false);
  44. $this->assertEquals('/app.php/testing', $url);
  45. }
  46. public function testRelativeUrlWithParameter()
  47. {
  48. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  49. $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
  50. $this->assertEquals('/app.php/testing/bar', $url);
  51. }
  52. public function testRelativeUrlWithNullParameter()
  53. {
  54. $routes = $this->getRoutes('test', new Route('/testing.{format}', array('format' => null)));
  55. $url = $this->getGenerator($routes)->generate('test', array(), false);
  56. $this->assertEquals('/app.php/testing', $url);
  57. }
  58. public function testRelativeUrlWithExtraParameters()
  59. {
  60. $routes = $this->getRoutes('test', new Route('/testing'));
  61. $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
  62. $this->assertEquals('/app.php/testing?foo=bar', $url);
  63. }
  64. public function testAbsoluteUrlWithExtraParameters()
  65. {
  66. $routes = $this->getRoutes('test', new Route('/testing'));
  67. $url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  68. $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
  69. }
  70. /**
  71. * @expectedException \InvalidArgumentException
  72. */
  73. public function testGenerateWithoutRoutes()
  74. {
  75. $routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
  76. $this->getGenerator($routes)->generate('test', array(), true);
  77. }
  78. /**
  79. * @expectedException \InvalidArgumentException
  80. */
  81. public function testGenerateForRouteWithoutManditoryParameter()
  82. {
  83. $routes = $this->getRoutes('test', new Route('/testing/{foo}'));
  84. $this->getGenerator($routes)->generate('test', array(), true);
  85. }
  86. /**
  87. * @expectedException \InvalidArgumentException
  88. */
  89. public function testGenerateForRouteWithInvalidOptionalParameter()
  90. {
  91. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
  92. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  93. }
  94. /**
  95. * @expectedException \InvalidArgumentException
  96. */
  97. public function testGenerateForRouteWithInvalidManditoryParameter()
  98. {
  99. $routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
  100. $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
  101. }
  102. protected function getGenerator(RouteCollection $routes, array $context = array())
  103. {
  104. $generator = new UrlGenerator($routes);
  105. $generator->setContext(array_replace(array(
  106. 'base_url' => '/app.php',
  107. 'method' => 'GET',
  108. 'host' => 'localhost',
  109. 'port' => 80,
  110. 'is_secure' => false,
  111. ), $context));
  112. return $generator;
  113. }
  114. protected function getRoutes($name, Route $route)
  115. {
  116. $routes = new RouteCollection();
  117. $routes->add($name, $route);
  118. return $routes;
  119. }
  120. }