UrlHelperTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Component\HttpFoundation\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\UrlHelper;
  15. use Symfony\Component\Routing\RequestContext;
  16. use Symfony\Component\Routing\RequestContextAwareInterface;
  17. class UrlHelperTest extends TestCase
  18. {
  19. /**
  20. * @dataProvider getGenerateAbsoluteUrlData
  21. */
  22. public function testGenerateAbsoluteUrl($expected, $path, $pathinfo)
  23. {
  24. $stack = new RequestStack();
  25. $stack->push(Request::create($pathinfo));
  26. $helper = new UrlHelper($stack);
  27. $this->assertEquals($expected, $helper->getAbsoluteUrl($path));
  28. }
  29. public static function getGenerateAbsoluteUrlData()
  30. {
  31. return [
  32. ['http://localhost/foo.png', '/foo.png', '/foo/bar.html'],
  33. ['http://localhost/foo/foo.png', 'foo.png', '/foo/bar.html'],
  34. ['http://localhost/foo/foo.png', 'foo.png', '/foo/bar'],
  35. ['http://localhost/foo/bar/foo.png', 'foo.png', '/foo/bar/'],
  36. ['http://example.com/baz', 'http://example.com/baz', '/'],
  37. ['https://example.com/baz', 'https://example.com/baz', '/'],
  38. ['//example.com/baz', '//example.com/baz', '/'],
  39. ['http://localhost/foo/bar?baz', '?baz', '/foo/bar'],
  40. ['http://localhost/foo/bar?baz=1', '?baz=1', '/foo/bar?foo=1'],
  41. ['http://localhost/foo/baz?baz=1', 'baz?baz=1', '/foo/bar?foo=1'],
  42. ['http://localhost/foo/bar#baz', '#baz', '/foo/bar'],
  43. ['http://localhost/foo/bar?0#baz', '#baz', '/foo/bar?0'],
  44. ['http://localhost/foo/bar?baz=1#baz', '?baz=1#baz', '/foo/bar?foo=1'],
  45. ['http://localhost/foo/baz?baz=1#baz', 'baz?baz=1#baz', '/foo/bar?foo=1'],
  46. ];
  47. }
  48. /**
  49. * @dataProvider getGenerateAbsoluteUrlRequestContextData
  50. */
  51. public function testGenerateAbsoluteUrlWithRequestContext($path, $baseUrl, $host, $scheme, $httpPort, $httpsPort, $expected)
  52. {
  53. if (!class_exists(RequestContext::class)) {
  54. $this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
  55. }
  56. $requestContext = new RequestContext($baseUrl, 'GET', $host, $scheme, $httpPort, $httpsPort, $path);
  57. $helper = new UrlHelper(new RequestStack(), $requestContext);
  58. $this->assertEquals($expected, $helper->getAbsoluteUrl($path));
  59. }
  60. /**
  61. * @dataProvider getGenerateAbsoluteUrlRequestContextData
  62. */
  63. public function testGenerateAbsoluteUrlWithRequestContextAwareInterface($path, $baseUrl, $host, $scheme, $httpPort, $httpsPort, $expected)
  64. {
  65. if (!class_exists(RequestContext::class)) {
  66. $this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
  67. }
  68. $requestContext = new RequestContext($baseUrl, 'GET', $host, $scheme, $httpPort, $httpsPort, $path);
  69. $contextAware = new class($requestContext) implements RequestContextAwareInterface {
  70. private $requestContext;
  71. public function __construct($requestContext)
  72. {
  73. $this->requestContext = $requestContext;
  74. }
  75. public function setContext(RequestContext $context)
  76. {
  77. $this->requestContext = $context;
  78. }
  79. public function getContext()
  80. {
  81. return $this->requestContext;
  82. }
  83. };
  84. $helper = new UrlHelper(new RequestStack(), $contextAware);
  85. $this->assertEquals($expected, $helper->getAbsoluteUrl($path));
  86. }
  87. /**
  88. * @dataProvider getGenerateAbsoluteUrlRequestContextData
  89. */
  90. public function testGenerateAbsoluteUrlWithoutRequestAndRequestContext($path)
  91. {
  92. if (!class_exists(RequestContext::class)) {
  93. $this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
  94. }
  95. $helper = new UrlHelper(new RequestStack());
  96. $this->assertEquals($path, $helper->getAbsoluteUrl($path));
  97. }
  98. public static function getGenerateAbsoluteUrlRequestContextData()
  99. {
  100. return [
  101. ['/foo.png', '/foo', 'localhost', 'http', 80, 443, 'http://localhost/foo.png'],
  102. ['foo.png', '/foo', 'localhost', 'http', 80, 443, 'http://localhost/foo/foo.png'],
  103. ['foo.png', '/foo/bar/', 'localhost', 'http', 80, 443, 'http://localhost/foo/bar/foo.png'],
  104. ['/foo.png', '/foo', 'localhost', 'https', 80, 443, 'https://localhost/foo.png'],
  105. ['foo.png', '/foo', 'localhost', 'https', 80, 443, 'https://localhost/foo/foo.png'],
  106. ['foo.png', '/foo/bar/', 'localhost', 'https', 80, 443, 'https://localhost/foo/bar/foo.png'],
  107. ['/foo.png', '/foo', 'localhost', 'http', 443, 80, 'http://localhost:443/foo.png'],
  108. ['/foo.png', '/foo', 'localhost', 'https', 443, 80, 'https://localhost:80/foo.png'],
  109. ];
  110. }
  111. public function testGenerateAbsoluteUrlWithScriptFileName()
  112. {
  113. $request = Request::create('http://localhost/app/web/app_dev.php');
  114. $request->server->set('SCRIPT_FILENAME', '/var/www/app/web/app_dev.php');
  115. $stack = new RequestStack();
  116. $stack->push($request);
  117. $helper = new UrlHelper($stack);
  118. $this->assertEquals(
  119. 'http://localhost/app/web/bundles/framework/css/structure.css',
  120. $helper->getAbsoluteUrl('/app/web/bundles/framework/css/structure.css')
  121. );
  122. }
  123. /**
  124. * @dataProvider getGenerateRelativePathData
  125. */
  126. public function testGenerateRelativePath($expected, $path, $pathinfo)
  127. {
  128. if (!method_exists(Request::class, 'getRelativeUriForPath')) {
  129. $this->markTestSkipped('Your version of Symfony HttpFoundation is too old.');
  130. }
  131. $stack = new RequestStack();
  132. $stack->push(Request::create($pathinfo));
  133. $urlHelper = new UrlHelper($stack);
  134. $this->assertEquals($expected, $urlHelper->getRelativePath($path));
  135. }
  136. public static function getGenerateRelativePathData()
  137. {
  138. return [
  139. ['../foo.png', '/foo.png', '/foo/bar.html'],
  140. ['../baz/foo.png', '/baz/foo.png', '/foo/bar.html'],
  141. ['baz/foo.png', 'baz/foo.png', '/foo/bar.html'],
  142. ['http://example.com/baz', 'http://example.com/baz', '/'],
  143. ['https://example.com/baz', 'https://example.com/baz', '/'],
  144. ['//example.com/baz', '//example.com/baz', '/'],
  145. ];
  146. }
  147. }