diff --git a/src/Colors/Cmyk/Color.php b/src/Colors/Cmyk/Color.php index 68c6e0db..dabacbbf 100644 --- a/src/Colors/Cmyk/Color.php +++ b/src/Colors/Cmyk/Color.php @@ -10,7 +10,7 @@ use Intervention\Image\Colors\Cmyk\Channels\Magenta; use Intervention\Image\Colors\Cmyk\Channels\Yellow; use Intervention\Image\Colors\Cmyk\Channels\Key; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; -use Intervention\Image\Drivers\AbstractInputHandler; +use Intervention\Image\InputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; @@ -44,11 +44,9 @@ class Color extends AbstractColor */ public static function create(mixed $input): ColorInterface { - return (new class ([ + return (new InputHandler([ Decoders\StringColorDecoder::class, - ]) extends AbstractInputHandler - { - })->handle($input); + ]))->handle($input); } /** diff --git a/src/Colors/Hsl/Color.php b/src/Colors/Hsl/Color.php index 3cc1d433..2871963f 100644 --- a/src/Colors/Hsl/Color.php +++ b/src/Colors/Hsl/Color.php @@ -9,7 +9,7 @@ use Intervention\Image\Colors\Hsl\Channels\Hue; use Intervention\Image\Colors\Hsl\Channels\Luminance; use Intervention\Image\Colors\Hsl\Channels\Saturation; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; -use Intervention\Image\Drivers\AbstractInputHandler; +use Intervention\Image\InputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; @@ -43,11 +43,9 @@ class Color extends AbstractColor */ public static function create(mixed $input): ColorInterface { - return (new class ([ + return (new InputHandler([ Decoders\StringColorDecoder::class, - ]) extends AbstractInputHandler - { - })->handle($input); + ]))->handle($input); } /** diff --git a/src/Colors/Hsv/Color.php b/src/Colors/Hsv/Color.php index e09a49b8..d4580149 100644 --- a/src/Colors/Hsv/Color.php +++ b/src/Colors/Hsv/Color.php @@ -9,7 +9,7 @@ use Intervention\Image\Colors\Hsv\Channels\Hue; use Intervention\Image\Colors\Hsv\Channels\Saturation; use Intervention\Image\Colors\Hsv\Channels\Value; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; -use Intervention\Image\Drivers\AbstractInputHandler; +use Intervention\Image\InputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; @@ -43,11 +43,9 @@ class Color extends AbstractColor */ public static function create(mixed $input): ColorInterface { - return (new class ([ + return (new InputHandler([ Decoders\StringColorDecoder::class, - ]) extends AbstractInputHandler - { - })->handle($input); + ]))->handle($input); } /** diff --git a/src/Colors/Rgb/Color.php b/src/Colors/Rgb/Color.php index 4473987f..40d831ac 100644 --- a/src/Colors/Rgb/Color.php +++ b/src/Colors/Rgb/Color.php @@ -9,7 +9,7 @@ use Intervention\Image\Colors\Rgb\Channels\Blue; use Intervention\Image\Colors\Rgb\Channels\Green; use Intervention\Image\Colors\Rgb\Channels\Red; use Intervention\Image\Colors\Rgb\Channels\Alpha; -use Intervention\Image\Drivers\AbstractInputHandler; +use Intervention\Image\InputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; @@ -53,14 +53,12 @@ class Color extends AbstractColor */ public static function create(mixed $input): ColorInterface { - return (new class ([ + return (new InputHandler([ Decoders\HexColorDecoder::class, Decoders\StringColorDecoder::class, Decoders\TransparentColorDecoder::class, Decoders\HtmlColornameDecoder::class, - ]) extends AbstractInputHandler - { - })->handle($input); + ]))->handle($input); } /** diff --git a/src/Decoders/ColorObjectDecoder.php b/src/Decoders/ColorObjectDecoder.php new file mode 100644 index 00000000..f81f7170 --- /dev/null +++ b/src/Decoders/ColorObjectDecoder.php @@ -0,0 +1,11 @@ +decode($input); - } catch (DecoderException $e) { - if (!$this->hasSuccessor()) { - throw new DecoderException($e->getMessage()); - } - - return $this->successor->handle($input); - } - - return $decoded; - } - - /** - * Determine if current decoder has a successor - * - * @return bool - */ - protected function hasSuccessor(): bool - { - return $this->successor !== null; - } - /** * Determine if the given input is GIF data format * diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php index bbef4608..62c76817 100644 --- a/src/Drivers/AbstractDriver.php +++ b/src/Drivers/AbstractDriver.php @@ -6,10 +6,13 @@ namespace Intervention\Image\Drivers; use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\NotSupportedException; +use Intervention\Image\InputHandler; use Intervention\Image\Interfaces\AnalyzerInterface; +use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\DriverInterface; use Intervention\Image\Interfaces\EncoderInterface; +use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; use Intervention\Image\Interfaces\SpecializableInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -25,6 +28,16 @@ abstract class AbstractDriver implements DriverInterface $this->checkHealth(); } + /** + * {@inheritdoc} + * + * @see DriverInterface::handleInput() + */ + public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface + { + return (new InputHandler($decoders, $this))->handle($input); + } + /** * {@inheritdoc} * diff --git a/src/Drivers/AbstractInputHandler.php b/src/Drivers/AbstractInputHandler.php deleted file mode 100644 index bb079546..00000000 --- a/src/Drivers/AbstractInputHandler.php +++ /dev/null @@ -1,71 +0,0 @@ - - */ - protected array $decoders = []; - - /** - * Create new input handler instance with given decoder classnames - * - * @param array $decoders - * @return void - */ - public function __construct(array $decoders = []) - { - $this->decoders = count($decoders) ? $decoders : $this->decoders; - } - - /** - * {@inheritdoc} - * - * @see InputHandlerInterface::handle() - */ - public function handle($input): ImageInterface|ColorInterface - { - return $this->chain()->handle($input); - } - - /** - * Stack the decoder array into a nested decoder object - * - * @throws DecoderException - * @return AbstractDecoder - */ - protected function chain(): AbstractDecoder - { - if (count($this->decoders) == 0) { - throw new DecoderException('No decoders found in ' . $this::class); - } - - // get last decoder in stack - list($decoder) = array_slice(array_reverse($this->decoders), 0, 1); - $chain = ($decoder instanceof DecoderInterface) ? $decoder : new $decoder(); - - // only accept DecoderInterface - if (!($chain instanceof DecoderInterface)) { - throw new DecoderException('Decoder must implement in ' . DecoderInterface::class); - } - - // build decoder chain - foreach (array_slice(array_reverse($this->decoders), 1) as $decoder) { - $chain = ($decoder instanceof DecoderInterface) ? new ($decoder::class)($chain) : new $decoder($chain); - } - - return $chain; - } -} diff --git a/src/Drivers/Gd/Decoders/ColorObjectDecoder.php b/src/Drivers/Gd/Decoders/ColorObjectDecoder.php index b1096aa6..51abb3c0 100644 --- a/src/Drivers/Gd/Decoders/ColorObjectDecoder.php +++ b/src/Drivers/Gd/Decoders/ColorObjectDecoder.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Intervention\Image\Drivers\Gd\Decoders; -use Intervention\Image\Drivers\AbstractDecoder; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; diff --git a/src/Drivers/Gd/Decoders/ImageObjectDecoder.php b/src/Drivers/Gd/Decoders/ImageObjectDecoder.php index aa5e9eb1..61b92674 100644 --- a/src/Drivers/Gd/Decoders/ImageObjectDecoder.php +++ b/src/Drivers/Gd/Decoders/ImageObjectDecoder.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Intervention\Image\Drivers\Gd\Decoders; -use Intervention\Image\Drivers\AbstractDecoder; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; diff --git a/src/Drivers/Gd/Driver.php b/src/Drivers/Gd/Driver.php index 090faebd..0542d220 100644 --- a/src/Drivers/Gd/Driver.php +++ b/src/Drivers/Gd/Driver.php @@ -11,7 +11,6 @@ use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Format; use Intervention\Image\FileExtension; use Intervention\Image\Image; -use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorProcessorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\DriverInterface; @@ -113,16 +112,6 @@ class Driver extends AbstractDriver return call_user_func($animation); } - /** - * {@inheritdoc} - * - * @see DriverInterface::handleInput() - */ - public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface - { - return (new InputHandler($this->specializeMultiple($decoders)))->handle($input); - } - /** * {@inheritdoc} * diff --git a/src/Drivers/Gd/InputHandler.php b/src/Drivers/Gd/InputHandler.php deleted file mode 100644 index d1628935..00000000 --- a/src/Drivers/Gd/InputHandler.php +++ /dev/null @@ -1,50 +0,0 @@ - - */ - protected array $decoders = [ - NativeObjectDecoder::class, - ImageObjectDecoder::class, - ColorObjectDecoder::class, - RgbHexColorDecoder::class, - RgbStringColorDecoder::class, - CmykStringColorDecoder::class, - HsvStringColorDecoder::class, - HslStringColorDecoder::class, - TransparentColorDecoder::class, - HtmlColornameDecoder::class, - FilePointerImageDecoder::class, - FilePathImageDecoder::class, - SplFileInfoImageDecoder::class, - BinaryImageDecoder::class, - DataUriImageDecoder::class, - Base64ImageDecoder::class, - ]; -} diff --git a/src/Drivers/Imagick/Decoders/ColorObjectDecoder.php b/src/Drivers/Imagick/Decoders/ColorObjectDecoder.php index 138046b5..7938c823 100644 --- a/src/Drivers/Imagick/Decoders/ColorObjectDecoder.php +++ b/src/Drivers/Imagick/Decoders/ColorObjectDecoder.php @@ -4,12 +4,12 @@ declare(strict_types=1); namespace Intervention\Image\Drivers\Imagick\Decoders; -use Intervention\Image\Drivers\AbstractDecoder; +use Intervention\Image\Drivers\SpecializableDecoder; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; -class ColorObjectDecoder extends AbstractDecoder +class ColorObjectDecoder extends SpecializableDecoder { public function decode(mixed $input): ImageInterface|ColorInterface { diff --git a/src/Drivers/Imagick/Driver.php b/src/Drivers/Imagick/Driver.php index 2c01f83b..fbd2f2d6 100644 --- a/src/Drivers/Imagick/Driver.php +++ b/src/Drivers/Imagick/Driver.php @@ -13,7 +13,6 @@ use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Format; use Intervention\Image\FileExtension; use Intervention\Image\Image; -use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorProcessorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\DriverInterface; @@ -116,16 +115,6 @@ class Driver extends AbstractDriver return call_user_func($animation); } - /** - * {@inheritdoc} - * - * @see DriverInterface::handleInput() - */ - public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface - { - return (new InputHandler($this->specializeMultiple($decoders)))->handle($input); - } - /** * {@inheritdoc} * diff --git a/src/Drivers/Imagick/InputHandler.php b/src/Drivers/Imagick/InputHandler.php deleted file mode 100644 index 5c7b748b..00000000 --- a/src/Drivers/Imagick/InputHandler.php +++ /dev/null @@ -1,50 +0,0 @@ - - */ - protected array $decoders = [ - NativeObjectDecoder::class, - ImageObjectDecoder::class, - ColorObjectDecoder::class, - RgbHexColorDecoder::class, - RgbStringColorDecoder::class, - CmykStringColorDecoder::class, - HsvStringColorDecoder::class, - HslStringColorDecoder::class, - TransparentColorDecoder::class, - HtmlColornameDecoder::class, - FilePointerImageDecoder::class, - FilePathImageDecoder::class, - SplFileInfoImageDecoder::class, - BinaryImageDecoder::class, - DataUriImageDecoder::class, - Base64ImageDecoder::class, - ]; -} diff --git a/src/InputHandler.php b/src/InputHandler.php new file mode 100644 index 00000000..9e19bc26 --- /dev/null +++ b/src/InputHandler.php @@ -0,0 +1,112 @@ + + */ + protected array $decoders = [ + // NativeObjectDecoder::class, + ImageObjectDecoder::class, + ColorObjectDecoder::class, + RgbHexColorDecoder::class, + RgbStringColorDecoder::class, + CmykStringColorDecoder::class, + HsvStringColorDecoder::class, + HslStringColorDecoder::class, + TransparentColorDecoder::class, + HtmlColornameDecoder::class, + FilePointerImageDecoder::class, + FilePathImageDecoder::class, + SplFileInfoImageDecoder::class, + BinaryImageDecoder::class, + DataUriImageDecoder::class, + Base64ImageDecoder::class, + ]; + + /** + * Driver with which the decoder classes are specialized + * + * @var null|DriverInterface + */ + protected ?DriverInterface $driver = null; + + /** + * Create new input handler instance with given decoder classnames + * + * @param DriverInterface $driver + * @param array $decoders + * @return void + */ + public function __construct(array $decoders = [], ?DriverInterface $driver = null) + { + $this->decoders = count($decoders) ? $decoders : $this->decoders; + $this->driver = $driver; + } + + /** + * {@inheritdoc} + * + * @see InputHandlerInterface::handle() + */ + public function handle($input): ImageInterface|ColorInterface + { + foreach ($this->decoders as $decoderClassname) { + // resolve river specialized decoder + $decoder = $this->resolve($decoderClassname); + + try { + return $decoder->decode($input); + } catch (DecoderException $e) { + // let next decoder try + } + } + + throw new DecoderException(isset($e) ? $e->getMessage() : ''); + } + + /** + * Resolve the given classname to an decoder object + * + * @param string|DecoderInterface $decoder + * @return DecoderInterface + * @throws NotSupportedException + */ + private function resolve(string|DecoderInterface $decoder): DecoderInterface + { + if (empty($this->driver)) { + return new $decoder(); + } + + return $this->driver->specialize(new $decoder()); + } +} diff --git a/tests/GdTestCase.php b/tests/GdTestCase.php index b4ea3f49..6afd44f9 100644 --- a/tests/GdTestCase.php +++ b/tests/GdTestCase.php @@ -14,7 +14,7 @@ abstract class GdTestCase extends BaseTestCase { public function readTestImage($filename = 'test.jpg'): Image { - return (new FilePathImageDecoder())->handle( + return (new FilePathImageDecoder())->decode( $this->getTestResourcePath($filename) ); } diff --git a/tests/ImagickTestCase.php b/tests/ImagickTestCase.php index c312b5a4..ef23695c 100644 --- a/tests/ImagickTestCase.php +++ b/tests/ImagickTestCase.php @@ -15,7 +15,7 @@ abstract class ImagickTestCase extends BaseTestCase { public function readTestImage($filename = 'test.jpg'): Image { - return (new FilePathImageDecoder())->handle( + return (new FilePathImageDecoder())->decode( $this->getTestResourcePath($filename) ); } diff --git a/tests/Unit/Drivers/AbstractDecoderTest.php b/tests/Unit/Drivers/AbstractDecoderTest.php index e156db8f..2a9ff77f 100644 --- a/tests/Unit/Drivers/AbstractDecoderTest.php +++ b/tests/Unit/Drivers/AbstractDecoderTest.php @@ -7,7 +7,6 @@ namespace Intervention\Image\Tests\Unit\Drivers; use PHPUnit\Framework\Attributes\CoversClass; use Exception; use Intervention\Image\Drivers\AbstractDecoder; -use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\CollectionInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; @@ -18,35 +17,6 @@ use stdClass; #[CoversClass(\Intervention\Image\Drivers\AbstractDecoder::class)] final class AbstractDecoderTest extends BaseTestCase { - public function testHandle(): void - { - $result = Mockery::mock(ColorInterface::class); - $decoder = Mockery::mock(AbstractDecoder::class); - $decoder->shouldReceive('decode')->with('test input')->andReturn($result); - $decoder->handle('test input'); - } - - public function testHandleFail(): void - { - $decoder = Mockery::mock(AbstractDecoder::class, []); - $decoder->shouldReceive('decode')->with('test input')->andThrow(DecoderException::class); - $this->expectException(DecoderException::class); - $decoder->handle('test input'); - } - - public function testHandleFailWithSuccessor(): void - { - $result = Mockery::mock(ColorInterface::class); - $successor = Mockery::mock(AbstractDecoder::class); - $successor->shouldReceive('decode')->with('test input')->andReturn($result); - $decoder = Mockery::mock( - AbstractDecoder::class, - [$successor] - ); - $decoder->shouldReceive('decode')->with('test input')->andThrow(DecoderException::class); - $decoder->handle('test input'); - } - public function testIsGifFormat(): void { $decoder = Mockery::mock(AbstractDecoder::class); diff --git a/tests/Unit/Drivers/AbstractInputHandlerTest.php b/tests/Unit/Drivers/AbstractInputHandlerTest.php deleted file mode 100644 index 0e0e1929..00000000 --- a/tests/Unit/Drivers/AbstractInputHandlerTest.php +++ /dev/null @@ -1,55 +0,0 @@ -shouldReceive('handle')->with('test image')->andReturn($image); - $chain->shouldReceive('decode')->with('test image')->andReturn(Mockery::mock(ImageInterface::class)); - - $modifier = $this->getModifier($chain); - $modifier->handle('test image'); - } - - public function testChainNoItems(): void - { - $handler = new class () extends AbstractInputHandler - { - }; - - $this->expectException(DecoderException::class); - $handler->handle('test'); - } - - private function getModifier(AbstractDecoder $chain): AbstractInputHandler - { - return new class ([$chain]) extends AbstractInputHandler - { - public function __construct(protected array $decoders = []) - { - // - } - - protected function chain(): AbstractDecoder - { - return $this->decoders[0]; - } - }; - } -} diff --git a/tests/Unit/Drivers/Gd/InputHandlerTest.php b/tests/Unit/Drivers/Gd/InputHandlerTest.php deleted file mode 100644 index f2e2e475..00000000 --- a/tests/Unit/Drivers/Gd/InputHandlerTest.php +++ /dev/null @@ -1,153 +0,0 @@ -expectException(DecoderException::class); - $handler->handle(''); - } - - public function testHandleBinaryImage(): void - { - $handler = new InputHandler(); - $input = file_get_contents($this->getTestResourcePath('test.jpg')); - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleGdImage(): void - { - $handler = new InputHandler(); - $result = $handler->handle(imagecreatetruecolor(3, 2)); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleSplFileInfo(): void - { - $handler = new InputHandler(); - $input = new SplFileInfo($this->getTestResourcePath('test.jpg')); - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleFilePathImage(): void - { - $handler = new InputHandler(); - $input = $this->getTestResourcePath('animation.gif'); - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleBase64Image(): void - { - $handler = new InputHandler(); - $input = base64_encode(file_get_contents($this->getTestResourcePath('animation.gif'))); - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleDataUriImage(): void - { - $handler = new InputHandler(); - $input = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNb' . - 'yblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleHexColor(): void - { - $handler = new InputHandler(); - $input = 'ccff33'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([204, 255, 51, 255], $result->toArray()); - - $handler = new InputHandler(); - $input = 'cf3'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([204, 255, 51, 255], $result->toArray()); - - $handler = new InputHandler(); - $input = '#123456'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([18, 52, 86, 255], $result->toArray()); - - $handler = new InputHandler(); - $input = '#333'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([51, 51, 51, 255], $result->toArray()); - - $handler = new InputHandler(); - $input = '#3333'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([51, 51, 51, 51], $result->toArray()); - - $handler = new InputHandler(); - $input = '#33333333'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([51, 51, 51, 51], $result->toArray()); - } - - public function testHandleRgbString(): void - { - $handler = new InputHandler(); - $result = $handler->handle('rgb(10, 20, 30)'); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([10, 20, 30, 255], $result->toArray()); - - $handler = new InputHandler(); - $result = $handler->handle('rgba(10, 20, 30, 1.0)'); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([10, 20, 30, 255], $result->toArray()); - } - - public function testHandleHsvString(): void - { - $handler = new InputHandler(); - $result = $handler->handle('hsv(10, 20, 30)'); - $this->assertInstanceOf(HsvColor::class, $result); - $this->assertEquals([10, 20, 30], $result->toArray()); - } - - public function testHandleCmykString(): void - { - $handler = new InputHandler(); - $result = $handler->handle('cmyk(10, 20, 30, 40)'); - $this->assertInstanceOf(CmykColor::class, $result); - $this->assertEquals([10, 20, 30, 40], $result->toArray()); - } - - public function testHandleTransparent(): void - { - $handler = new InputHandler(); - $input = 'transparent'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([255, 255, 255, 0], $result->toArray()); - } -} diff --git a/tests/Unit/Drivers/Imagick/InputHandlerTest.php b/tests/Unit/Drivers/Imagick/InputHandlerTest.php deleted file mode 100644 index 1b4fbf39..00000000 --- a/tests/Unit/Drivers/Imagick/InputHandlerTest.php +++ /dev/null @@ -1,157 +0,0 @@ -expectException(DecoderException::class); - $handler->handle(''); - } - - public function testHandleBinaryImage(): void - { - $handler = new InputHandler(); - $input = file_get_contents($this->getTestResourcePath('animation.gif')); - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleImagick(): void - { - $imagick = new Imagick(); - $imagick->newImage(3, 2, new ImagickPixel('rgba(255, 255, 255, 255)'), 'png'); - $handler = new InputHandler(); - $result = $handler->handle($imagick); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleSplFileInfo(): void - { - $handler = new InputHandler(); - $input = new SplFileInfo($this->getTestResourcePath('test.jpg')); - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleFilePathImage(): void - { - $handler = new InputHandler(); - $input = $this->getTestResourcePath('animation.gif'); - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleBase64Image(): void - { - $handler = new InputHandler(); - $input = base64_encode(file_get_contents($this->getTestResourcePath('animation.gif'))); - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleDataUriImage(): void - { - $handler = new InputHandler(); - $input = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACN' . - 'byblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; - $result = $handler->handle($input); - $this->assertInstanceOf(Image::class, $result); - } - - public function testHandleHexColor(): void - { - $handler = new InputHandler(); - $input = 'ccff33'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([204, 255, 51, 255], $result->toArray()); - - $handler = new InputHandler(); - $input = 'cf3'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([204, 255, 51, 255], $result->toArray()); - - $handler = new InputHandler(); - $input = '#123456'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([18, 52, 86, 255], $result->toArray()); - - $handler = new InputHandler(); - $input = '#333'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([51, 51, 51, 255], $result->toArray()); - - $handler = new InputHandler(); - $input = '#3333'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([51, 51, 51, 51], $result->toArray()); - - $handler = new InputHandler(); - $input = '#33333333'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([51, 51, 51, 51], $result->toArray()); - } - - public function testHandleRgbString(): void - { - $handler = new InputHandler(); - $result = $handler->handle('rgb(10, 20, 30)'); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([10, 20, 30, 255], $result->toArray()); - - $handler = new InputHandler(); - $result = $handler->handle('rgba(10, 20, 30, 1.0)'); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([10, 20, 30, 255], $result->toArray()); - } - - public function testHandleCmykString(): void - { - $handler = new InputHandler(); - $result = $handler->handle('cmyk(10, 20, 30, 40)'); - $this->assertInstanceOf(CmykColor::class, $result); - $this->assertEquals([10, 20, 30, 40], $result->toArray()); - } - - public function testHandleHsvString(): void - { - $handler = new InputHandler(); - $result = $handler->handle('hsv(10, 20, 30)'); - $this->assertInstanceOf(HsvColor::class, $result); - $this->assertEquals([10, 20, 30], $result->toArray()); - } - - public function testHandleTransparent(): void - { - $handler = new InputHandler(); - $input = 'transparent'; - $result = $handler->handle($input); - $this->assertInstanceOf(RgbColor::class, $result); - $this->assertEquals([255, 255, 255, 0], $result->toArray()); - } -}