This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Refactor routing and dispatch logic #48
Merged
weierophinney
merged 4 commits into
zendframework:release-3.0.0
from
weierophinney:feature/result-as-middleware
Feb 13, 2018
+268
−104
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-expressive-router for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-router/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zend\Expressive\Router; | ||
|
||
use Fig\Http\Message\StatusCodeInterface as StatusCode; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
/** | ||
* Emit a 405 Method Not Allowed response | ||
* | ||
* If the request composes a route result, and the route result represents a | ||
* failure due to request method, this middleware will emit a 405 response, | ||
* along with an Allow header indicating allowed methods, as reported by the | ||
* route result. | ||
* | ||
* If no route result is composed, and/or it's not the result of a method | ||
* failure, it passes handling to the provided handler. | ||
*/ | ||
class MethodNotAllowedMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* Response prototype for 405 responses. | ||
* | ||
* @var ResponseInterface | ||
*/ | ||
private $responsePrototype; | ||
|
||
public function __construct(ResponseInterface $responsePrototype) | ||
{ | ||
$this->responsePrototype = $responsePrototype; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface | ||
{ | ||
$routeResult = $request->getAttribute(RouteResult::class, false); | ||
if (! $routeResult || ! $routeResult->isMethodFailure()) { | ||
return $handler->handle($request); | ||
} | ||
|
||
return $this->responsePrototype | ||
->withStatus(StatusCode::STATUS_METHOD_NOT_ALLOWED) | ||
->withHeader('Allow', implode(',', $routeResult->getAllowedMethods())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,10 @@ | |
namespace ZendTest\Expressive\Router; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Expressive\Router\DispatchMiddleware; | ||
use Zend\Expressive\Router\Route; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not used anymore, same goes for the $responsePrototype property. |
||
|
@@ -51,18 +51,19 @@ public function testInvokesHandlerIfRequestDoesNotContainRouteResult() | |
$this->assertSame($this->response, $response); | ||
} | ||
|
||
public function testInvokesMatchedMiddlewareWhenRouteResult() | ||
public function testInvokesRouteResultWhenPresent() | ||
{ | ||
$this->handler->handle()->shouldNotBeCalled(); | ||
|
||
$routedMiddleware = $this->prophesize(MiddlewareInterface::class); | ||
$routedMiddleware | ||
->process($this->request->reveal(), $this->handler->reveal()) | ||
$this->handler->handle(Argument::any())->shouldNotBeCalled(); | ||
|
||
$routeResult = $this->prophesize(RouteResult::class); | ||
$routeResult | ||
->process( | ||
Argument::that([$this->request, 'reveal']), | ||
Argument::that([$this->handler, 'reveal']) | ||
) | ||
->willReturn($this->response); | ||
|
||
$routeResult = RouteResult::fromRoute(new Route('/', $routedMiddleware->reveal())); | ||
|
||
$this->request->getAttribute(RouteResult::class, false)->willReturn($routeResult); | ||
$this->request->getAttribute(RouteResult::class, false)->will([$routeResult, 'reveal']); | ||
|
||
$response = $this->middleware->process($this->request->reveal(), $this->handler->reveal()); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-expressive-router for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-router/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ZendTest\Expressive\Router; | ||
|
||
use Fig\Http\Message\StatusCodeInterface as StatusCode; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused. |
||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Expressive\Router\MethodNotAllowedMiddleware; | ||
use Zend\Expressive\Router\RouteResult; | ||
|
||
class MethodNotAllowedMiddlewareTest extends TestCase | ||
{ | ||
/** @var RequestHandlerInterface|ObjectProphecy */ | ||
private $handler; | ||
|
||
/** @var MethodNotAllowedMiddleware */ | ||
private $middleware; | ||
|
||
/** @var ServerRequestInterface|ObjectProphecy */ | ||
private $request; | ||
|
||
/** @var ResponseInterface|ObjectProphecy */ | ||
private $response; | ||
|
||
public function setUp() | ||
{ | ||
$this->handler = $this->prophesize(RequestHandlerInterface::class); | ||
$this->request = $this->prophesize(ServerRequestInterface::class); | ||
$this->response = $this->prophesize(ResponseInterface::class); | ||
$this->middleware = new MethodNotAllowedMiddleware($this->response->reveal()); | ||
} | ||
|
||
public function testDelegatesToHandlerIfNoRouteResultPresentInRequest() | ||
{ | ||
$this->request->getAttribute(RouteResult::class, false)->willReturn(false); | ||
$this->handler->handle(Argument::that([$this->request, 'reveal']))->will([$this->response, 'reveal']); | ||
|
||
$this->response->withStatus(Argument::any())->shouldNotBeCalled(); | ||
$this->response->withHeader('Allow', Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->assertSame( | ||
$this->response->reveal(), | ||
$this->middleware->process($this->request->reveal(), $this->handler->reveal()) | ||
); | ||
} | ||
|
||
public function testDelegatesToHandlerIfRouteResultNotAMethodFailure() | ||
{ | ||
$result = $this->prophesize(RouteResult::class); | ||
$result->isMethodFailure()->willReturn(false); | ||
|
||
$this->request->getAttribute(RouteResult::class, false)->will([$result, 'reveal']); | ||
$this->handler->handle(Argument::that([$this->request, 'reveal']))->will([$this->response, 'reveal']); | ||
|
||
$this->response->withStatus(Argument::any())->shouldNotBeCalled(); | ||
$this->response->withHeader('Allow', Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->assertSame( | ||
$this->response->reveal(), | ||
$this->middleware->process($this->request->reveal(), $this->handler->reveal()) | ||
); | ||
} | ||
|
||
public function testReturns405ResponseWithAllowHeaderIfResultDueToMethodFailure() | ||
{ | ||
$result = $this->prophesize(RouteResult::class); | ||
$result->isMethodFailure()->willReturn(true); | ||
$result->getAllowedMethods()->willReturn(['GET', 'POST']); | ||
|
||
$this->request->getAttribute(RouteResult::class, false)->will([$result, 'reveal']); | ||
$this->handler->handle(Argument::that([$this->request, 'reveal']))->shouldNotBeCalled(); | ||
|
||
$this->response->withStatus(StatusCode::STATUS_METHOD_NOT_ALLOWED)->will([$this->response, 'reveal']); | ||
$this->response->withHeader('Allow', 'GET,POST')->will([$this->response, 'reveal']); | ||
|
||
$this->assertSame( | ||
$this->response->reveal(), | ||
$this->middleware->process($this->request->reveal(), $this->handler->reveal()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second param
false
can be removed, we are not using that isfalse
, the same result we get withnull
(default value).