Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from boesing/not-found-handler-to-request-handler
- Loading branch information
Showing
4 changed files
with
110 additions
and
19 deletions.
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/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; | ||
} | ||
} |
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,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()) | ||
); | ||
} | ||
} |