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
|
|
|
* Import functions |
15
|
|
|
*/ |
16
|
|
|
use function Sunrise\Http\Router\path_build; |
17
|
|
|
use function Sunrise\Http\Router\path_regex; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* RouteTest |
21
|
|
|
*/ |
22
|
|
|
class RouteTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function testConstructor() : void |
29
|
|
|
{ |
30
|
|
|
$routeName = Fixture\TestRoute::getTestRouteName(); |
31
|
|
|
$routePath = Fixture\TestRoute::getTestRoutePath(); |
32
|
|
|
$routeMethods = Fixture\TestRoute::getTestRouteMethods(); |
33
|
|
|
$routeRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler(); |
34
|
|
|
|
35
|
|
|
$route = new Route( |
36
|
|
|
$routeName, |
37
|
|
|
$routePath, |
38
|
|
|
$routeMethods, |
39
|
|
|
$routeRequestHandler |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
43
|
|
|
$this->assertSame($routeName, $route->getName()); |
44
|
|
|
$this->assertSame($routePath, $route->getPath()); |
45
|
|
|
$this->assertSame($routeMethods, $route->getMethods()); |
46
|
|
|
$this->assertSame($routeRequestHandler, $route->getRequestHandler()); |
47
|
|
|
$this->assertSame([], $route->getMiddlewares()); |
48
|
|
|
$this->assertSame([], $route->getAttributes()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function testConstructorWithOptionalParams() : void |
55
|
|
|
{ |
56
|
|
|
$routeName = Fixture\TestRoute::getTestRouteName(); |
57
|
|
|
$routePath = Fixture\TestRoute::getTestRoutePath(); |
58
|
|
|
$routeMethods = Fixture\TestRoute::getTestRouteMethods(); |
59
|
|
|
$routeRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler(); |
60
|
|
|
$routeMiddlewares = Fixture\TestRoute::getTestRouteMiddlewares(); |
61
|
|
|
$routeAttributes = Fixture\TestRoute::getTestRouteAttributes(); |
62
|
|
|
|
63
|
|
|
$route = new Route( |
64
|
|
|
$routeName, |
65
|
|
|
$routePath, |
66
|
|
|
$routeMethods, |
67
|
|
|
$routeRequestHandler, |
68
|
|
|
$routeMiddlewares, |
69
|
|
|
$routeAttributes |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$this->assertSame($routeName, $route->getName()); |
73
|
|
|
$this->assertSame($routePath, $route->getPath()); |
74
|
|
|
$this->assertSame($routeMethods, $route->getMethods()); |
75
|
|
|
$this->assertSame($routeRequestHandler, $route->getRequestHandler()); |
76
|
|
|
$this->assertSame($routeMiddlewares, $route->getMiddlewares()); |
77
|
|
|
$this->assertSame($routeAttributes, $route->getAttributes()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function testSetName() : void |
84
|
|
|
{ |
85
|
|
|
$route = new Fixture\TestRoute(); |
86
|
|
|
$newRouteName = Fixture\TestRoute::getTestRouteName(); |
87
|
|
|
|
88
|
|
|
$this->assertNotSame($route->getName(), $newRouteName); |
89
|
|
|
$this->assertSame($route, $route->setName($newRouteName)); |
90
|
|
|
$this->assertSame($newRouteName, $route->getName()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function testSetPath() : void |
97
|
|
|
{ |
98
|
|
|
$route = new Fixture\TestRoute(); |
99
|
|
|
$newRoutePath = Fixture\TestRoute::getTestRoutePath(); |
100
|
|
|
|
101
|
|
|
$this->assertNotSame($route->getPath(), $newRoutePath); |
102
|
|
|
$this->assertSame($route, $route->setPath($newRoutePath)); |
103
|
|
|
$this->assertSame($newRoutePath, $route->getPath()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function testSetMethods() : void |
110
|
|
|
{ |
111
|
|
|
$route = new Fixture\TestRoute(); |
112
|
|
|
$newRouteMethods = Fixture\TestRoute::getTestRouteMethods(); |
113
|
|
|
|
114
|
|
|
$this->assertNotSame($route->getMethods(), $newRouteMethods); |
115
|
|
|
$this->assertSame($route, $route->setMethods(...$newRouteMethods)); |
116
|
|
|
$this->assertSame($newRouteMethods, $route->getMethods()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function testSetLowercasedMethods() : void |
123
|
|
|
{ |
124
|
|
|
$route = new Fixture\TestRoute(); |
125
|
|
|
$route->setMethods('foo', 'bar'); |
126
|
|
|
|
127
|
|
|
$this->assertSame(['FOO', 'BAR'], $route->getMethods()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
public function testSetRequestHandler() : void |
134
|
|
|
{ |
135
|
|
|
$route = new Fixture\TestRoute(); |
136
|
|
|
$newRouteRequestHandler = Fixture\TestRoute::getTestRouteRequestHandler(); |
137
|
|
|
|
138
|
|
|
$this->assertNotSame($route->getRequestHandler(), $newRouteRequestHandler); |
139
|
|
|
$this->assertSame($route, $route->setRequestHandler($newRouteRequestHandler)); |
140
|
|
|
$this->assertSame($newRouteRequestHandler, $route->getRequestHandler()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function testSetMiddlewares() : void |
147
|
|
|
{ |
148
|
|
|
$route = new Fixture\TestRoute(); |
149
|
|
|
$newRouteMiddlewares = Fixture\TestRoute::getTestRouteMiddlewares(); |
150
|
|
|
|
151
|
|
|
$this->assertNotSame($route->getMiddlewares(), $newRouteMiddlewares); |
152
|
|
|
$this->assertSame($route, $route->setMiddlewares(...$newRouteMiddlewares)); |
153
|
|
|
$this->assertSame($newRouteMiddlewares, $route->getMiddlewares()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public function testSetAttributes() : void |
160
|
|
|
{ |
161
|
|
|
$route = new Fixture\TestRoute(); |
162
|
|
|
$newRouteAttributes = Fixture\TestRoute::getTestRouteAttributes(); |
163
|
|
|
|
164
|
|
|
$this->assertNotSame($route->getAttributes(), $newRouteAttributes); |
165
|
|
|
$this->assertSame($route, $route->setAttributes($newRouteAttributes)); |
166
|
|
|
$this->assertSame($newRouteAttributes, $route->getAttributes()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
public function testWithAddedAttributes() : void |
173
|
|
|
{ |
174
|
|
|
$route = new Fixture\TestRoute(); |
175
|
|
|
$extraAttributes = Fixture\TestRoute::getTestRouteAttributes(); |
176
|
|
|
$expectedAttributes = $route->getAttributes() + $extraAttributes; |
177
|
|
|
$routeClone = $route->withAddedAttributes($extraAttributes); |
178
|
|
|
|
179
|
|
|
$this->assertInstanceOf(RouteInterface::class, $routeClone); |
180
|
|
|
$this->assertNotSame($route, $routeClone); |
181
|
|
|
$this->assertSame($expectedAttributes, $routeClone->getAttributes()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function testBuildPath() : void |
188
|
|
|
{ |
189
|
|
|
$route = new Fixture\TestRoute(); |
190
|
|
|
$route->setPath('/foo(/bar/{baz}/qux)/quux'); |
191
|
|
|
|
192
|
|
|
$this->assertSame(path_build($route->getPath()), $route->buildPath()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
public function testBuildRegex() : void |
199
|
|
|
{ |
200
|
|
|
$route = new Fixture\TestRoute(); |
201
|
|
|
$route->setPath('/foo(/bar/{baz<\w+>}/qux)/quux'); |
202
|
|
|
|
203
|
|
|
$this->assertSame(path_regex($route->getPath()), $route->buildRegex()); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|