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

Implement fromRoute() and getMatchedRoute() #23

Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 47 additions & 1 deletion src/RouteResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,39 @@ class RouteResult
*/
private $matchedMiddleware;

/**
* Route matched during routing
*
* @since 1.3.0
* @var Route $route
*/
private $route;

/**
* @var bool Success state of routing.
*/
private $success;

/**
* Create an instance representing a route succes from the matching route.
*
* @param Route $route
* @param array $params Parameters associated with the matched route, if any.
* @return static
*/
public static function fromRoute(Route $route, array $params = [])
{
$result = new self();
$result->success = true;
$result->route = $route;
$result->matchedParams = $params;
return $result;
}

/**
* Create an instance repesenting a route success.
*
* @deprecated since 1.3.0; will be removed in 2.0.0.
* @param string $name Name of matched route.
* @param callable|string $middleware Middleware associated with the
* matched route.
Expand Down Expand Up @@ -107,6 +132,17 @@ public function isSuccess()
return $this->success;
}

/**
* Retrieve the route that resulted in the route match.
*
* @return false|null|Route false if representing a routing failure;
* null if not created via fromRoute(); Route instance otherwise.
*/
public function getMatchedRoute()
{
return $this->isFailure() ? false : $this->route;
}

/**
* Retrieve the matched route name, if possible.
*
Expand All @@ -121,6 +157,10 @@ public function getMatchedRouteName()
return false;
}

if (! $this->matchedRouteName && $this->route) {
$this->matchedRouteName = $this->route->getName();
}

return $this->matchedRouteName;
}

Expand All @@ -136,6 +176,10 @@ public function getMatchedMiddleware()
return false;
}

if (! $this->matchedMiddleware && $this->route) {
$this->matchedMiddleware = $this->route->getMiddleware();
}

return $this->matchedMiddleware;
}

Expand Down Expand Up @@ -183,7 +227,9 @@ public function isMethodFailure()
public function getAllowedMethods()
{
if ($this->isSuccess()) {
return [];
return $this->route
? $this->route->getAllowedMethods()
: [];
}

if (null === $this->allowedMethods) {
Expand Down
29 changes: 29 additions & 0 deletions test/RouteResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,33 @@ public function testRouteSuccessMethodFailure()
);
$this->assertFalse($result->isMethodFailure());
}

public function testFromRouteShouldComposeRouteInResult()
{
$route = $this->prophesize(Route::class);

$result = RouteResult::fromRoute($route->reveal(), ['foo' => 'bar']);
$this->assertInstanceOf(RouteResult::class, $result);
$this->assertTrue($result->isSuccess());
$this->assertSame($route->reveal(), $result->getMatchedRoute());

return ['route' => $route, 'result' => $result];
}

/**
* @depends testFromRouteShouldComposeRouteInResult
*/
public function testAllAccessorsShouldReturnExpectedDataWhenResultCreatedViaFromRoute(array $data)
{
$result = $data['result'];
$route = $data['route'];

$route->getName()->willReturn('route');
$route->getMiddleware()->willReturn(__METHOD__);
$route->getAllowedMethods()->willReturn(['HEAD', 'OPTIONS', 'GET']);

$this->assertEquals('route', $result->getMatchedRouteName());
$this->assertEquals(__METHOD__, $result->getMatchedMiddleware());
$this->assertEquals(['HEAD', 'OPTIONS', 'GET'], $result->getAllowedMethods());
}
}