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,112 +3,141 @@ 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'));
$this->assertEquals(640, $imageManipulator->getImageWidth($image));
$this->assertEquals(480, $imageManipulator->getImageHeight($image));
}
/**
* @dataProvider imageManipulatorProvider
*/
public function testNonImage($imageManipulator)
{
$this->assertNull($imageManipulator->loadFromBuffer($this->getTestFile('flash.swf')));
}
/**
* @dataProvider imageManipulatorProvider
*/
public function testImageResizing($imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->resizeImage($image, 400, 500);
$this->assertEquals(400, $imageManipulator->getImageWidth($image));
$this->assertEquals(500, $imageManipulator->getImageHeight($image));
}
/**
* @dataProvider imageManipulatorProvider
*/
public function testImageCroppingBleedWidth($imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 640, 480, 200, 200);
$this->assertEquals(440, $imageManipulator->getImageWidth($image));
$this->assertEquals(280, $imageManipulator->getImageHeight($image));
}
/**
* @dataProvider imageManipulatorProvider
*/
public function testImageCroppingBleedPosition($imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 640, 480, -200, -200);
$this->assertEquals(440, $imageManipulator->getImageWidth($image));
$this->assertEquals(280, $imageManipulator->getImageHeight($image));
}
/**
* @dataProvider imageManipulatorProvider
*/
public function testImageCroppingBleedBoth($imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 642, 481, -1, -1);
$this->assertEquals(640, $imageManipulator->getImageWidth($image));
$this->assertEquals(480, $imageManipulator->getImageHeight($image));
}
/**
* @dataProvider imageManipulatorProvider
*/
public function testImageCroppingMaxBleeding($imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 100, 100, 1000, 1000);
$this->assertEquals(1, $imageManipulator->getImageWidth($image));
$this->assertEquals(1, $imageManipulator->getImageHeight($image));
}
/**
* @dataProvider imageManipulatorProvider
*/
public function testSaving($imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$jpegBuffer = $imageManipulator->saveToBuffer($image, \Szurubooru\Services\ImageManipulation\IImageManipulator::FORMAT_JPEG);
$pngBuffer = $imageManipulator->saveToBuffer($image, \Szurubooru\Services\ImageManipulation\IImageManipulator::FORMAT_PNG);
$this->assertEquals('image/jpeg', \Szurubooru\Helpers\MimeHelper::getMimeTypeFromBuffer($jpegBuffer));
$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 static function getGdImageManipulator()
{
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'))
{ {
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg')); return new \Szurubooru\Services\ImageManipulation\ImageManipulator(
$this->assertEquals(640, $imageManipulator->getImageWidth($image)); self::getImagickImageManipulator(),
$this->assertEquals(480, $imageManipulator->getImageHeight($image)); self::getGdImageManipulator());
} }
} return null;
public function testNonImage()
{
foreach ($this->getImageManipulators() as $imageManipulator)
{
$this->assertNull($imageManipulator->loadFromBuffer($this->getTestFile('flash.swf')));
}
}
public function testImageResizing()
{
foreach ($this->getImageManipulators() as $imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->resizeImage($image, 400, 500);
$this->assertEquals(400, $imageManipulator->getImageWidth($image));
$this->assertEquals(500, $imageManipulator->getImageHeight($image));
}
}
public function testImageCroppingBleedWidth()
{
foreach ($this->getImageManipulators() as $imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 640, 480, 200, 200);
$this->assertEquals(440, $imageManipulator->getImageWidth($image));
$this->assertEquals(280, $imageManipulator->getImageHeight($image));
}
}
public function testImageCroppingBleedPosition()
{
foreach ($this->getImageManipulators() as $imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 640, 480, -200, -200);
$this->assertEquals(440, $imageManipulator->getImageWidth($image));
$this->assertEquals(280, $imageManipulator->getImageHeight($image));
}
}
public function testImageCroppingBleedBoth()
{
foreach ($this->getImageManipulators() as $imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 642, 481, -1, -1);
$this->assertEquals(640, $imageManipulator->getImageWidth($image));
$this->assertEquals(480, $imageManipulator->getImageHeight($image));
}
}
public function testImageCroppingMaxBleeding()
{
foreach ($this->getImageManipulators() as $imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$imageManipulator->cropImage($image, 100, 100, 1000, 1000);
$this->assertEquals(1, $imageManipulator->getImageWidth($image));
$this->assertEquals(1, $imageManipulator->getImageHeight($image));
}
}
public function testSaving()
{
foreach ($this->getImageManipulators() as $imageManipulator)
{
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
$jpegBuffer = $imageManipulator->saveToBuffer($image, \Szurubooru\Services\ImageManipulation\IImageManipulator::FORMAT_JPEG);
$pngBuffer = $imageManipulator->saveToBuffer($image, \Szurubooru\Services\ImageManipulation\IImageManipulator::FORMAT_PNG);
$this->assertEquals('image/jpeg', \Szurubooru\Helpers\MimeHelper::getMimeTypeFromBuffer($jpegBuffer));
$this->assertEquals('image/png', \Szurubooru\Helpers\MimeHelper::getMimeTypeFromBuffer($pngBuffer));
}
}
private function getImageManipulators()
{
return $this->imageManipulators;
} }
} }