From 2f0d0f5fb7d803d4e558356eb96ee85ec5e39941 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 22 Sep 2014 14:00:31 +0200 Subject: [PATCH] Fixed image manipulator tests without extensions --- tests/Services/ImageManipulatorTest.php | 231 +++++++++++++----------- 1 file changed, 130 insertions(+), 101 deletions(-) diff --git a/tests/Services/ImageManipulatorTest.php b/tests/Services/ImageManipulatorTest.php index e31e75e7..5bbeb716 100644 --- a/tests/Services/ImageManipulatorTest.php +++ b/tests/Services/ImageManipulatorTest.php @@ -3,112 +3,141 @@ namespace Szurubooru\Tests\Services; class ImageManipulatorTest extends \Szurubooru\Tests\AbstractTestCase { - private $imageManipulators; - - public function setUp() + public static function imageManipulatorProvider() { - parent::setUp(); - - $imagickImageManipulator = new \Szurubooru\Services\ImageManipulation\ImagickImageManipulator(); - $gdImageManipulator = new \Szurubooru\Services\ImageManipulation\GdImageManipulator(); - $autoImageManipulator = new \Szurubooru\Services\ImageManipulation\ImageManipulator( - $imagickImageManipulator, - $gdImageManipulator); - - $this->imageManipulators = [ - $imagickImageManipulator, - $gdImageManipulator, - $autoImageManipulator, - ]; + $manipulators = []; + $manipulators[] = self::getImagickImageManipulator(); + $manipulators[] = self::getGdImageManipulator(); + $manipulators[] = self::getAutoImageManipulator(); + return array_map(function($manipulator) + { + return [$manipulator]; + }, array_filter($manipulators)); } - 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')); - $this->assertEquals(640, $imageManipulator->getImageWidth($image)); - $this->assertEquals(480, $imageManipulator->getImageHeight($image)); + return new \Szurubooru\Services\ImageManipulation\ImageManipulator( + self::getImagickImageManipulator(), + self::getGdImageManipulator()); } - } - - 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; + return null; } }