Skip to content

Commit

Permalink
test: move Encoder test
Browse files Browse the repository at this point in the history
  • Loading branch information
konnng-dev committed Jun 3, 2024
1 parent c5eb1e2 commit a602e8f
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 201 deletions.
21 changes: 17 additions & 4 deletions src/Api/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace League\Glide\Api;

use Exception;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\ImageInterface;

/**
* Encoder Api class to convert a given image to a specific format.
*/
class Encode {

class Encode
{
/**
* The manipulation params.
*/
Expand All @@ -26,6 +25,20 @@ public function __construct(array $params = [])
$this->params = $params;
}

/**
* Set the manipulation params.
*
* @param array $params The manipulation params.
*
* @return $this
*/
public function setParams(array $params)
{
$this->params = $params;

return $this;
}

/**
* Get a specific manipulation param.
*/
Expand Down Expand Up @@ -71,7 +84,7 @@ public function run(ImageInterface $image): EncodedImageInterface
$encoderOptions['interlaced'] = $shouldInterlace;
break;
default:
throw new Exception("Invalid format provided: {$format}");
throw new \Exception("Invalid format provided: {$format}");
}

return $image->encodeByExtension(...$encoderOptions);
Expand Down
254 changes: 254 additions & 0 deletions tests/Api/EncodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
<?php

namespace League\Glide\Api;

use Intervention\Image\Encoders\MediaTypeEncoder;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Mockery;
use Mockery\Mock;
use PHPUnit\Framework\TestCase;

class EncodeTest extends TestCase
{
/**
* @var Encode
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $encoder;
/**
* @var ImageInterface
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $jpg;
/**
* @var ImageInterface
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $png;
/**
* @var ImageInterface
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $gif;
/**
* @var ImageInterface
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $tif;
/**
* @var ImageInterface
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $webp;
/**
* @var ImageInterface
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $avif;
/**
* @var ImageInterface
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $heic;

public function setUp(): void
{
$manager = ImageManager::gd();
$this->jpg = $manager->read(
$manager->create(100, 100)->encode(new MediaTypeEncoder('image/jpeg'))->toFilePointer()
);
$this->png = $manager->read(
$manager->create(100, 100)->encode(new MediaTypeEncoder('image/png'))->toFilePointer()
);
$this->gif = $manager->read(
$manager->create(100, 100)->encode(new MediaTypeEncoder('image/gif'))->toFilePointer()
);

if (function_exists('imagecreatefromwebp')) {
$this->webp = $manager->read(
$manager->create(100, 100)->encode(new MediaTypeEncoder('image/webp'))->toFilePointer()
);
}

if (function_exists('imagecreatefromavif')) {
$this->avif = $manager->read(
$manager->create(100, 100)->encode(new MediaTypeEncoder('image/avif'))->toFilePointer()
);
}

$this->encoder = new Encode();
}

public function tearDown(): void
{
\Mockery::close();
}

public function testCreateInstance(): void
{
/**
* @psalm-suppress ArgumentTypeCoercion
*/
$this->assertInstanceOf('League\Glide\Api\Encode', $this->encoder);
}

public function testRun(): void
{
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->jpg)));
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->png)));
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->gif)));
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->jpg)));
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->png)));
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->gif)));
$this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->jpg)));
$this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->png)));
$this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->gif)));
$this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->jpg)));
$this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->png)));
$this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->gif)));

if (function_exists('imagecreatefromwebp')) {
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->webp)));
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->webp)));
$this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->webp)));
$this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->webp)));
$this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->jpg)));
$this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->png)));
$this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->gif)));
$this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->webp)));
}
if (function_exists('imagecreatefromavif')) {
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->avif)));
$this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->avif)));
$this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->avif)));
$this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->avif)));
$this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->jpg)));
$this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->png)));
$this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->gif)));
$this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->avif)));
}

if (function_exists('imagecreatefromwebp') && function_exists('imagecreatefromavif')) {
$this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->avif)));
$this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->webp)));
}
}

