Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Feb 6, 2025
1 parent 770ffa5 commit ee471d3
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/Image/Darkroom/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function defaults(): array
protected function grayscale(Image $image, array $options): Image
{
if ($options['grayscale'] === true) {
$image->setColorspace(Image::COLORSPACE_GRAY);
$image->setImageColorspace(Image::COLORSPACE_GRAY);
}

return $image;
Expand All @@ -112,7 +112,9 @@ protected function sharpen(Image $image, array $options): Image
}

$amount = max(1, min(100, $options['sharpen'])) / 100;
return $image->sharepenImage(0.0, $amount);
$image->sharpenImage(0.0, $amount);

return $image;
}

/**
Expand Down
189 changes: 175 additions & 14 deletions tests/Image/Darkroom/ImagickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Kirby\Image\Darkroom;

use Imagick as Image;
use Kirby\Filesystem\Dir;
use Kirby\Filesystem\F;
use Kirby\TestCase;
use ReflectionClass;

/**
* @coversDefaultClass \Kirby\Image\Darkroom\Imagick
Expand All @@ -31,19 +33,19 @@ public function testProcess()
copy(static::FIXTURES . '/cat.jpg', $file = static::TMP . '/cat.jpg');

$this->assertSame([
'blur' => false,
'crop' => false,
'format' => null,
'grayscale' => false,
'height' => 500,
'quality' => 90,
'scaleHeight' => 1.0,
'scaleWidth' => 1.0,
'sharpen' => null,
'width' => 500,
'interlace' => false,
'threads' => 1,
'sourceWidth' => 500,
'blur' => false,
'crop' => false,
'format' => null,
'grayscale' => false,
'height' => 500,
'quality' => 90,
'scaleHeight' => 1.0,
'scaleWidth' => 1.0,
'sharpen' => null,
'width' => 500,
'interlace' => false,
'threads' => 1,
'sourceWidth' => 500,
'sourceHeight' => 500
], $im->process($file));
}
Expand Down Expand Up @@ -82,7 +84,7 @@ public function testKeepColorProfileStripMeta(string $basename, bool $crop)
$command = 'identify -format "%[profile:icc]" ' . escapeshellarg($file) . ' 2>/dev/null';
$before = shell_exec($command);
$im->process($file);
$after = shell_exec($command);
$after = shell_exec($command);
$this->assertSame($before, $after);

// ensure that other metadata has been stripped
Expand All @@ -106,4 +108,163 @@ public static function keepColorProfileStripMetaProvider(): array
['png-srgb-gps.png', true],
];
}

/**
* @dataProvider autoOrientProvider
*/
public function testAutoOrient(int $orientation, array $expectedTransformations)
{
$image = $this->createMock(Image::class);
$image->method('getImageOrientation')->willReturn($orientation);

foreach ($expectedTransformations as $method) {
$image->expects($this->once())->method($method);
}

$image->expects($this->once())->method('setImageOrientation')
->with(Image::ORIENTATION_TOPLEFT);

$imagick = new Imagick();
$result = (new ReflectionClass($imagick))->getMethod('autoOrient');
$result->invoke($imagick, $image);
}

public static function autoOrientProvider(): array
{
return [
'No change' => [Image::ORIENTATION_TOPLEFT, []],
'Flop' => [Image::ORIENTATION_TOPRIGHT, ['flopImage']],
'Rotate 180' => [Image::ORIENTATION_BOTTOMRIGHT, ['rotateImage']],
'Flop + Rotate 180' => [Image::ORIENTATION_BOTTOMLEFT, ['flopImage', 'rotateImage']],
'Flop + Rotate -90' => [Image::ORIENTATION_LEFTTOP, ['flopImage', 'rotateImage']],
'Rotate 90' => [Image::ORIENTATION_RIGHTTOP, ['rotateImage']],
'Flop + Rotate 90' => [Image::ORIENTATION_RIGHTBOTTOM, ['flopImage', 'rotateImage']],
'Rotate -90' => [Image::ORIENTATION_LEFTBOTTOM, ['rotateImage']],
];
}

public function testSharpen()
{
$image = $this->createMock(Image::class);

$image->expects($this->once())
->method('sharpenImage')
->with($this->equalTo(0), $this->equalTo(0.5));

$imagick = new Imagick();
$reflection = new ReflectionClass($imagick);
$method = $reflection->getMethod('sharpen');
$method->invoke($imagick, $image, ['sharpen' => 50]);
}

