2018-08-29 13:58:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
2019-07-09 21:43:18 +02:00
|
|
|
use Engelsystem\Application;
|
2018-11-13 17:48:07 +01:00
|
|
|
use Engelsystem\Http\Exceptions\HttpException;
|
2019-07-09 21:43:18 +02:00
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
|
|
|
use Engelsystem\Http\Psr7ServiceProvider;
|
|
|
|
use Engelsystem\Http\Request;
|
2018-08-29 13:58:50 +02:00
|
|
|
use Engelsystem\Http\Response;
|
2019-07-09 21:43:18 +02:00
|
|
|
use Engelsystem\Http\ResponseServiceProvider;
|
|
|
|
use Engelsystem\Http\Validation\Validator;
|
2018-08-29 13:58:50 +02:00
|
|
|
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;
|
2019-07-09 21:43:18 +02:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
2018-08-29 13:58:50 +02:00
|
|
|
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);
|
2018-12-18 02:16:25 +01:00
|
|
|
$psrResponse->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('content-type')
|
|
|
|
->willReturn([]);
|
2018-08-29 13:58:50 +02:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2018-12-18 02:16:25 +01:00
|
|
|
$response->expects($this->exactly(4))
|
2018-08-29 13:58:50 +02:00
|
|
|
->method('getStatusCode')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
200,
|
|
|
|
418,
|
2018-12-18 02:16:25 +01:00
|
|
|
505,
|
2018-08-29 13:58:50 +02:00
|
|
|
505
|
|
|
|
);
|
2018-12-18 02:16:25 +01:00
|
|
|
$response->expects($this->exactly(4))
|
|
|
|
->method('getHeader')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
['application/json']
|
|
|
|
);
|
2018-08-29 13:58:50 +02:00
|
|
|
|
|
|
|
$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);
|
2018-12-18 02:16:25 +01:00
|
|
|
$errorHandler->process($request, $returnResponseHandler);
|
2018-08-29 13:58:50 +02:00
|
|
|
}
|
2018-11-13 17:48:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\ErrorHandler::process
|
|
|
|
*/
|
2019-07-09 21:43:18 +02:00
|
|
|
public function testProcessHttpException()
|
2018-11-13 17:48:07 +01:00
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $psrResponse */
|
|
|
|
$psrResponse = $this->getMockForAbstractClass(ResponseInterface::class);
|
|
|
|
/** @var ReturnResponseMiddlewareHandler|MockObject $returnResponseHandler */
|
|
|
|
$returnResponseHandler = $this->getMockBuilder(ReturnResponseMiddlewareHandler::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$psrResponse->expects($this->once())
|
|
|
|
->method('getStatusCode')
|
|
|
|
->willReturn(300);
|
2018-12-18 02:16:25 +01:00
|
|
|
$psrResponse->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('content-type')
|
|
|
|
->willReturn([]);
|
2018-11-13 17:48:07 +01:00
|
|
|
|
|
|
|
$returnResponseHandler->expects($this->once())
|
|
|
|
->method('handle')
|
|
|
|
->willReturnCallback(function () {
|
|
|
|
throw new HttpException(300, 'Some response', ['lor' => 'em']);
|
|
|
|
});
|
|
|
|
|
|
|
|
/** @var ErrorHandler|MockObject $errorHandler */
|
|
|
|
$errorHandler = $this->getMockBuilder(ErrorHandler::class)
|
|
|
|
->disableOriginalConstructor()
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['createResponse'])
|
2018-11-13 17:48:07 +01:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$errorHandler->expects($this->once())
|
|
|
|
->method('createResponse')
|
|
|
|
->with('Some response', 300, ['lor' => 'em'])
|
|
|
|
->willReturn($psrResponse);
|
|
|
|
|
|
|
|
$return = $errorHandler->process($request, $returnResponseHandler);
|
|
|
|
$this->assertEquals($psrResponse, $return);
|
|
|
|
}
|
2018-11-20 18:57:11 +01:00
|
|
|
|
2019-07-09 21:43:18 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\ErrorHandler::process
|
|
|
|
* @covers \Engelsystem\Middleware\ErrorHandler::getPreviousUrl
|
|
|
|
*/
|
|
|
|
public function testProcessValidationException()
|
|
|
|
{
|
|
|
|
/** @var TwigLoader|MockObject $twigLoader */
|
|
|
|
$twigLoader = $this->createMock(TwigLoader::class);
|
|
|
|
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
|
|
|
$validator = $this->createMock(Validator::class);
|
|
|
|
|
|
|
|
$handler->expects($this->exactly(2))
|
|
|
|
->method('handle')
|
|
|
|
->willReturnCallback(function () use ($validator) {
|
|
|
|
throw new ValidationException($validator);
|
|
|
|
});
|
|
|
|
|
|
|
|
$validator->expects($this->exactly(2))
|
|
|
|
->method('getErrors')
|
|
|
|
->willReturn(['foo' => ['validation.foo.numeric']]);
|
|
|
|
|
|
|
|
$session = new Session(new MockArraySessionStorage());
|
|
|
|
$session->set('errors', ['validation' => ['foo' => ['validation.foo.required']]]);
|
2019-07-21 13:24:47 +02:00
|
|
|
$request = Request::create(
|
|
|
|
'/foo/bar',
|
|
|
|
'POST',
|
|
|
|
['foo' => 'bar', 'password' => 'Test123', 'password_confirmation' => 'Test1234']
|
|
|
|
);
|
2019-07-09 21:43:18 +02:00
|
|
|
$request->setSession($session);
|
|
|
|
|
|
|
|
/** @var Application $app */
|
|
|
|
$app = app();
|
|
|
|
(new ResponseServiceProvider($app))->register();
|
|
|
|
(new Psr7ServiceProvider($app))->register();
|
|
|
|
|
|
|
|
$errorHandler = new ErrorHandler($twigLoader);
|
|
|
|
|
|
|
|
$return = $errorHandler->process($request, $handler);
|
|
|
|
|
|
|
|
$this->assertEquals(302, $return->getStatusCode());
|
|
|
|
$this->assertEquals('/', $return->getHeaderLine('location'));
|
|
|
|
$this->assertEquals([
|
|
|
|
'errors' => [
|
|
|
|
'validation' => [
|
|
|
|
'foo' => [
|
|
|
|
'validation.foo.required',
|
|
|
|
'validation.foo.numeric',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'form-data' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
],
|
|
|
|
], $session->all());
|
|
|
|
|
|
|
|
$request = $request->withAddedHeader('referer', '/foo/batz');
|
|
|
|
$return = $errorHandler->process($request, $handler);
|
|
|
|
|
|
|
|
$this->assertEquals('/foo/batz', $return->getHeaderLine('location'));
|
|
|
|
}
|
|
|
|
|
2018-11-20 18:57:11 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\ErrorHandler::process
|
|
|
|
*/
|
|
|
|
public function testProcessContentTypeSniffer()
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
/** @var TwigLoader|MockObject $twigLoader */
|
|
|
|
$twigLoader = $this->createMock(TwigLoader::class);
|
2019-07-09 21:43:18 +02:00
|
|
|
$response = new Response('<!DOCTYPE html><html lang="en"><body><h1>Hi!</h1></body></html>', 500);
|
2018-11-20 18:57:11 +01:00
|
|
|
$returnResponseHandler = new ReturnResponseMiddlewareHandler($response);
|
|
|
|
|
|
|
|
/** @var ErrorHandler|MockObject $errorHandler */
|
|
|
|
$errorHandler = new ErrorHandler($twigLoader);
|
|
|
|
|
|
|
|
$return = $errorHandler->process($request, $returnResponseHandler);
|
|
|
|
$this->assertEquals($response, $return);
|
|
|
|
}
|
2018-08-29 13:58:50 +02:00
|
|
|
}
|