public function testGetFormat(): void
{
$image = \Mockery::mock(ImageInterface::class, function (Mock $mock) {
$this->assertMediaType($mock, 'image/jpeg')->once();
$this->assertMediaType($mock, 'image/png')->once();
$this->assertMediaType($mock, 'image/gif')->once();
$this->assertMediaType($mock, 'image/bmp')->once();
$this->assertMediaType($mock, 'image/jpeg')->twice();

if (function_exists('imagecreatefromwebp')) {
$this->assertMediaType($mock, 'image/webp')->once();
}

if (function_exists('imagecreatefromavif')) {
$this->assertMediaType($mock, 'image/avif')->once();
}
});

$this->assertSame('jpg', $this->encoder->setParams(['fm' => 'jpg'])->getFormat($image));
$this->assertSame('png', $this->encoder->setParams(['fm' => 'png'])->getFormat($image));
$this->assertSame('gif', $this->encoder->setParams(['fm' => 'gif'])->getFormat($image));
$this->assertSame('jpg', $this->encoder->setParams(['fm' => null])->getFormat($image));
$this->assertSame('png', $this->encoder->setParams(['fm' => null])->getFormat($image));
$this->assertSame('gif', $this->encoder->setParams(['fm' => null])->getFormat($image));
$this->assertSame('jpg', $this->encoder->setParams(['fm' => null])->getFormat($image));

$this->assertSame('jpg', $this->encoder->setParams(['fm' => ''])->getFormat($image));
$this->assertSame('jpg', $this->encoder->setParams(['fm' => 'invalid'])->getFormat($image));

if (function_exists('imagecreatefromwebp')) {
$this->assertSame('webp', $this->encoder->setParams(['fm' => null])->getFormat($image));
}

if (function_exists('imagecreatefromavif')) {
$this->assertSame('avif', $this->encoder->setParams(['fm' => null])->getFormat($image));
}
}

public function testGetQuality(): void
{
$this->assertSame(100, $this->encoder->setParams(['q' => '100'])->getQuality());
$this->assertSame(100, $this->encoder->setParams(['q' => 100])->getQuality());
$this->assertSame(90, $this->encoder->setParams(['q' => null])->getQuality());
$this->assertSame(90, $this->encoder->setParams(['q' => 'a'])->getQuality());
$this->assertSame(50, $this->encoder->setParams(['q' => '50.50'])->getQuality());
$this->assertSame(90, $this->encoder->setParams(['q' => '-1'])->getQuality());
$this->assertSame(90, $this->encoder->setParams(['q' => '101'])->getQuality());
}

/**
* Test functions that require the imagick extension.
*/
public function testWithImagick(): void
{
if (!extension_loaded('imagick')) {
$this->markTestSkipped(
'The imagick extension is not available.'
);
}
$manager = ImageManager::imagick();
// These need to be recreated with the imagick driver selected in the manager
$this->jpg = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/jpeg'))->toFilePointer());
$this->png = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/png'))->toFilePointer());
$this->gif = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/gif'))->toFilePointer());
$this->heic = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/heic'))->toFilePointer());
$this->tif = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/tiff'))->toFilePointer());

$this->assertSame('image/tiff', $this->getMime($this->encoder->setParams(['fm' => 'tiff'])->run($this->jpg)));
$this->assertSame('image/tiff', $this->getMime($this->encoder->setParams(['fm' => 'tiff'])->run($this->png)));
$this->assertSame('image/tiff', $this->getMime($this->encoder->setParams(['fm' => 'tiff'])->run($this->gif)));
$this->assertSame('image/tiff', $this->getMime($this->encoder->setParams(['fm' => 'tiff'])->run($this->heic)));
}

public function testSupportedFormats(): void
{
$expected = [
'avif' => 'image/avif',
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'pjpg' => 'image/jpeg',
'png' => 'image/png',
'webp' => 'image/webp',
'tiff' => 'image/tiff',
'heic' => 'image/heic',
];

$this->assertSame($expected, Encode::supportedFormats());
}

protected function getMime(EncodedImageInterface $image): string
{
return $image->mediaType();
}

/**
* Creates an assertion to check media type.
*/
/**
* @psalm-suppress MoreSpecificReturnType
*/
protected function assertMediaType(Mock $mock, string $mediaType): Mockery\Expectation
{
/**
* @psalm-suppress LessSpecificReturnStatement, UndefinedMagicMethod
*/
return $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => $mediaType]));
}
}
Loading

0 comments on commit a602e8f

Please sign in to comment.