1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-19 12:11:26 +02:00

Add InputHandlerTest

This commit is contained in:
Oliver Vogel
2024-05-15 18:05:34 +02:00
parent a12b646f82
commit 25058825c2
7 changed files with 83 additions and 9 deletions

View File

@@ -44,9 +44,9 @@ class Color extends AbstractColor
*/ */
public static function create(mixed $input): ColorInterface public static function create(mixed $input): ColorInterface
{ {
return (new InputHandler([ return InputHandler::withDecoders([
Decoders\StringColorDecoder::class, Decoders\StringColorDecoder::class,
]))->handle($input); ])->handle($input);
} }
/** /**

View File

@@ -43,9 +43,9 @@ class Color extends AbstractColor
*/ */
public static function create(mixed $input): ColorInterface public static function create(mixed $input): ColorInterface
{ {
return (new InputHandler([ return InputHandler::withDecoders([
Decoders\StringColorDecoder::class, Decoders\StringColorDecoder::class,
]))->handle($input); ])->handle($input);
} }
/** /**

View File

@@ -43,9 +43,9 @@ class Color extends AbstractColor
*/ */
public static function create(mixed $input): ColorInterface public static function create(mixed $input): ColorInterface
{ {
return (new InputHandler([ return InputHandler::withDecoders([
Decoders\StringColorDecoder::class, Decoders\StringColorDecoder::class,
]))->handle($input); ])->handle($input);
} }
/** /**

View File

@@ -53,12 +53,12 @@ class Color extends AbstractColor
*/ */
public static function create(mixed $input): ColorInterface public static function create(mixed $input): ColorInterface
{ {
return (new InputHandler([ return InputHandler::withDecoders([
Decoders\HexColorDecoder::class, Decoders\HexColorDecoder::class,
Decoders\StringColorDecoder::class, Decoders\StringColorDecoder::class,
Decoders\TransparentColorDecoder::class, Decoders\TransparentColorDecoder::class,
Decoders\HtmlColornameDecoder::class, Decoders\HtmlColornameDecoder::class,
]))->handle($input); ])->handle($input);
} }
/** /**

View File

@@ -53,7 +53,7 @@ abstract class AbstractDriver implements DriverInterface
*/ */
public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface
{ {
return (new InputHandler($decoders, $this))->handle($input); return InputHandler::withDecoders($decoders, $this)->handle($input);
} }
/** /**

View File

@@ -75,6 +75,18 @@ class InputHandler implements InputHandlerInterface
$this->driver = $driver; $this->driver = $driver;
} }
/**
* Static factory method
*
* @param array<string|DecoderInterface> $decoders
* @param null|DriverInterface $driver
* @return InputHandler
*/
public static function withDecoders(array $decoders, ?DriverInterface $driver = null): self
{
return new self($decoders, $driver);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* *

View File

@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Unit;
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Tests\BaseTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
#[RequiresPhpExtension('gd')]
#[RequiresPhpExtension('imagick')]
#[CoversClass(InputHandler::class)]
class InputHandlerTest extends BaseTestCase
{
#[DataProvider('testHandleProvider')]
public function testHandleDefaultDecoders(string $driver, mixed $input, string $outputClassname): void
{
$handler = new InputHandler(driver: new $driver());
if ($outputClassname === ImageInterface::class || $outputClassname === ColorInterface::class) {
$this->assertInstanceOf($outputClassname, $handler->handle($input));
} else {
$this->expectException($outputClassname);
$handler->handle($input);
}
}
public static function testHandleProvider(): array
{
$base = [
[null, DecoderException::class],
['', DecoderException::class],
['fff', ColorInterface::class],
['rgba(0, 0, 0, 0)', ColorInterface::class],
['cmyk(0, 0, 0, 0)', ColorInterface::class],
['hsv(0, 0, 0)', ColorInterface::class],
['hsl(0, 0, 0)', ColorInterface::class],
['transparent', ColorInterface::class],
['steelblue', ColorInterface::class],
[self::getTestResourcePath(), ImageInterface::class],
[file_get_contents(self::getTestResourcePath()), ImageInterface::class],
];
$data = [];
$drivers = [GdDriver::class, ImagickDriver::class];
foreach ($drivers as $driver) {
foreach ($base as $line) {
array_unshift($line, $driver); // prepend driver
$data[] = $line;
}
}
return $data;
}
}