1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asmaster\TwigExtension\tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Twig_ExtensionInterface; |
9
|
|
|
use Asmaster\TwigExtension\Psr7UriExtension; |
10
|
|
|
|
11
|
|
|
class Psr7UriExtensionTest extends PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ServerRequestInterface|\PHPUnit_Framework_MockObject_MockObject |
15
|
|
|
*/ |
16
|
|
|
protected $request; |
17
|
|
|
|
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
$this->request = $this->getMockBuilder(ServerRequestInterface::class) |
21
|
|
|
->disableOriginalConstructor() |
22
|
|
|
->getMock(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testExtension() |
26
|
|
|
{ |
27
|
|
|
$extension = new Psr7UriExtension($this->request); |
28
|
|
|
|
29
|
|
|
$this->assertSame('psr7_uri', $extension->getName()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider getFunctions() |
34
|
|
|
*/ |
35
|
|
|
public function testExtensionFunctions($function) |
36
|
|
|
{ |
37
|
|
|
$extension = new Psr7UriExtension($this->request); |
38
|
|
|
|
39
|
|
|
$functions = array_map( |
40
|
|
|
function ($function) { |
41
|
|
|
return $function->getName(); |
42
|
|
|
}, |
43
|
|
|
$extension->getFunctions() |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$this->assertContains($function, $functions); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getFunctions() |
50
|
|
|
{ |
51
|
|
|
return [[ |
52
|
|
|
'absolute_url', |
53
|
|
|
'relative_url' |
54
|
|
|
]]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @dataProvider getGenerateAbsoluteUrlData() |
59
|
|
|
*/ |
60
|
|
|
public function testGenerateAbsoluteUrl($path, $expected, $scheme, $host, $port, $basePath) |
61
|
|
|
{ |
62
|
|
|
$request = $this->request |
|
|
|
|
63
|
|
|
->expects($this->any()) |
64
|
|
|
->method('getUri') |
65
|
|
|
->willReturn($this->createUri($scheme, $host, $port, $basePath)); |
66
|
|
|
|
67
|
|
|
$extension = new Psr7UriExtension($this->request); |
68
|
|
|
$absoluteUrl = $extension->generateAbsoluteUrl($path); |
69
|
|
|
|
70
|
|
|
$this->assertEquals($expected, $absoluteUrl); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getGenerateAbsoluteUrlData() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
['/foo.png', '/foo.png', 'http', '', null, '/foo.png'], |
77
|
|
|
['/foo.png', 'http://localhost/foo.png', 'http', 'localhost', null, '/'], |
78
|
|
|
['/foo.png', 'http://localhost/foo.png', 'http', 'localhost', null, '/foo.png'], |
79
|
|
|
['foo.png', 'http://localhost/foo/foo.png', 'http', 'localhost', null, '/foo'], |
80
|
|
|
['foo.png', 'http://localhost/bar/foo.png', 'http', 'localhost', null, '/bar'], |
81
|
|
|
['foo.png', 'http://localhost/foo/bar/foo.png', 'http', 'localhost', null, '/foo/bar'], |
82
|
|
|
['/foo.png', 'http://localhost:8080/foo.png', 'http', 'localhost', 8080, '/'], |
83
|
|
|
['/foo.png', 'https://localhost/foo.png', 'https', 'localhost', null, '/'], |
84
|
|
|
['/', 'http://localhost/', 'http', 'localhost', null, '/'], |
85
|
|
|
['//', '//', 'http', 'localhost', null, '/'] |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @dataProvider getGenerateRelativeUrlData() |
91
|
|
|
*/ |
92
|
|
|
public function testGenerateRelativeUrl($path, $expected, $scheme, $host, $port, $basePath) |
93
|
|
|
{ |
94
|
|
|
$request = $this->request |
|
|
|
|
95
|
|
|
->expects($this->any()) |
96
|
|
|
->method('getUri') |
97
|
|
|
->willReturn($this->createUri($scheme, $host, $port, $basePath)); |
98
|
|
|
|
99
|
|
|
$extension = new Psr7UriExtension($this->request); |
100
|
|
|
$relativeUrl = $extension->generateRelativeUrl($path); |
101
|
|
|
|
102
|
|
|
$this->assertEquals($expected, $relativeUrl); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getGenerateRelativeUrlData() |
106
|
|
|
{ |
107
|
|
|
return [ |
108
|
|
|
['/a/b/c/foo.png', 'foo.png', 'http', 'localhost', null, '/a/b/c/d'], |
109
|
|
|
['/a/b/foo.png', '../foo.png', 'http', 'localhost', null, '/a/b/c/d'], |
110
|
|
|
['/a/b/c/d', '', 'http', 'localhost', null, '/a/b/c/d'], |
111
|
|
|
['/a/b/c/', './', 'http', 'localhost', null, '/a/b/c/d'], |
112
|
|
|
['/a/b/c/other', 'other', 'http', 'localhost', null, '/a/b/c/d'], |
113
|
|
|
['/a/b/z/foo.png', '../z/foo.png', 'http', 'localhost', null, '/a/b/c/d'], |
114
|
|
|
['/a/b/c/this:that', './this:that', 'http', 'localhost', null, '/a/b/c/d'], |
115
|
|
|
['/a/b/c/foo/this:that', 'foo/this:that', 'http', 'localhost', null, '/a/b/c/d'], |
116
|
|
|
['/', '/', 'http', 'localhost', null, ''], |
117
|
|
|
['//', '//', 'http', 'localhost', null, ''] |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $scheme |
123
|
|
|
* @param string $host |
124
|
|
|
* @param string $port |
125
|
|
|
* @param string $basePath |
126
|
|
|
* |
127
|
|
|
* @return UriInterface $uri New request URI to use. |
128
|
|
|
*/ |
129
|
|
|
protected function createUri($scheme, $host, $port, $basePath) |
130
|
|
|
{ |
131
|
|
|
$uri = $this->getMockBuilder(UriInterface::class) |
132
|
|
|
->disableOriginalConstructor() |
133
|
|
|
->getMock(); |
134
|
|
|
|
135
|
|
|
$uri->expects($this->any())->method('getScheme')->willReturn($scheme); |
136
|
|
|
$uri->expects($this->any())->method('getHost')->willReturn($host); |
137
|
|
|
$uri->expects($this->any())->method('getPort')->willReturn($port); |
138
|
|
|
$uri->expects($this->any())->method('getPath')->willReturn($basePath); |
139
|
|
|
|
140
|
|
|
return $uri; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: