Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Added the any() method. #116

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/book/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, $this->httpRouteMethods, $name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass a null or Router\Route::HTTP_METHOD_ANY for the third argument. $httpRouteMethods only really exists to determine what methods are valid overloading methods.

}

/**
* Overload pipe() operation.
*
Expand Down
20 changes: 20 additions & 0 deletions test/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move ReflectionClass to use statement

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spiffyjr I agree with @samsonasik - but have a different suggestion:

  • Import ReflectionProperty into the file.
  • Use the following:
    php $app = $this->getApp(); $r = new ReflectionProperty($app, 'httpRouteMethods'); $r->setAccessible(true); $routeMethods = $r->getValue($app);

Since you're interested in a specific property, do reflection directly on it, instead of pulling it from class reflection. 😄

$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
*/
Expand Down