Skip to content

Commit

Permalink
Merge pull request #8 from boesing/not-found-handler-to-request-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
boesing committed Oct 20, 2020
2 parents fea8fd1 + b70fce4 commit 54e0bfc
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 19 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -14,11 +14,11 @@ details.

### Changed

- Nothing.
- [#8](https://github.com/laminas/laminas-stratigility/pull/8) As the `NotFoundHandler` is technically a request handler, we are moving it to the `Handler` namespace with only implementing the `RequestHandlerInterface` instead of the `MiddlewareInterface`.

### Deprecated

- Nothing.
- [#8](https://github.com/laminas/laminas-stratigility/pull/8) Marking `NotFoundHandler` in the `Middleware` namespace as deprecated in favor of the new `NotFoundHandler` in the `Handler` namespace.

### Removed

Expand Down
54 changes: 54 additions & 0 deletions src/Handler/NotFoundHandler.php
@@ -0,0 +1,54 @@
<?php

/**
* @see https://github.com/laminas/laminas-stratigility for the canonical source repository
* @copyright https://github.com/laminas/laminas-stratigility/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-stratigility/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace Laminas\Stratigility\Handler;

use Fig\Http\Message\StatusCodeInterface as StatusCode;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
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
Expand Up @@ -10,20 +10,22 @@

namespace Laminas\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 Laminas\Stratigility\Handler\NotFoundHandler as NotFoundRequestHandler;

use function sprintf;

/**
* @deprecated Will be removed in v4 in favor of {@see \Laminas\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 @@ -32,23 +34,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 \Laminas\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
@@ -0,0 +1,44 @@
<?php
/**
* @see https://github.com/laminas/laminas-stratigility for the canonical source repository
* @copyright https://github.com/laminas/laminas-stratigility/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-stratigility/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace LaminasTest\Stratigility\Handler;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Laminas\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())
);
}
}

0 comments on commit 54e0bfc

Please sign in to comment.