|
8 | 8 |
|
9 | 9 | final class RouterTest extends TestCase |
10 | 10 | { |
| 11 | + public function setUp(): void |
| 12 | + { |
| 13 | + Router::setAllowOverride(false); |
| 14 | + } |
| 15 | + |
11 | 16 | public function tearDown(): void |
12 | 17 | { |
13 | 18 | Router::reset(); |
@@ -137,6 +142,104 @@ public function testCanMatchMix(): void |
137 | 142 | $this->assertEquals($routeGET, Router::match(Http::REQUEST_METHOD_GET, '/register/lorem/ipsum')?->route); |
138 | 143 | } |
139 | 144 |
|
| 145 | + public function testCanMatchMethodAlias(): void |
| 146 | + { |
| 147 | + $route = new Route(Http::REQUEST_METHOD_GET, '/userinfo'); |
| 148 | + $route->aliasMethod(Http::REQUEST_METHOD_POST); |
| 149 | + |
| 150 | + Router::addRoute($route); |
| 151 | + |
| 152 | + $this->assertEquals($route, Router::match(Http::REQUEST_METHOD_GET, '/userinfo')?->route); |
| 153 | + $this->assertEquals($route, Router::match(Http::REQUEST_METHOD_POST, '/userinfo')?->route); |
| 154 | + $this->assertNull(Router::match(Http::REQUEST_METHOD_PUT, '/userinfo')); |
| 155 | + |
| 156 | + $this->assertSame(Http::REQUEST_METHOD_GET, $route->getMethod()); |
| 157 | + $this->assertSame([Http::REQUEST_METHOD_POST], $route->getAliasMethods()); |
| 158 | + } |
| 159 | + |
| 160 | + public function testCanMatchMethodAliasWithPlaceholder(): void |
| 161 | + { |
| 162 | + $route = new Route(Http::REQUEST_METHOD_GET, '/users/:id'); |
| 163 | + $route->aliasMethod(Http::REQUEST_METHOD_POST); |
| 164 | + |
| 165 | + Router::addRoute($route); |
| 166 | + |
| 167 | + $match = Router::match(Http::REQUEST_METHOD_POST, '/users/abc-123'); |
| 168 | + |
| 169 | + $this->assertEquals($route, $match?->route); |
| 170 | + $this->assertSame(['id' => 'abc-123'], $match?->params); |
| 171 | + } |
| 172 | + |
| 173 | + public function testMethodAliasCrossesPathAliasesRegardlessOfOrder(): void |
| 174 | + { |
| 175 | + $routeA = new Route(Http::REQUEST_METHOD_GET, '/a'); |
| 176 | + $routeA |
| 177 | + ->alias('/a-old') |
| 178 | + ->aliasMethod(Http::REQUEST_METHOD_POST); |
| 179 | + |
| 180 | + Router::addRoute($routeA); |
| 181 | + |
| 182 | + $routeB = new Route(Http::REQUEST_METHOD_GET, '/b'); |
| 183 | + $routeB |
| 184 | + ->aliasMethod(Http::REQUEST_METHOD_POST) |
| 185 | + ->alias('/b-old'); |
| 186 | + |
| 187 | + Router::addRoute($routeB); |
| 188 | + |
| 189 | + $this->assertEquals($routeA, Router::match(Http::REQUEST_METHOD_POST, '/a')?->route); |
| 190 | + $this->assertEquals($routeA, Router::match(Http::REQUEST_METHOD_POST, '/a-old')?->route); |
| 191 | + $this->assertEquals($routeB, Router::match(Http::REQUEST_METHOD_POST, '/b')?->route); |
| 192 | + $this->assertEquals($routeB, Router::match(Http::REQUEST_METHOD_POST, '/b-old')?->route); |
| 193 | + } |
| 194 | + |
| 195 | + public function testCannotRegisterDuplicateMethodAlias(): void |
| 196 | + { |
| 197 | + $routePOST = new Route(Http::REQUEST_METHOD_POST, '/userinfo'); |
| 198 | + Router::addRoute($routePOST); |
| 199 | + |
| 200 | + $routeGET = new Route(Http::REQUEST_METHOD_GET, '/userinfo'); |
| 201 | + |
| 202 | + $this->expectException(\Exception::class); |
| 203 | + $this->expectExceptionMessage('Route for (POST:userinfo) already registered.'); |
| 204 | + $routeGET->aliasMethod(Http::REQUEST_METHOD_POST); |
| 205 | + } |
| 206 | + |
| 207 | + public function testCanOverrideMethodAlias(): void |
| 208 | + { |
| 209 | + Router::setAllowOverride(true); |
| 210 | + |
| 211 | + try { |
| 212 | + $routePOST = new Route(Http::REQUEST_METHOD_POST, '/userinfo'); |
| 213 | + Router::addRoute($routePOST); |
| 214 | + |
| 215 | + $routeGET = new Route(Http::REQUEST_METHOD_GET, '/userinfo'); |
| 216 | + Router::addRoute($routeGET); |
| 217 | + $routeGET->aliasMethod(Http::REQUEST_METHOD_POST); |
| 218 | + |
| 219 | + $this->assertEquals($routeGET, Router::match(Http::REQUEST_METHOD_POST, '/userinfo')?->route); |
| 220 | + } finally { |
| 221 | + Router::setAllowOverride(false); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + public function testCannotRegisterMethodAliasForUnknownMethod(): void |
| 226 | + { |
| 227 | + $route = new Route(Http::REQUEST_METHOD_GET, '/userinfo'); |
| 228 | + |
| 229 | + $this->expectException(\Exception::class); |
| 230 | + $this->expectExceptionMessage('Method (TRACE) not supported.'); |
| 231 | + $route->aliasMethod('TRACE'); |
| 232 | + } |
| 233 | + |
| 234 | + public function testCannotRegisterMethodAliasOnWildcardRoute(): void |
| 235 | + { |
| 236 | + $route = new Route('', ''); |
| 237 | + |
| 238 | + $this->expectException(\Exception::class); |
| 239 | + $this->expectExceptionMessage('Method aliases are not supported for the wildcard route.'); |
| 240 | + $route->aliasMethod(Http::REQUEST_METHOD_POST); |
| 241 | + } |
| 242 | + |
140 | 243 | public function testCanMatchFilename(): void |
141 | 244 | { |
142 | 245 | $routeGET = new Route(Http::REQUEST_METHOD_GET, '/robots.txt'); |
|
0 commit comments