diff --git a/doc/book/application.md b/doc/book/application.md index f6341c1f..c2696834 100644 --- a/doc/book/application.md +++ b/doc/book/application.md @@ -116,9 +116,9 @@ where: This method is typically only used if you want a single middleware to handle multiple HTTP request methods. -### get(), post(), put(), patch(), delete() +### get(), post(), put(), patch(), delete(), any() -Each of the methods `get()`, `post()`, `put()`, `patch()`, and `delete()` +Each of the methods `get()`, `post()`, `put()`, `patch()`, `delete()`, and `any()` proxies to `route()` and has the signature: ```php diff --git a/src/Application.php b/src/Application.php index 07fb8bc5..eaebb027 100644 --- a/src/Application.php +++ b/src/Application.php @@ -155,6 +155,17 @@ public function __call($method, $args) return call_user_func_array([$this, 'route'], $args); } + /** + * @param string|Router\Route $path + * @param callable|string $middleware Middleware (or middleware service name) to associate with route. + * @param null|string $name the name of the route + * @return Router\Route + */ + public function any($path, $middleware, $name = null) + { + return $this->route($path, $middleware, null, $name); + } + /** * Overload pipe() operation. * diff --git a/test/ApplicationTest.php b/test/ApplicationTest.php index 2f9c4ebc..67ad48c4 100644 --- a/test/ApplicationTest.php +++ b/test/ApplicationTest.php @@ -68,6 +68,26 @@ public function testRouteMethodReturnsRouteInstance() $this->assertSame($this->noopMiddleware, $route->getMiddleware()); } + public function testAnyRouteMethod() + { + $this->router->addRoute(Argument::type(Route::class))->shouldBeCalled(); + $route = $this->getApp()->any('/foo', $this->noopMiddleware); + $this->assertInstanceOf(Route::class, $route); + $this->assertEquals('/foo', $route->getPath()); + $this->assertSame($this->noopMiddleware, $route->getMiddleware()); + + $reflClass = new \ReflectionClass($this->getApp()); + $routeMethods = $reflClass->getProperty('httpRouteMethods'); + $routeMethods->setAccessible(true); + $routeMethods = $routeMethods->getValue($this->getApp()); + + foreach ($routeMethods as $method) { + $this->assertTrue($route->allowsMethod($method)); + } + + $this->assertSame($routeMethods, $route->getAllowedMethods()); + } + /** * @dataProvider commonHttpMethods */