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'; + }; + } }