2018-07-15 22:17:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Illuminate\Tests\Http;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Testing\FileFactory;
|
2023-10-26 22:09:14 +08:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
|
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
2019-09-10 17:16:05 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-07-15 22:17:06 +02:00
|
|
|
|
2021-10-28 21:56:26 +02:00
|
|
|
/**
|
2022-01-20 16:59:14 +03:00
|
|
|
* @link https://www.php.net/manual/en/function.gd-info.php
|
2021-10-28 21:56:26 +02:00
|
|
|
*/
|
2023-10-26 22:09:14 +08:00
|
|
|
#[RequiresPhpExtension('gd')]
|
2018-07-15 22:17:06 +02:00
|
|
|
class HttpTestingFileFactoryTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
public function testImagePng()
|
|
|
|
|
{
|
2022-01-20 16:59:14 +03:00
|
|
|
if (! $this->isGDSupported('PNG Support')) {
|
|
|
|
|
$this->markTestSkipped('Requires PNG support.');
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-15 22:17:06 +02:00
|
|
|
$image = (new FileFactory)->image('test.png', 15, 20);
|
|
|
|
|
|
|
|
|
|
$info = getimagesize($image->getRealPath());
|
|
|
|
|
|
|
|
|
|
$this->assertSame('image/png', $info['mime']);
|
|
|
|
|
$this->assertSame(15, $info[0]);
|
|
|
|
|
$this->assertSame(20, $info[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImageJpeg()
|
|
|
|
|
{
|
2022-01-20 16:59:14 +03:00
|
|
|
if (! $this->isGDSupported('JPEG Support')) {
|
|
|
|
|
$this->markTestSkipped('Requires JPEG support.');
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 18:26:07 +04:30
|
|
|
$jpeg = (new FileFactory)->image('test.jpeg', 15, 20);
|
|
|
|
|
$jpg = (new FileFactory)->image('test.jpg');
|
2018-07-15 22:17:06 +02:00
|
|
|
|
2021-06-21 18:26:07 +04:30
|
|
|
$info = getimagesize($jpeg->getRealPath());
|
2018-07-15 22:17:06 +02:00
|
|
|
|
|
|
|
|
$this->assertSame('image/jpeg', $info['mime']);
|
|
|
|
|
$this->assertSame(15, $info[0]);
|
|
|
|
|
$this->assertSame(20, $info[1]);
|
2021-06-21 18:26:07 +04:30
|
|
|
$this->assertSame(
|
|
|
|
|
'image/jpeg',
|
|
|
|
|
mime_content_type($jpg->getRealPath())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImageGif()
|
|
|
|
|
{
|
2022-01-20 16:59:14 +03:00
|
|
|
if (! $this->isGDSupported('GIF Create Support')) {
|
|
|
|
|
$this->markTestSkipped('Requires GIF Create support.');
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 18:26:07 +04:30
|
|
|
$image = (new FileFactory)->image('test.gif');
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'image/gif',
|
|
|
|
|
mime_content_type($image->getRealPath())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImageWebp()
|
|
|
|
|
{
|
2022-01-20 16:59:14 +03:00
|
|
|
if (! $this->isGDSupported('WebP Support')) {
|
|
|
|
|
$this->markTestSkipped('Requires Webp support.');
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 18:26:07 +04:30
|
|
|
$image = (new FileFactory)->image('test.webp');
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'image/webp',
|
|
|
|
|
mime_content_type($image->getRealPath())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImageWbmp()
|
|
|
|
|
{
|
2022-01-20 16:59:14 +03:00
|
|
|
if (! $this->isGDSupported('WBMP Support')) {
|
|
|
|
|
$this->markTestSkipped('Requires WBMP support.');
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 18:26:07 +04:30
|
|
|
$image = (new FileFactory)->image('test.wbmp');
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'image/vnd.wap.wbmp',
|
|
|
|
|
getimagesize($image->getRealPath())['mime']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImageBmp()
|
|
|
|
|
{
|
|
|
|
|
$image = (new FileFactory)->image('test.bmp');
|
|
|
|
|
|
|
|
|
|
$imagePath = $image->getRealPath();
|
|
|
|
|
|
2023-07-27 16:29:38 +02:00
|
|
|
if (version_compare(PHP_VERSION, '8.3.0-dev', '>=')) {
|
|
|
|
|
$this->assertSame('image/bmp', mime_content_type($imagePath));
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertSame('image/x-ms-bmp', mime_content_type($imagePath));
|
|
|
|
|
}
|
2018-07-15 22:17:06 +02:00
|
|
|
}
|
2019-12-18 07:53:23 +00:00
|
|
|
|
|
|
|
|
public function testCreateWithMimeType()
|
|
|
|
|
{
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'audio/webm',
|
|
|
|
|
(new FileFactory)->create('someaudio.webm', 0, 'audio/webm')->getMimeType()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCreateWithoutMimeType()
|
|
|
|
|
{
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'video/webm',
|
|
|
|
|
(new FileFactory)->create('someaudio.webm')->getMimeType()
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-01-20 16:59:14 +03:00
|
|
|
|
2023-10-26 22:09:14 +08:00
|
|
|
#[DataProvider('generateImageDataProvider')]
|
2023-04-24 09:43:14 -04:00
|
|
|
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'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 16:59:14 +03:00
|
|
|
/**
|
|
|
|
|
* @param string $driver
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function isGDSupported(string $driver = 'GD Version'): bool
|
|
|
|
|
{
|
|
|
|
|
$gdInfo = gd_info();
|
|
|
|
|
|
|
|
|
|
if (isset($gdInfo[$driver])) {
|
|
|
|
|
return $gdInfo[$driver];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-15 22:17:06 +02:00
|
|
|
}
|