Added generic error pages
parent
427315195b
commit
a2c47304d8
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Middleware;
|
||||
|
||||
use Engelsystem\Http\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Twig_LoaderInterface as TwigLoader;
|
||||
|
||||
class ErrorHandler implements MiddlewareInterface
|
||||
{
|
||||
/** @var TwigLoader */
|
||||
protected $loader;
|
||||
|
||||
/** @var string */
|
||||
protected $viewPrefix = 'errors/';
|
||||
|
||||
/**
|
||||
* @param TwigLoader $loader
|
||||
*/
|
||||
public function __construct(TwigLoader $loader)
|
||||
{
|
||||
$this->loader = $loader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles any error messages
|
||||
*
|
||||
* Should be added at the beginning
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @param RequestHandlerInterface $handler
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function process(
|
||||
ServerRequestInterface $request,
|
||||
RequestHandlerInterface $handler
|
||||
): ResponseInterface {
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
if ($statusCode < 400 || !$response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$view = $this->selectView($statusCode);
|
||||
|
||||
return $response->withView(
|
||||
$this->viewPrefix . $view,
|
||||
[
|
||||
'status' => $statusCode,
|
||||
'content' => $response->getContent(),
|
||||
],
|
||||
$statusCode,
|
||||
$response->getHeaders()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a view based on the given status code
|
||||
*
|
||||
* @param int $statusCode
|
||||
* @return string
|
||||
*/
|
||||
protected function selectView(int $statusCode): string
|
||||
{
|
||||
$hundreds = intdiv($statusCode, 100);
|
||||
|
||||
$viewsList = [$statusCode, $hundreds, $hundreds * 100];
|
||||
foreach ($viewsList as $view) {
|
||||
if ($this->loader->exists($this->viewPrefix . $view)) {
|
||||
return $view;
|
||||
}
|
||||
}
|
||||
|
||||
return 'default';
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
{% extends "layouts/app.twig" %}
|
||||
|
||||
{% block title %}Error {{ status }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="alert alert-info">{{ content }}</div>
|
||||
{% endblock %}
|
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Middleware;
|
||||
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Middleware\ErrorHandler;
|
||||
use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Twig_LoaderInterface as TwigLoader;
|
||||
|
||||
class ErrorHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Middleware\ErrorHandler::__construct
|
||||
* @covers \Engelsystem\Middleware\ErrorHandler::process
|
||||
* @covers \Engelsystem\Middleware\ErrorHandler::selectView
|
||||
*/
|
||||
public function testProcess()
|
||||
{
|
||||
/** @var TwigLoader|MockObject $twigLoader */
|
||||
$twigLoader = $this->createMock(TwigLoader::class);
|
||||
/** @var ServerRequestInterface|MockObject $request */
|
||||
$request = $this->createMock(ServerRequestInterface::class);
|
||||
/** @var ResponseInterface|MockObject $psrResponse */
|
||||
$psrResponse = $this->getMockForAbstractClass(ResponseInterface::class);
|
||||
$returnResponseHandler = new ReturnResponseMiddlewareHandler($psrResponse);
|
||||
|
||||
$psrResponse->expects($this->once())
|
||||
->method('getStatusCode')
|
||||
->willReturn(505);
|
||||
|
||||
$errorHandler = new ErrorHandler($twigLoader);
|
||||
|
||||
$return = $errorHandler->process($request, $returnResponseHandler);
|
||||
$this->assertEquals($psrResponse, $return, 'Plain PSR-7 Response should be passed directly');
|
||||
|
||||
/** @var Response|MockObject $response */
|
||||
$response = $this->createMock(Response::class);
|
||||
|
||||
$response->expects($this->exactly(3))
|
||||
->method('getStatusCode')
|
||||
->willReturnOnConsecutiveCalls(
|
||||
200,
|
||||
418,
|
||||
505
|
||||
);
|
||||
|
||||
$returnResponseHandler->setResponse($response);
|
||||
$return = $errorHandler->process($request, $returnResponseHandler);
|
||||
$this->assertEquals($response, $return, 'Only Responses >= 400 should be processed');
|
||||
|
||||
$twigLoader->expects($this->exactly(4))
|
||||
->method('exists')
|
||||
->withConsecutive(
|
||||
['errors/418'],
|
||||
['errors/4'],
|
||||
['errors/400'],
|
||||
['errors/505']
|
||||
)
|
||||
->willReturnOnConsecutiveCalls(
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true
|
||||
);
|
||||
|
||||
$response->expects($this->exactly(2))
|
||||
->method('getContent')
|
||||
->willReturnOnConsecutiveCalls(
|
||||
'Teapot',
|
||||
'Internal Error!'
|
||||
);
|
||||
|
||||
$response->expects($this->exactly(2))
|
||||
->method('withView')
|
||||
->withConsecutive(
|
||||
['errors/default', ['status' => 418, 'content' => 'Teapot'], 418],
|
||||
['errors/505', ['status' => 505, 'content' => 'Internal Error!'], 505]
|
||||
)
|
||||
->willReturn($response);
|
||||
|
||||
$errorHandler->process($request, $returnResponseHandler);
|
||||
$errorHandler->process($request, $returnResponseHandler);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue