From 5a0d1854b6f61712afa5c28953e0c07dd1bbf983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFck=20Piera?= Date: Sun, 11 Oct 2026 19:24:53 +0200 Subject: [PATCH] Test non-HTTP exceptions and disabled debug mode --- tests/FunctionalTest.php | 27 +++++++++++++++++++++++++++ tests/app/config/routing.php | 3 +++ tests/app/src/TestController.php | 8 ++++++++ 3 files changed, 38 insertions(+) diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index a37040e..73b473d 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -37,6 +37,33 @@ protected function tearDown(): void } } + public function testItDoesNotDisplayGifWhenDebugIsDisabled(): void + { + $client = static::createClient(['environment' => 'dev', 'debug' => false]); + + $client->request('GET', '/error-404'); + $response = $client->getResponse(); + + self::assertSame(404, $response->getStatusCode()); + self::assertStringNotContainsString('data-gif', (string) $response->getContent()); + self::assertFalse(static::getContainer()->has('gif_exception.listener.replace_image')); + } + + public function testItFallsBackToOtherGifsForNonHttpExceptions(): void + { + $client = static::createClient(['environment' => 'dev', 'debug' => true]); + + $client->request('GET', '/database-error'); + $response = $client->getResponse(); + + self::assertSame(500, $response->getStatusCode()); + + $image = $this->getImage((string) $response->getContent()); + + self::assertTrue($image->hasAttribute('data-gif'), 'Image was not replaced.'); + self::assertStringMatchesFormat('%s/gifexception/images/other/%s.gif', $image->getAttribute('src')); + } + public function testItDoesNotDisplayGifOnExceptionPageIfTheBundleIsNotEnabled(): void { $client = static::createClient(['environment' => 'prod', 'debug' => true]); diff --git a/tests/app/config/routing.php b/tests/app/config/routing.php index c34bb89..9b66e5b 100644 --- a/tests/app/config/routing.php +++ b/tests/app/config/routing.php @@ -16,6 +16,9 @@ ->requirements(['status' => '\d+']) ->defaults(['_controller' => 'Joli\GifExceptionBundle\Tests\app\src\TestController::errorAction']) ; + $routes->add('test_routing_database_error', '/database-error') + ->defaults(['_controller' => 'Joli\GifExceptionBundle\Tests\app\src\TestController::databaseErrorAction']) + ; try { $routes->import('@WebProfilerBundle/Resources/config/routing/wdt.xml') diff --git a/tests/app/src/TestController.php b/tests/app/src/TestController.php index 9c82b4d..b25135a 100644 --- a/tests/app/src/TestController.php +++ b/tests/app/src/TestController.php @@ -22,4 +22,12 @@ public function errorAction(Request $request): void throw new HttpException($statusCode, \sprintf('This is HTTP %s error page!', $statusCode)); } + + public function databaseErrorAction(): void + { + // Mimic a PDOException whose code is a SQLSTATE string, not an int + throw new class('Database is on fire!') extends \PDOException { + protected $code = 'HY000'; + }; + } }