AbstractImageTest.php 2.54 KB
Newer Older
Antonio Ramirez committed
1 2 3 4
<?php
namespace yiiunit\extensions\imagine;

use Yii;
5
use yii\imagine\Image;
Antonio Ramirez committed
6 7 8
use Imagine\Image\Point;
use yiiunit\VendorTestCase;

Qiang Xue committed
9
Yii::setAlias('@yii/imagine', __DIR__ . '/../../../../extensions/imagine');
Antonio Ramirez committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

abstract class AbstractImageTest extends VendorTestCase
{
	protected $imageFile;
	protected $watermarkFile;
	protected $runtimeTextFile;
	protected $runtimeWatermarkFile;

	protected function setUp()
	{
		$this->imageFile = Yii::getAlias('@yiiunit/data/imagine/large') . '.jpg';
		$this->watermarkFile = Yii::getAlias('@yiiunit/data/imagine/xparent') . '.gif';
		$this->runtimeTextFile = Yii::getAlias('@yiiunit/runtime/image-text-test') . '.png';
		$this->runtimeWatermarkFile = Yii::getAlias('@yiiunit/runtime/image-watermark-test') . '.png';
		parent::setUp();
	}

	protected function tearDown()
	{
		@unlink($this->runtimeTextFile);
		@unlink($this->runtimeWatermarkFile);
	}

33 34 35
	public function testText()
	{
		if (!$this->isFontTestSupported()) {
Antonio Ramirez committed
36 37 38 39 40
			$this->markTestSkipped('Skipping ImageGdTest Gd not installed');
		}

		$fontFile = Yii::getAlias('@yiiunit/data/imagine/GothamRnd-Light') . '.otf';

Qiang Xue committed
41
		$img = Image::text($this->imageFile, 'Yii-2 Image', $fontFile, [0, 0], [
Antonio Ramirez committed
42 43 44 45 46 47 48 49 50 51 52
			'size' => 12,
			'color' => '000'
		]);

		$img->save($this->runtimeTextFile);
		$this->assertTrue(file_exists($this->runtimeTextFile));

	}

	public function testCrop()
	{
53 54
		$point = [20, 20];
		$img = Image::crop($this->imageFile, 100, 100, $point);
Antonio Ramirez committed
55 56 57 58 59 60 61 62

		$this->assertEquals(100, $img->getSize()->getWidth());
		$this->assertEquals(100, $img->getSize()->getHeight());

	}

	public function testWatermark()
	{
63
		$img = Image::watermark($this->imageFile, $this->watermarkFile);
Antonio Ramirez committed
64 65 66 67 68 69 70
		$img->save($this->runtimeWatermarkFile);
		$this->assertTrue(file_exists($this->runtimeWatermarkFile));
	}

	public function testFrame()
	{
		$frameSize = 5;
71
		$original = Image::getImagine()->open($this->imageFile);
Antonio Ramirez committed
72
		$originalSize = $original->getSize();
73
		$img = Image::frame($this->imageFile, $frameSize, '666', 0);
Antonio Ramirez committed
74 75 76 77 78 79 80
		$size = $img->getSize();

		$this->assertEquals($size->getWidth(), $originalSize->getWidth() + ($frameSize * 2));
	}

	public function testThumbnail()
	{
81
		$img = Image::thumbnail($this->imageFile, 120, 120);
Antonio Ramirez committed
82 83 84 85 86 87 88 89

		$this->assertEquals(120, $img->getSize()->getWidth());
		$this->assertEquals(120, $img->getSize()->getHeight());
	}

	/**
	 * @expectedException \yii\base\InvalidConfigException
	 */
90 91 92 93
	public function testShouldThrowExceptionOnDriverInvalidArgument()
	{
		Image::setImagine(null);
		Image::$driver = 'fake-driver';
Qiang Xue committed
94
		Image::getImagine();
Antonio Ramirez committed
95 96 97 98
	}

	abstract protected function isFontTestSupported();
}