diff --git a/src/Handler/NotFoundHandler.php b/src/Handler/NotFoundHandler.php new file mode 100644 index 0000000..149d01e --- /dev/null +++ b/src/Handler/NotFoundHandler.php @@ -0,0 +1,54 @@ +responseFactory = function () use ($responseFactory) : ResponseInterface { + return $responseFactory(); + }; + } + + /** + * Creates and returns a 404 response. + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + /** @var ResponseInterface $response */ + $response = ($this->responseFactory)() + ->withStatus(StatusCode::STATUS_NOT_FOUND); + $response->getBody()->write(sprintf( + 'Cannot %s %s', + $request->getMethod(), + (string) $request->getUri() + )); + return $response; + } +} diff --git a/src/Middleware/NotFoundHandler.php b/src/Middleware/NotFoundHandler.php index 0e3ca0f..aac04cc 100644 --- a/src/Middleware/NotFoundHandler.php +++ b/src/Middleware/NotFoundHandler.php @@ -9,20 +9,22 @@ namespace Zend\Stratigility\Middleware; -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; +use Zend\Stratigility\Handler\NotFoundHandler as NotFoundRequestHandler; -use function sprintf; - +/** + * @deprecated Will be removed in v4 in favor of {@see \Zend\Stratigility\Handler\NotFoundHandler} + */ final class NotFoundHandler implements MiddlewareInterface { + /** - * @var callable + * @var NotFoundRequestHandler */ - private $responseFactory; + private $notFoundHandler; /** * @param callable $responseFactory A factory capable of returning an @@ -31,23 +33,14 @@ final class NotFoundHandler implements MiddlewareInterface */ public function __construct(callable $responseFactory) { - $this->responseFactory = function () use ($responseFactory) : ResponseInterface { - return $responseFactory(); - }; + $this->notFoundHandler = new NotFoundRequestHandler($responseFactory); } /** - * Creates and returns a 404 response. + * Uses the {@see \Zend\Stratigility\Handler\NotFoundHandler} to create a 404 response. */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { - $response = ($this->responseFactory)() - ->withStatus(StatusCode::STATUS_NOT_FOUND); - $response->getBody()->write(sprintf( - 'Cannot %s %s', - $request->getMethod(), - (string) $request->getUri() - )); - return $response; + return $this->notFoundHandler->handle($request); } } diff --git a/test/Handler/NotFoundHandlerTest.php b/test/Handler/NotFoundHandlerTest.php new file mode 100644 index 0000000..0902075 --- /dev/null +++ b/test/Handler/NotFoundHandlerTest.php @@ -0,0 +1,44 @@ +prophesize(StreamInterface::class); + $stream->write('Cannot POST https://example.com/foo'); + + $response = $this->prophesize(ResponseInterface::class); + $response->withStatus(404)->will([$response, 'reveal']); + $response->getBody()->will([$stream, 'reveal']); + + $request = $this->prophesize(ServerRequestInterface::class); + $request->getMethod()->willReturn('POST'); + $request->getUri()->willReturn('https://example.com/foo'); + + $responseFactory = function () use ($response) { + return $response->reveal(); + }; + + $middleware = new NotFoundHandler($responseFactory); + + $this->assertSame( + $response->reveal(), + $middleware->handle($request->reveal()) + ); + } +}