Moved development error handler setup to ConfigureEnvironmentServiceProvider

main
Igor Scheller 5 years ago committed by msquare
parent 2605204af8
commit 984ce25728

@ -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');
}

@ -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');
}
}

@ -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());
}
}

Loading…
Cancel
Save