146 lines
4.4 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
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2014-03-27 23:55:06 +07:00
*/
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));
2014-05-04 15:13:31 +07:00
$this->assertEquals($oImage->isWatermark(), 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 testImages()
{
$images = array(
array('mars.jpg', 'image/jpeg', 'jpg', 'imagecreatefromjpeg', 'imagejpeg'),
array('mario.gif', 'image/gif', 'gif', 'imagecreatefromgif', 'imagegif'),
array('firefox.png', 'image/png', 'png', 'imagecreatefrompng', 'imagepng'),
array('duke_nukem.bmp', 'image/bmp', 'bmp', null, null),
array('angela_merkel.tif', 'image/tiff', 'tif', null, null),
);
foreach ($images as $imageData) {
list($source, $type, $extension, $createFunction, $imageFunction) = $imageData;
$source = __DIR__ . "/../_files/images/" . $source;
$image = new Image($source);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image);
$this->assertEquals($image->getSource(), $source);
$this->assertEquals($image->getMediaId(), md5($source));
$this->assertEquals($image->getImageType(), $type);
$this->assertEquals($image->getImageExtension(), $extension);
$this->assertEquals($image->getImageCreateFunction(), $createFunction);
$this->assertEquals($image->getImageFunction(), $imageFunction);
2014-05-04 15:13:31 +07:00
$this->assertFalse($image->isMemImage());
}
}
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('height' => 210, 'align' => 'center')
2014-03-23 12:37:31 -04:00
);
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-04-12 10:23:31 +07:00
/**
* Test invalid local image
*
* @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException
2014-03-30 11:09:14 +07:00
*/
public function testInvalidImageLocal()
{
new Image(__DIR__ . "/../_files/images/thisisnotarealimage");
}
2014-03-30 11:09:14 +07:00
/**
* Test invalid PHP Image
*
* @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException
2014-03-30 11:09:14 +07:00
*/
public function testInvalidImagePhp()
{
$object = new Image('test.php');
$object->getSource();
}
2014-03-30 11:09:14 +07:00
/**
* Test unsupported image
*
* @expectedException \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException
2014-03-30 11:09:14 +07:00
*/
public function testUnsupportedImage()
2014-03-30 11:09:14 +07:00
{
$object = new Image('http://samples.libav.org/image-samples/RACECAR.BMP');
$object->getSource();
2014-03-30 11:09:14 +07:00
}
2014-04-06 15:19:09 +07:00
/**
* Get relation Id
2014-04-06 15:19:09 +07:00
*/
public function testRelationID()
2014-04-06 15:19:09 +07:00
{
$oImage = new Image(__DIR__ . "/../_files/images/earth.jpg", array('width' => 100));
$iVal = rand(1, 1000);
$oImage->setRelationId($iVal);
$this->assertEquals($oImage->getRelationId(), $iVal);
2014-04-06 15:19:09 +07:00
}
/**
* Test archived image
*/
public function testArchivedImage()
{
$archiveFile = __DIR__ . "/../_files/documents/reader.docx";
$imageFile = 'word/media/image1.jpeg';
$image = new Image("zip://{$archiveFile}#{$imageFile}");
$this->assertEquals('image/jpeg', $image->getImageType());
}
}