engelsystem/tests/Unit/Exceptions/HandlerTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
5.1 KiB
PHP
Raw Normal View History

2017-11-20 17:08:05 +01:00
<?php
namespace Engelsystem\Test\Unit\Exceptions;
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;
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
*/
public function testCreate(): void
{
/** @var Handler|MockObject $handler */
2017-11-24 15:08:43 +01:00
$handler = new Handler();
$this->assertInstanceOf(Handler::class, $handler);
$this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment());
$anotherHandler = new Handler(Handler::ENV_DEVELOPMENT);
$this->assertEquals(Handler::ENV_DEVELOPMENT, $anotherHandler->getEnvironment());
}
/**
* @covers \Engelsystem\Exceptions\Handler::errorHandler()
*/
public function testErrorHandler(): void
{
/** @var Handler|MockObject $handler */
2017-11-24 15:08:43 +01:00
$handler = $this->getMockBuilder(Handler::class)
->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()
*/
public function testExceptionHandler(): void
{
$exception = new Exception();
$errorMessage = 'Oh noes, an error!';
2017-11-24 15:08:43 +01:00
/** @var HandlerInterface|MockObject $handlerMock */
2017-11-24 15:08:43 +01:00
$handlerMock = $this->getMockForAbstractClass(HandlerInterface::class);
$handlerMock->expects($this->atLeastOnce())
2017-11-24 15:08:43 +01:00
->method('report')
->with($exception);
$handlerMock->expects($this->atLeastOnce())
2017-11-24 15:08:43 +01:00
->method('render')
->with($this->isInstanceOf(Request::class), $exception)
->willReturnCallback(function () use ($errorMessage): void {
echo $errorMessage;
});
2017-11-24 15:08:43 +01:00
/** @var Handler|MockObject $handler */
2017-11-24 15:08:43 +01:00
$handler = $this->getMockBuilder(Handler::class)
->onlyMethods(['terminateApplicationImmediately'])
2017-11-24 15:08:43 +01:00
->getMock();
$handler->expects($this->once())
->method('terminateApplicationImmediately');
2017-11-24 15:08:43 +01:00
$handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock);
$this->expectOutputString($errorMessage);
2017-11-24 15:08:43 +01:00
$handler->exceptionHandler($exception);
$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::register()
*/
public function testRegister(): void
{
/** @var Handler|MockObject $handler */
2017-11-20 17:08:05 +01:00
$handler = $this->getMockForAbstractClass(Handler::class);
$handler->register();
2017-11-24 15:08:43 +01:00
set_error_handler($errorHandler = set_error_handler('var_dump'));
$this->assertEquals($handler, array_shift($errorHandler));
set_exception_handler($exceptionHandler = set_error_handler('var_dump'));
$this->assertEquals($handler, array_shift($exceptionHandler));
restore_error_handler();
restore_exception_handler();
2017-11-20 17:08:05 +01:00
}
/**
* @covers \Engelsystem\Exceptions\Handler::getEnvironment()
* @covers \Engelsystem\Exceptions\Handler::setEnvironment()
2017-11-20 17:08:05 +01:00
*/
public function testEnvironment(): void
{
2017-11-24 15:08:43 +01:00
$handler = new Handler();
2017-11-20 17:08:05 +01:00
$handler->setEnvironment(Handler::ENV_DEVELOPMENT);
$this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment());
$handler->setEnvironment(Handler::ENV_PRODUCTION);
$this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment());
}
2017-11-24 15:08:43 +01:00
/**
* @covers \Engelsystem\Exceptions\Handler::getHandler()
* @covers \Engelsystem\Exceptions\Handler::setHandler()
2017-11-24 15:08:43 +01:00
*/
public function testHandler(): void
{
$handler = new Handler();
/** @var HandlerInterface|MockObject $devHandler */
2017-11-24 15:08:43 +01:00
$devHandler = $this->getMockForAbstractClass(HandlerInterface::class);
/** @var HandlerInterface|MockObject $prodHandler */
2017-11-24 15:08:43 +01:00
$prodHandler = $this->getMockForAbstractClass(HandlerInterface::class);
$handler->setHandler(Handler::ENV_DEVELOPMENT, $devHandler);
$handler->setHandler(Handler::ENV_PRODUCTION, $prodHandler);
$this->assertEquals($devHandler, $handler->getHandler(Handler::ENV_DEVELOPMENT));
$this->assertEquals($prodHandler, $handler->getHandler(Handler::ENV_PRODUCTION));
$this->assertCount(2, $handler->getHandler());
}
/**
* @covers \Engelsystem\Exceptions\Handler::getRequest()
* @covers \Engelsystem\Exceptions\Handler::setRequest()
2017-11-24 15:08:43 +01:00
*/
public function testRequest(): void
{
$handler = new Handler();
/** @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
}