diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 849cbe73..4647ca6e 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -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 diff --git a/tests/Providers/InputDataProvider.php b/tests/Providers/InputDataProvider.php new file mode 100644 index 00000000..9844fd01 --- /dev/null +++ b/tests/Providers/InputDataProvider.php @@ -0,0 +1,52 @@ + $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 $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 $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 $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 $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()); diff --git a/tests/Unit/Drivers/Imagick/DriverTest.php b/tests/Unit/Drivers/Imagick/DriverTest.php index ee7d53d7..fbb6955c 100644 --- a/tests/Unit/Drivers/Imagick/DriverTest.php +++ b/tests/Unit/Drivers/Imagick/DriverTest.php @@ -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 $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 $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 $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 $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 $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());