From fd8e88486da8ec2f2ca714b1575aae7cde11fc2b Mon Sep 17 00:00:00 2001 From: Mike Willbanks Date: Fri, 20 Nov 2015 14:47:17 -0600 Subject: [PATCH] Forward port #187 --- CHANGELOG.md | 21 +++++++++++++++++++++ src/Application.php | 2 ++ test/RouteMiddlewareTest.php | 31 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc1d8892..0c520f4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,27 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. +## 1.0.0rc3 - TBD + +Third release candidate. + +### Added + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- [#187](https://github.com/zendframework/zend-expressive/pull/187) + Inject the route result as an attribute + ## 1.0.0rc2 - 2015-10-20 Second release candidate. diff --git a/src/Application.php b/src/Application.php index ee5bdab3..585fbecb 100644 --- a/src/Application.php +++ b/src/Application.php @@ -325,6 +325,8 @@ public function routeMiddleware(ServerRequestInterface $request, ResponseInterfa return $next($request, $response); } + // Inject the actual route result, as well as individual matched parameters. + $request = $request->withAttribute(Router\RouteResult::class, $result); foreach ($result->getMatchedParams() as $param => $value) { $request = $request->withAttribute($param, $value); } diff --git a/test/RouteMiddlewareTest.php b/test/RouteMiddlewareTest.php index c95c2009..6b132f84 100644 --- a/test/RouteMiddlewareTest.php +++ b/test/RouteMiddlewareTest.php @@ -529,4 +529,35 @@ public function testWithOnlyRootPathRouteDefinedRoutingToSubPathsShouldReturn404 $this->assertInstanceOf(Response::class, $result); $this->assertNotEquals(405, $result->getStatusCode()); } + + /** + * @group 186 + */ + public function testInjectsRouteResultAsAttribute() + { + $matches = ['id' => 'IDENTIFIER']; + $triggered = false; + $middleware = function ($request, $response, $next) use ($matches, &$triggered) { + $routeResult = $request->getAttribute(RouteResult::class, false); + $this->assertInstanceOf(RouteResult::class, $routeResult); + $this->assertTrue($routeResult->isSuccess()); + $this->assertSame($matches, $routeResult->getMatchedParams()); + $triggered = true; + return $response; + }; + $next = function ($request, $response, $err = null) { + $this->fail('Should not hit next'); + }; + + $request = new ServerRequest(); + $response = new Response(); + $result = RouteResult::fromRouteMatch('resource', $middleware, $matches); + + $this->router->match($request)->willReturn($result); + + $app = $this->getApplication(); + $test = $app->routeMiddleware($request, $response, $next); + $this->assertSame($response, $test); + $this->assertTrue($triggered); + } }