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

Commit

Permalink
Also inject RouteMatch to mirror Expressive behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Dec 19, 2016
1 parent 79fe956 commit 05f3e6f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/MiddlewareListener.php
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,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);
}
Expand Down
9 changes: 6 additions & 3 deletions test/MiddlewareListenerTest.php
Expand Up @@ -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;
}
Expand All @@ -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()
Expand Down

0 comments on commit 05f3e6f

Please sign in to comment.