Dashboard sipadu mbip
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UrlHelperTest.php 5.5KB

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