first commit

This commit is contained in:
julien
2026-03-20 22:13:41 +01:00
commit 41f8b3afb4
323 changed files with 27222 additions and 0 deletions

View File

@@ -0,0 +1,126 @@
<?php
declare(strict_types=1);
namespace Tests\Kernel;
use Netig\Netslim\Kernel\Persistence\Infrastructure\DatabaseNotProvisionedException;
use Netig\Netslim\Kernel\Runtime\Http\DefaultErrorHandler;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Slim\Exception\HttpNotFoundException;
use Slim\Psr7\Factory\ServerRequestFactory;
use Slim\Psr7\Response;
use Slim\Views\Twig;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class DefaultErrorHandlerTest extends TestCase
{
/** @var ResponseFactoryInterface&MockObject */
private ResponseFactoryInterface $responseFactory;
/** @var Twig&MockObject */
private Twig $twig;
protected function setUp(): void
{
$this->responseFactory = $this->createMock(ResponseFactoryInterface::class);
$this->responseFactory
->method('createResponse')
->willReturnCallback(static fn (int $statusCode): Response => new Response($statusCode));
$this->twig = $this->createMock(Twig::class);
}
public function testInvokeRendersFriendlyNotFoundPage(): void
{
$request = (new ServerRequestFactory())->createServerRequest('GET', '/missing');
$exception = new HttpNotFoundException($request);
$this->twig
->expects(self::once())
->method('render')
->willReturnCallback(function (Response $response, string $template, array $data): Response {
self::assertSame(404, $response->getStatusCode());
self::assertSame('@Kernel/error.twig', $template);
self::assertSame(404, $data['status']);
self::assertSame('La page demandée est introuvable.', $data['message']);
$response->getBody()->write('404: ' . $data['message']);
return $response;
});
$handler = new DefaultErrorHandler($this->responseFactory, $this->twig, false);
$response = $handler($request, $exception, false, true, true);
self::assertSame(404, $response->getStatusCode());
self::assertStringContainsString('404: La page demandée est introuvable.', (string) $response->getBody());
}
public function testInvokeRendersDatabaseProvisioningErrorAs503(): void
{
$request = (new ServerRequestFactory())->createServerRequest('GET', '/');
$exception = new DatabaseNotProvisionedException('La base de données doit être provisionnée.');
$this->twig
->expects(self::once())
->method('render')
->willReturnCallback(function (Response $response, string $template, array $data): Response {
self::assertSame(503, $response->getStatusCode());
self::assertSame('@Kernel/error.twig', $template);
self::assertSame(503, $data['status']);
self::assertSame('La base de données doit être provisionnée.', $data['message']);
$response->getBody()->write('503: ' . $data['message']);
return $response;
});
$handler = new DefaultErrorHandler($this->responseFactory, $this->twig, false);
$response = $handler($request, $exception, false, true, true);
self::assertSame(503, $response->getStatusCode());
self::assertStringContainsString('503: La base de données doit être provisionnée.', (string) $response->getBody());
}
public function testInvokeDoesNotRethrowHttpExceptionsInDevelopment(): void
{
$request = (new ServerRequestFactory())->createServerRequest('GET', '/missing');
$exception = new HttpNotFoundException($request);
$this->twig
->expects(self::once())
->method('render')
->willReturnCallback(function (Response $response, string $template, array $data): Response {
self::assertSame(404, $response->getStatusCode());
self::assertSame('@Kernel/error.twig', $template);
self::assertSame(404, $data['status']);
self::assertSame('La page demandée est introuvable.', $data['message']);
$response->getBody()->write('404 dev: ' . $data['message']);
return $response;
});
$handler = new DefaultErrorHandler($this->responseFactory, $this->twig, true);
$response = $handler($request, $exception, true, true, true);
self::assertSame(404, $response->getStatusCode());
self::assertStringContainsString('404 dev: La page demandée est introuvable.', (string) $response->getBody());
}
public function testInvokeRethrowsUnexpectedExceptionsInDevelopment(): void
{
$request = (new ServerRequestFactory())->createServerRequest('GET', '/boom');
$exception = new \RuntimeException('boom');
$this->twig->expects(self::never())->method('render');
$handler = new DefaultErrorHandler($this->responseFactory, $this->twig, true);
$this->expectExceptionObject($exception);
$handler($request, $exception, true, true, true);
}
}