UrlGeneratorTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /** @var RouteCollection */
  17. private $routeCollection;
  18. /** @var UrlGenerator */
  19. private $generator;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $this->routeCollection = new RouteCollection();
  24. $this->generator = new UrlGenerator($this->routeCollection);
  25. }
  26. public function testAbsoluteUrlWithPort80()
  27. {
  28. $this->routeCollection->add('test', new Route('/testing'));
  29. $this->generator->setContext(array(
  30. 'base_url'=>'/app.php',
  31. 'method'=>'GET',
  32. 'host'=>'localhost',
  33. 'port'=>80,
  34. 'is_secure'=>false));
  35. $url = $this->generator->generate('test', array(), true);
  36. $this->assertEquals('http://localhost/app.php/testing', $url);
  37. }
  38. public function testAbsoluteSecureUrlWithPort443()
  39. {
  40. $this->routeCollection->add('test', new Route('/testing'));
  41. $this->generator->setContext(array(
  42. 'base_url'=>'/app.php',
  43. 'method'=>'GET',
  44. 'host'=>'localhost',
  45. 'port'=>443,
  46. 'is_secure'=>true));
  47. $url = $this->generator->generate('test', array(), true);
  48. $this->assertEquals('https://localhost/app.php/testing', $url);
  49. }
  50. public function testAbsoluteUrlWithNonStandardPort()
  51. {
  52. $this->routeCollection->add('test', new Route('/testing'));
  53. $this->generator->setContext(array(
  54. 'base_url'=>'/app.php',
  55. 'method'=>'GET',
  56. 'host'=>'localhost',
  57. 'port'=>8080,
  58. 'is_secure'=>false));
  59. $url = $this->generator->generate('test', array(), true);
  60. $this->assertEquals('http://localhost:8080/app.php/testing', $url);
  61. }
  62. public function testAbsoluteSecureUrlWithNonStandardPort()
  63. {
  64. $this->routeCollection->add('test', new Route('/testing'));
  65. $this->generator->setContext(array(
  66. 'base_url'=>'/app.php',
  67. 'method'=>'GET',
  68. 'host'=>'localhost',
  69. 'port'=>8080,
  70. 'is_secure'=>true));
  71. $url = $this->generator->generate('test', array(), true);
  72. $this->assertEquals('https://localhost:8080/app.php/testing', $url);
  73. }
  74. public function testRelativeUrlWithoutParameters()
  75. {
  76. $this->routeCollection->add('test', new Route('/testing'));
  77. $this->generator->setContext(array(
  78. 'base_url'=>'/app.php',
  79. 'method'=>'GET',
  80. 'host'=>'localhost',
  81. 'port'=>80,
  82. 'is_secure'=>false));
  83. $url = $this->generator->generate('test', array(), false);
  84. $this->assertEquals('/app.php/testing', $url);
  85. }
  86. public function testRelativeUrlWithParameter()
  87. {
  88. $this->routeCollection->add('test', new Route('/testing/{foo}'));
  89. $this->generator->setContext(array(
  90. 'base_url'=>'/app.php',
  91. 'method'=>'GET',
  92. 'host'=>'localhost',
  93. 'port'=>80,
  94. 'is_secure'=>false));
  95. $url = $this->generator->generate('test', array('foo' => 'bar'), false);
  96. $this->assertEquals('/app.php/testing/bar', $url);
  97. }
  98. public function testRelativeUrlWithNullParameter()
  99. {
  100. $this->routeCollection->add('test', new Route('/testing.{format}', array('format' => null)));
  101. $this->generator->setContext(array(
  102. 'base_url'=>'/app.php',
  103. 'method'=>'GET',
  104. 'host'=>'localhost',
  105. 'port'=>80,
  106. 'is_secure'=>false));
  107. $url = $this->generator->generate('test', array(), false);
  108. $this->assertEquals('/app.php/testing', $url);
  109. }
  110. public function testRelativeUrlWithExtraParameters()
  111. {
  112. $this->routeCollection->add('test', new Route('/testing'));
  113. $this->generator->setContext(array(
  114. 'base_url'=>'/app.php',
  115. 'method'=>'GET',
  116. 'host'=>'localhost',
  117. 'port'=>80,
  118. 'is_secure'=>false));
  119. $url = $this->generator->generate('test', array('foo' => 'bar'), false);
  120. $this->assertEquals('/app.php/testing?foo=bar', $url);
  121. }
  122. public function testAbsoluteUrlWithExtraParameters()
  123. {
  124. $this->routeCollection->add('test', new Route('/testing'));
  125. $this->generator->setContext(array(
  126. 'base_url'=>'/app.php',
  127. 'method'=>'GET',
  128. 'host'=>'localhost',
  129. 'port'=>80,
  130. 'is_secure'=>false));
  131. $url = $this->generator->generate('test', array('foo' => 'bar'), true);
  132. $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
  133. }
  134. /**
  135. * @expectedException \InvalidArgumentException
  136. */
  137. public function testGenerateWithoutRoutes()
  138. {
  139. $this->generator->generate('test', array(), true);
  140. }
  141. /**
  142. * @expectedException \InvalidArgumentException
  143. */
  144. public function testGenerateForRouteWithoutManditoryParameter()
  145. {
  146. $this->routeCollection->add('test', new Route('/testing/{foo}'));
  147. $this->generator->generate('test', array(), true);
  148. }
  149. /**
  150. * @expectedException \InvalidArgumentException
  151. */
  152. public function testGenerateForRouteWithInvalidOptionalParameter()
  153. {
  154. $route = new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+'));
  155. $this->routeCollection->add('test', $route);
  156. $this->generator->generate('test', array('foo' => 'bar'), true);
  157. }
  158. /**
  159. * @expectedException \InvalidArgumentException
  160. */
  161. public function testGenerateForRouteWithInvalidManditoryParameter()
  162. {
  163. $route = new Route('/testing/{foo}', array(), array('foo' => 'd+'));
  164. $this->routeCollection->add('test', $route);
  165. $this->generator->generate('test', array('foo' => 'bar'), true);
  166. }
  167. }