summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2017-11-24 15:08:43 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2017-11-25 11:27:38 +0100
commit25e434bce4986b48bd72729a55aa1096e5a76be3 (patch)
treeb254fafaf789b4ebcc980fec8281675415706750 /tests
parent6eea072376cc9fd1034342a0e1d2173681268138 (diff)
Refactored ExceptionHandler
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Exceptions/ExceptionsServiceProviderTest.php94
-rw-r--r--tests/Unit/Exceptions/HandlerTest.php108
-rw-r--r--tests/Unit/Exceptions/Handlers/LegacyDevelopmentTest.php35
-rw-r--r--tests/Unit/Exceptions/Handlers/LegacyTest.php55
-rw-r--r--tests/Unit/Exceptions/Handlers/WhoopsTest.php83
5 files changed, 364 insertions, 11 deletions
diff --git a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php
index 01fb2f11..4f2ae654 100644
--- a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php
+++ b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php
@@ -2,30 +2,108 @@
namespace Engelsystem\Test\Unit\Exceptions;
-use Engelsystem\Exceptions\BasicHandler as ExceptionHandler;
use Engelsystem\Exceptions\ExceptionsServiceProvider;
use Engelsystem\Exceptions\Handler;
+use Engelsystem\Exceptions\Handlers\HandlerInterface;
+use Engelsystem\Exceptions\Handlers\Legacy;
+use Engelsystem\Exceptions\Handlers\LegacyDevelopment;
+use Engelsystem\Exceptions\Handlers\Whoops;
+use Engelsystem\Http\Request;
use Engelsystem\Test\Unit\ServiceProviderTest;
-use PHPUnit_Framework_MockObject_MockObject;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
class ExceptionsServiceProviderTest extends ServiceProviderTest
{
/**
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register()
+ * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addProductionHandler()
+ * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addDevelopmentHandler()
*/
public function testRegister()
{
- /** @var PHPUnit_Framework_MockObject_MockObject|ExceptionHandler $exceptionHandler */
- $exceptionHandler = $this->getMockBuilder(ExceptionHandler::class)
+ $app = $this->getApp(['make', 'instance', 'bind']);
+
+ /** @var MockObject|Handler $handler */
+ $handler = $this->createMock(Handler::class);
+ $this->setExpects($handler, 'register');
+ /** @var Legacy|MockObject $legacyHandler */
+ $legacyHandler = $this->createMock(Legacy::class);
+ /** @var LegacyDevelopment|MockObject $developmentHandler */
+ $developmentHandler = $this->createMock(LegacyDevelopment::class);
+
+ $whoopsHandler = $this->getMockBuilder(Whoops::class)
+ ->setConstructorArgs([$app])
->getMock();
- $app = $this->getApp(['make', 'instance', 'bind']);
+ $app->expects($this->exactly(3))
+ ->method('instance')
+ ->withConsecutive(
+ ['error.handler.production', $legacyHandler],
+ ['error.handler.development', $whoopsHandler],
+ ['error.handler', $handler]
+ );
- $this->setExpects($app, 'make', [ExceptionHandler::class], $exceptionHandler);
- $this->setExpects($app, 'instance', ['error.handler', $exceptionHandler]);
- $this->setExpects($app, 'bind', [Handler::class, 'error.handler']);
+ $app->expects($this->exactly(4))
+ ->method('make')
+ ->withConsecutive(
+ [Handler::class],
+ [Legacy::class],
+ [LegacyDevelopment::class],
+ [Whoops::class]
+ )
+ ->willReturnOnConsecutiveCalls(
+ $handler,
+ $legacyHandler,
+ $developmentHandler,
+ $whoopsHandler
+ );
+
+ $app->expects($this->exactly(2))
+ ->method('bind')
+ ->withConsecutive(
+ [HandlerInterface::class, 'error.handler.production'],
+ [Handler::class, 'error.handler']
+ );
+
+ $handler->expects($this->exactly(2))
+ ->method('setHandler')
+ ->withConsecutive(
+ [Handler::ENV_PRODUCTION, $legacyHandler],
+ [Handler::ENV_DEVELOPMENT, $whoopsHandler]
+ );
$serviceProvider = new ExceptionsServiceProvider($app);
$serviceProvider->register();
}
+
+ /**
+ * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::boot()
+ */
+ public function testBoot()
+ {
+ /** @var MockObject|Handler $handler */
+ $handler = $this->createMock(Handler::class);
+
+ /** @var MockObject|Request $request */
+ $request = $this->createMock(Request::class);
+
+ $handler->expects($this->once())
+ ->method('setRequest')
+ ->with($request);
+
+ $app = $this->getApp(['get']);
+ $app->expects($this->exactly(2))
+ ->method('get')
+ ->withConsecutive(
+ ['error.handler'],
+ ['request']
+ )
+ ->willReturnOnConsecutiveCalls(
+ $handler,
+ $request
+ );
+
+ $provider = new ExceptionsServiceProvider($app);
+ $provider->boot();
+ }
}
diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php
index 29759be7..40202be8 100644
--- a/tests/Unit/Exceptions/HandlerTest.php
+++ b/tests/Unit/Exceptions/HandlerTest.php
@@ -3,6 +3,10 @@
namespace Engelsystem\Test\Unit\Exceptions;
use Engelsystem\Exceptions\Handler;
+use Engelsystem\Exceptions\Handlers\HandlerInterface;
+use Engelsystem\Http\Request;
+use ErrorException;
+use Exception;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as Mock;
@@ -10,14 +14,80 @@ class HandlerTest extends TestCase
{
/**
* @covers \Engelsystem\Exceptions\Handler::__construct()
+ */
+ public function testCreate()
+ {
+ /** @var Handler|Mock $handler */
+ $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()
+ {
+ /** @var Handler|Mock $handler */
+ $handler = $this->getMockBuilder(Handler::class)
+ ->setMethods(['exceptionHandler'])
+ ->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()
+ {
+ $exception = new Exception();
+
+ /** @var HandlerInterface|Mock $handlerMock */
+ $handlerMock = $this->getMockForAbstractClass(HandlerInterface::class);
+ $handlerMock->expects($this->once())
+ ->method('report')
+ ->with($exception);
+ $handlerMock->expects($this->once())
+ ->method('render')
+ ->with($this->isInstanceOf(Request::class), $exception);
+
+ /** @var Handler|Mock $handler */
+ $handler = $this->getMockBuilder(Handler::class)
+ ->setMethods(['die'])
+ ->getMock();
+ $handler->expects($this->once())
+ ->method('die');
+
+ $handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock);
+
+ $handler->exceptionHandler($exception);
+ }
+
+ /**
* @covers \Engelsystem\Exceptions\Handler::register()
*/
public function testRegister()
{
/** @var Handler|Mock $handler */
$handler = $this->getMockForAbstractClass(Handler::class);
- $this->assertInstanceOf(Handler::class, $handler);
$handler->register();
+
+ 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();
}
/**
@@ -26,8 +96,7 @@ class HandlerTest extends TestCase
*/
public function testEnvironment()
{
- /** @var Handler|Mock $handler */
- $handler = $this->getMockForAbstractClass(Handler::class);
+ $handler = new Handler();
$handler->setEnvironment(Handler::ENV_DEVELOPMENT);
$this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment());
@@ -35,4 +104,37 @@ class HandlerTest extends TestCase
$handler->setEnvironment(Handler::ENV_PRODUCTION);
$this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment());
}
+
+ /**
+ * @covers \Engelsystem\Exceptions\Handler::setHandler()
+ * @covers \Engelsystem\Exceptions\Handler::getHandler()
+ */
+ public function testHandler()
+ {
+ $handler = new Handler();
+ /** @var HandlerInterface|Mock $devHandler */
+ $devHandler = $this->getMockForAbstractClass(HandlerInterface::class);
+ /** @var HandlerInterface|Mock $prodHandler */
+ $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::setRequest()
+ * @covers \Engelsystem\Exceptions\Handler::getRequest()
+ */
+ public function testRequest()
+ {
+ $handler = new Handler();
+ /** @var Request|Mock $request */
+ $request = $this->createMock(Request::class);
+
+ $handler->setRequest($request);
+ $this->assertEquals($request, $handler->getRequest());
+ }
}
diff --git a/tests/Unit/Exceptions/Handlers/LegacyDevelopmentTest.php b/tests/Unit/Exceptions/Handlers/LegacyDevelopmentTest.php
new file mode 100644
index 00000000..d5390c9e
--- /dev/null
+++ b/tests/Unit/Exceptions/Handlers/LegacyDevelopmentTest.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Exceptions\handlers;
+
+
+use Engelsystem\Exceptions\Handlers\LegacyDevelopment;
+use Engelsystem\Http\Request;
+use ErrorException;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as Mock;
+
+class LegacyDevelopmentTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::render()
+ * @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::formatStackTrace()
+ */
+ public function testRender()
+ {
+ $handler = new LegacyDevelopment();
+ /** @var Request|Mock $request */
+ $request = $this->createMock(Request::class);
+ $exception = new ErrorException('Lorem Ipsum', 4242, 1, 'foo.php', 9999);
+
+ $regex = sprintf(
+ '%%<pre.*>.*ErrorException.*4242.*Lorem Ipsum.*%s.*%s.*%s.*</pre>%%is',
+ 'foo.php',
+ 9999,
+ __FUNCTION__
+ );
+ $this->expectOutputRegex($regex);
+
+ $handler->render($request, $exception);
+ }
+}
diff --git a/tests/Unit/Exceptions/Handlers/LegacyTest.php b/tests/Unit/Exceptions/Handlers/LegacyTest.php
new file mode 100644
index 00000000..04b214f2
--- /dev/null
+++ b/tests/Unit/Exceptions/Handlers/LegacyTest.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Exceptions\handlers;
+
+
+use Engelsystem\Exceptions\Handlers\Legacy;
+use Engelsystem\Http\Request;
+use Exception;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as Mock;
+
+class LegacyTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Exceptions\Handlers\Legacy::render()
+ */
+ public function testRender()
+ {
+ $handler = new Legacy();
+ /** @var Request|Mock $request */
+ $request = $this->createMock(Request::class);
+ /** @var Exception|Mock $exception */
+ $exception = $this->createMock(Exception::class);
+
+ $this->expectOutputRegex('/.*error occurred.*/i');
+
+ $handler->render($request, $exception);
+ }
+
+ /**
+ * @covers \Engelsystem\Exceptions\Handlers\Legacy::report()
+ * @covers \Engelsystem\Exceptions\Handlers\Legacy::stripBasePath()
+ */
+ public function testReport()
+ {
+ $handler = new Legacy();
+ $exception = new Exception('Lorem Ipsum', 4242);
+ $line = __LINE__ - 1;
+
+ $log = tempnam(sys_get_temp_dir(), 'engelsystem-log');
+ $errorLog = ini_get('error_log');
+ ini_set('error_log', $log);
+ $handler->report($exception);
+ ini_set('error_log', $errorLog);
+ $logContent = file_get_contents($log);
+ unset($log);
+
+ $this->assertContains('4242', $logContent);
+ $this->assertContains('Lorem Ipsum', $logContent);
+ $this->assertContains(basename(__FILE__), $logContent);
+ $this->assertContains((string)$line, $logContent);
+ $this->assertContains(__FUNCTION__, $logContent);
+ $this->assertContains(json_encode(__CLASS__), $logContent);
+ }
+}
diff --git a/tests/Unit/Exceptions/Handlers/WhoopsTest.php b/tests/Unit/Exceptions/Handlers/WhoopsTest.php
new file mode 100644
index 00000000..261ee83f
--- /dev/null
+++ b/tests/Unit/Exceptions/Handlers/WhoopsTest.php
@@ -0,0 +1,83 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Exceptions\handlers;
+
+
+use Engelsystem\Application;
+use Engelsystem\Exceptions\Handlers\Whoops;
+use Engelsystem\Http\Request;
+use Exception;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as Mock;
+use Whoops\Handler\JsonResponseHandler;
+use Whoops\Handler\PrettyPageHandler;
+use Whoops\Run as WhoopsRunner;
+use Whoops\RunInterface as WhoopsRunnerInterface;
+
+class WhoopsTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Exceptions\Handlers\Whoops
+ */
+ public function testRender()
+ {
+ /** @var Application|Mock $app */
+ $app = $this->createMock(Application::class);
+ /** @var Request|Mock $request */
+ $request = $this->createMock(Request::class);
+ $request->expects($this->once())
+ ->method('isXmlHttpRequest')
+ ->willReturn(true);
+ /** @var WhoopsRunnerInterface|Mock $whoopsRunner */
+ $whoopsRunner = $this->getMockForAbstractClass(WhoopsRunnerInterface::class);
+ /** @var PrettyPageHandler|Mock $prettyPageHandler */
+ $prettyPageHandler = $this->createMock(PrettyPageHandler::class);
+ $prettyPageHandler
+ ->expects($this->atLeastOnce())
+ ->method('setApplicationPaths');
+ $prettyPageHandler
+ ->expects($this->once())
+ ->method('setApplicationPaths');
+ $prettyPageHandler
+ ->expects($this->once())
+ ->method('addDataTable');
+ /** @var JsonResponseHandler|Mock $jsonResponseHandler */
+ $jsonResponseHandler = $this->createMock(JsonResponseHandler::class);
+ $jsonResponseHandler->expects($this->once())
+ ->method('setJsonApi')
+ ->with(true);
+ $jsonResponseHandler->expects($this->once())
+ ->method('addTraceToOutput')
+ ->with(true);
+ /** @var Exception|Mock $exception */
+ $exception = $this->createMock(Exception::class);
+
+ $app->expects($this->exactly(3))
+ ->method('make')
+ ->withConsecutive(
+ [WhoopsRunner::class],
+ [PrettyPageHandler::class],
+ [JsonResponseHandler::class]
+ )
+ ->willReturnOnConsecutiveCalls(
+ $whoopsRunner,
+ $prettyPageHandler,
+ $jsonResponseHandler
+ );
+
+ $whoopsRunner
+ ->expects($this->exactly(2))
+ ->method('pushHandler')
+ ->withConsecutive(
+ [$prettyPageHandler],
+ [$jsonResponseHandler]
+ );
+ $whoopsRunner
+ ->expects($this->once())
+ ->method('handleException')
+ ->with($exception);
+
+ $handler = new Whoops($app);
+ $handler->render($request, $exception);
+ }
+}