Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Handler/NotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* @see https://github.com/zendframework/zend-stratigility for the canonical source repository
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace Zend\Stratigility\Handler;

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 function sprintf;

final class NotFoundHandler implements RequestHandlerInterface
{
/**
* @var callable
*/
private $responseFactory;

/**
* @param callable $responseFactory A factory capable of returning an
* empty ResponseInterface instance to update and return when returning
* an 404 response.
*/
public function __construct(callable $responseFactory)
{
$this->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;
}
}
27 changes: 10 additions & 17 deletions src/Middleware/NotFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
44 changes: 44 additions & 0 deletions test/Handler/NotFoundHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @see https://github.com/zendframework/zend-stratigility for the canonical source repository
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace ZendTest\Stratigility\Handler;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Zend\Stratigility\Handler\NotFoundHandler;

class NotFoundHandlerTest extends TestCase
{
public function testReturnsResponseWith404StatusAndErrorMessageInBody()
{
$stream = $this->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())
);
}
}