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

Commit 05f3e6f

Browse files
committed
Also inject RouteMatch to mirror Expressive behaviour
1 parent 79fe956 commit 05f3e6f

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/MiddlewareListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Zend\EventManager\EventManagerInterface;
1515
use Zend\Psr7Bridge\Psr7ServerRequest as Psr7Request;
1616
use Zend\Psr7Bridge\Psr7Response;
17+
use Zend\Router\RouteMatch;
1718

1819
class MiddlewareListener extends AbstractListenerAggregate
1920
{
@@ -59,7 +60,7 @@ public function onDispatch(MvcEvent $event)
5960

6061
$caughtException = null;
6162
try {
62-
$psr7Request = Psr7Request::fromZend($request);
63+
$psr7Request = Psr7Request::fromZend($request)->withAttribute(RouteMatch::class, $routeMatch);
6364
foreach ($routeMatch->getParams() as $key => $value) {
6465
$psr7Request = $psr7Request->withAttribute($key, $value);
6566
}

test/MiddlewareListenerTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ public function testSuccessfullyDispatchesMiddleware()
9191
public function testMatchedRouteParamsAreInjectedToRequestAsAttributes()
9292
{
9393
$matchedRouteParam = uniqid('matched param', true);
94+
$routeAttribute = null;
9495

9596
$event = $this->createMvcEvent(
9697
'foo',
97-
function (ServerRequestInterface $request, ResponseInterface $response) {
98+
function (ServerRequestInterface $request, ResponseInterface $response) use (&$routeAttribute) {
99+
$routeAttribute = $request->getAttribute(RouteMatch::class);
98100
$response->getBody()->write($request->getAttribute('myParam', 'param did not exist'));
99101
return $response;
100102
}
@@ -106,8 +108,9 @@ function (ServerRequestInterface $request, ResponseInterface $response) {
106108

107109
$listener = new MiddlewareListener();
108110
$return = $listener->onDispatch($event);
109-
self::assertInstanceOf(Response::class, $return);
110-
self::assertSame($matchedRouteParam, $return->getBody());
111+
$this->assertInstanceOf(Response::class, $return);
112+
$this->assertSame($matchedRouteParam, $return->getBody());
113+
$this->assertSame($this->routeMatch->reveal(), $routeAttribute);
111114
}
112115

113116
public function testTriggersErrorForUncallableMiddleware()

0 commit comments

Comments
 (0)