2018-08-11 23:46:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
|
|
|
use Engelsystem\Application;
|
|
|
|
use Engelsystem\Exceptions\Handler;
|
|
|
|
use Engelsystem\Http\Response;
|
|
|
|
use Engelsystem\Middleware\ExceptionHandler;
|
|
|
|
use Engelsystem\Test\Unit\Middleware\Stub\ExceptionMiddlewareHandler;
|
|
|
|
use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler;
|
|
|
|
use Illuminate\Contracts\Container\Container as ContainerInterface;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-08-11 23:46:28 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
|
|
|
class ExceptionHandlerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\ExceptionHandler::__construct
|
|
|
|
* @covers \Engelsystem\Middleware\ExceptionHandler::process
|
|
|
|
*/
|
|
|
|
public function testRegister(): void
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var ContainerInterface|MockObject $container */
|
2018-08-11 23:46:28 +02:00
|
|
|
$container = $this->getMockForAbstractClass(ContainerInterface::class);
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
2018-08-11 23:46:28 +02:00
|
|
|
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var ResponseInterface|MockObject $response */
|
2018-08-11 23:46:28 +02:00
|
|
|
$response = $this->getMockBuilder(Response::class)->getMock();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Handler|MockObject $errorHandler */
|
2018-08-11 23:46:28 +02:00
|
|
|
$errorHandler = $this->getMockBuilder(Handler::class)->getMock();
|
|
|
|
$returnResponseHandler = new ReturnResponseMiddlewareHandler($response);
|
|
|
|
$throwExceptionHandler = new ExceptionMiddlewareHandler();
|
|
|
|
|
|
|
|
Application::setInstance($container);
|
|
|
|
|
|
|
|
$container->expects($this->exactly(2))
|
|
|
|
->method('get')
|
|
|
|
->withConsecutive(['error.handler'], ['psr7.response'])
|
|
|
|
->willReturnOnConsecutiveCalls($errorHandler, $response);
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('withContent')
|
|
|
|
->willReturn($response);
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('withStatus')
|
|
|
|
->with(500)
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$handler = new ExceptionHandler($container);
|
|
|
|
$return = $handler->process($request, $returnResponseHandler);
|
|
|
|
$this->assertEquals($response, $return);
|
|
|
|
|
|
|
|
$return = $handler->process($request, $throwExceptionHandler);
|
|
|
|
$this->assertEquals($response, $return);
|
|
|
|
}
|
|
|
|
}
|