feat(ReserverController): Modifie la route de création de session.
🐛 fix(ErrorListener): Corrige l'envoi de mails d'erreur en prod.
♻️ refactor(FlowReserve): Simplifie la validation du panier de réservation.
 test(ErrorListener): Ajoute des tests pour la gestion des erreurs.
```
This commit is contained in:
Serreau Jovann
2026-01-31 14:17:34 +01:00
parent 18ac532914
commit db6c5d5fa6
4 changed files with 97 additions and 42 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Tests\Security;
use App\Security\ErrorListener;
use App\Service\Mailer\Mailer;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -17,12 +18,14 @@ use Twig\Environment;
class ErrorListenerTest extends TestCase
{
private $twig;
private $mailer;
private $listener;
protected function setUp(): void
{
$this->twig = $this->createMock(Environment::class);
$this->listener = new ErrorListener($this->twig);
$this->mailer = $this->createMock(Mailer::class);
$this->listener = new ErrorListener($this->mailer, $this->twig);
}
public function testOnKernelExceptionInDevModeDoesNothing()
@@ -32,11 +35,50 @@ class ErrorListenerTest extends TestCase
$request = new Request();
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, new \Exception());
// Le mailer ne doit jamais être appelé en dev
$this->mailer->expects($this->never())->method('send');
$this->listener->onKernelException($event);
$this->assertNull($event->getResponse());
unset($_ENV['APP_ENV']); // Cleanup
unset($_ENV['APP_ENV']);
}
public function testOnKernelExceptionSendsEmailFor404()
{
$_ENV['APP_ENV'] = 'prod';
$kernel = $this->createMock(HttpKernelInterface::class);
$request = Request::create('/une-page-existante');
$exception = new NotFoundHttpException('Not found');
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception);
// On vérifie que le mail est bien envoyé pour une 404 standard
$this->mailer->expects($this->once())
->method('send')
->with($this->anything(), $this->anything(), $this->stringContains('404 Page introuvable'));
$this->listener->onKernelException($event);
unset($_ENV['APP_ENV']);
}
public function testOnKernelExceptionDoesNotSendEmailForBotTarget()
{
$_ENV['APP_ENV'] = 'prod';
$kernel = $this->createMock(HttpKernelInterface::class);
$request = Request::create('/.env'); // URL typique de bot
$exception = new NotFoundHttpException('Not found');
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception);
// Le mailer ne doit PAS être appelé car c'est une cible de bot
$this->mailer->expects($this->never())->method('send');
$this->listener->onKernelException($event);
unset($_ENV['APP_ENV']);
}
public function testOnKernelExceptionJsonRequest()
@@ -45,7 +87,7 @@ class ErrorListenerTest extends TestCase
$kernel = $this->createMock(HttpKernelInterface::class);
$request = new Request();
$request->headers->set('Accept', 'application/json');
$exception = new NotFoundHttpException('Not found');
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception);
@@ -54,11 +96,7 @@ class ErrorListenerTest extends TestCase
$response = $event->getResponse();
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(404, $response->getStatusCode());
$content = json_decode($response->getContent(), true);
$this->assertEquals('error', $content['status']);
$this->assertEquals('Resource not found', $content['message']);
unset($_ENV['APP_ENV']);
}
@@ -67,8 +105,8 @@ class ErrorListenerTest extends TestCase
$_ENV['APP_ENV'] = 'prod';
$kernel = $this->createMock(HttpKernelInterface::class);
$request = new Request();
$exception = new \Exception('Error');
$exception = new \Exception('500 Error');
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception);
$this->twig->expects($this->once())
@@ -79,10 +117,8 @@ class ErrorListenerTest extends TestCase
$this->listener->onKernelException($event);
$response = $event->getResponse();
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals('<html>Error</html>', $response->getContent());
unset($_ENV['APP_ENV']);
}
}