196 lines
6.1 KiB
PHP
Raw Normal View History

2014-03-09 19:09:22 +01:00
<?php
2014-03-27 23:55:06 +07:00
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Tests\Element;
2014-03-09 19:09:22 +01:00
use PhpOffice\PhpWord\Element\Image;
2014-03-09 19:09:22 +01:00
/**
* Test class for PhpOffice\PhpWord\Element\Image
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
2014-03-09 15:04:23 -04:00
class ImageTest extends \PHPUnit_Framework_TestCase
{
2014-03-30 11:09:14 +07:00
/**
* New instance
*/
2014-03-09 15:04:23 -04:00
public function testConstruct()
{
2014-03-23 12:37:31 -04:00
$src = __DIR__ . "/../_files/images/firefox.png";
$oImage = new Image($src);
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
2014-03-09 15:04:23 -04:00
$this->assertEquals($oImage->getSource(), $src);
$this->assertEquals($oImage->getMediaId(), md5($src));
$this->assertEquals($oImage->getIsWatermark(), false);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* New instance with style
*/
2014-03-09 15:04:23 -04:00
public function testConstructWithStyle()
{
2014-03-23 12:37:31 -04:00
$src = __DIR__ . "/../_files/images/firefox.png";
$oImage = new Image(
$src,
array('width' => 210, 'height' => 210, 'align' => 'center',
'wrappingStyle' => \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_BEHIND)
);
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle());
2014-03-09 15:04:23 -04:00
}
/**
2014-03-30 11:09:14 +07:00
* Valid image types
*/
public function testValidImageTypes()
{
2014-03-23 12:37:31 -04:00
new Image(__DIR__ . "/../_files/images/mars_noext_jpg");
new Image(__DIR__ . "/../_files/images/mars.jpg");
new Image(__DIR__ . "/../_files/images/mario.gif");
new Image(__DIR__ . "/../_files/images/firefox.png");
new Image(__DIR__ . "/../_files/images/duke_nukem.bmp");
new Image(__DIR__ . "/../_files/images/angela_merkel.tif");
}
/**
2014-03-30 11:09:14 +07:00
* Image not found
*
* @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException
*/
public function testImageNotFound()
{
2014-03-23 12:37:31 -04:00
new Image(__DIR__ . "/../_files/images/thisisnotarealimage");
}
/**
2014-03-30 11:09:14 +07:00
* Invalid image types
*
* @expectedException \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException
*/
public function testInvalidImageTypes()
{
2014-03-23 12:37:31 -04:00
new Image(__DIR__ . "/../_files/images/alexz-johnson.pcx");
}
2014-03-30 11:09:14 +07:00
/**
* Get style
*/
2014-03-09 15:04:23 -04:00
public function testStyle()
{
2014-03-23 12:37:31 -04:00
$oImage = new Image(
__DIR__ . "/../_files/images/earth.jpg",
array('width' => 210, 'height' => 210, 'align' => 'center')
);
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Get relation Id
*/
2014-03-09 15:04:23 -04:00
public function testRelationID()
{
2014-03-23 12:37:31 -04:00
$oImage = new Image(__DIR__ . "/../_files/images/earth.jpg");
2014-03-09 15:04:23 -04:00
$iVal = rand(1, 1000);
$oImage->setRelationId($iVal);
$this->assertEquals($oImage->getRelationId(), $iVal);
}
2014-03-30 11:09:14 +07:00
/**
* Get is watermark
*/
2014-03-09 15:04:23 -04:00
public function testWatermark()
{
2014-03-23 12:37:31 -04:00
$oImage = new Image(__DIR__ . "/../_files/images/earth.jpg");
2014-03-09 15:04:23 -04:00
$oImage->setIsWatermark(true);
$this->assertEquals($oImage->getIsWatermark(), true);
}
2014-03-30 11:09:14 +07:00
/**
* Test PNG
*/
public function testPNG()
{
$src = __DIR__ . "/../_files/images/firefox.png";
$oImage = new Image($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
$this->assertEquals($oImage->getSource(), $src);
$this->assertEquals($oImage->getMediaId(), md5($src));
$this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefrompng');
$this->assertEquals($oImage->getImageFunction(), 'imagepng');
$this->assertEquals($oImage->getImageExtension(), 'png');
$this->assertEquals($oImage->getImageType(), 'image/png');
}
2014-03-30 11:09:14 +07:00
/**
* Test GIF
*/
public function testGIF()
{
$src = __DIR__ . "/../_files/images/mario.gif";
$oImage = new Image($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
$this->assertEquals($oImage->getSource(), $src);
$this->assertEquals($oImage->getMediaId(), md5($src));
$this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefromgif');
$this->assertEquals($oImage->getImageFunction(), 'imagegif');
$this->assertEquals($oImage->getImageExtension(), 'gif');
$this->assertEquals($oImage->getImageType(), 'image/gif');
}
2014-03-30 11:09:14 +07:00
/**
* Test JPG
*/
public function testJPG()
{
$src = __DIR__ . "/../_files/images/earth.jpg";
$oImage = new Image($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
$this->assertEquals($oImage->getSource(), $src);
$this->assertEquals($oImage->getMediaId(), md5($src));
$this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefromjpeg');
$this->assertEquals($oImage->getImageFunction(), 'imagejpeg');
$this->assertEquals($oImage->getImageExtension(), 'jpg');
$this->assertEquals($oImage->getImageType(), 'image/jpeg');
}
2014-03-30 11:09:14 +07:00
/**
* Test BMP
*/
public function testBMP()
{
$oImage = new Image(__DIR__ . "/../_files/images/duke_nukem.bmp");
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
$this->assertEquals($oImage->getImageCreateFunction(), null);
$this->assertEquals($oImage->getImageFunction(), null);
$this->assertEquals($oImage->getImageExtension(), 'bmp');
$this->assertEquals($oImage->getImageType(), 'image/bmp');
}
2014-03-30 11:09:14 +07:00
/**
* Test TIFF
*/
public function testTIFF()
{
$oImage = new Image(__DIR__ . "/../_files/images/angela_merkel.tif");
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
2014-03-30 11:09:14 +07:00
$this->assertEquals($oImage->getImageCreateFunction(), null);
$this->assertEquals($oImage->getImageFunction(), null);
$this->assertEquals($oImage->getImageType(), 'image/tiff');
}
}