Extracted exception handler registration to a dedicated class

This commit is contained in:
Chris Kankiewicz
2020-02-23 18:45:27 -07:00
parent 1732a61c9e
commit 933b04de54
5 changed files with 83 additions and 11 deletions

View File

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

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Bootstrap;
namespace App\Exceptions;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Exceptions;
use PHLAK\Config\Config;
use Slim\App;
class ExceptionManager
{
/** @var App The application */
protected $app;
/** @var Config The application config */
protected $config;
/**
* Create a new ExceptionManager object.
*
* @param \Slim\App $app
* @param \PHLAK\Config\Config $config
*/
public function __construct(App $app, Config $config)
{
$this->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);
}
}

View File

@@ -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;

View File

@@ -0,0 +1,37 @@
<?php
namespace Tests\Exceptions;
use App\Exceptions\ErrorHandler;
use App\Exceptions\ExceptionManager;
use Slim\App;
use Slim\Middleware\ErrorMiddleware;
use Tests\TestCase;
class ExceptionManagerTest extends TestCase
{
public function test_it_sets_the_default_error_handler(): void
{
$errorMiddleware = $this->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))();
}
}