1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asmaster\EquipTwigTests\Extension; |
4
|
|
|
|
5
|
|
|
use Zend\Diactoros\Uri; |
6
|
|
|
use Zend\Diactoros\Stream; |
7
|
|
|
use Zend\Diactoros\ServerRequest; |
8
|
|
|
use Asmaster\EquipTwig\Extension\RequestExtension; |
9
|
|
|
|
10
|
|
|
class RequestExtensionTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
public function testAddExtension() |
13
|
|
|
{ |
14
|
|
|
if (!class_exists('\Twig_Environment')) { |
15
|
|
|
$this->markTestSkipped('Twig is not installed'); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
$request = $this->getMock(ServerRequest::class); |
19
|
|
|
$extenstion = new RequestExtension($request); |
20
|
|
|
|
21
|
|
|
$loader = $this->getMock('\Twig_LoaderInterface'); |
22
|
|
|
|
23
|
|
|
$twig = new \Twig_Environment($loader); |
24
|
|
|
$twig->addExtension($extenstion); |
25
|
|
|
|
26
|
|
|
$this->assertArrayHasKey('request', $twig->getExtensions()); |
27
|
|
|
$this->assertArrayHasKey('absolute_url', $twig->getFunctions()); |
28
|
|
|
$this->assertArrayHasKey('relative_url', $twig->getFunctions()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @dataProvider getGenerateAbsoluteUrlData() |
33
|
|
|
*/ |
34
|
|
|
public function testGenerateAbsoluteUrl($path, $url, $expected) |
35
|
|
|
{ |
36
|
|
|
$request = $this->createRequest($url); |
37
|
|
|
|
38
|
|
|
$extension = new RequestExtension($request); |
39
|
|
|
$absoluteUrl = $extension->generateAbsoluteUrl($path); |
40
|
|
|
|
41
|
|
|
$this->assertEquals($expected, $absoluteUrl); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getGenerateAbsoluteUrlData() |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
['/foo.png', '', '/foo.png'], |
48
|
|
|
['/foo.png', 'http://localhost/foo', 'http://localhost/foo.png'], |
49
|
|
|
['foo.png', 'http://localhost/foo', 'http://localhost/foo/foo.png'], |
50
|
|
|
['foo.png', 'http://localhost/bar', 'http://localhost/bar/foo.png'], |
51
|
|
|
['foo.png', 'http://localhost/foo/bar/', 'http://localhost/foo/bar/foo.png'], |
52
|
|
|
['/foo.png', 'http://localhost:80', 'http://localhost/foo.png'], |
53
|
|
|
['/foo.png', 'http://localhost:8080', 'http://localhost:8080/foo.png'], |
54
|
|
|
['/foo.png', 'https://localhost:443', 'https://localhost/foo.png'], |
55
|
|
|
['/', 'http://localhost', 'http://localhost/'], |
56
|
|
|
['//', 'http://localhost', '//'] |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @dataProvider getGenerateRelativeUrlData() |
62
|
|
|
*/ |
63
|
|
|
public function testGenerateRelativeUrl($path, $url, $expected) |
64
|
|
|
{ |
65
|
|
|
$request = $this->createRequest($url); |
66
|
|
|
|
67
|
|
|
$extension = new RequestExtension($request); |
68
|
|
|
$relativeUrl = $extension->generateRelativeUrl($path); |
69
|
|
|
|
70
|
|
|
$this->assertEquals($expected, $relativeUrl); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getGenerateRelativeUrlData() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
['/a/b/c/foo.png', 'http://localhost/a/b/c/d', 'foo.png'], |
77
|
|
|
['/a/b/foo.png', 'http://localhost/a/b/c/d', '../foo.png'], |
78
|
|
|
['/a/b/c/d', 'http://localhost/a/b/c/d', ''], |
79
|
|
|
['/a/b/c/', 'http://localhost/a/b/c/d', './'], |
80
|
|
|
['/a/b/c/other', 'http://localhost/a/b/c/d', 'other'], |
81
|
|
|
['/a/b/z/foo.png', 'http://localhost/a/b/c/d', '../z/foo.png'], |
82
|
|
|
['/a/b/c/this:that', 'http://localhost/a/b/c/d', './this:that'], |
83
|
|
|
['/a/b/c/foo/this:that', 'http://localhost/a/b/c/d', 'foo/this:that'], |
84
|
|
|
['/', 'http://localhost', '/'], |
85
|
|
|
['//', 'http://localhost', '//'] |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $url |
91
|
|
|
* |
92
|
|
|
* @return ServerRequest $request |
93
|
|
|
*/ |
94
|
|
|
public function createRequest($url) |
95
|
|
|
{ |
96
|
|
|
$uri = new Uri($url); |
97
|
|
|
$request = new ServerRequest( |
98
|
|
|
$server = [], |
99
|
|
|
$files = [], |
100
|
|
|
$uri = $uri, |
101
|
|
|
$method = 'GET', |
102
|
|
|
$body = 'php://input', |
103
|
|
|
$headers = [] |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
return $request; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|