Skip to content

Commit cfef695

Browse files
committed
Add NotFoundHandler
1 parent c4ef3f2 commit cfef695

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

src/Handler/NotFoundHandler.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HttpSoft\Basis\Handler;
6+
7+
use HttpSoft\Basis\TemplateRendererInterface;
8+
use HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface;
9+
use Psr\Http\Message\ResponseFactoryInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\ServerRequestInterface;
12+
use Psr\Http\Server\RequestHandlerInterface;
13+
14+
final class NotFoundHandler implements RequestHandlerInterface
15+
{
16+
/**
17+
* @var ResponseFactoryInterface
18+
*/
19+
private ResponseFactoryInterface $responseFactory;
20+
21+
/**
22+
* @var TemplateRendererInterface
23+
*/
24+
private TemplateRendererInterface $template;
25+
26+
/**
27+
* @var string
28+
*/
29+
private string $view;
30+
31+
/**
32+
* @var bool
33+
*/
34+
private bool $debug;
35+
36+
/**
37+
* @param ResponseFactoryInterface $responseFactory
38+
* @param TemplateRendererInterface $template
39+
* @param string $view
40+
* @param bool $debug
41+
*/
42+
public function __construct(
43+
ResponseFactoryInterface $responseFactory,
44+
TemplateRendererInterface $template,
45+
string $view,
46+
bool $debug = false
47+
) {
48+
$this->responseFactory = $responseFactory;
49+
$this->template = $template;
50+
$this->view = $view;
51+
$this->debug = $debug;
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
public function handle(ServerRequestInterface $request): ResponseInterface
58+
{
59+
$response = $this->responseFactory->createResponse(ErrorResponseGeneratorInterface::STATUS_NOT_FOUND);
60+
61+
$response->getBody()->write($this->template->render($this->view, [
62+
'debug' => $this->debug,
63+
'request' => $request,
64+
]));
65+
66+
return $response;
67+
}
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HttpSoft\Tests\Basis\Handler;
6+
7+
use HttpSoft\Basis\Handler\NotFoundHandler;
8+
use HttpSoft\Basis\Response\CustomResponseFactory;
9+
use HttpSoft\Basis\TemplateRendererInterface;
10+
use HttpSoft\Message\ServerRequestFactory;
11+
use HttpSoft\Tests\Basis\TestAsset\TemplateRenderer;
12+
use PHPUnit\Framework\TestCase;
13+
use Psr\Http\Message\ResponseFactoryInterface;
14+
use Psr\Http\Message\ServerRequestInterface;
15+
16+
use function get_class;
17+
use function trim;
18+
19+
class NotFoundHandlerTest extends TestCase
20+
{
21+
/**
22+
* @var ServerRequestInterface
23+
*/
24+
private ServerRequestInterface $request;
25+
26+
/**
27+
* @var TemplateRendererInterface
28+
*/
29+
private TemplateRendererInterface $renderer;
30+
31+
/**
32+
* @var ResponseFactoryInterface
33+
*/
34+
private ResponseFactoryInterface $responseFactory;
35+
36+
public function setUp(): void
37+
{
38+
$this->request = (new ServerRequestFactory())->createServerRequest('GET', 'http://example.com');
39+
$this->renderer = new TemplateRenderer();
40+
$this->responseFactory = new CustomResponseFactory();
41+
}
42+
43+
public function testHandleWithoutDebugMode(): void
44+
{
45+
$handler = new NotFoundHandler($this->responseFactory, $this->renderer, 'not-found', false);
46+
$response = $handler->handle($this->request);
47+
48+
$expectedContent = '404 Not Found';
49+
$this->assertSame($expectedContent, trim((string) $response->getBody()));
50+
}
51+
52+
public function testHandleWithDebugMode(): void
53+
{
54+
$handler = new NotFoundHandler($this->responseFactory, $this->renderer, 'not-found', true);
55+
$response = $handler->handle($this->request);
56+
57+
$expectedContent = get_class($this->renderer->getEngine()) . ':' . get_class($this->request);
58+
$this->assertSame($expectedContent, trim((string) $response->getBody()));
59+
}
60+
}

0 commit comments

Comments
 (0)