Store variables from URI as uriVariables attributes

This commit is contained in:
PJ Dietz 2015-05-12 17:56:15 -04:00
parent 22a17e42bb
commit 1bb93434b2
3 changed files with 9 additions and 6 deletions

View File

@ -7,6 +7,7 @@ use Psr\Http\Message\ServerRequestInterface;
class RegexRoute extends Route class RegexRoute extends Route
{ {
private $capturesAttribute = "uriVariables";
private $captures; private $captures;
public function getType() public function getType()
@ -35,7 +36,7 @@ class RegexRoute extends Route
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next) public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
{ {
if ($this->captures) { if ($this->captures) {
$request = $request->withAttribute("path", $this->captures); $request = $request->withAttribute($this->capturesAttribute, $this->captures);
} }
return parent::dispatch($request, $response, $next); return parent::dispatch($request, $response, $next);
} }

View File

@ -42,6 +42,8 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::matchesRequestTarget
* @covers ::dispatch
* @dataProvider matchingRouteProvider * @dataProvider matchingRouteProvider
*/ */
public function testProvidesCapturesAsRequestAttributes($pattern, $path, $expectedCaptures) public function testProvidesCapturesAsRequestAttributes($pattern, $path, $expectedCaptures)
@ -57,7 +59,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
$route->matchesRequestTarget($path); $route->matchesRequestTarget($path);
$route->dispatch($request->reveal(), $response->reveal(), $next); $route->dispatch($request->reveal(), $response->reveal(), $next);
$request->withAttribute("path", $expectedCaptures)->shouldHaveBeenCalled(); $request->withAttribute("uriVariables", $expectedCaptures)->shouldHaveBeenCalled();
} }
public function matchingRouteProvider() public function matchingRouteProvider()
@ -78,6 +80,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::matchesRequestTarget
* @dataProvider mismatchingRouteProvider * @dataProvider mismatchingRouteProvider
*/ */
public function testDoesNotMatchNonmatchingTarget($pattern, $path) public function testDoesNotMatchNonmatchingTarget($pattern, $path)
@ -96,6 +99,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::matchesRequestTarget
* @dataProvider invalidRouteProvider * @dataProvider invalidRouteProvider
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */

View File

@ -56,11 +56,9 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase
$route->matchesRequestTarget($path); $route->matchesRequestTarget($path);
$route->dispatch($request->reveal(), $response->reveal(), $next); $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; return array_intersect_assoc($path, $expectedCaptures) == $expectedCaptures;
}))->shouldHaveBeenCalled(); }))->shouldHaveBeenCalled();
//$request->withAttribute("path", $expectedCaptures)->shouldHaveBeenCalled();
} }
public function matchingTemplateProvider() public function matchingTemplateProvider()
@ -111,7 +109,7 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase
$route->matchesRequestTarget($path); $route->matchesRequestTarget($path);
$route->dispatch($request->reveal(), $response->reveal(), $next); $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; return array_intersect_assoc($path, $expectedCaptures) == $expectedCaptures;
}))->shouldHaveBeenCalled(); }))->shouldHaveBeenCalled();
} }