1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
<?php
namespace Illuminate\Tests\Http;
use Illuminate\Http\Testing\FileFactory;
use PHPUnit\Framework\TestCase;
/**
* @requires extension gd
*
* @link https://www.php.net/manual/en/function.gd-info.php
*/
class HttpTestingFileFactoryTest extends TestCase
{
public function testImagePng()
{
if (! $this->isGDSupported('PNG Support')) {
$this->markTestSkipped('Requires PNG support.');
}
$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()
{
if (! $this->isGDSupported('JPEG Support')) {
$this->markTestSkipped('Requires JPEG support.');
}
$jpeg = (new FileFactory)->image('test.jpeg', 15, 20);
$jpg = (new FileFactory)->image('test.jpg');
$info = getimagesize($jpeg->getRealPath());
$this->assertSame('image/jpeg', $info['mime']);
$this->assertSame(15, $info[0]);
$this->assertSame(20, $info[1]);
$this->assertSame(
'image/jpeg',
mime_content_type($jpg->getRealPath())
);
}
public function testImageGif()
{
if (! $this->isGDSupported('GIF Create Support')) {
$this->markTestSkipped('Requires GIF Create support.');
}
$image = (new FileFactory)->image('test.gif');
$this->assertSame(
'image/gif',
mime_content_type($image->getRealPath())
);
}
public function testImageWebp()
{
if (! $this->isGDSupported('WebP Support')) {
$this->markTestSkipped('Requires Webp support.');
}
$image = (new FileFactory)->image('test.webp');
$this->assertSame(
'image/webp',
mime_content_type($image->getRealPath())
);
}
public function testImageWbmp()
{
if (! $this->isGDSupported('WBMP Support')) {
$this->markTestSkipped('Requires WBMP support.');
}
$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();
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));
}
}
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()
);
}
/** @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
*/
private function isGDSupported(string $driver = 'GD Version'): bool
{
$gdInfo = gd_info();
if (isset($gdInfo[$driver])) {
return $gdInfo[$driver];
}
return false;
}
}
|