Fixed image manipulator tests without extensions

This commit is contained in:
Marcin Kurczewski 2014-09-22 14:00:31 +02:00
parent caaf1acba7
commit 2f0d0f5fb7

View file

@ -3,101 +3,109 @@ namespace Szurubooru\Tests\Services;
class ImageManipulatorTest extends \Szurubooru\Tests\AbstractTestCase class ImageManipulatorTest extends \Szurubooru\Tests\AbstractTestCase
{ {
private $imageManipulators; public static function imageManipulatorProvider()
public function setUp()
{ {
parent::setUp(); $manipulators = [];
$manipulators[] = self::getImagickImageManipulator();
$imagickImageManipulator = new \Szurubooru\Services\ImageManipulation\ImagickImageManipulator(); $manipulators[] = self::getGdImageManipulator();
$gdImageManipulator = new \Szurubooru\Services\ImageManipulation\GdImageManipulator(); $manipulators[] = self::getAutoImageManipulator();
$autoImageManipulator = new \Szurubooru\Services\ImageManipulation\ImageManipulator( return array_map(function($manipulator)
$imagickImageManipulator, {
$gdImageManipulator); return [$manipulator];
}, array_filter($manipulators));
$this->imageManipulators = [
$imagickImageManipulator,
$gdImageManipulator,
$autoImageManipulator,
];
} }
public function testImageSize() public function testImagickAvailability()
{ {
foreach ($this->getImageManipulators() as $imageManipulator) if (!self::getImagickImageManipulator())
$this->markTestSkipped('Imagick is not installed');
$this->assertTrue(true);
}
public function testGdAvailability()
{
if (!self::getGdImageManipulator())
$this->markTestSkipped('Gd is not installed');
$this->assertTrue(true);
}
/**
* @dataProvider imageManipulatorProvider
*/
public function testImageSize($imageManipulator)
{ {
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg')); $image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$this->assertEquals(640, $imageManipulator->getImageWidth($image)); $this->assertEquals(640, $imageManipulator->getImageWidth($image));
$this->assertEquals(480, $imageManipulator->getImageHeight($image)); $this->assertEquals(480, $imageManipulator->getImageHeight($image));
} }
}
public function testNonImage() /**
{ * @dataProvider imageManipulatorProvider
foreach ($this->getImageManipulators() as $imageManipulator) */
public function testNonImage($imageManipulator)
{ {
$this->assertNull($imageManipulator->loadFromBuffer($this->getTestFile('flash.swf'))); $this->assertNull($imageManipulator->loadFromBuffer($this->getTestFile('flash.swf')));
} }
}
public function testImageResizing() /**
{ * @dataProvider imageManipulatorProvider
foreach ($this->getImageManipulators() as $imageManipulator) */
public function testImageResizing($imageManipulator)
{ {
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg')); $image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->resizeImage($image, 400, 500); $imageManipulator->resizeImage($image, 400, 500);
$this->assertEquals(400, $imageManipulator->getImageWidth($image)); $this->assertEquals(400, $imageManipulator->getImageWidth($image));
$this->assertEquals(500, $imageManipulator->getImageHeight($image)); $this->assertEquals(500, $imageManipulator->getImageHeight($image));
} }
}
public function testImageCroppingBleedWidth() /**
{ * @dataProvider imageManipulatorProvider
foreach ($this->getImageManipulators() as $imageManipulator) */
public function testImageCroppingBleedWidth($imageManipulator)
{ {
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg')); $image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 640, 480, 200, 200); $imageManipulator->cropImage($image, 640, 480, 200, 200);
$this->assertEquals(440, $imageManipulator->getImageWidth($image)); $this->assertEquals(440, $imageManipulator->getImageWidth($image));
$this->assertEquals(280, $imageManipulator->getImageHeight($image)); $this->assertEquals(280, $imageManipulator->getImageHeight($image));
} }
}
public function testImageCroppingBleedPosition() /**
{ * @dataProvider imageManipulatorProvider
foreach ($this->getImageManipulators() as $imageManipulator) */
public function testImageCroppingBleedPosition($imageManipulator)
{ {
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg')); $image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 640, 480, -200, -200); $imageManipulator->cropImage($image, 640, 480, -200, -200);
$this->assertEquals(440, $imageManipulator->getImageWidth($image)); $this->assertEquals(440, $imageManipulator->getImageWidth($image));
$this->assertEquals(280, $imageManipulator->getImageHeight($image)); $this->assertEquals(280, $imageManipulator->getImageHeight($image));
} }
}
public function testImageCroppingBleedBoth() /**
{ * @dataProvider imageManipulatorProvider
foreach ($this->getImageManipulators() as $imageManipulator) */
public function testImageCroppingBleedBoth($imageManipulator)
{ {
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg')); $image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 642, 481, -1, -1); $imageManipulator->cropImage($image, 642, 481, -1, -1);
$this->assertEquals(640, $imageManipulator->getImageWidth($image)); $this->assertEquals(640, $imageManipulator->getImageWidth($image));
$this->assertEquals(480, $imageManipulator->getImageHeight($image)); $this->assertEquals(480, $imageManipulator->getImageHeight($image));
} }
}
public function testImageCroppingMaxBleeding() /**
{ * @dataProvider imageManipulatorProvider
foreach ($this->getImageManipulators() as $imageManipulator) */
public function testImageCroppingMaxBleeding($imageManipulator)
{ {
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg')); $image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 100, 100, 1000, 1000); $imageManipulator->cropImage($image, 100, 100, 1000, 1000);
$this->assertEquals(1, $imageManipulator->getImageWidth($image)); $this->assertEquals(1, $imageManipulator->getImageWidth($image));
$this->assertEquals(1, $imageManipulator->getImageHeight($image)); $this->assertEquals(1, $imageManipulator->getImageHeight($image));
} }
}
public function testSaving() /**
{ * @dataProvider imageManipulatorProvider
foreach ($this->getImageManipulators() as $imageManipulator) */
public function testSaving($imageManipulator)
{ {
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg')); $image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$jpegBuffer = $imageManipulator->saveToBuffer($image, \Szurubooru\Services\ImageManipulation\IImageManipulator::FORMAT_JPEG); $jpegBuffer = $imageManipulator->saveToBuffer($image, \Szurubooru\Services\ImageManipulation\IImageManipulator::FORMAT_JPEG);
@ -105,10 +113,31 @@ class ImageManipulatorTest extends \Szurubooru\Tests\AbstractTestCase
$this->assertEquals('image/jpeg', \Szurubooru\Helpers\MimeHelper::getMimeTypeFromBuffer($jpegBuffer)); $this->assertEquals('image/jpeg', \Szurubooru\Helpers\MimeHelper::getMimeTypeFromBuffer($jpegBuffer));
$this->assertEquals('image/png', \Szurubooru\Helpers\MimeHelper::getMimeTypeFromBuffer($pngBuffer)); $this->assertEquals('image/png', \Szurubooru\Helpers\MimeHelper::getMimeTypeFromBuffer($pngBuffer));
} }
private static function getImagickImageManipulator()
{
if (extension_loaded('imagick'))
return new \Szurubooru\Services\ImageManipulation\ImagickImageManipulator();
else
return null;
} }
private function getImageManipulators() private static function getGdImageManipulator()
{ {
return $this->imageManipulators; if (extension_loaded('gd'))
return new \Szurubooru\Services\ImageManipulation\GdImageManipulator();
else
return null;
}
private static function getAutoImageManipulator()
{
if (extension_loaded('gd') and extension_loaded('imagick'))
{
return new \Szurubooru\Services\ImageManipulation\ImageManipulator(
self::getImagickImageManipulator(),
self::getGdImageManipulator());
}
return null;
} }
} }