1
|
|
|
<?php |
2
|
|
|
namespace Slince\Routing\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Slince\Routing\Exception\InvalidArgumentException; |
6
|
|
|
use Slince\Routing\Generator; |
7
|
|
|
use Slince\Routing\Route; |
8
|
|
|
use Zend\Diactoros\ServerRequest; |
9
|
|
|
|
10
|
|
|
class GeneratorTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
protected $request; |
13
|
|
|
|
14
|
|
|
public function setUp() |
15
|
|
|
{ |
16
|
|
|
$this->request = new ServerRequest([], [], 'http://localhost/foo-bar'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testAbsoluteUrlWithPort80() |
20
|
|
|
{ |
21
|
|
|
$request = new ServerRequest([], [], 'http://localhost/foo-bar'); |
22
|
|
|
$route = new Route('/foo', 'action'); |
23
|
|
|
$generator = new Generator($request); |
24
|
|
|
$this->assertEquals('http://localhost/foo', $generator->generate($route, [], true)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testAbsoluteSecureUrlWithPort443() |
28
|
|
|
{ |
29
|
|
|
$request = new ServerRequest([], [], 'https://localhost/foo-bar'); |
30
|
|
|
$route = new Route('/foo', 'action'); |
31
|
|
|
$generator = new Generator($request); |
32
|
|
|
$this->assertEquals('https://localhost/foo', $generator->generate($route, [], true)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testAbsoluteUrlWithNonStandardPort() |
36
|
|
|
{ |
37
|
|
|
$request = new ServerRequest([], [], 'http://localhost:8080/foo-bar'); |
38
|
|
|
$route = new Route('/foo', 'action'); |
39
|
|
|
$generator = new Generator($request); |
40
|
|
|
$this->assertEquals('http://localhost:8080/foo', $generator->generate($route, [], true)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testAbsoluteSecureUrlWithNonStandardPort() |
44
|
|
|
{ |
45
|
|
|
$request = new ServerRequest([], [], 'https://localhost:8080/foo-bar'); |
46
|
|
|
$route = new Route('/foo', 'action'); |
47
|
|
|
$generator = new Generator($request); |
48
|
|
|
$this->assertEquals('https://localhost:8080/foo', $generator->generate($route, [], true)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testRelativeUrlWithoutParameters() |
52
|
|
|
{ |
53
|
|
|
$route = new Route('/foo', 'action'); |
54
|
|
|
$generator = new Generator(); |
55
|
|
|
$this->assertEquals('/foo', $generator->generate($route, [])); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testRelativeUrlWithParameter() |
59
|
|
|
{ |
60
|
|
|
$route = new Route('/foo/{bar}', 'action'); |
61
|
|
|
$generator = new Generator(); |
62
|
|
|
$this->assertEquals('/foo/hello', $generator->generate($route, ['bar' => 'hello'])); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testRelativeUrlWithNullParameter() |
66
|
|
|
{ |
67
|
|
|
$route = new Route('/foo.{format}', 'action'); |
68
|
|
|
$route->setDefault('format', null); |
69
|
|
|
$generator = new Generator(); |
70
|
|
|
$this->assertEquals('/foo', $generator->generate($route)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
public function testNotPassedOptionalParameterInBetween() |
75
|
|
|
{ |
76
|
|
|
$route = new Route('/foo/{page}', 'action'); |
77
|
|
|
$route->setDefault('page', 1); |
78
|
|
|
$generator = new Generator(); |
79
|
|
|
$this->assertEquals('/foo', $generator->generate($route)); |
80
|
|
|
$this->assertEquals('/foo', $generator->generate($route, ['page' => 1])); |
81
|
|
|
$this->assertEquals('/foo/0', $generator->generate($route, ['page' => 0])); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGenerateUrlWithExtraParameters() |
85
|
|
|
{ |
86
|
|
|
$route = new Route('/foo/{page}', 'action'); |
87
|
|
|
$route->setDefault('page', 1); |
88
|
|
|
$generator = new Generator(); |
89
|
|
|
$this->assertEquals('/foo/2?foo=bar&bar=baz', $generator->generate($route, [ |
90
|
|
|
'page' => 2, |
91
|
|
|
'foo' => 'bar', |
92
|
|
|
'bar' => 'baz' |
93
|
|
|
])); |
94
|
|
|
$this->assertEquals('/foo', $generator->generate($route, [ |
95
|
|
|
'page' => 1, |
96
|
|
|
'foo' => null, |
97
|
|
|
])); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
public function testCustomHasHigherPriorityThanDefault() |
102
|
|
|
{ |
103
|
|
|
$route = new Route('/foo/{page}', 'action'); |
104
|
|
|
$route->setDefault('page', 1); |
105
|
|
|
$generator = new Generator(); |
106
|
|
|
$this->assertEquals('/foo/2', $generator->generate($route, [ |
107
|
|
|
'page' => 2, |
108
|
|
|
])); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
public function testGenerateForRouteWithInvalidParameter() |
113
|
|
|
{ |
114
|
|
|
$route = new Route('/foo/{page}', 'action'); |
115
|
|
|
$route->setRequirement('page', '\d+'); |
116
|
|
|
$generator = new Generator(); |
117
|
|
|
$this->expectException(InvalidArgumentException::class); |
118
|
|
|
$generator->generate($route, [ |
119
|
|
|
'page' => 'non_number', |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testRequiredParamAndEmptyPassed() |
124
|
|
|
{ |
125
|
|
|
$this->expectException(InvalidArgumentException::class); |
126
|
|
|
$route = new Route('/foo/{page}', 'action'); |
127
|
|
|
$route->setRequirement('page', '\d+'); |
128
|
|
|
$generator = new Generator(); |
129
|
|
|
$this->expectException(InvalidArgumentException::class); |
130
|
|
|
$generator->generate($route, [ |
131
|
|
|
'page' => '' |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testSchemeRequirementForcesAbsoluteUrl() |
136
|
|
|
{ |
137
|
|
|
$route = new Route('/foo', 'action'); |
138
|
|
|
$route->setSchemes(['https']); |
139
|
|
|
$request = new ServerRequest([], [], 'http://localhost/foo-bar'); |
140
|
|
|
$generator = new Generator($request); |
141
|
|
|
$this->assertEquals('https://localhost/foo', $generator->generate($route, [], true)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testSchemeRequirementCreatesUrlFoCurrentRequiredScheme() |
145
|
|
|
{ |
146
|
|
|
$route = new Route('/foo', 'action'); |
147
|
|
|
$route->setSchemes(['https', 'http']); |
148
|
|
|
$request = new ServerRequest([], [], 'http://localhost/foo-bar'); |
149
|
|
|
$generator = new Generator($request); |
150
|
|
|
$this->assertEquals('http://localhost/foo', $generator->generate($route, [], true)); |
151
|
|
|
} |
152
|
|
|
} |