From 933b04de54554dde2356cc16ca8602be49d9f1b9 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Sun, 23 Feb 2020 18:45:27 -0700 Subject: [PATCH] Extracted exception handler registration to a dedicated class --- app/src/Bootstrap/AppManager.php | 11 +---- .../ErrorHandler.php | 2 +- app/src/Exceptions/ExceptionManager.php | 42 +++++++++++++++++++ .../ErrorHandlerTest.php | 2 +- tests/Exceptions/ExceptionManagerTest.php | 37 ++++++++++++++++ 5 files changed, 83 insertions(+), 11 deletions(-) rename app/src/{Bootstrap => Exceptions}/ErrorHandler.php (98%) create mode 100644 app/src/Exceptions/ExceptionManager.php rename tests/{Bootstrap => Exceptions}/ErrorHandlerTest.php (98%) create mode 100644 tests/Exceptions/ExceptionManagerTest.php diff --git a/app/src/Bootstrap/AppManager.php b/app/src/Bootstrap/AppManager.php index ab1208c..785fd46 100644 --- a/app/src/Bootstrap/AppManager.php +++ b/app/src/Bootstrap/AppManager.php @@ -2,13 +2,13 @@ namespace App\Bootstrap; +use App\Exceptions\ExceptionManager; use App\Middlewares; use App\Providers; use DI\Bridge\Slim\Bridge; use DI\Container; use Invoker\CallableResolver; use Middlewares as HttpMiddlewares; -use PHLAK\Config\Config; use Slim\App; use Tightenco\Collect\Support\Collection; @@ -56,14 +56,7 @@ class AppManager $app = Bridge::create($this->container); $this->registerMiddlewares($app); - $this->container->call(function (App $app, Config $config): void { - if (! $config->get('app.debug', false)) { - return; - } - - $errorMiddleware = $app->addErrorMiddleware(true, true, true); - $errorMiddleware->setDefaultErrorHandler(ErrorHandler::class); - }); + $this->container->call(ExceptionManager::class); return $app; } diff --git a/app/src/Bootstrap/ErrorHandler.php b/app/src/Exceptions/ErrorHandler.php similarity index 98% rename from app/src/Bootstrap/ErrorHandler.php rename to app/src/Exceptions/ErrorHandler.php index 04d377e..4c8fd2d 100644 --- a/app/src/Bootstrap/ErrorHandler.php +++ b/app/src/Exceptions/ErrorHandler.php @@ -1,6 +1,6 @@ app = $app; + $this->config = $config; + } + + /** + * Set up and configure exception handling. + * + * @return void + */ + public function __invoke(): void + { + if ($this->config->get('app.debug', false)) { + return; + } + + $errorMiddleware = $this->app->addErrorMiddleware(true, true, true); + $errorMiddleware->setDefaultErrorHandler(ErrorHandler::class); + } +} diff --git a/tests/Bootstrap/ErrorHandlerTest.php b/tests/Exceptions/ErrorHandlerTest.php similarity index 98% rename from tests/Bootstrap/ErrorHandlerTest.php rename to tests/Exceptions/ErrorHandlerTest.php index 73361c2..fbc609d 100644 --- a/tests/Bootstrap/ErrorHandlerTest.php +++ b/tests/Exceptions/ErrorHandlerTest.php @@ -2,7 +2,7 @@ namespace Tests\Bootstrap; -use App\Bootstrap\ErrorHandler; +use App\Exceptions\ErrorHandler; use App\Providers\TwigProvider; use Exception; use Slim\Psr7\Request; diff --git a/tests/Exceptions/ExceptionManagerTest.php b/tests/Exceptions/ExceptionManagerTest.php new file mode 100644 index 0000000..6b8aa6d --- /dev/null +++ b/tests/Exceptions/ExceptionManagerTest.php @@ -0,0 +1,37 @@ +createMock(ErrorMiddleware::class); + $errorMiddleware->expects($this->once()) + ->method('setDefaultErrorHandler') + ->with(ErrorHandler::class); + + $app = $this->createMock(App::class); + $app->expects($this->once()) + ->method('addErrorMiddleware') + ->willReturn($errorMiddleware); + + (new ExceptionManager($app, $this->config))(); + } + + public function test_it_does_not_set_the_default_error_handler_when_debug_is_enabled(): void + { + $this->config->set('app.debug', true); + + $app = $this->createMock(App::class); + $app->expects($this->never())->method('addErrorMiddleware'); + + (new ExceptionManager($app, $this->config))(); + } +}