1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-25 23:06:13 +02:00

Add type hints in tests

This commit is contained in:
Oliver Vogel
2025-06-01 07:57:46 +02:00
parent 2af1ea158f
commit 5f3cc8a7a4
25 changed files with 62 additions and 27 deletions

View File

@@ -16,17 +16,17 @@ use PHPUnit\Framework\ExpectationFailedException;
abstract class BaseTestCase extends MockeryTestCase abstract class BaseTestCase extends MockeryTestCase
{ {
public static function getTestResourcePath($filename = 'test.jpg'): string public static function getTestResourcePath(string $filename = 'test.jpg'): string
{ {
return sprintf('%s/resources/%s', __DIR__, $filename); return sprintf('%s/resources/%s', __DIR__, $filename);
} }
public static function getTestResourceData($filename = 'test.jpg'): string public static function getTestResourceData(string $filename = 'test.jpg'): string
{ {
return file_get_contents(self::getTestResourcePath($filename)); return file_get_contents(self::getTestResourcePath($filename));
} }
public static function getTestResourcePointer($filename = 'test.jpg') public static function getTestResourcePointer(string $filename = 'test.jpg'): mixed
{ {
$pointer = fopen('php://temp', 'rw'); $pointer = fopen('php://temp', 'rw');
fwrite($pointer, self::getTestResourceData($filename)); fwrite($pointer, self::getTestResourceData($filename));
@@ -39,9 +39,8 @@ abstract class BaseTestCase extends MockeryTestCase
* Assert that given color equals the given color channel values in the given optional tolerance * Assert that given color equals the given color channel values in the given optional tolerance
* *
* @throws ExpectationFailedException * @throws ExpectationFailedException
* @return void
*/ */
protected function assertColor(int $r, int $g, int $b, int $a, ColorInterface $color, int $tolerance = 0) protected function assertColor(int $r, int $g, int $b, int $a, ColorInterface $color, int $tolerance = 0): void
{ {
// build errorMessage // build errorMessage
$errorMessage = function (int $r, int $g, $b, int $a, ColorInterface $color): string { $errorMessage = function (int $r, int $g, $b, int $a, ColorInterface $color): string {
@@ -90,7 +89,7 @@ abstract class BaseTestCase extends MockeryTestCase
); );
} }
protected function assertTransparency(ColorInterface $color) protected function assertTransparency(ColorInterface $color): void
{ {
$this->assertInstanceOf(RgbColor::class, $color); $this->assertInstanceOf(RgbColor::class, $color);
$channel = $color->channel(Alpha::class); $channel = $color->channel(Alpha::class);

View File

@@ -12,7 +12,7 @@ use Intervention\Image\Image;
abstract class GdTestCase extends BaseTestCase abstract class GdTestCase extends BaseTestCase
{ {
public static function readTestImage($filename = 'test.jpg'): Image public static function readTestImage(string $filename = 'test.jpg'): Image
{ {
return (new Driver())->specialize(new FilePathImageDecoder())->decode( return (new Driver())->specialize(new FilePathImageDecoder())->decode(
static::getTestResourcePath($filename) static::getTestResourcePath($filename)

View File

@@ -14,7 +14,7 @@ use Intervention\Image\Image;
abstract class ImagickTestCase extends BaseTestCase abstract class ImagickTestCase extends BaseTestCase
{ {
public static function readTestImage($filename = 'test.jpg'): Image public static function readTestImage(string $filename = 'test.jpg'): Image
{ {
return (new Driver())->specialize(new FilePathImageDecoder())->decode( return (new Driver())->specialize(new FilePathImageDecoder())->decode(
static::getTestResourcePath($filename) static::getTestResourcePath($filename)

View File

@@ -14,6 +14,9 @@ use PHPUnit\Framework\Attributes\DataProvider;
#[CoversClass(StringColorDecoder::class)] #[CoversClass(StringColorDecoder::class)]
final class StringColorDecoderTest extends BaseTestCase final class StringColorDecoderTest extends BaseTestCase
{ {
/**
* @param $channelValues array<int>
*/
#[DataProvider('decodeDataProvier')] #[DataProvider('decodeDataProvier')]
public function testDecode(string $input, string $classname, array $channelValues): void public function testDecode(string $input, string $classname, array $channelValues): void
{ {

View File

@@ -14,6 +14,9 @@ use PHPUnit\Framework\Attributes\DataProvider;
#[CoversClass(StringColorDecoder::class)] #[CoversClass(StringColorDecoder::class)]
final class StringColorDecoderTest extends BaseTestCase final class StringColorDecoderTest extends BaseTestCase
{ {
/**
* @param $channelValues array<int>
*/
#[DataProvider('decodeDataProvier')] #[DataProvider('decodeDataProvier')]
public function testDecodeHsv(string $input, string $classname, array $channelValues): void public function testDecodeHsv(string $input, string $classname, array $channelValues): void
{ {

View File

@@ -14,6 +14,9 @@ use PHPUnit\Framework\Attributes\DataProvider;
#[CoversClass(HexColorDecoder::class)] #[CoversClass(HexColorDecoder::class)]
final class HexColorDecoderTest extends BaseTestCase final class HexColorDecoderTest extends BaseTestCase
{ {
/**
* @param $channelValues array<int>
*/
#[DataProvider('decodeDataProvier')] #[DataProvider('decodeDataProvier')]
public function testDecode(string $input, string $classname, array $channelValues): void public function testDecode(string $input, string $classname, array $channelValues): void
{ {

View File

@@ -14,6 +14,9 @@ use PHPUnit\Framework\Attributes\DataProvider;
#[CoversClass(HtmlColorNameDecoder::class)] #[CoversClass(HtmlColorNameDecoder::class)]
final class HtmlColornameDecoderTest extends BaseTestCase final class HtmlColornameDecoderTest extends BaseTestCase
{ {
/**
* @param $channelValues array<int>
*/
#[DataProvider('decodeDataProvier')] #[DataProvider('decodeDataProvier')]
public function testDecode(string $input, string $classname, array $channelValues): void public function testDecode(string $input, string $classname, array $channelValues): void
{ {

View File

@@ -14,6 +14,9 @@ use PHPUnit\Framework\Attributes\DataProvider;
#[CoversClass(StringColorDecoder::class)] #[CoversClass(StringColorDecoder::class)]
final class StringColorDecoderTest extends BaseTestCase final class StringColorDecoderTest extends BaseTestCase
{ {
/**
* @param $channelValues array<int>
*/
#[DataProvider('decodeDataProvier')] #[DataProvider('decodeDataProvier')]
public function testDecode(string $input, string $classname, array $channelValues): void public function testDecode(string $input, string $classname, array $channelValues): void
{ {

View File

@@ -45,7 +45,7 @@ final class DriverTest extends BaseTestCase
public function testCreateAnimation(): void public function testCreateAnimation(): void
{ {
$image = $this->driver->createAnimation(function ($animation) { $image = $this->driver->createAnimation(function ($animation): void {
$animation->add($this->getTestResourcePath('red.gif'), .25); $animation->add($this->getTestResourcePath('red.gif'), .25);
$animation->add($this->getTestResourcePath('green.gif'), .25); $animation->add($this->getTestResourcePath('green.gif'), .25);
})->setLoops(5); })->setLoops(5);

View File

@@ -24,7 +24,7 @@ final class TextModifierTest extends GdTestCase
$modifier = new class ('test', new Point(), $font) extends TextModifier $modifier = new class ('test', new Point(), $font) extends TextModifier
{ {
public function test() public function test(): ColorInterface
{ {
return $this->textColor(); return $this->textColor();
} }

View File

@@ -45,7 +45,7 @@ final class DriverTest extends BaseTestCase
public function testCreateAnimation(): void public function testCreateAnimation(): void
{ {
$image = $this->driver->createAnimation(function ($animation) { $image = $this->driver->createAnimation(function ($animation): void {
$animation->add($this->getTestResourcePath('red.gif'), .25); $animation->add($this->getTestResourcePath('red.gif'), .25);
$animation->add($this->getTestResourcePath('green.gif'), .25); $animation->add($this->getTestResourcePath('green.gif'), .25);
})->setLoops(5); })->setLoops(5);

View File

@@ -24,7 +24,7 @@ final class TextModifierTest extends ImagickTestCase
$modifier = new class ('test', new Point(), $font) extends TextModifier $modifier = new class ('test', new Point(), $font) extends TextModifier
{ {
public function test() public function test(): ColorInterface
{ {
return $this->textColor(); return $this->textColor();
} }

View File

@@ -25,16 +25,19 @@ use PHPUnit\Framework\Attributes\DataProvider;
#[CoversClass(FileExtensionEncoder::class)] #[CoversClass(FileExtensionEncoder::class)]
final class FileExtensionEncoderTest extends BaseTestCase final class FileExtensionEncoderTest extends BaseTestCase
{ {
/**
* @param $options array<string, int>
*/
private function testEncoder(string|FileExtension $extension, array $options = []): EncoderInterface private function testEncoder(string|FileExtension $extension, array $options = []): EncoderInterface
{ {
$encoder = new class ($extension, ...$options) extends FileExtensionEncoder $encoder = new class ($extension, ...$options) extends FileExtensionEncoder
{ {
public function __construct($mediaType, ...$options) public function __construct(string|FileExtension $extension, mixed ...$options)
{ {
parent::__construct($mediaType, ...$options); parent::__construct($extension, ...$options);
} }
public function test($extension) public function test(string|FileExtension $extension): EncoderInterface
{ {
return $this->encoderByFileExtension($extension); return $this->encoderByFileExtension($extension);
} }

View File

@@ -25,16 +25,19 @@ use PHPUnit\Framework\Attributes\DataProvider;
#[CoversClass(MediaTypeEncoder::class)] #[CoversClass(MediaTypeEncoder::class)]
final class MediaTypeEncoderTest extends BaseTestCase final class MediaTypeEncoderTest extends BaseTestCase
{ {
/**
* @param $options array<string, int>
*/
private function testEncoder(string|MediaType $mediaType, array $options = []): EncoderInterface private function testEncoder(string|MediaType $mediaType, array $options = []): EncoderInterface
{ {
$encoder = new class ($mediaType, ...$options) extends MediaTypeEncoder $encoder = new class ($mediaType, ...$options) extends MediaTypeEncoder
{ {
public function __construct($mediaType, ...$options) public function __construct(string|MediaType $mediaType, mixed ...$options)
{ {
parent::__construct($mediaType, ...$options); parent::__construct($mediaType, ...$options);
} }
public function test($mediaType) public function test(string|MediaType $mediaType): EncoderInterface
{ {
return $this->encoderByMediaType($mediaType); return $this->encoderByMediaType($mediaType);
} }

View File

@@ -14,7 +14,7 @@ final class BezierFactoryTest extends BaseTestCase
{ {
public function testFactoryCallback(): void public function testFactoryCallback(): void
{ {
$factory = new BezierFactory(function ($bezier) { $factory = new BezierFactory(function ($bezier): void {
$bezier->background('f00'); $bezier->background('f00');
$bezier->border('ff0', 10); $bezier->border('ff0', 10);
$bezier->point(300, 260); $bezier->point(300, 260);

View File

@@ -15,7 +15,7 @@ final class CircleFactoryTest extends BaseTestCase
{ {
public function testFactoryCallback(): void public function testFactoryCallback(): void
{ {
$factory = new CircleFactory(new Point(1, 2), function ($circle) { $factory = new CircleFactory(new Point(1, 2), function ($circle): void {
$circle->background('fff'); $circle->background('fff');
$circle->border('ccc', 10); $circle->border('ccc', 10);
$circle->radius(100); $circle->radius(100);

View File

@@ -15,7 +15,7 @@ final class EllipseFactoryTest extends BaseTestCase
{ {
public function testFactoryCallback(): void public function testFactoryCallback(): void
{ {
$factory = new EllipseFactory(new Point(1, 2), function ($ellipse) { $factory = new EllipseFactory(new Point(1, 2), function ($ellipse): void {
$ellipse->background('fff'); $ellipse->background('fff');
$ellipse->border('ccc', 10); $ellipse->border('ccc', 10);
$ellipse->width(100); $ellipse->width(100);

View File

@@ -14,7 +14,7 @@ final class LineFactoryTest extends BaseTestCase
{ {
public function testFactoryCallback(): void public function testFactoryCallback(): void
{ {
$factory = new LineFactory(function ($line) { $factory = new LineFactory(function ($line): void {
$line->color('fff'); $line->color('fff');
$line->background('fff'); $line->background('fff');
$line->border('fff', 10); $line->border('fff', 10);

View File

@@ -14,7 +14,7 @@ final class PolygonFactoryTest extends BaseTestCase
{ {
public function testFactoryCallback(): void public function testFactoryCallback(): void
{ {
$factory = new PolygonFactory(function ($polygon) { $factory = new PolygonFactory(function ($polygon): void {
$polygon->background('fff'); $polygon->background('fff');
$polygon->border('ccc', 10); $polygon->border('ccc', 10);
$polygon->point(1, 2); $polygon->point(1, 2);

View File

@@ -15,7 +15,7 @@ final class RectangleFactoryTest extends BaseTestCase
{ {
public function testFactoryCallback(): void public function testFactoryCallback(): void
{ {
$factory = new RectangleFactory(new Point(1, 2), function ($rectangle) { $factory = new RectangleFactory(new Point(1, 2), function ($rectangle): void {
$rectangle->background('fff'); $rectangle->background('fff');
$rectangle->border('ccc', 10); $rectangle->border('ccc', 10);
$rectangle->width(100); $rectangle->width(100);

View File

@@ -51,6 +51,9 @@ final class RectangleResizerTest extends TestCase
$this->assertInstanceOf(RectangleResizer::class, $resizer); $this->assertInstanceOf(RectangleResizer::class, $resizer);
} }
/**
* @param $resizeParameters array<string, int>
*/
#[DataProvider('resizeDataProvider')] #[DataProvider('resizeDataProvider')]
public function testResize(Rectangle $input, array $resizeParameters, Rectangle $result): void public function testResize(Rectangle $input, array $resizeParameters, Rectangle $result): void
{ {
@@ -68,6 +71,9 @@ final class RectangleResizerTest extends TestCase
yield [new Rectangle(300, 200), [], new Rectangle(300, 200)]; yield [new Rectangle(300, 200), [], new Rectangle(300, 200)];
} }
/**
* @param $resizeParameters array<string, int>
*/
#[DataProvider('resizeDownDataProvider')] #[DataProvider('resizeDownDataProvider')]
public function testResizeDown(Rectangle $input, array $resizeParameters, Rectangle $result): void public function testResizeDown(Rectangle $input, array $resizeParameters, Rectangle $result): void
{ {
@@ -88,6 +94,9 @@ final class RectangleResizerTest extends TestCase
yield [new Rectangle(800, 600), [], new Rectangle(800, 600)]; yield [new Rectangle(800, 600), [], new Rectangle(800, 600)];
} }
/**
* @param $resizeParameters array<string, int>
*/
#[DataProvider('scaleDataProvider')] #[DataProvider('scaleDataProvider')]
public function testScale(Rectangle $input, array $resizeParameters, Rectangle $result): void public function testScale(Rectangle $input, array $resizeParameters, Rectangle $result): void
{ {
@@ -119,6 +128,9 @@ final class RectangleResizerTest extends TestCase
yield [new Rectangle(800, 600), [], new Rectangle(800, 600)]; yield [new Rectangle(800, 600), [], new Rectangle(800, 600)];
} }
/**
* @param $resizeParameters array<string, int>
*/
#[DataProvider('scaleDownDataProvider')] #[DataProvider('scaleDownDataProvider')]
public function testScaleDown(Rectangle $input, array $resizeParameters, Rectangle $result): void public function testScaleDown(Rectangle $input, array $resizeParameters, Rectangle $result): void
{ {

View File

@@ -58,7 +58,7 @@ final class ImageManagerTestGd extends BaseTestCase
public function testAnimate(): void public function testAnimate(): void
{ {
$manager = new ImageManager(Driver::class); $manager = new ImageManager(Driver::class);
$image = $manager->animate(function ($animation) { $image = $manager->animate(function ($animation): void {
$animation->add($this->getTestResourcePath('red.gif'), .25); $animation->add($this->getTestResourcePath('red.gif'), .25);
}); });
$this->assertInstanceOf(ImageInterface::class, $image); $this->assertInstanceOf(ImageInterface::class, $image);

View File

@@ -58,7 +58,7 @@ final class ImageManagerTestImagick extends BaseTestCase
public function testAnimate(): void public function testAnimate(): void
{ {
$manager = new ImageManager(Driver::class); $manager = new ImageManager(Driver::class);
$image = $manager->animate(function ($animation) { $image = $manager->animate(function ($animation): void {
$animation->add($this->getTestResourcePath('red.gif'), .25); $animation->add($this->getTestResourcePath('red.gif'), .25);
}); });
$this->assertInstanceOf(ImageInterface::class, $image); $this->assertInstanceOf(ImageInterface::class, $image);

View File

@@ -18,7 +18,10 @@ final class TextModifierTest extends BaseTestCase
{ {
$modifier = new class ('test', new Point(), new Font()) extends TextModifier $modifier = new class ('test', new Point(), new Font()) extends TextModifier
{ {
public function testStrokeOffsets(FontInterface $font) /**
* @return array<?Point>
*/
public function testStrokeOffsets(FontInterface $font): array
{ {
return $this->strokeOffsets($font); return $this->strokeOffsets($font);
} }

View File

@@ -24,7 +24,7 @@ final class FontFactoryTest extends BaseTestCase
public function testBuildWithCallback(): void public function testBuildWithCallback(): void
{ {
$factory = new FontFactory(function (FontFactory $font) { $factory = new FontFactory(function (FontFactory $font): void {
$font->filename($this->getTestResourcePath('test.ttf')); $font->filename($this->getTestResourcePath('test.ttf'));
$font->color('#b01735'); $font->color('#b01735');
$font->size(70); $font->size(70);