2017-11-20 17:08:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Exceptions;
|
|
|
|
|
2023-01-24 19:23:57 +01:00
|
|
|
use Engelsystem\Environment;
|
2017-11-20 17:08:05 +01:00
|
|
|
use Engelsystem\Exceptions\Handler;
|
2017-11-24 15:08:43 +01:00
|
|
|
use Engelsystem\Exceptions\Handlers\HandlerInterface;
|
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
use ErrorException;
|
|
|
|
use Exception;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-11-20 17:08:05 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class HandlerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Exceptions\Handler::__construct()
|
2017-11-24 15:08:43 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testCreate(): void
|
2017-11-24 15:08:43 +01:00
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Handler|MockObject $handler */
|
2017-11-24 15:08:43 +01:00
|
|
|
$handler = new Handler();
|
|
|
|
$this->assertInstanceOf(Handler::class, $handler);
|
2023-01-24 19:23:57 +01:00
|
|
|
$this->assertEquals(Environment::PRODUCTION, $handler->getEnvironment());
|
2017-11-24 15:08:43 +01:00
|
|
|
|
2023-01-24 19:23:57 +01:00
|
|
|
$anotherHandler = new Handler(Environment::DEVELOPMENT);
|
|
|
|
$this->assertEquals(Environment::DEVELOPMENT, $anotherHandler->getEnvironment());
|
2017-11-24 15:08:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Exceptions\Handler::errorHandler()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testErrorHandler(): void
|
2017-11-24 15:08:43 +01:00
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Handler|MockObject $handler */
|
2017-11-24 15:08:43 +01:00
|
|
|
$handler = $this->getMockBuilder(Handler::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['exceptionHandler'])
|
2017-11-24 15:08:43 +01:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$handler->expects($this->once())
|
|
|
|
->method('exceptionHandler')
|
|
|
|
->with($this->isInstanceOf(ErrorException::class));
|
|
|
|
|
|
|
|
$handler->errorHandler(1, 'Foo and bar!', '/lo/rem.php', 123);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Exceptions\Handler::exceptionHandler()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testExceptionHandler(): void
|
2017-11-24 15:08:43 +01:00
|
|
|
{
|
|
|
|
$exception = new Exception();
|
2018-08-07 03:06:21 +02:00
|
|
|
$errorMessage = 'Oh noes, an error!';
|
2017-11-24 15:08:43 +01:00
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var HandlerInterface|MockObject $handlerMock */
|
2017-11-24 15:08:43 +01:00
|
|
|
$handlerMock = $this->getMockForAbstractClass(HandlerInterface::class);
|
2018-08-07 03:06:21 +02:00
|
|
|
$handlerMock->expects($this->atLeastOnce())
|
2017-11-24 15:08:43 +01:00
|
|
|
->method('report')
|
|
|
|
->with($exception);
|
2018-08-07 03:06:21 +02:00
|
|
|
$handlerMock->expects($this->atLeastOnce())
|
2017-11-24 15:08:43 +01:00
|
|
|
->method('render')
|
2018-08-07 03:06:21 +02:00
|
|
|
->with($this->isInstanceOf(Request::class), $exception)
|
2022-12-14 19:15:20 +01:00
|
|
|
->willReturnCallback(function () use ($errorMessage): void {
|
2018-08-07 03:06:21 +02:00
|
|
|
echo $errorMessage;
|
|
|
|
});
|
2017-11-24 15:08:43 +01:00
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Handler|MockObject $handler */
|
2017-11-24 15:08:43 +01:00
|
|
|
$handler = $this->getMockBuilder(Handler::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['terminateApplicationImmediately'])
|
2017-11-24 15:08:43 +01:00
|
|
|
->getMock();
|
|
|
|
$handler->expects($this->once())
|
2018-08-04 22:01:07 +02:00
|
|
|
->method('terminateApplicationImmediately');
|
2017-11-24 15:08:43 +01:00
|
|
|
|
2023-01-24 19:23:57 +01:00
|
|
|
$handler->setHandler(Environment::PRODUCTION, $handlerMock);
|
2017-11-24 15:08:43 +01:00
|
|
|
|
2018-08-07 03:06:21 +02:00
|
|
|
$this->expectOutputString($errorMessage);
|
2017-11-24 15:08:43 +01:00
|
|
|
$handler->exceptionHandler($exception);
|
2018-08-07 03:06:21 +02:00
|
|
|
|
|
|
|
$return = $handler->exceptionHandler($exception, true);
|
|
|
|
$this->assertEquals($errorMessage, $return);
|
2017-11-24 15:08:43 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 17:08:05 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Exceptions\Handler::getEnvironment()
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Exceptions\Handler::setEnvironment()
|
2017-11-20 17:08:05 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testEnvironment(): void
|
2017-11-20 17:08:05 +01:00
|
|
|
{
|
2017-11-24 15:08:43 +01:00
|
|
|
$handler = new Handler();
|
2017-11-20 17:08:05 +01:00
|
|
|
|
2023-01-24 19:23:57 +01:00
|
|
|
$handler->setEnvironment(Environment::DEVELOPMENT);
|
|
|
|
$this->assertEquals(Environment::DEVELOPMENT, $handler->getEnvironment());
|
2017-11-20 17:08:05 +01:00
|
|
|
|
2023-01-24 19:23:57 +01:00
|
|
|
$handler->setEnvironment(Environment::PRODUCTION);
|
|
|
|
$this->assertEquals(Environment::PRODUCTION, $handler->getEnvironment());
|
2017-11-20 17:08:05 +01:00
|
|
|
}
|
2017-11-24 15:08:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Exceptions\Handler::getHandler()
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Exceptions\Handler::setHandler()
|
2017-11-24 15:08:43 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testHandler(): void
|
2017-11-24 15:08:43 +01:00
|
|
|
{
|
|
|
|
$handler = new Handler();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var HandlerInterface|MockObject $devHandler */
|
2017-11-24 15:08:43 +01:00
|
|
|
$devHandler = $this->getMockForAbstractClass(HandlerInterface::class);
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var HandlerInterface|MockObject $prodHandler */
|
2017-11-24 15:08:43 +01:00
|
|
|
$prodHandler = $this->getMockForAbstractClass(HandlerInterface::class);
|
|
|
|
|
2023-01-24 19:23:57 +01:00
|
|
|
$handler->setHandler(Environment::DEVELOPMENT, $devHandler);
|
|
|
|
$handler->setHandler(Environment::PRODUCTION, $prodHandler);
|
|
|
|
$this->assertEquals($devHandler, $handler->getHandler(Environment::DEVELOPMENT));
|
|
|
|
$this->assertEquals($prodHandler, $handler->getHandler(Environment::PRODUCTION));
|
2017-11-24 15:08:43 +01:00
|
|
|
$this->assertCount(2, $handler->getHandler());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Exceptions\Handler::getRequest()
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Exceptions\Handler::setRequest()
|
2017-11-24 15:08:43 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRequest(): void
|
2017-11-24 15:08:43 +01:00
|
|
|
{
|
|
|
|
$handler = new Handler();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Request|MockObject $request */
|
2017-11-24 15:08:43 +01:00
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
|
|
|
|
$handler->setRequest($request);
|
|
|
|
$this->assertEquals($request, $handler->getRequest());
|
|
|
|
}
|
2017-11-20 17:08:05 +01:00
|
|
|
}
|