From 89a54685f719d21676a6b85457951b27c7c53081 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Mon, 24 Apr 2023 09:43:14 -0400 Subject: [PATCH] [10.x] Throw LogicException when calling `FileFactory@image()` if mimetype is not supported (#46859) * throw exception if FileFactory does not support mimetype * style * formatting * rely on $functionName --------- Co-authored-by: Taylor Otwell --- src/Illuminate/Http/Testing/FileFactory.php | 18 +++++++++++++++- tests/Http/HttpTestingFileFactoryTest.php | 23 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Testing/FileFactory.php b/src/Illuminate/Http/Testing/FileFactory.php index 9e25d72de8f3..aa4b0296b025 100644 --- a/src/Illuminate/Http/Testing/FileFactory.php +++ b/src/Illuminate/Http/Testing/FileFactory.php @@ -2,6 +2,8 @@ namespace Illuminate\Http\Testing; +use LogicException; + class FileFactory { /** @@ -49,6 +51,8 @@ public function createWithContent($name, $content) * @param int $width * @param int $height * @return \Illuminate\Http\Testing\File + * + * @throws \LogicException */ public function image($name, $width = 10, $height = 10) { @@ -64,9 +68,15 @@ public function image($name, $width = 10, $height = 10) * @param int $height * @param string $extension * @return resource + * + * @throws \LogicException */ protected function generateImage($width, $height, $extension) { + if (! function_exists('imagecreatetruecolor')) { + throw new LogicException('GD extension is not installed.'); + } + return tap(tmpfile(), function ($temp) use ($width, $height, $extension) { ob_start(); @@ -76,7 +86,13 @@ protected function generateImage($width, $height, $extension) $image = imagecreatetruecolor($width, $height); - call_user_func("image{$extension}", $image); + if (! function_exists($functionName = "image{$extension}")) { + ob_get_clean(); + + throw new LogicException("{$functionName} function is not defined and image cannot be generated."); + } + + call_user_func($functionName, $image); fwrite($temp, ob_get_clean()); }); diff --git a/tests/Http/HttpTestingFileFactoryTest.php b/tests/Http/HttpTestingFileFactoryTest.php index 0e6eb7438053..a3c9a561e1ba 100644 --- a/tests/Http/HttpTestingFileFactoryTest.php +++ b/tests/Http/HttpTestingFileFactoryTest.php @@ -116,6 +116,29 @@ public function testCreateWithoutMimeType() ); } + /** @dataProvider generateImageDataProvider */ + public function testCallingCreateWithoutGDLoadedThrowsAnException(string $fileExtension, string $driver) + { + if ($this->isGDSupported($driver)) { + $this->markTestSkipped("Requires no {$driver}"); + } + + $this->expectException(\LogicException::class); + (new FileFactory)->image("test.{$fileExtension}"); + } + + public static function generateImageDataProvider(): array + { + return [ + 'jpeg' => ['jpeg', 'JPEG Support'], + 'png' => ['png', 'PNG Support'], + 'gif' => ['gif', 'GIF Create Support'], + 'webp' => ['webp', 'WebP Support'], + 'wbmp' => ['wbmp', 'WBMP Support'], + 'bmp' => ['bmp', 'BMP Support'], + ]; + } + /** * @param string $driver * @return bool