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