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

Add route match params to request attributes #210

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
7 changes: 6 additions & 1 deletion src/MiddlewareListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -59,7 +60,11 @@ public function onDispatch(MvcEvent $event)

$caughtException = null;
try {
$return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response));
$psr7Request = Psr7Request::fromZend($request)->withAttribute(RouteMatch::class, $routeMatch);
foreach ($routeMatch->getParams() as $key => $value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think will be nice to inject also while RouteMatch (as we have it in expressive: https://github.com/zendframework/zend-expressive/blob/master/src/Application.php#L431-L435), not just individual matched params.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added

$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
Expand Down
38 changes: 35 additions & 3 deletions test/MiddlewareListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

class MiddlewareListenerTest extends TestCase
{
/**
* @var \Prophecy\Prophecy\ObjectProphecy
*/
private $routeMatch;

/**
* Create an MvcEvent, populated with everything it needs.
*
Expand All @@ -34,8 +39,9 @@ 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);
$this->routeMatch->getParams()->willReturn([]);

$eventManager = new EventManager();

Expand All @@ -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;
}
Expand Down Expand Up @@ -82,6 +88,31 @@ public function testSuccessfullyDispatchesMiddleware()
$this->assertEquals('Test!', $return->getBody());
}

public function testMatchedRouteParamsAreInjectedToRequestAsAttributes()
{
$matchedRouteParam = uniqid('matched param', true);
$routeAttribute = null;

$event = $this->createMvcEvent(
'foo',
function (ServerRequestInterface $request, ResponseInterface $response) use (&$routeAttribute) {
$routeAttribute = $request->getAttribute(RouteMatch::class);
$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);
$this->assertInstanceOf(Response::class, $return);
$this->assertSame($matchedRouteParam, $return->getBody());
$this->assertSame($this->routeMatch->reveal(), $routeAttribute);
}

public function testTriggersErrorForUncallableMiddleware()
{
$event = $this->createMvcEvent('path');
Expand Down Expand Up @@ -125,6 +156,7 @@ public function testCanLoadFromAbstractFactory()
$response = new Response();
$routeMatch = $this->prophesize(RouteMatch::class);
$routeMatch->getParam('middleware', false)->willReturn('test');
$routeMatch->getParams()->willReturn([]);

$eventManager = new EventManager();

Expand Down