HttpFoundationExtensionTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Bridge\Twig\Tests\Extension;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\RequestContext;
  16. class HttpFoundationExtensionTest 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. $extension = new HttpFoundationExtension($stack);
  26. $this->assertEquals($expected, $extension->generateAbsoluteUrl($path));
  27. }
  28. public function getGenerateAbsoluteUrlData()
  29. {
  30. return array(
  31. array('http://localhost/foo.png', '/foo.png', '/foo/bar.html'),
  32. array('http://localhost/foo/foo.png', 'foo.png', '/foo/bar.html'),
  33. array('http://localhost/foo/foo.png', 'foo.png', '/foo/bar'),
  34. array('http://localhost/foo/bar/foo.png', 'foo.png', '/foo/bar/'),
  35. array('http://example.com/baz', 'http://example.com/baz', '/'),
  36. array('https://example.com/baz', 'https://example.com/baz', '/'),
  37. array('//example.com/baz', '//example.com/baz', '/'),
  38. );
  39. }
  40. /**
  41. * @dataProvider getGenerateAbsoluteUrlRequestContextData
  42. */
  43. public function testGenerateAbsoluteUrlWithRequestContext($path, $baseUrl, $host, $scheme, $httpPort, $httpsPort, $expected)
  44. {
  45. if (!class_exists('Symfony\Component\Routing\RequestContext')) {
  46. $this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
  47. }
  48. $requestContext = new RequestContext($baseUrl, 'GET', $host, $scheme, $httpPort, $httpsPort, $path);
  49. $extension = new HttpFoundationExtension(new RequestStack(), $requestContext);
  50. $this->assertEquals($expected, $extension->generateAbsoluteUrl($path));
  51. }
  52. /**
  53. * @dataProvider getGenerateAbsoluteUrlRequestContextData
  54. */
  55. public function testGenerateAbsoluteUrlWithoutRequestAndRequestContext($path)
  56. {
  57. if (!class_exists('Symfony\Component\Routing\RequestContext')) {
  58. $this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
  59. }
  60. $extension = new HttpFoundationExtension(new RequestStack());
  61. $this->assertEquals($path, $extension->generateAbsoluteUrl($path));
  62. }
  63. public function getGenerateAbsoluteUrlRequestContextData()
  64. {
  65. return array(
  66. array('/foo.png', '/foo', 'localhost', 'http', 80, 443, 'http://localhost/foo.png'),
  67. array('foo.png', '/foo', 'localhost', 'http', 80, 443, 'http://localhost/foo/foo.png'),
  68. array('foo.png', '/foo/bar/', 'localhost', 'http', 80, 443, 'http://localhost/foo/bar/foo.png'),
  69. array('/foo.png', '/foo', 'localhost', 'https', 80, 443, 'https://localhost/foo.png'),
  70. array('foo.png', '/foo', 'localhost', 'https', 80, 443, 'https://localhost/foo/foo.png'),
  71. array('foo.png', '/foo/bar/', 'localhost', 'https', 80, 443, 'https://localhost/foo/bar/foo.png'),
  72. array('/foo.png', '/foo', 'localhost', 'http', 443, 80, 'http://localhost:443/foo.png'),
  73. array('/foo.png', '/foo', 'localhost', 'https', 443, 80, 'https://localhost:80/foo.png'),
  74. );
  75. }
  76. public function testGenerateAbsoluteUrlWithScriptFileName()
  77. {
  78. $request = Request::create('http://localhost/app/web/app_dev.php');
  79. $request->server->set('SCRIPT_FILENAME', '/var/www/app/web/app_dev.php');
  80. $stack = new RequestStack();
  81. $stack->push($request);
  82. $extension = new HttpFoundationExtension($stack);
  83. $this->assertEquals(
  84. 'http://localhost/app/web/bundles/framework/css/structure.css',
  85. $extension->generateAbsoluteUrl('/app/web/bundles/framework/css/structure.css')
  86. );
  87. }
  88. /**
  89. * @dataProvider getGenerateRelativePathData()
  90. */
  91. public function testGenerateRelativePath($expected, $path, $pathinfo)
  92. {
  93. if (!method_exists('Symfony\Component\HttpFoundation\Request', 'getRelativeUriForPath')) {
  94. $this->markTestSkipped('Your version of Symfony HttpFoundation is too old.');
  95. }
  96. $stack = new RequestStack();
  97. $stack->push(Request::create($pathinfo));
  98. $extension = new HttpFoundationExtension($stack);
  99. $this->assertEquals($expected, $extension->generateRelativePath($path));
  100. }
  101. public function getGenerateRelativePathData()
  102. {
  103. return array(
  104. array('../foo.png', '/foo.png', '/foo/bar.html'),
  105. array('../baz/foo.png', '/baz/foo.png', '/foo/bar.html'),
  106. array('baz/foo.png', 'baz/foo.png', '/foo/bar.html'),
  107. array('http://example.com/baz', 'http://example.com/baz', '/'),
  108. array('https://example.com/baz', 'https://example.com/baz', '/'),
  109. array('//example.com/baz', '//example.com/baz', '/'),
  110. );
  111. }
  112. }