Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Throw LogicException when calling FileFactory@image() if mimetype is not supported #46859

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions 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,6 +86,12 @@ protected function generateImage($width, $height, $extension)

$image = imagecreatetruecolor($width, $height);

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("image{$extension}", $image);
cosmastech marked this conversation as resolved.
Show resolved Hide resolved

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