From 1bb93434b22b24dc60056cf80b89d50932261229 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Tue, 12 May 2015 17:56:15 -0400 Subject: [PATCH] Store variables from URI as uriVariables attributes --- src/Routing/Route/RegexRoute.php | 3 ++- test/tests/unit/Routing/Route/RegexRouteTest.php | 6 +++++- test/tests/unit/Routing/Route/TemplateRouteTest.php | 6 ++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Routing/Route/RegexRoute.php b/src/Routing/Route/RegexRoute.php index 4259b39..f3d3ae2 100644 --- a/src/Routing/Route/RegexRoute.php +++ b/src/Routing/Route/RegexRoute.php @@ -7,6 +7,7 @@ use Psr\Http\Message\ServerRequestInterface; class RegexRoute extends Route { + private $capturesAttribute = "uriVariables"; private $captures; public function getType() @@ -35,7 +36,7 @@ class RegexRoute extends Route public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next) { if ($this->captures) { - $request = $request->withAttribute("path", $this->captures); + $request = $request->withAttribute($this->capturesAttribute, $this->captures); } return parent::dispatch($request, $response, $next); } diff --git a/test/tests/unit/Routing/Route/RegexRouteTest.php b/test/tests/unit/Routing/Route/RegexRouteTest.php index b8b48b4..c890d8b 100644 --- a/test/tests/unit/Routing/Route/RegexRouteTest.php +++ b/test/tests/unit/Routing/Route/RegexRouteTest.php @@ -42,6 +42,8 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase } /** + * @covers ::matchesRequestTarget + * @covers ::dispatch * @dataProvider matchingRouteProvider */ public function testProvidesCapturesAsRequestAttributes($pattern, $path, $expectedCaptures) @@ -57,7 +59,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase $route->matchesRequestTarget($path); $route->dispatch($request->reveal(), $response->reveal(), $next); - $request->withAttribute("path", $expectedCaptures)->shouldHaveBeenCalled(); + $request->withAttribute("uriVariables", $expectedCaptures)->shouldHaveBeenCalled(); } public function matchingRouteProvider() @@ -78,6 +80,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase } /** + * @covers ::matchesRequestTarget * @dataProvider mismatchingRouteProvider */ public function testDoesNotMatchNonmatchingTarget($pattern, $path) @@ -96,6 +99,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase } /** + * @covers ::matchesRequestTarget * @dataProvider invalidRouteProvider * @expectedException \RuntimeException */ diff --git a/test/tests/unit/Routing/Route/TemplateRouteTest.php b/test/tests/unit/Routing/Route/TemplateRouteTest.php index a55f906..f3e69a2 100644 --- a/test/tests/unit/Routing/Route/TemplateRouteTest.php +++ b/test/tests/unit/Routing/Route/TemplateRouteTest.php @@ -56,11 +56,9 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase $route->matchesRequestTarget($path); $route->dispatch($request->reveal(), $response->reveal(), $next); - $request->withAttribute("path", Argument::that(function ($path) use ($expectedCaptures) { + $request->withAttribute("uriVariables", Argument::that(function ($path) use ($expectedCaptures) { return array_intersect_assoc($path, $expectedCaptures) == $expectedCaptures; }))->shouldHaveBeenCalled(); - - //$request->withAttribute("path", $expectedCaptures)->shouldHaveBeenCalled(); } public function matchingTemplateProvider() @@ -111,7 +109,7 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase $route->matchesRequestTarget($path); $route->dispatch($request->reveal(), $response->reveal(), $next); - $request->withAttribute("path", Argument::that(function ($path) use ($expectedCaptures) { + $request->withAttribute("uriVariables", Argument::that(function ($path) use ($expectedCaptures) { return array_intersect_assoc($path, $expectedCaptures) == $expectedCaptures; }))->shouldHaveBeenCalled(); }