1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Sunrise\Http\Router\Tests; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Import classes |
7
|
|
|
*/ |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Sunrise\Http\Router\Route; |
10
|
|
|
use Sunrise\Http\Router\RouteInterface; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* RouteTest |
15
|
|
|
*/ |
16
|
|
|
class RouteTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function testConstructor() : void |
23
|
|
|
{ |
24
|
|
|
$routeName = Fixture\TestRoute::getTestRouteName(); |
25
|
|
|
$routePath = Fixture\TestRoute::getTestRoutePath(); |
26
|
|
|
$routeMethods = Fixture\TestRoute::getTestRouteMethods(); |
27
|
|
|
$routeRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler(); |
28
|
|
|
|
29
|
|
|
$route = new Route( |
30
|
|
|
$routeName, |
31
|
|
|
$routePath, |
32
|
|
|
$routeMethods, |
33
|
|
|
$routeRequestHandler |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
37
|
|
|
$this->assertSame($routeName, $route->getName()); |
38
|
|
|
$this->assertSame($routePath, $route->getPath()); |
39
|
|
|
$this->assertSame($routeMethods, $route->getMethods()); |
40
|
|
|
$this->assertSame($routeRequestHandler, $route->getRequestHandler()); |
41
|
|
|
$this->assertSame([], $route->getMiddlewares()); |
42
|
|
|
$this->assertSame([], $route->getAttributes()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function testConstructorWithOptionalParams() : void |
49
|
|
|
{ |
50
|
|
|
$routeName = Fixture\TestRoute::getTestRouteName(); |
51
|
|
|
$routePath = Fixture\TestRoute::getTestRoutePath(); |
52
|
|
|
$routeMethods = Fixture\TestRoute::getTestRouteMethods(); |
53
|
|
|
$routeRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler(); |
54
|
|
|
$routeMiddlewares = Fixture\TestRoute::getTestRouteMiddlewares(); |
55
|
|
|
$routeAttributes = Fixture\TestRoute::getTestRouteAttributes(); |
56
|
|
|
|
57
|
|
|
$route = new Route( |
58
|
|
|
$routeName, |
59
|
|
|
$routePath, |
60
|
|
|
$routeMethods, |
61
|
|
|
$routeRequestHandler, |
62
|
|
|
$routeMiddlewares, |
63
|
|
|
$routeAttributes |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->assertSame($routeName, $route->getName()); |
67
|
|
|
$this->assertSame($routePath, $route->getPath()); |
68
|
|
|
$this->assertSame($routeMethods, $route->getMethods()); |
69
|
|
|
$this->assertSame($routeRequestHandler, $route->getRequestHandler()); |
70
|
|
|
$this->assertSame($routeMiddlewares, $route->getMiddlewares()); |
71
|
|
|
$this->assertSame($routeAttributes, $route->getAttributes()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function testSetName() : void |
78
|
|
|
{ |
79
|
|
|
$route = new Fixture\TestRoute(); |
80
|
|
|
$newRouteName = Fixture\TestRoute::getTestRouteName(); |
81
|
|
|
|
82
|
|
|
$this->assertNotSame($route->getName(), $newRouteName); |
83
|
|
|
$this->assertSame($route, $route->setName($newRouteName)); |
84
|
|
|
$this->assertSame($newRouteName, $route->getName()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function testSetPath() : void |
91
|
|
|
{ |
92
|
|
|
$route = new Fixture\TestRoute(); |
93
|
|
|
$newRoutePath = Fixture\TestRoute::getTestRoutePath(); |
94
|
|
|
|
95
|
|
|
$this->assertNotSame($route->getPath(), $newRoutePath); |
96
|
|
|
$this->assertSame($route, $route->setPath($newRoutePath)); |
97
|
|
|
$this->assertSame($newRoutePath, $route->getPath()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function testSetMethods() : void |
104
|
|
|
{ |
105
|
|
|
$route = new Fixture\TestRoute(); |
106
|
|
|
$newRouteMethods = Fixture\TestRoute::getTestRouteMethods(); |
107
|
|
|
|
108
|
|
|
$this->assertNotSame($route->getMethods(), $newRouteMethods); |
109
|
|
|
$this->assertSame($route, $route->setMethods(...$newRouteMethods)); |
110
|
|
|
$this->assertSame($newRouteMethods, $route->getMethods()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function testSetLowercasedMethods() : void |
117
|
|
|
{ |
118
|
|
|
$route = new Fixture\TestRoute(); |
119
|
|
|
$route->setMethods('foo', 'bar'); |
120
|
|
|
|
121
|
|
|
$this->assertSame(['FOO', 'BAR'], $route->getMethods()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function testSetRequestHandler() : void |
128
|
|
|
{ |
129
|
|
|
$route = new Fixture\TestRoute(); |
130
|
|
|
$newRouteRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler(); |
131
|
|
|
|
132
|
|
|
$this->assertNotSame($route->getRequestHandler(), $newRouteRequestHandler); |
133
|
|
|
$this->assertSame($route, $route->setRequestHandler($newRouteRequestHandler)); |
134
|
|
|
$this->assertSame($newRouteRequestHandler, $route->getRequestHandler()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function testSetMiddlewares() : void |
141
|
|
|
{ |
142
|
|
|
$route = new Fixture\TestRoute(); |
143
|
|
|
$newRouteMiddlewares = Fixture\TestRoute::getTestRouteMiddlewares(); |
144
|
|
|
|
145
|
|
|
$this->assertNotSame($route->getMiddlewares(), $newRouteMiddlewares); |
146
|
|
|
$this->assertSame($route, $route->setMiddlewares(...$newRouteMiddlewares)); |
147
|
|
|
$this->assertSame($newRouteMiddlewares, $route->getMiddlewares()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function testSetAttributes() : void |
154
|
|
|
{ |
155
|
|
|
$route = new Fixture\TestRoute(); |
156
|
|
|
$newRouteAttributes = Fixture\TestRoute::getTestRouteAttributes(); |
157
|
|
|
|
158
|
|
|
$this->assertNotSame($route->getAttributes(), $newRouteAttributes); |
159
|
|
|
$this->assertSame($route, $route->setAttributes($newRouteAttributes)); |
160
|
|
|
$this->assertSame($newRouteAttributes, $route->getAttributes()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function testWithAddedAttributes() : void |
167
|
|
|
{ |
168
|
|
|
$route = new Fixture\TestRoute(); |
169
|
|
|
$extraAttributes = Fixture\TestRoute::getTestRouteAttributes(); |
170
|
|
|
$expectedAttributes = $route->getAttributes() + $extraAttributes; |
171
|
|
|
$routeClone = $route->withAddedAttributes($extraAttributes); |
172
|
|
|
|
173
|
|
|
$this->assertInstanceOf(RouteInterface::class, $routeClone); |
174
|
|
|
$this->assertNotSame($route, $routeClone); |
175
|
|
|
$this->assertSame($expectedAttributes, $routeClone->getAttributes()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
|
|
public function testBuildPath() : void |
182
|
|
|
{ |
183
|
|
|
$route = new Fixture\TestRoute(); |
184
|
|
|
|
185
|
|
|
$route->setPath('/foo/{bar}'); |
186
|
|
|
$this->assertSame('/foo/bar', $route->buildPath(['bar' => 'bar'])); |
187
|
|
|
|
188
|
|
|
$route->setPath('/foo/{bar<\w+>}'); |
189
|
|
|
$this->assertSame('/foo/bar', $route->buildPath(['bar' => 'bar'], true)); |
190
|
|
|
|
191
|
|
|
$route->setPath('/foo/{bar<\d+>}'); |
192
|
|
|
$this->assertSame('/foo/100', $route->buildPath(['bar' => 100], true)); |
193
|
|
|
|
194
|
|
|
$route = $route->withAddedAttributes(['bar' => 'bar']); |
195
|
|
|
$this->assertSame('/foo/100', $route->buildPath(['bar' => 100], true)); |
196
|
|
|
|
197
|
|
|
$route = $route->withAddedAttributes(['bar' => 100]); |
198
|
|
|
$this->assertSame('/foo/100', $route->buildPath([], true)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function testBuildPathWithMissingAttribute() : void |
205
|
|
|
{ |
206
|
|
|
$route = new Fixture\TestRoute(); |
207
|
|
|
$route->setPath('/foo/{bar}/{baz}/quux'); |
208
|
|
|
|
209
|
|
|
$this->expectException(InvalidArgumentException::class); |
210
|
|
|
$this->expectExceptionMessage('[' . $route->getPath() . '] missing attribute "baz".'); |
211
|
|
|
|
212
|
|
|
$route->buildPath([ |
213
|
|
|
'bar' => 'bar', |
214
|
|
|
]); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function testBuildPathWithInvalidAttributeValue() : void |
221
|
|
|
{ |
222
|
|
|
$route = new Fixture\TestRoute(); |
223
|
|
|
$route->setPath('/foo/{bar<\w+>}/{baz<\d+>}/quux'); |
224
|
|
|
|
225
|
|
|
$this->expectException(InvalidArgumentException::class); |
226
|
|
|
$this->expectExceptionMessage('[' . $route->getPath() . '] "baz" must match "\d+".'); |
227
|
|
|
|
228
|
|
|
$route->buildPath([ |
229
|
|
|
'bar' => 'bar', |
230
|
|
|
'baz' => 'baz', |
231
|
|
|
], true); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
public function testBuildRegex() : void |
238
|
|
|
{ |
239
|
|
|
$route = new Fixture\TestRoute(); |
240
|
|
|
|
241
|
|
|
$route->setPath('/'); |
242
|
|
|
$this->assertSame('#^/$#uD', $route->buildRegex()); |
243
|
|
|
|
244
|
|
|
$route->setPath('/{foo}'); |
245
|
|
|
$this->assertSame('#^/(?<foo>[^/]+)$#uD', $route->buildRegex()); |
246
|
|
|
|
247
|
|
|
$route->setPath('/{foo}(/{bar})'); |
248
|
|
|
$this->assertSame('#^/(?<foo>[^/]+)(?:/(?<bar>[^/]+))?$#uD', $route->buildRegex()); |
249
|
|
|
|
250
|
|
|
$route->setPath('/{foo}(/{bar})/{baz<\w+>}'); |
251
|
|
|
$this->assertSame('#^/(?<foo>[^/]+)(?:/(?<bar>[^/]+))?/(?<baz>\w+)$#uD', $route->buildRegex()); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|