-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter out parameter that do not match target encoder (#1333)
Filter out parameters/options that are passed to a format-unspecific encoder like AutoEncoder::class or MediaTypeEncoder::class but are not available in the format specific target encoder.
- Loading branch information
1 parent
5464ca5
commit 33cbb21
Showing
3 changed files
with
181 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Intervention\Image\Tests\Unit\Encoders; | ||
|
||
use Intervention\Image\Encoders\AvifEncoder; | ||
use Intervention\Image\Encoders\BmpEncoder; | ||
use Intervention\Image\Encoders\GifEncoder; | ||
use Intervention\Image\Encoders\HeicEncoder; | ||
use Intervention\Image\Encoders\Jpeg2000Encoder; | ||
use Intervention\Image\Encoders\JpegEncoder; | ||
use Intervention\Image\Encoders\MediaTypeEncoder; | ||
use Intervention\Image\Encoders\PngEncoder; | ||
use Intervention\Image\Encoders\TiffEncoder; | ||
use Intervention\Image\Encoders\WebpEncoder; | ||
use Intervention\Image\Exceptions\EncoderException; | ||
use Intervention\Image\Interfaces\EncoderInterface; | ||
use Intervention\Image\MediaType; | ||
use Intervention\Image\Tests\BaseTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
||
final class MediaTypeEncoderTest extends BaseTestCase | ||
{ | ||
private function testEncoder(string|MediaType $mediaType, array $options = []): EncoderInterface | ||
{ | ||
$encoder = new class ($mediaType, ...$options) extends MediaTypeEncoder | ||
{ | ||
public function __construct($mediaType, ...$options) | ||
{ | ||
parent::__construct($mediaType, ...$options); | ||
} | ||
|
||
public function test($mediaType) | ||
{ | ||
return $this->encoderByMediaType($mediaType); | ||
} | ||
}; | ||
|
||
return $encoder->test($mediaType); | ||
} | ||
|
||
#[DataProvider('targetEncoderProvider')] | ||
public function testEncoderByMediaType( | ||
string|MediaType $mediaType, | ||
string $targetEncoderClassname, | ||
): void { | ||
$this->assertInstanceOf( | ||
$targetEncoderClassname, | ||
$this->testEncoder($mediaType) | ||
); | ||
} | ||
|
||
public static function targetEncoderProvider(): array | ||
{ | ||
return [ | ||
['image/webp', WebpEncoder::class], | ||
['image/avif', AvifEncoder::class], | ||
['image/jpeg', JpegEncoder::class], | ||
['image/bmp', BmpEncoder::class], | ||
['image/gif', GifEncoder::class], | ||
['image/png', PngEncoder::class], | ||
['image/png', PngEncoder::class], | ||
['image/tiff', TiffEncoder::class], | ||
['image/jp2', Jpeg2000Encoder::class], | ||
['image/heic', HeicEncoder::class], | ||
[MediaType::IMAGE_WEBP, WebpEncoder::class], | ||
[MediaType::IMAGE_AVIF, AvifEncoder::class], | ||
[MediaType::IMAGE_JPEG, JpegEncoder::class], | ||
[MediaType::IMAGE_BMP, BmpEncoder::class], | ||
[MediaType::IMAGE_GIF, GifEncoder::class], | ||
[MediaType::IMAGE_PNG, PngEncoder::class], | ||
[MediaType::IMAGE_TIFF, TiffEncoder::class], | ||
[MediaType::IMAGE_JP2, Jpeg2000Encoder::class], | ||
[MediaType::IMAGE_HEIC, HeicEncoder::class], | ||
[MediaType::IMAGE_HEIF, HeicEncoder::class], | ||
]; | ||
} | ||
|
||
public function testArgumentsNotSupportedByTargetEncoder(): void | ||
{ | ||
$encoder = $this->testEncoder( | ||
'image/png', | ||
[ | ||
'interlaced' => true, // is not ignored | ||
'quality' => 10, // is ignored because png encoder has no quality argument | ||
], | ||
); | ||
|
||
$this->assertInstanceOf(PngEncoder::class, $encoder); | ||
$this->assertTrue($encoder->interlaced); | ||
} | ||
|
||
public function testEncoderByFileExtensionUnknown(): void | ||
{ | ||
$this->expectException(EncoderException::class); | ||
$this->testEncoder('test'); | ||
} | ||
} |