1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-21 05:01:20 +02:00

Externalize data provider

This commit is contained in:
Oliver Vogel
2025-08-01 17:30:09 +02:00
parent dd1e8448c7
commit 60209b8f8f
5 changed files with 131 additions and 80 deletions

View File

@@ -11,29 +11,13 @@ use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\EncodedImage;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Tests\Traits\CanGenerateTestData;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use PHPUnit\Framework\ExpectationFailedException;
abstract class BaseTestCase extends MockeryTestCase
{
public static function getTestResourcePath(string $filename = 'test.jpg'): string
{
return sprintf('%s/resources/%s', __DIR__, $filename);
}
public static function getTestResourceData(string $filename = 'test.jpg'): string
{
return file_get_contents(self::getTestResourcePath($filename));
}
public static function getTestResourcePointer(string $filename = 'test.jpg'): mixed
{
$pointer = fopen('php://temp', 'rw');
fwrite($pointer, self::getTestResourceData($filename));
rewind($pointer);
return $pointer;
}
use CanGenerateTestData;
/**
* Assert that given color equals the given color channel values in the given optional tolerance

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Providers;
use Generator;
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Tests\Traits\CanGenerateTestData;
final class InputDataProvider
{
use CanGenerateTestData;
public static function handleImageInputDataProvider(): Generator
{
yield [
self::getTestResourcePath('test.jpg'),
[],
ImageInterface::class,
];
yield [
self::getTestResourceData('test.jpg'),
[],
ImageInterface::class,
];
}
public static function handleColorInputDataProvider(): Generator
{
yield [
'ffffff',
[],
ColorInterface::class,
];
yield [
'ffffff',
[new HexColorDecoder()],
ColorInterface::class,
];
yield [
'ffffff',
[HexColorDecoder::class],
ColorInterface::class,
];
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Traits;
trait CanGenerateTestData
{
public static function getTestResourcePath(string $filename = 'test.jpg'): string
{
return sprintf('%s/../resources/%s', __DIR__, $filename);
}
public static function getTestResourceData(string $filename = 'test.jpg'): string
{
return file_get_contents(self::getTestResourcePath($filename));
}
public static function getTestResourcePointer(string $filename = 'test.jpg'): mixed
{
$pointer = fopen('php://temp', 'rw');
fwrite($pointer, self::getTestResourceData($filename));
rewind($pointer);
return $pointer;
}
}

View File

@@ -7,7 +7,6 @@ namespace Intervention\Image\Tests\Unit\Drivers\Gd;
use Generator;
use Intervention\Image\Analyzers\WidthAnalyzer as GenericWidthAnalyzer;
use Intervention\Image\Colors\Rgb\Colorspace;
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
use Intervention\Image\Decoders\FilePathImageDecoder as GenericFilePathImageDecoder;
use Intervention\Image\Drivers\Gd\Analyzers\WidthAnalyzer;
use Intervention\Image\Drivers\Gd\Decoders\FilePathImageDecoder;
@@ -20,7 +19,6 @@ use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\FileExtension;
use Intervention\Image\Format;
use Intervention\Image\Interfaces\AnalyzerInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorProcessorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\DriverInterface;
@@ -29,8 +27,10 @@ use Intervention\Image\Interfaces\SpecializableInterface;
use Intervention\Image\MediaType;
use Intervention\Image\Modifiers\ResizeModifier as GenericResizeModifier;
use Intervention\Image\Tests\BaseTestCase;
use Intervention\Image\Tests\Providers\InputDataProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
#[RequiresPhpExtension('gd')]
@@ -74,8 +74,8 @@ final class DriverTest extends BaseTestCase
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProvider('handleImageInputDataProvider')]
#[DataProvider('handleColorInputDataProvider')]
#[DataProviderExternal(InputDataProvider::class, 'handleImageInputDataProvider')]
#[DataProviderExternal(InputDataProvider::class, 'handleColorInputDataProvider')]
public function testHandleInput(mixed $input, array $decoders, string $resultClassname): void
{
$this->assertInstanceOf($resultClassname, $this->driver->handleInput($input, $decoders));
@@ -84,7 +84,7 @@ final class DriverTest extends BaseTestCase
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProvider('handleImageInputDataProvider')]
#[DataProviderExternal(InputDataProvider::class, 'handleImageInputDataProvider')]
public function testHandleImageInput(mixed $input, array $decoders, string $resultClassname): void
{
$this->assertInstanceOf($resultClassname, $this->driver->handleImageInput($input, $decoders));
@@ -93,7 +93,7 @@ final class DriverTest extends BaseTestCase
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProvider('handleColorInputDataProvider')]
#[DataProviderExternal(InputDataProvider::class, 'handleColorInputDataProvider')]
public function testHandleColorInput(mixed $input, array $decoders, string $resultClassname): void
{
$this->assertInstanceOf($resultClassname, $this->driver->handleColorInput($input, $decoders));
@@ -102,8 +102,8 @@ final class DriverTest extends BaseTestCase
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProvider('handleImageInputDataProvider')]
public function testHandleColorInputFail(mixed $input, array $decoders): void
#[DataProviderExternal(InputDataProvider::class, 'handleImageInputDataProvider')]
public function testHandleColorInputFail(mixed $input, array $decoders, string $resultClassname): void
{
$this->expectException(DecoderException::class);
$this->driver->handleColorInput($input, $decoders);
@@ -112,49 +112,13 @@ final class DriverTest extends BaseTestCase
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProvider('handleColorInputDataProvider')]
public function testHandleImageInputFail(mixed $input, array $decoders): void
#[DataProviderExternal(InputDataProvider::class, 'handleColorInputDataProvider')]
public function testHandleImageInputFail(mixed $input, array $decoders, string $resultClassname): void
{
$this->expectException(DecoderException::class);
$this->driver->handleImageInput($input, $decoders);
}
public static function handleImageInputDataProvider(): Generator
{
yield [
self::getTestResourcePath('test.jpg'),
[],
ImageInterface::class,
];
yield [
self::getTestResourceData('test.jpg'),
[],
ImageInterface::class,
];
}
public static function handleColorInputDataProvider(): Generator
{
yield [
'ffffff',
[],
ColorInterface::class,
];
yield [
'ffffff',
[new HexColorDecoder()],
ColorInterface::class,
];
yield [
'ffffff',
[HexColorDecoder::class],
ColorInterface::class,
];
}
public function testColorProcessor(): void
{
$result = $this->driver->colorProcessor(new Colorspace());

View File

@@ -16,6 +16,7 @@ use Intervention\Image\Drivers\Imagick\Decoders\FilePathImageDecoder;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\Drivers\Imagick\Encoders\PngEncoder;
use Intervention\Image\Drivers\Imagick\Modifiers\ResizeModifier;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\FileExtension;
use Intervention\Image\Format;
@@ -27,8 +28,10 @@ use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializableInterface;
use Intervention\Image\MediaType;
use Intervention\Image\Tests\BaseTestCase;
use Intervention\Image\Tests\Providers\InputDataProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
#[RequiresPhpExtension('imagick')]
@@ -69,34 +72,55 @@ final class DriverTest extends BaseTestCase
$this->assertEquals(2, $image->count());
}
public function testHandleInputImage(): void
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProviderExternal(InputDataProvider::class, 'handleImageInputDataProvider')]
#[DataProviderExternal(InputDataProvider::class, 'handleColorInputDataProvider')]
public function testHandleInput(mixed $input, array $decoders, string $resultClassname): void
{
$result = $this->driver->handleInput($this->getTestResourcePath('test.jpg'));
$this->assertInstanceOf(ImageInterface::class, $result);
$this->assertInstanceOf($resultClassname, $this->driver->handleInput($input, $decoders));
}
public function testHandleInputColor(): void
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProviderExternal(InputDataProvider::class, 'handleImageInputDataProvider')]
public function testHandleImageInput(mixed $input, array $decoders, string $resultClassname): void
{
$result = $this->driver->handleInput('ffffff');
$this->assertInstanceOf(ColorInterface::class, $result);
$this->assertInstanceOf($resultClassname, $this->driver->handleImageInput($input, $decoders));
}
public function testHandleInputObjects(): void
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProviderExternal(InputDataProvider::class, 'handleColorInputDataProvider')]
public function testHandleColorInput(mixed $input, array $decoders, string $resultClassname): void
{
$result = $this->driver->handleInput('ffffff', [
new HexColorDecoder()
]);
$this->assertInstanceOf(ColorInterface::class, $result);
$this->assertInstanceOf($resultClassname, $this->driver->handleColorInput($input, $decoders));
}
public function testHandleInputClassnames(): void
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProviderExternal(InputDataProvider::class, 'handleImageInputDataProvider')]
public function testHandleColorInputFail(mixed $input, array $decoders, string $resultClassname): void
{
$result = $this->driver->handleInput('ffffff', [
HexColorDecoder::class
]);
$this->assertInstanceOf(ColorInterface::class, $result);
$this->expectException(DecoderException::class);
$this->driver->handleColorInput($input, $decoders);
}
/**
* @param array<string|DecoderInterface> $decoders
*/
#[DataProviderExternal(InputDataProvider::class, 'handleColorInputDataProvider')]
public function testHandleImageInputFail(mixed $input, array $decoders, string $resultClassname): void
{
$this->expectException(DecoderException::class);
$this->driver->handleImageInput($input, $decoders);
}
public function testColorProcessor(): void
{
$result = $this->driver->colorProcessor(new Colorspace());