public function testInterlace()
{
$im = new Imagick();

copy(
static::FIXTURES . '/cat.jpg',
$file = static::TMP . '/interlace.jpg'
);

$im->process($file);

$processedImage = new Image($file);
$this->assertEquals(Image::INTERLACE_NO, $processedImage->getInterlaceScheme());
}

public function testInterlaceEnabled()
{
$im = new Imagick(['interlace' => true]);

copy(
static::FIXTURES . '/cat.jpg',
$file = static::TMP . '/interlace.jpg'
);

$im->process($file);

$processedImage = new Image($file);
$this->assertEquals(Image::INTERLACE_LINE, $processedImage->getInterlaceScheme());

Check failure on line 187 in tests/Image/Darkroom/ImagickTest.php

View workflow job for this annotation

GitHub Actions / Unit tests - PHP 8.2

Failed asserting that 1 matches expected 2.

Check failure on line 187 in tests/Image/Darkroom/ImagickTest.php

View workflow job for this annotation

GitHub Actions / Unit tests - PHP 8.3

Failed asserting that 1 matches expected 2.

Check failure on line 187 in tests/Image/Darkroom/ImagickTest.php

View workflow job for this annotation

GitHub Actions / Unit tests - PHP 8.4

Failed asserting that 1 matches expected 2.
}

public function testGrayscale()
{
$im = new Imagick(['grayscale' => true]);

copy(
static::FIXTURES . '/cat.jpg',
$file = static::TMP . '/grayscale.jpg'
);

$im->process($file);

$processedImage = new Image($file);
$this->assertEquals(Image::COLORSPACE_GRAY, $processedImage->getImageColorspace());
}

public function testCoalesce()
{
copy(
static::FIXTURES . '/animated.gif',
$file = static::TMP . '/coalesce.gif'
);

$image = new Image($file);
$this->assertSame(3, $image->getNumberImages());

$im = new Imagick();
$im->process($file);

$image = new Image($file);
$this->assertSame(3, $image->getNumberImages());

Check failure on line 219 in tests/Image/Darkroom/ImagickTest.php

View workflow job for this annotation

GitHub Actions / Unit tests - PHP 8.2

Failed asserting that 1 is identical to 3.

Check failure on line 219 in tests/Image/Darkroom/ImagickTest.php

View workflow job for this annotation

GitHub Actions / Unit tests - PHP 8.3

Failed asserting that 1 is identical to 3.

Check failure on line 219 in tests/Image/Darkroom/ImagickTest.php

View workflow job for this annotation

GitHub Actions / Unit tests - PHP 8.4

Failed asserting that 1 is identical to 3.
}

public function testResize()
{
$im = new Imagick([
'crop' => true,
'width' => 200,
'height' => 150
]);

copy(
static::FIXTURES . '/cat.jpg',
$file = static::TMP . '/resize.jpg'
);

$im->process($file);
$image = new Image($file);

$this->assertEquals(200, $image->getImageWidth());
$this->assertEquals(150, $image->getImageHeight());
}

/**
* @dataProvider resizeGravityProvider
*/
public function testResizeGravity(string $crop, int $gravity)
{
$image = $this->createMock(Image::class);
$image->method('getImageGravity')->willReturn($gravity);
$image->expects($this->once())->method('setGravity')->with($gravity);

$imagick = new Imagick();
$result = (new ReflectionClass($imagick))->getMethod('resize');
$result->invoke($imagick, $image, ['crop' => $crop, 'width' => 200, 'height' => 150]);
}

public static function resizeGravityProvider(): array
{
return [
['top left', Image::GRAVITY_NORTHWEST],
['top', Image::GRAVITY_NORTH],
['top right', Image::GRAVITY_NORTHEAST],
['left', Image::GRAVITY_WEST],
['right', Image::GRAVITY_EAST],
['bottom left', Image::GRAVITY_SOUTHWEST],
['bottom', Image::GRAVITY_SOUTH],
['bottom right', Image::GRAVITY_SOUTHEAST],
['center', Image::GRAVITY_CENTER]
];
}
}
Binary file added tests/Image/fixtures/image/animated.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ee471d3

Please sign in to comment.