Enhance unit tests

This commit is contained in:
Ivan Lanin 2014-04-06 15:19:09 +07:00
parent 8d2c6ebd16
commit a218202dbd
9 changed files with 65 additions and 7 deletions

View File

@ -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)

View File

@ -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);
}

View File

@ -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)
{

View File

@ -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
*/

View File

@ -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)

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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');
}
}

View File

@ -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');
}
}