1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-09 21:50:47 +02:00

Refactor InputHandler logic

This commit is contained in:
Oliver Vogel
2024-05-06 16:38:11 +02:00
parent 2e0cbf2408
commit cf0291f9c1
22 changed files with 152 additions and 654 deletions

View File

@@ -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)
);
}

View File

@@ -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)
);
}

View File

@@ -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);

View File

@@ -1,55 +0,0 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Unit\Drivers;
use PHPUnit\Framework\Attributes\CoversClass;
use Intervention\Image\Drivers\AbstractDecoder;
use Intervention\Image\Drivers\AbstractInputHandler;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Tests\BaseTestCase;
use Mockery;
#[CoversClass(\Intervention\Image\Drivers\AbstractInputHandler::class)]
final class AbstractInputHandlerTest extends BaseTestCase
{
public function testHandle(): void
{
$image = Mockery::mock(ImageInterface::class);
$chain = Mockery::mock(AbstractDecoder::class);
$chain->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];
}
};
}
}

View File

@@ -1,153 +0,0 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Unit\Drivers\Gd;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Image;
use Intervention\Image\Drivers\Gd\InputHandler;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Tests\BaseTestCase;
use SplFileInfo;
#[RequiresPhpExtension('gd')]
#[CoversClass(\Intervention\Image\Drivers\Gd\InputHandler::class)]
final class InputHandlerTest extends BaseTestCase
{
public function testHandleEmptyString(): void
{
$handler = new InputHandler();
$this->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());
}
}

View File

@@ -1,157 +0,0 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Unit\Drivers\Imagick;
use Imagick;
use ImagickPixel;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Drivers\Imagick\InputHandler;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Tests\BaseTestCase;
use SplFileInfo;
#[RequiresPhpExtension('imagick')]
#[CoversClass(\Intervention\Image\Drivers\Imagick\InputHandler::class)]
final class InputHandlerTest extends BaseTestCase
{
public function testHandleEmptyString(): void
{
$handler = new InputHandler();
$this->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());
}
}