mirror of
https://github.com/Intervention/image.git
synced 2025-08-19 04:01:30 +02:00
Add InputHandlerTest
This commit is contained in:
@@ -44,9 +44,9 @@ class Color extends AbstractColor
|
||||
*/
|
||||
public static function create(mixed $input): ColorInterface
|
||||
{
|
||||
return (new InputHandler([
|
||||
return InputHandler::withDecoders([
|
||||
Decoders\StringColorDecoder::class,
|
||||
]))->handle($input);
|
||||
])->handle($input);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -43,9 +43,9 @@ class Color extends AbstractColor
|
||||
*/
|
||||
public static function create(mixed $input): ColorInterface
|
||||
{
|
||||
return (new InputHandler([
|
||||
return InputHandler::withDecoders([
|
||||
Decoders\StringColorDecoder::class,
|
||||
]))->handle($input);
|
||||
])->handle($input);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -43,9 +43,9 @@ class Color extends AbstractColor
|
||||
*/
|
||||
public static function create(mixed $input): ColorInterface
|
||||
{
|
||||
return (new InputHandler([
|
||||
return InputHandler::withDecoders([
|
||||
Decoders\StringColorDecoder::class,
|
||||
]))->handle($input);
|
||||
])->handle($input);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -53,12 +53,12 @@ class Color extends AbstractColor
|
||||
*/
|
||||
public static function create(mixed $input): ColorInterface
|
||||
{
|
||||
return (new InputHandler([
|
||||
return InputHandler::withDecoders([
|
||||
Decoders\HexColorDecoder::class,
|
||||
Decoders\StringColorDecoder::class,
|
||||
Decoders\TransparentColorDecoder::class,
|
||||
Decoders\HtmlColornameDecoder::class,
|
||||
]))->handle($input);
|
||||
])->handle($input);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -53,7 +53,7 @@ abstract class AbstractDriver implements DriverInterface
|
||||
*/
|
||||
public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface
|
||||
{
|
||||
return (new InputHandler($decoders, $this))->handle($input);
|
||||
return InputHandler::withDecoders($decoders, $this)->handle($input);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -75,6 +75,18 @@ class InputHandler implements InputHandlerInterface
|
||||
$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}
|
||||
*
|
||||
|
62
tests/Unit/InputHandlerTest.php
Normal file
62
tests/Unit/InputHandlerTest.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user