Skip to content

Commit d991fdc

Browse files
committed
Added image writer tests
1 parent 68de9ab commit d991fdc

File tree

4 files changed

+84
-25
lines changed

4 files changed

+84
-25
lines changed

Classes/PHPWord/Writer/Word2007.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* @version 0.7.0
2626
*/
2727

28+
use PhpOffice\PhpWord\Exceptions\InvalidImageException;
29+
use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
30+
2831
/**
2932
* Class PHPWord_Writer_Word2007
3033
*/
@@ -191,25 +194,40 @@ public function save($pFilename = null)
191194
}
192195
}
193196

194-
private function _chkContentTypes($src)
197+
/**
198+
* @param string $src
199+
*/
200+
private function checkContentTypes($src)
195201
{
196-
$srcInfo = pathinfo($src);
197-
$extension = strtolower($srcInfo['extension']);
198-
if (substr($extension, 0, 3) == 'php') {
202+
$supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);
203+
204+
$extension = null;
205+
if (stripos(strrev($src), strrev('.php')) === 0) {
199206
$extension = 'php';
207+
} else {
208+
$imageType = exif_imagetype($src);
209+
if ($imageType === IMAGETYPE_JPEG) {
210+
$extension = 'jpg';
211+
} elseif ($imageType === IMAGETYPE_GIF) {
212+
$extension = 'gif';
213+
} elseif ($imageType === IMAGETYPE_PNG) {
214+
$extension = 'png';
215+
} elseif ($imageType === IMAGETYPE_BMP) {
216+
$extension = 'bmp';
217+
} elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) {
218+
$extension = 'tif';
219+
}
200220
}
201-
$_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff', 'php');
202-
203-
if (in_array($extension, $_supportedImageTypes)) {
204-
$imagedata = getimagesize($src);
205-
$imagetype = image_type_to_mime_type($imagedata[2]);
206-
$imageext = image_type_to_extension($imagedata[2]);
207-
$imageext = str_replace('.', '', $imageext);
208-
if ($imageext == 'jpeg') {
209-
$imageext = 'jpg';
221+
222+
if (in_array($extension, $supportedImageTypes)) {
223+
$imageData = getimagesize($src);
224+
$imageType = image_type_to_mime_type($imageData[2]);
225+
$imageExtension = str_replace('.', '', image_type_to_extension($imageData[2]));
226+
if ($imageExtension === 'jpeg') {
227+
$imageExtension = 'jpg';
210228
}
211-
if (!in_array($imagetype, $this->_imageTypes)) {
212-
$this->_imageTypes[$imageext] = $imagetype;
229+
if (!in_array($imageType, $this->_imageTypes)) {
230+
$this->_imageTypes[$imageExtension] = $imageType;
213231
}
214232
} else {
215233
if (!in_array($extension, $this->_objectTypes)) {
@@ -258,10 +276,10 @@ private function _addFileToPackage($objZip, $element)
258276
$objZip->addFromString('word/' . $element['target'], $imageContents);
259277
imagedestroy($image);
260278

261-
$this->_chkContentTypes($element['source']);
279+
$this->checkContentTypes($element['source']);
262280
} else {
263281
$objZip->addFile($element['source'], 'word/' . $element['target']);
264-
$this->_chkContentTypes($element['source']);
282+
$this->checkContentTypes($element['source']);
265283
}
266284
}
267285
}

Tests/PHPWord/Writer/Word2007Test.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use PHPUnit_Framework_TestCase;
55
use PHPWord_Writer_Word2007;
66
use PHPWord;
7+
use PHPWord\Tests\TestHelperDOCX;
78

89
/**
910
* Class Word2007Test
@@ -13,9 +14,11 @@
1314
*/
1415
class Word2007Test extends \PHPUnit_Framework_TestCase
1516
{
16-
/**
17-
* Test construct
18-
*/
17+
public function tearDown()
18+
{
19+
TestHelperDOCX::clear();
20+
}
21+
1922
public function testConstruct()
2023
{
2124
$object = new PHPWord_Writer_Word2007(new PHPWord());
@@ -35,9 +38,6 @@ public function testConstruct()
3538
}
3639
}
3740

38-
/**
39-
* Test save()
40-
*/
4141
public function testSave()
4242
{
4343
$phpWord = new PHPWord();
@@ -60,4 +60,29 @@ public function testSave()
6060
$this->assertTrue(file_exists($file));
6161
unlink($file);
6262
}
63-
}
63+
64+
/**
65+
* @covers PHPWord_Writer_Word2007::checkContentTypes
66+
*/
67+
public function testCheckContentTypes()
68+
{
69+
$phpWord = new PHPWord();
70+
$section = $phpWord->createSection();
71+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
72+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg");
73+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif");
74+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png");
75+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp");
76+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif");
77+
78+
$doc = TestHelperDOCX::getDocument($phpWord);
79+
$mediaPath = $doc->getPath() . "/word/media";
80+
81+
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg", $mediaPath . "/section_image1.jpg");
82+
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg", $mediaPath . "/section_image2.jpg");
83+
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif", $mediaPath . "/section_image3.gif");
84+
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png", $mediaPath . "/section_image4.png");
85+
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp", $mediaPath . "/section_image5.bmp");
86+
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif", $mediaPath . "/section_image6.tif");
87+
}
88+
}

Tests/_inc/TestHelperDOCX.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ public static function deleteDir($dir)
5959

6060
rmdir($dir);
6161
}
62-
}
62+
}

Tests/_inc/XmlDocument.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,20 @@ public function getElement($path, $file = 'word/document.xml')
6363
$elements = $this->xpath->query($path);
6464
return $elements->item(0);
6565
}
66+
67+
/**
68+
* @return string
69+
*/
70+
public function getFile()
71+
{
72+
return $this->file;
73+
}
74+
75+
/**
76+
* @return string
77+
*/
78+
public function getPath()
79+
{
80+
return $this->path;
81+
}
6682
}

0 commit comments

Comments
 (0)