From 984ce25728ee481239ea9e02632044751f43b1f4 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 21 Apr 2020 12:39:54 +0200 Subject: [PATCH] Moved development error handler setup to ConfigureEnvironmentServiceProvider --- includes/application.php | 14 -------------- .../ConfigureEnvironmentServiceProvider.php | 14 ++++++++++++++ .../ConfigureEnvironmentServiceProviderTest.php | 8 ++++++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/includes/application.php b/includes/application.php index 3a6ed9a6..9be48860 100644 --- a/includes/application.php +++ b/includes/application.php @@ -2,16 +2,12 @@ use Engelsystem\Application; use Engelsystem\Config\Config; -use Engelsystem\Exceptions\Handler; -use Engelsystem\Exceptions\Handlers\HandlerInterface; - /** * Include the autoloader */ require_once __DIR__ . '/autoload.php'; - /** * Initialize and bootstrap the application */ @@ -19,13 +15,3 @@ $app = new Application(realpath(__DIR__ . DIRECTORY_SEPARATOR . '..')); $appConfig = $app->make(Config::class); $appConfig->set(require config_path('app.php')); $app->bootstrap($appConfig); - - -/** - * Configure environment - */ -if (config('environment') == 'development') { - $errorHandler = $app->get('error.handler'); - $errorHandler->setEnvironment(Handler::ENV_DEVELOPMENT); - $app->bind(HandlerInterface::class, 'error.handler.development'); -} diff --git a/src/Helpers/ConfigureEnvironmentServiceProvider.php b/src/Helpers/ConfigureEnvironmentServiceProvider.php index a01490b8..1660de1e 100644 --- a/src/Helpers/ConfigureEnvironmentServiceProvider.php +++ b/src/Helpers/ConfigureEnvironmentServiceProvider.php @@ -5,6 +5,8 @@ namespace Engelsystem\Helpers; use Carbon\CarbonTimeZone; use Engelsystem\Config\Config; use Engelsystem\Container\ServiceProvider; +use Engelsystem\Exceptions\Handler; +use Engelsystem\Exceptions\Handlers\HandlerInterface; class ConfigureEnvironmentServiceProvider extends ServiceProvider { @@ -20,6 +22,7 @@ class ConfigureEnvironmentServiceProvider extends ServiceProvider if ($config->get('environment') == 'development') { $this->displayErrors(true); $this->errorReporting(E_ALL); + $this->setupDevErrorHandler(); } } @@ -50,4 +53,15 @@ class ConfigureEnvironmentServiceProvider extends ServiceProvider { error_reporting($errorReporting); } + + /** + * Setup the development error handler + */ + protected function setupDevErrorHandler() + { + /** @var Handler $errorHandler */ + $errorHandler = $this->app->get('error.handler'); + $errorHandler->setEnvironment(Handler::ENV_DEVELOPMENT); + $this->app->bind(HandlerInterface::class, 'error.handler.development'); + } } diff --git a/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php b/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php index a202c64f..45be5097 100644 --- a/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php +++ b/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php @@ -4,6 +4,7 @@ namespace Engelsystem\Test\Unit\Helpers; use Carbon\CarbonTimeZone; use Engelsystem\Config\Config; +use Engelsystem\Exceptions\Handler; use Engelsystem\Helpers\ConfigureEnvironmentServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit\Framework\MockObject\MockObject; @@ -12,12 +13,16 @@ class ConfigureEnvironmentServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Helpers\ConfigureEnvironmentServiceProvider::register + * @covers \Engelsystem\Helpers\ConfigureEnvironmentServiceProvider::setupDevErrorHandler */ public function testRegister() { $config = new Config(['timezone' => 'Australia/Eucla', 'environment' => 'production']); $this->app->instance('config', $config); + $handler = new Handler(); + $this->app->instance('error.handler', $handler); + /** @var ConfigureEnvironmentServiceProvider|MockObject $serviceProvider */ $serviceProvider = $this->getMockBuilder(ConfigureEnvironmentServiceProvider::class) ->setConstructorArgs([$this->app]) @@ -37,7 +42,10 @@ class ConfigureEnvironmentServiceProviderTest extends ServiceProviderTest ->with(E_ALL); $serviceProvider->register(); + $this->assertNotEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment()); + $config->set('environment', 'development'); $serviceProvider->register(); + $this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment()); } }