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

Commit

Permalink
Merge branch 'hotfix/210'
Browse files Browse the repository at this point in the history
Close #210
  • Loading branch information
akrabat committed Dec 20, 2016
2 parents c6d6392 + 41b3a41 commit 20d3797
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#210](https://github.com/zendframework/zend-mvc/pull/210) copies the
`RouteMatch` and its parameters to the PSR-7 `ServerRequest` object so that
they are available to middleware.

## 3.0.3 - 2016-08-29

Expand Down
7 changes: 6 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,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) {
$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
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

0 comments on commit 20d3797

Please sign in to comment.