Skip to content

Commit

Permalink
[10.x] Throw LogicException when calling FileFactory@image() if mim…
Browse files Browse the repository at this point in the history
…etype is not supported (laravel#46859)

* throw exception if FileFactory does not support mimetype

* style

* formatting

* rely on $functionName

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
cosmastech and taylorotwell committed Apr 24, 2023
1 parent 9cfd7e1 commit 89a5468
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Illuminate/Http/Testing/FileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Http\Testing;

use LogicException;

class FileFactory
{
/**
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();

Expand All @@ -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());
});
Expand Down
23 changes: 23 additions & 0 deletions tests/Http/HttpTestingFileFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 89a5468

Please sign in to comment.