From 0ed895c5dcbd5307c65a80b48ab009c1209ec689 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 15 Jan 2024 12:10:49 +0100 Subject: [PATCH] Add image tests --- tests/Drivers/Gd/ImageTest.php | 58 ++++++++++++++++++++++++++-- tests/Drivers/Imagick/ImageTest.php | 60 +++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 7 deletions(-) diff --git a/tests/Drivers/Gd/ImageTest.php b/tests/Drivers/Gd/ImageTest.php index a3c1385f..d5968834 100644 --- a/tests/Drivers/Gd/ImageTest.php +++ b/tests/Drivers/Gd/ImageTest.php @@ -4,16 +4,19 @@ namespace Intervention\Image\Tests\Drivers\Gd; use Intervention\Image\Analyzers\WidthAnalyzer; use Intervention\Image\Collection; +use Intervention\Image\Colors\Hsl\Colorspace; use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Drivers\Gd\Core; use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\Drivers\Gd\Frame; use Intervention\Image\EncodedImage; use Intervention\Image\Encoders\PngEncoder; +use Intervention\Image\Exceptions\EncoderException; use Intervention\Image\Exceptions\NotSupportedException; use Intervention\Image\Image; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; +use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ResolutionInterface; use Intervention\Image\Interfaces\SizeInterface; use Intervention\Image\Modifiers\GreyscaleModifier; @@ -87,9 +90,28 @@ class ImageTest extends TestCase $this->assertTrue($this->image->isAnimated()); } - public function testLoops(): void + public function testSetGetLoops(): void { $this->assertEquals(0, $this->image->loops()); + $result = $this->image->setLoops(10); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertEquals(10, $this->image->loops()); + } + + public function testRemoveAnimation(): void + { + $this->assertTrue($this->image->isAnimated()); + $result = $this->image->removeAnimation(); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertFalse($this->image->isAnimated()); + } + + public function testSliceAnimation(): void + { + $this->assertEquals(2, $this->image->count()); + $result = $this->image->sliceAnimation(0, 1); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertEquals(1, $this->image->count()); } public function testExif(): void @@ -166,6 +188,22 @@ class ImageTest extends TestCase unlink($path); } + public function testSaveFallback(): void + { + $path = __DIR__ . '/tmp.unknown'; + $result = $this->readTestImage('blue.gif')->save($path); + $this->assertInstanceOf(Image::class, $result); + $this->assertFileExists($path); + $this->assertMediaType('image/gif', file_get_contents($path)); + unlink($path); + } + + public function testSaveUndeterminedPath(): void + { + $this->expectException(EncoderException::class); + $this->createTestImage(2, 3)->save(); + } + public function testWidthHeightSize(): void { $this->assertEquals(3, $this->image->width()); @@ -178,9 +216,23 @@ class ImageTest extends TestCase $this->assertInstanceOf(ColorspaceInterface::class, $this->image->colorspace()); } - public function testResolution(): void + public function testSetColorspace(): void { - $this->assertInstanceOf(ResolutionInterface::class, $this->image->resolution()); + $this->expectException(NotSupportedException::class); + $this->image->setColorspace(Colorspace::class); + } + + public function testSetGetResolution(): void + { + $resolution = $this->image->resolution(); + $this->assertInstanceOf(ResolutionInterface::class, $resolution); + $this->assertEquals(96, $resolution->x()); + $this->assertEquals(96, $resolution->y()); + $result = $this->image->setResolution(300, 300); + $resolution = $this->image->resolution(); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertEquals(300, $resolution->x()); + $this->assertEquals(300, $resolution->y()); } public function testPickColor(): void diff --git a/tests/Drivers/Imagick/ImageTest.php b/tests/Drivers/Imagick/ImageTest.php index 5ecf956e..217e0c20 100644 --- a/tests/Drivers/Imagick/ImageTest.php +++ b/tests/Drivers/Imagick/ImageTest.php @@ -5,16 +5,21 @@ namespace Intervention\Image\Tests\Drivers\Imagick; use Imagick; use Intervention\Image\Analyzers\WidthAnalyzer; use Intervention\Image\Collection; +use Intervention\Image\Colors\Hsl\Colorspace as HslColorspace; +use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace; use Intervention\Image\Colors\Rgb\Color; +use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Drivers\Imagick\Core; use Intervention\Image\Drivers\Imagick\Driver; use Intervention\Image\Drivers\Imagick\Frame; use Intervention\Image\EncodedImage; use Intervention\Image\Encoders\PngEncoder; use Intervention\Image\Exceptions\ColorException; +use Intervention\Image\Exceptions\EncoderException; use Intervention\Image\Image; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; +use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ResolutionInterface; use Intervention\Image\Interfaces\SizeInterface; use Intervention\Image\Modifiers\GreyscaleModifier; @@ -86,9 +91,28 @@ class ImageTest extends TestCase $this->assertTrue($this->image->isAnimated()); } - public function testLoops(): void + public function testSetGetLoops(): void { $this->assertEquals(3, $this->image->loops()); + $result = $this->image->setLoops(10); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertEquals(10, $this->image->loops()); + } + + public function testRemoveAnimation(): void + { + $this->assertTrue($this->image->isAnimated()); + $result = $this->image->removeAnimation(); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertFalse($this->image->isAnimated()); + } + + public function testSliceAnimation(): void + { + $this->assertEquals(8, $this->image->count()); + $result = $this->image->sliceAnimation(0, 2); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertEquals(2, $this->image->count()); } public function testExif(): void @@ -165,6 +189,22 @@ class ImageTest extends TestCase unlink($path); } + public function testSaveFallback(): void + { + $path = __DIR__ . '/tmp.unknown'; + $result = $this->readTestImage('blue.gif')->save($path); + $this->assertInstanceOf(Image::class, $result); + $this->assertFileExists($path); + $this->assertMediaType('image/gif', file_get_contents($path)); + unlink($path); + } + + public function testSaveUndeterminedPath(): void + { + $this->expectException(EncoderException::class); + $this->createTestImage(2, 3)->save(); + } + public function testWidthHeightSize(): void { $this->assertEquals(20, $this->image->width()); @@ -172,14 +212,26 @@ class ImageTest extends TestCase $this->assertInstanceOf(SizeInterface::class, $this->image->size()); } - public function testColorspace(): void + public function testSetGetColorspace(): void { $this->assertInstanceOf(ColorspaceInterface::class, $this->image->colorspace()); + $this->assertInstanceOf(RgbColorspace::class, $this->image->colorspace()); + $result = $this->image->setColorspace(CmykColorspace::class); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertInstanceOf(CmykColorspace::class, $this->image->colorspace()); } - public function testResolution(): void + public function testSetGetResolution(): void { - $this->assertInstanceOf(ResolutionInterface::class, $this->image->resolution()); + $resolution = $this->image->resolution(); + $this->assertInstanceOf(ResolutionInterface::class, $resolution); + $this->assertEquals(0, $resolution->x()); + $this->assertEquals(0, $resolution->y()); + $result = $this->image->setResolution(300, 300); + $resolution = $this->image->resolution(); + $this->assertInstanceOf(ImageInterface::class, $result); + $this->assertEquals(300, $resolution->x()); + $this->assertEquals(300, $resolution->y()); } public function testPickColor(): void