From e84fbfa26cfcbc5d641de7ba6402ddf17fd03433 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Mon, 12 Dec 2016 16:17:03 +0000 Subject: [PATCH 1/3] Added failing test case testMatchedRouteParamsAreInjectedToRequestAsAttributes --- test/MiddlewareListenerTest.php | 34 ++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/test/MiddlewareListenerTest.php b/test/MiddlewareListenerTest.php index 8e32e1c3b..e1f525367 100644 --- a/test/MiddlewareListenerTest.php +++ b/test/MiddlewareListenerTest.php @@ -24,6 +24,12 @@ class MiddlewareListenerTest extends TestCase { + /** + * @var \Prophecy\Prophecy\ObjectProphecy + */ + private $routeMatch; + + /** * Create an MvcEvent, populated with everything it needs. * @@ -34,8 +40,8 @@ class MiddlewareListenerTest extends TestCase public function createMvcEvent($middlewareMatched, $middleware = null) { $response = new Response(); - $routeMatch = $this->prophesize(RouteMatch::class); - $routeMatch->getParam('middleware', false)->willReturn($middlewareMatched); + $this->routeMatch = $this->prophesize(RouteMatch::class); + $this->routeMatch->getParam('middleware', false)->willReturn($middlewareMatched); $eventManager = new EventManager(); @@ -54,7 +60,7 @@ public function createMvcEvent($middlewareMatched, $middleware = null) $event->setRequest(new Request()); $event->setResponse($response); $event->setApplication($application->reveal()); - $event->setRouteMatch($routeMatch->reveal()); + $event->setRouteMatch($this->routeMatch->reveal()); return $event; } @@ -82,6 +88,28 @@ public function testSuccessfullyDispatchesMiddleware() $this->assertEquals('Test!', $return->getBody()); } + public function testMatchedRouteParamsAreInjectedToRequestAsAttributes() + { + $matchedRouteParam = uniqid('matched param', true); + + $event = $this->createMvcEvent( + 'foo', + function (ServerRequestInterface $request, ResponseInterface $response) { + $response->getBody()->write($request->getAttribute('myParam', 'param did not exist')); + return $response; + } + ); + + $this->routeMatch->getParams()->willReturn([ + 'myParam' => $matchedRouteParam, + ]); + + $listener = new MiddlewareListener(); + $return = $listener->onDispatch($event); + self::assertInstanceOf(Response::class, $return); + self::assertSame($matchedRouteParam, $return->getBody()); + } + public function testTriggersErrorForUncallableMiddleware() { $event = $this->createMvcEvent('path'); From 79fe95619b7a1ba865ba9445afcc5a6366f5cdd1 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Mon, 12 Dec 2016 16:24:14 +0000 Subject: [PATCH 2/3] Copy route match params into PSR-7 request attributes --- src/MiddlewareListener.php | 6 +++++- test/MiddlewareListenerTest.php | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/MiddlewareListener.php b/src/MiddlewareListener.php index 5ea4efe15..b55259b94 100644 --- a/src/MiddlewareListener.php +++ b/src/MiddlewareListener.php @@ -59,7 +59,11 @@ public function onDispatch(MvcEvent $event) $caughtException = null; try { - $return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response)); + $psr7Request = Psr7Request::fromZend($request); + foreach ($routeMatch->getParams() as $key => $value) { + $psr7Request = $psr7Request->withAttribute($key, $value); + } + $return = $middleware($psr7Request, Psr7Response::fromZend($response)); } catch (\Throwable $ex) { $caughtException = $ex; } catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced diff --git a/test/MiddlewareListenerTest.php b/test/MiddlewareListenerTest.php index e1f525367..4417acacb 100644 --- a/test/MiddlewareListenerTest.php +++ b/test/MiddlewareListenerTest.php @@ -29,7 +29,6 @@ class MiddlewareListenerTest extends TestCase */ private $routeMatch; - /** * Create an MvcEvent, populated with everything it needs. * @@ -42,6 +41,7 @@ public function createMvcEvent($middlewareMatched, $middleware = null) $response = new Response(); $this->routeMatch = $this->prophesize(RouteMatch::class); $this->routeMatch->getParam('middleware', false)->willReturn($middlewareMatched); + $this->routeMatch->getParams()->willReturn([]); $eventManager = new EventManager(); @@ -153,6 +153,7 @@ public function testCanLoadFromAbstractFactory() $response = new Response(); $routeMatch = $this->prophesize(RouteMatch::class); $routeMatch->getParam('middleware', false)->willReturn('test'); + $routeMatch->getParams()->willReturn([]); $eventManager = new EventManager(); From 05f3e6f4d1693524ed5b97d303742bbf8b4564ab Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Mon, 19 Dec 2016 14:20:08 +0000 Subject: [PATCH 3/3] Also inject RouteMatch to mirror Expressive behaviour --- src/MiddlewareListener.php | 3 ++- test/MiddlewareListenerTest.php | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MiddlewareListener.php b/src/MiddlewareListener.php index b55259b94..f93e360e6 100644 --- a/src/MiddlewareListener.php +++ b/src/MiddlewareListener.php @@ -14,6 +14,7 @@ use Zend\EventManager\EventManagerInterface; use Zend\Psr7Bridge\Psr7ServerRequest as Psr7Request; use Zend\Psr7Bridge\Psr7Response; +use Zend\Router\RouteMatch; class MiddlewareListener extends AbstractListenerAggregate { @@ -59,7 +60,7 @@ public function onDispatch(MvcEvent $event) $caughtException = null; try { - $psr7Request = Psr7Request::fromZend($request); + $psr7Request = Psr7Request::fromZend($request)->withAttribute(RouteMatch::class, $routeMatch); foreach ($routeMatch->getParams() as $key => $value) { $psr7Request = $psr7Request->withAttribute($key, $value); } diff --git a/test/MiddlewareListenerTest.php b/test/MiddlewareListenerTest.php index 4417acacb..20bbe5330 100644 --- a/test/MiddlewareListenerTest.php +++ b/test/MiddlewareListenerTest.php @@ -91,10 +91,12 @@ public function testSuccessfullyDispatchesMiddleware() public function testMatchedRouteParamsAreInjectedToRequestAsAttributes() { $matchedRouteParam = uniqid('matched param', true); + $routeAttribute = null; $event = $this->createMvcEvent( 'foo', - function (ServerRequestInterface $request, ResponseInterface $response) { + function (ServerRequestInterface $request, ResponseInterface $response) use (&$routeAttribute) { + $routeAttribute = $request->getAttribute(RouteMatch::class); $response->getBody()->write($request->getAttribute('myParam', 'param did not exist')); return $response; } @@ -106,8 +108,9 @@ function (ServerRequestInterface $request, ResponseInterface $response) { $listener = new MiddlewareListener(); $return = $listener->onDispatch($event); - self::assertInstanceOf(Response::class, $return); - self::assertSame($matchedRouteParam, $return->getBody()); + $this->assertInstanceOf(Response::class, $return); + $this->assertSame($matchedRouteParam, $return->getBody()); + $this->assertSame($this->routeMatch->reveal(), $routeAttribute); } public function testTriggersErrorForUncallableMiddleware()