diff --git a/src/Routing/Route/RegexRoute.php b/src/Routing/Route/RegexRoute.php index cff17a6..3b55b6b 100644 --- a/src/Routing/Route/RegexRoute.php +++ b/src/Routing/Route/RegexRoute.php @@ -2,30 +2,40 @@ namespace WellRESTed\Routing\Route; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; + class RegexRoute extends Route { - private $pattern; + private $captures; - public function __construct($pattern, $middleware) + public function getType() { - parent::__construct($middleware); - $this->pattern = $pattern; + return RouteInterface::TYPE_PATTERN; } /** * @param string $requestTarget - * @param array $captures * @return bool * @throws \RuntimeException */ - public function matchesRequestTarget($requestTarget, &$captures = null) + public function matchesRequestTarget($requestTarget) { - $matched = @preg_match($this->pattern, $requestTarget, $captures); + $matched = @preg_match($this->getTarget(), $requestTarget, $captures); if ($matched) { + $this->captures = $captures; return true; } elseif ($matched === false) { - throw new \RuntimeException("Invalid regular expression: " . $this->pattern); + throw new \RuntimeException("Invalid regular expression: " . $this->getTarget()); } return false; } + + public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) + { + if ($this->captures) { + $request = $request->withAttribute("path", $this->captures); + } + parent::dispatch($request, $response); + } } diff --git a/test/tests/unit/Routing/Route/RegexRouteTest.php b/test/tests/unit/Routing/Route/RegexRouteTest.php index 2c7f9c0..cc31d0d 100644 --- a/test/tests/unit/Routing/Route/RegexRouteTest.php +++ b/test/tests/unit/Routing/Route/RegexRouteTest.php @@ -4,41 +4,56 @@ namespace WellRESTed\Test\Unit\Routing\Route; use Prophecy\Argument; use WellRESTed\Routing\Route\RegexRoute; +use WellRESTed\Routing\Route\RouteInterface; /** - * @covers WellRESTed\Routing\Route\RegexRoute + * @coversDefaultClass WellRESTed\Routing\Route\RegexRoute + * @uses WellRESTed\Routing\Route\RegexRoute * @uses WellRESTed\Routing\Route\Route */ class RegexRouteTest extends \PHPUnit_Framework_TestCase { - private $request; - private $response; - private $middleware; + private $methodMap; public function setUp() { - $this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); - $this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); - $this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); + $this->methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); } /** + * @covers ::getType + */ + public function testReturnsPatternType() + { + $route = new RegexRoute("/", $this->methodMap->reveal()); + $this->assertSame(RouteInterface::TYPE_PATTERN, $route->getType()); + } + + /** + * @covers ::matchesRequestTarget * @dataProvider matchingRouteProvider */ - public function testMatchesPattern($pattern, $path) + public function testMatchesTarget($pattern, $path) { - $route = new RegexRoute($pattern, $this->middleware->reveal()); + $route = new RegexRoute($pattern, $this->methodMap->reveal()); $this->assertTrue($route->matchesRequestTarget($path)); } /** * @dataProvider matchingRouteProvider */ - public function testExtractsCaptures($pattern, $path, $expectedCaptures) + public function testProvidesCapturesAsRequestAttributes($pattern, $path, $expectedCaptures) { - $route = new RegexRoute($pattern, $this->middleware->reveal()); - $route->matchesRequestTarget($path, $captures); - $this->assertEquals($expectedCaptures, $captures); + $request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); + $request->withAttribute(Argument::cetera())->willReturn($request->reveal()); + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + $responseReveal = $response->reveal(); + + $route = new RegexRoute($pattern, $this->methodMap->reveal()); + $route->matchesRequestTarget($path); + $route->dispatch($request->reveal(), $responseReveal); + + $request->withAttribute("path", $expectedCaptures)->shouldHaveBeenCalled(); } public function matchingRouteProvider() @@ -61,9 +76,9 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase /** * @dataProvider mismatchingRouteProvider */ - public function testFailsToMatchMismatchingPattern($pattern, $path) + public function testDoesNotMatchNonmatchingTarget($pattern, $path) { - $route = new RegexRoute($pattern, $this->middleware->reveal()); + $route = new RegexRoute($pattern, $this->methodMap->reveal()); $this->assertFalse($route->matchesRequestTarget($path)); } @@ -82,7 +97,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase */ public function testThrowsExceptionOnInvalidPattern($pattern) { - $route = new RegexRoute($pattern, $this->middleware->reveal()); + $route = new RegexRoute($pattern, $this->methodMap->reveal()); $route->matchesRequestTarget("/"); }