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