From a218202dbdbe1d9f5d3da2d43598d4efff71194a Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 6 Apr 2014 15:19:09 +0700 Subject: [PATCH] Enhance unit tests --- src/PhpWord/Container/Section.php | 2 ++ src/PhpWord/Element/Image.php | 13 +++++++--- src/PhpWord/TOC.php | 2 ++ src/PhpWord/Writer/Word2007.php | 3 ++- src/PhpWord/Writer/Word2007/ContentTypes.php | 3 +-- tests/PhpWord/Tests/Container/FooterTest.php | 2 ++ tests/PhpWord/Tests/Container/HeaderTest.php | 12 +++++++++ tests/PhpWord/Tests/Container/SectionTest.php | 25 +++++++++++++++++++ tests/PhpWord/Tests/Element/ImageTest.php | 10 ++++++++ 9 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index 01e7fafe..26823df4 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -97,6 +97,8 @@ class Section extends Container * * @param mixed $styleFont * @param mixed $styleTOC + * @param integer $minDepth + * @param integer $maxDepth * @return TOC */ public function addTOC($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9) diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index ec82665e..fec8791c 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -102,10 +102,13 @@ class Image extends Element // Check supported types if ($this->isMemImage) { $supportedTypes = array('image/jpeg', 'image/gif', 'image/png'); - $imgData = getimagesize($source); + $imgData = @getimagesize($source); + if (!is_array($imgData)) { + throw new InvalidImageException(); + } $this->imageType = $imgData['mime']; // string if (!in_array($this->imageType, $supportedTypes)) { - throw new UnsupportedImageTypeException; + throw new UnsupportedImageTypeException(); } } else { $supportedTypes = array( @@ -114,17 +117,19 @@ class Image extends Element \IMAGETYPE_TIFF_II, \IMAGETYPE_TIFF_MM ); if (!file_exists($source)) { - throw new InvalidImageException; + throw new InvalidImageException(); } $imgData = getimagesize($source); if (function_exists('exif_imagetype')) { $this->imageType = exif_imagetype($source); } else { + // @codeCoverageIgnoreStart $tmp = getimagesize($source); $this->imageType = $tmp[2]; + // @codeCoverageIgnoreEnd } if (!in_array($this->imageType, $supportedTypes)) { - throw new UnsupportedImageTypeException; + throw new UnsupportedImageTypeException(); } $this->imageType = \image_type_to_mime_type($this->imageType); } diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index a992cd55..2793d2f0 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -73,6 +73,8 @@ class TOC * * @param mixed $styleFont * @param array $styleTOC + * @param integer $minDepth + * @param integer $maxDepth */ public function __construct($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9) { diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index f7b49cfe..8fe90339 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -229,7 +229,8 @@ class Word2007 extends Writer implements IWriter /** * Add header/footer content * - * @param PhpOffice\PhpWord\Container\Section $section + * @param \PhpOffice\PhpWord\Container\Section $section + * @param mixed $objZip * @param string $elmType * @param integer $rID */ diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 7b371b07..8eb10bc1 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -60,9 +60,8 @@ class ContentTypes extends WriterPart * Write content types element * * @param XMLWriter $xmlWriter XML Writer + * @param array $parts * @param boolean $isDefault - * @param string $partName Part name - * @param string $contentType Content type * @throws Exception */ private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault) diff --git a/tests/PhpWord/Tests/Container/FooterTest.php b/tests/PhpWord/Tests/Container/FooterTest.php index bcbf45a9..db982dc5 100644 --- a/tests/PhpWord/Tests/Container/FooterTest.php +++ b/tests/PhpWord/Tests/Container/FooterTest.php @@ -162,6 +162,8 @@ class FooterTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oFooter->setRelationId($iVal); + $this->assertEquals($oFooter->getRelationId(), $iVal); + $this->assertEquals(Footer::AUTO, $oFooter->getType()); } } diff --git a/tests/PhpWord/Tests/Container/HeaderTest.php b/tests/PhpWord/Tests/Container/HeaderTest.php index 9d13d57c..ef1703f3 100644 --- a/tests/PhpWord/Tests/Container/HeaderTest.php +++ b/tests/PhpWord/Tests/Container/HeaderTest.php @@ -231,4 +231,16 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $header = new Header(1); $header->addFootnote(); } + + /** + * Set/get type + */ + public function testSetGetType() + { + $object = new Header(1); + $this->assertEquals(Header::AUTO, $object->getType()); + + $object->setType('ODD'); + $this->assertEquals(Header::AUTO, $object->getType()); + } } diff --git a/tests/PhpWord/Tests/Container/SectionTest.php b/tests/PhpWord/Tests/Container/SectionTest.php index 49b59b20..e077ff82 100644 --- a/tests/PhpWord/Tests/Container/SectionTest.php +++ b/tests/PhpWord/Tests/Container/SectionTest.php @@ -9,7 +9,9 @@ namespace PhpOffice\PhpWord\Tests\Container; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Container\Header; use PhpOffice\PhpWord\Style; /** @@ -141,4 +143,27 @@ class SectionTest extends \PHPUnit_Framework_TestCase } $this->assertFalse($object->hasDifferentFirstPage()); } + + /** + * Add header has different first page + */ + public function testHasDifferentFirstPage() + { + $object = new Section(1); + $header = $object->addHeader(); + $header->setType(Header::FIRST); + $this->assertTrue($object->hasDifferentFirstPage()); + } + + /** + * Add header exception + * + * @expectedException Exception + * @expectedExceptionMesssage Invalid header/footer type. + */ + public function testAddHeaderException() + { + $object = new Section(1); + $header = $object->addHeader('ODD'); + } } diff --git a/tests/PhpWord/Tests/Element/ImageTest.php b/tests/PhpWord/Tests/Element/ImageTest.php index b0c3d88c..5b9aaa45 100644 --- a/tests/PhpWord/Tests/Element/ImageTest.php +++ b/tests/PhpWord/Tests/Element/ImageTest.php @@ -192,4 +192,14 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageFunction(), null); $this->assertEquals($oImage->getImageType(), 'image/tiff'); } + + /** + * Test PHP Image + * + * @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException + */ + public function testPhpImage() + { + $object = new Image('test.php'); + } }