Added image writer tests

This commit is contained in:
Gabriel Bull 2014-03-12 10:09:42 -04:00
parent 68de9ab31b
commit d991fdc6d1
4 changed files with 85 additions and 26 deletions

View File

@ -25,6 +25,9 @@
* @version 0.7.0 * @version 0.7.0
*/ */
use PhpOffice\PhpWord\Exceptions\InvalidImageException;
use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
/** /**
* Class PHPWord_Writer_Word2007 * Class PHPWord_Writer_Word2007
*/ */
@ -191,25 +194,40 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter
} }
} }
private function _chkContentTypes($src) /**
* @param string $src
*/
private function checkContentTypes($src)
{ {
$srcInfo = pathinfo($src); $supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);
$extension = strtolower($srcInfo['extension']);
if (substr($extension, 0, 3) == 'php') {
$extension = 'php';
}
$_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff', 'php');
if (in_array($extension, $_supportedImageTypes)) { $extension = null;
$imagedata = getimagesize($src); if (stripos(strrev($src), strrev('.php')) === 0) {
$imagetype = image_type_to_mime_type($imagedata[2]); $extension = 'php';
$imageext = image_type_to_extension($imagedata[2]); } else {
$imageext = str_replace('.', '', $imageext); $imageType = exif_imagetype($src);
if ($imageext == 'jpeg') { if ($imageType === IMAGETYPE_JPEG) {
$imageext = 'jpg'; $extension = 'jpg';
} elseif ($imageType === IMAGETYPE_GIF) {
$extension = 'gif';
} elseif ($imageType === IMAGETYPE_PNG) {
$extension = 'png';
} elseif ($imageType === IMAGETYPE_BMP) {
$extension = 'bmp';
} elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) {
$extension = 'tif';
} }
if (!in_array($imagetype, $this->_imageTypes)) { }
$this->_imageTypes[$imageext] = $imagetype;
if (in_array($extension, $supportedImageTypes)) {
$imageData = getimagesize($src);
$imageType = image_type_to_mime_type($imageData[2]);
$imageExtension = str_replace('.', '', image_type_to_extension($imageData[2]));
if ($imageExtension === 'jpeg') {
$imageExtension = 'jpg';
}
if (!in_array($imageType, $this->_imageTypes)) {
$this->_imageTypes[$imageExtension] = $imageType;
} }
} else { } else {
if (!in_array($extension, $this->_objectTypes)) { if (!in_array($extension, $this->_objectTypes)) {
@ -258,10 +276,10 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter
$objZip->addFromString('word/' . $element['target'], $imageContents); $objZip->addFromString('word/' . $element['target'], $imageContents);
imagedestroy($image); imagedestroy($image);
$this->_chkContentTypes($element['source']); $this->checkContentTypes($element['source']);
} else { } else {
$objZip->addFile($element['source'], 'word/' . $element['target']); $objZip->addFile($element['source'], 'word/' . $element['target']);
$this->_chkContentTypes($element['source']); $this->checkContentTypes($element['source']);
} }
} }
} }

View File

@ -4,6 +4,7 @@ namespace PHPWord\Tests\Writer;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
use PHPWord_Writer_Word2007; use PHPWord_Writer_Word2007;
use PHPWord; use PHPWord;
use PHPWord\Tests\TestHelperDOCX;
/** /**
* Class Word2007Test * Class Word2007Test
@ -13,9 +14,11 @@ use PHPWord;
*/ */
class Word2007Test extends \PHPUnit_Framework_TestCase class Word2007Test extends \PHPUnit_Framework_TestCase
{ {
/** public function tearDown()
* Test construct {
*/ TestHelperDOCX::clear();
}
public function testConstruct() public function testConstruct()
{ {
$object = new PHPWord_Writer_Word2007(new PHPWord()); $object = new PHPWord_Writer_Word2007(new PHPWord());
@ -35,9 +38,6 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
} }
} }
/**
* Test save()
*/
public function testSave() public function testSave()
{ {
$phpWord = new PHPWord(); $phpWord = new PHPWord();
@ -60,4 +60,29 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
$this->assertTrue(file_exists($file)); $this->assertTrue(file_exists($file));
unlink($file); unlink($file);
} }
/**
* @covers PHPWord_Writer_Word2007::checkContentTypes
*/
public function testCheckContentTypes()
{
$phpWord = new PHPWord();
$section = $phpWord->createSection();
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg");
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif");
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png");
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp");
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif");
$doc = TestHelperDOCX::getDocument($phpWord);
$mediaPath = $doc->getPath() . "/word/media";
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg", $mediaPath . "/section_image1.jpg");
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg", $mediaPath . "/section_image2.jpg");
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif", $mediaPath . "/section_image3.gif");
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png", $mediaPath . "/section_image4.png");
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp", $mediaPath . "/section_image5.bmp");
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif", $mediaPath . "/section_image6.tif");
}
} }

View File

@ -63,4 +63,20 @@ class XmlDocument
$elements = $this->xpath->query($path); $elements = $this->xpath->query($path);
return $elements->item(0); return $elements->item(0);
} }
/**
* @return string
*/
public function getFile()
{
return $this->file;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
} }