Files
netslim-core/tests/Kernel/ErrorHandlerConfiguratorTest.php
2026-03-20 22:13:41 +01:00

109 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Kernel;
use Netig\Netslim\Kernel\Persistence\Infrastructure\DatabaseNotProvisionedException;
use Netig\Netslim\Kernel\Runtime\Http\ErrorHandlerConfigurator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Slim\Factory\AppFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
use Slim\Views\Twig;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class ErrorHandlerConfiguratorTest extends TestCase
{
/** @var Twig&MockObject */
private Twig $twig;
/** @var ContainerInterface&MockObject */
private ContainerInterface $container;
private ?string $originalAppEnv = null;
protected function setUp(): void
{
$this->originalAppEnv = $_ENV['APP_ENV'] ?? null;
$_ENV['APP_ENV'] = 'production';
$this->twig = $this->createMock(Twig::class);
$logger = $this->createStub(LoggerInterface::class);
$this->container = $this->createMock(ContainerInterface::class);
$this->container
->method('get')
->willReturnMap([
[LoggerInterface::class, $logger],
[Twig::class, $this->twig],
]);
}
protected function tearDown(): void
{
if ($this->originalAppEnv === null) {
unset($_ENV['APP_ENV']);
} else {
$_ENV['APP_ENV'] = $this->originalAppEnv;
}
}
public function testConfigureRegistersHandlerThatRenders404Responses(): void
{
$app = AppFactory::create();
$this->twig
->expects(self::once())
->method('render')
->willReturnCallback(function ($response, string $template, array $data) {
self::assertSame('@Kernel/error.twig', $template);
self::assertSame(404, $data['status']);
self::assertSame('La page demandée est introuvable.', $data['message']);
$response->getBody()->write('configured 404');
return $response;
});
(new ErrorHandlerConfigurator())->configure($app, $this->container);
$request = (new ServerRequestFactory())->createServerRequest('GET', '/missing');
$response = $app->handle($request);
self::assertSame(404, $response->getStatusCode());
self::assertStringContainsString('configured 404', (string) $response->getBody());
}
public function testConfigureRegistersHandlerThatRenders503Responses(): void
{
$app = AppFactory::create();
$app->get('/db-check', function (): never {
throw new DatabaseNotProvisionedException('Provisionnement requis');
});
$this->twig
->expects(self::once())
->method('render')
->willReturnCallback(function ($response, string $template, array $data) {
self::assertSame('@Kernel/error.twig', $template);
self::assertSame(503, $data['status']);
self::assertSame('Provisionnement requis', $data['message']);
$response->getBody()->write('configured 503');
return $response;
});
(new ErrorHandlerConfigurator())->configure($app, $this->container);
$request = (new ServerRequestFactory())->createServerRequest('GET', '/db-check');
$response = $app->handle($request);
self::assertSame(503, $response->getStatusCode());
self::assertStringContainsString('configured 503', (string) $response->getBody());
}
}