Unit tests enhancement

This commit is contained in:
Ivan Lanin 2014-03-28 13:50:53 +07:00
parent 2d178a6724
commit 7a424318d2
27 changed files with 379 additions and 187 deletions

View File

@ -436,13 +436,14 @@ class DocumentProperties
*/ */
public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null) public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null)
{ {
if (($propertyType === null) || (!in_array($propertyType, array( $propertyTypes = array(
self::PROPERTY_TYPE_INTEGER, self::PROPERTY_TYPE_INTEGER,
self::PROPERTY_TYPE_FLOAT, self::PROPERTY_TYPE_FLOAT,
self::PROPERTY_TYPE_STRING, self::PROPERTY_TYPE_STRING,
self::PROPERTY_TYPE_DATE, self::PROPERTY_TYPE_DATE,
self::PROPERTY_TYPE_BOOLEAN self::PROPERTY_TYPE_BOOLEAN
)))) { );
if (($propertyType === null) || (!in_array($propertyType, $propertyTypes))) {
if ($propertyValue === null) { if ($propertyValue === null) {
$propertyType = self::PROPERTY_TYPE_STRING; $propertyType = self::PROPERTY_TYPE_STRING;
} elseif (is_float($propertyValue)) { } elseif (is_float($propertyValue)) {

View File

@ -31,10 +31,7 @@ class Footnote
/** /**
* Add new Footnote Element * Add new Footnote Element
* *
* @param string $linkSrc * @return int Reference ID
* @param string $linkName
*
* @return mixed
*/ */
public static function addFootnoteElement(\PhpOffice\PhpWord\Section\Footnote $footnote) public static function addFootnoteElement(\PhpOffice\PhpWord\Section\Footnote $footnote)
{ {
@ -70,7 +67,7 @@ class Footnote
* *
* @param string $linkSrc * @param string $linkSrc
* *
* @return mixed * @return int Reference ID
*/ */
public static function addFootnoteLinkElement($linkSrc) public static function addFootnoteLinkElement($linkSrc)
{ {

View File

@ -69,11 +69,10 @@ class Media
$file = null; $file = null;
if ($type === 'image') { if ($type === 'image') {
$cImg++; $cImg++;
$isMemImage = false;
if (!is_null($image)) { if (!is_null($image)) {
$isMemImage = $image->getIsMemImage(); $isMemImage = $image->getIsMemImage();
$extension = $image->getImageExtension(); $extension = $image->getImageExtension();
} else {
$isMemImage = false;
} }
if ($isMemImage) { if ($isMemImage) {
$media['isMemImage'] = true; $media['isMemImage'] = true;
@ -181,11 +180,10 @@ class Media
$rID = $cImg + 1; $rID = $cImg + 1;
$cImg++; $cImg++;
$media = array(); $media = array();
$isMemImage = false;
if (!is_null($image)) { if (!is_null($image)) {
$isMemImage = $image->getIsMemImage(); $isMemImage = $image->getIsMemImage();
$extension = $image->getImageExtension(); $extension = $image->getImageExtension();
} else {
$isMemImage = false;
} }
if ($isMemImage) { if ($isMemImage) {
$media['isMemImage'] = true; $media['isMemImage'] = true;
@ -244,11 +242,10 @@ class Media
$cImg = self::countFooterMediaElements($key); $cImg = self::countFooterMediaElements($key);
$rID = $cImg + 1; $rID = $cImg + 1;
$cImg++; $cImg++;
$isMemImage = false;
if (!is_null($image)) { if (!is_null($image)) {
$isMemImage = $image->getIsMemImage(); $isMemImage = $image->getIsMemImage();
$extension = $image->getImageExtension(); $extension = $image->getImageExtension();
} else {
$isMemImage = false;
} }
if ($isMemImage) { if ($isMemImage) {
$media['isMemImage'] = true; $media['isMemImage'] = true;

View File

@ -9,7 +9,6 @@
namespace PhpOffice\PhpWord; namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Exceptions\InvalidImageException;
use PhpOffice\PhpWord\Exceptions\InvalidObjectException; use PhpOffice\PhpWord\Exceptions\InvalidObjectException;
use PhpOffice\PhpWord\Section\Footer; use PhpOffice\PhpWord\Section\Footer;
use PhpOffice\PhpWord\Section\Image; use PhpOffice\PhpWord\Section\Image;
@ -211,72 +210,61 @@ class Section
/** /**
* Add a OLE-Object Element * Add a OLE-Object Element
* *
* All exceptions should be handled by PhpOffice\PhpWord\Section\Object
*
* @param string $src * @param string $src
* @param mixed $style * @param mixed $style
* @return \PhpOffice\PhpWord\Section\Object * @return \PhpOffice\PhpWord\Section\Object
* @throws \PhpOffice\PhpWord\Exceptions\InvalidObjectException
*/ */
public function addObject($src, $style = null) public function addObject($src, $style = null)
{ {
$object = new Object($src, $style); $object = new Object($src, $style);
if (!is_null($object->getSource())) { if (!is_null($object->getSource())) {
$inf = pathinfo($src); $inf = pathinfo($src);
$ext = $inf['extension']; $ext = $inf['extension'];
if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
$ext = substr($ext, 0, -1); $ext = substr($ext, 0, -1);
} }
$icon = __DIR__ . "/_staticDocParts/_{$ext}.png";
$iconSrc = __DIR__ . '/_staticDocParts/'; $rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon));
if (!\file_exists($iconSrc . '_' . $ext . '.png')) {
$iconSrc = $iconSrc . '_default.png';
} else {
$iconSrc .= '_' . $ext . '.png';
}
$rIDimg = Media::addSectionMediaElement($iconSrc, 'image', new Image($iconSrc));
$data = Media::addSectionMediaElement($src, 'oleObject'); $data = Media::addSectionMediaElement($src, 'oleObject');
$rID = $data[0]; $rID = $data[0];
$objectId = $data[1]; $objectId = $data[1];
$object->setRelationId($rID); $object->setRelationId($rID);
$object->setObjectId($objectId); $object->setObjectId($objectId);
$object->setImageRelationId($rIDimg); $object->setImageRelationId($rIDimg);
$this->_elementCollection[] = $object; $this->_elementCollection[] = $object;
return $object; return $object;
} else { } else {
throw new InvalidObjectException; throw new InvalidObjectException();
} }
} }
/** /**
* Add a Image Element * Add image element
*
* All exceptions should be handled by PhpOffice\PhpWord\Section\Image
* *
* @param string $src * @param string $src
* @param mixed $style * @param mixed $style
* @return \PhpOffice\PhpWord\Section\Image * @return \PhpOffice\PhpWord\Section\Image
* @throws \PhpOffice\PhpWord\Exceptions\InvalidImageException
*/ */
public function addImage($src, $style = null) public function addImage($src, $style = null)
{ {
$image = new Image($src, $style); $image = new Image($src, $style);
if (!is_null($image->getSource())) { $rID = Media::addSectionMediaElement($src, 'image', $image);
$rID = Media::addSectionMediaElement($src, 'image', $image); $image->setRelationId($rID);
$image->setRelationId($rID); $this->_elementCollection[] = $image;
$this->_elementCollection[] = $image; return $image;
return $image;
} else {
throw new InvalidImageException;
}
} }
/** /**
* Add a by PHP created Image Element * Add memory image element
* *
* @param string $link
* @param mixed $style
* @deprecated * @deprecated
*
* @param string $src
* @param mixed $style
*/ */
public function addMemoryImage($src, $style = null) public function addMemoryImage($src, $style = null)
{ {

View File

@ -131,7 +131,7 @@ class Footer
/** /**
* Add a by PHP created Image Element * Add a by PHP created Image Element
* *
* @param string $link * @param string $src
* @param mixed $style * @param mixed $style
* @deprecated * @deprecated
*/ */

View File

@ -160,7 +160,7 @@ class Header
/** /**
* Add a by PHP created Image Element * Add a by PHP created Image Element
* *
* @param string $link * @param string $src
* @param mixed $style * @param mixed $style
* @deprecated * @deprecated
*/ */

View File

@ -58,7 +58,7 @@ class Object
*/ */
public function __construct($src, $style = null) public function __construct($src, $style = null)
{ {
$_supportedObjectTypes = array('xls', 'doc', 'ppt'); $_supportedObjectTypes = array('xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx');
$inf = pathinfo($src); $inf = pathinfo($src);
if (\file_exists($src) && in_array($inf['extension'], $_supportedObjectTypes)) { if (\file_exists($src) && in_array($inf['extension'], $_supportedObjectTypes)) {

View File

@ -205,7 +205,7 @@ class Cell
/** /**
* Add a by PHP created Image Element * Add a by PHP created Image Element
* *
* @param string $link * @param string $src
* @param mixed $style * @param mixed $style
* @deprecated * @deprecated
*/ */

View File

@ -11,9 +11,11 @@ namespace PhpOffice\PhpWord\Shared;
use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Settings;
// @codeCoverageIgnoreStart
if (!defined('DATE_W3C')) { if (!defined('DATE_W3C')) {
define('DATE_W3C', 'Y-m-d\TH:i:sP'); define('DATE_W3C', 'Y-m-d\TH:i:sP');
} }
// @codeCoverageIgnoreEnd
/** /**
* XMLWriter wrapper * XMLWriter wrapper

View File

@ -208,16 +208,6 @@ class Table
return $this->_firstRow; return $this->_firstRow;
} }
/**
* Get Last Row Style
*
* @return \PhpOffice\PhpWord\Style\Table
*/
public function getLastRow()
{
return $this->_lastRow;
}
/** /**
* Get background * Get background
* *

View File

@ -17,37 +17,37 @@ use PhpOffice\PhpWord\Style\Font;
class TOC class TOC
{ {
/** /**
* Title Elements * Title elements
* *
* @var array * @var array
*/ */
private static $_titles = array(); private static $_titles = array();
/** /**
* TOC Style * TOC style
* *
* @var array * @var PhpOffice\PhpWord\Style\TOC
*/ */
private static $_styleTOC; private static $_styleTOC;
/** /**
* Font Style * Font style
* *
* @var array * @var PhpOffice\PhpWord\Style\Font|array|string
*/ */
private static $_styleFont; private static $_styleFont;
/** /**
* Title Anchor * Title anchor
* *
* @var array * @var int
*/ */
private static $_anchor = 252634154; private static $_anchor = 252634154;
/** /**
* Title Bookmark * Title bookmark
* *
* @var array * @var int
*/ */
private static $_bookmarkId = 0; private static $_bookmarkId = 0;
@ -55,7 +55,7 @@ class TOC
/** /**
* Create a new Table-of-Contents Element * Create a new Table-of-Contents Element
* *
* @param array $styleFont * @param mixed $styleFont
* @param array $styleTOC * @param array $styleTOC
*/ */
public function __construct($styleFont = null, $styleTOC = null) public function __construct($styleFont = null, $styleTOC = null)
@ -74,7 +74,6 @@ class TOC
if (!is_null($styleFont)) { if (!is_null($styleFont)) {
if (is_array($styleFont)) { if (is_array($styleFont)) {
self::$_styleFont = new Font(); self::$_styleFont = new Font();
foreach ($styleFont as $key => $value) { foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;

View File

@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Autoloader;
/** /**
* Test class for PhpOffice\PhpWord\Autoloader * Test class for PhpOffice\PhpWord\Autoloader
* *
* @coversDefaultClass \PhpOffice\PhpWord\Autoloader
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class AutoloaderTest extends \PHPUnit_Framework_TestCase class AutoloaderTest extends \PHPUnit_Framework_TestCase
{ {
/**
* Register
*/
public function testRegister() public function testRegister()
{ {
Autoloader::register(); Autoloader::register();
@ -28,6 +30,9 @@ class AutoloaderTest extends \PHPUnit_Framework_TestCase
); );
} }
/**
* Autoload
*/
public function testAutoload() public function testAutoload()
{ {
$declared = \get_declared_classes(); $declared = \get_declared_classes();

View File

@ -14,11 +14,13 @@ use PhpOffice\PhpWord\DocumentProperties;
/** /**
* Test class for PhpOffice\PhpWord\DocumentProperties * Test class for PhpOffice\PhpWord\DocumentProperties
* *
* @coversDefaultClass \PhpOffice\PhpWord\DocumentProperties
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
{ {
/**
* Creator
*/
public function testCreator() public function testCreator()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -29,6 +31,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getCreator()); $this->assertEquals('AAA', $oProperties->getCreator());
} }
/**
* Last modified by
*/
public function testLastModifiedBy() public function testLastModifiedBy()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -39,6 +44,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getLastModifiedBy()); $this->assertEquals('AAA', $oProperties->getLastModifiedBy());
} }
/**
* Created
*/
public function testCreated() public function testCreated()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -50,6 +58,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($iTime, $oProperties->getCreated()); $this->assertEquals($iTime, $oProperties->getCreated());
} }
/**
* Modified
*/
public function testModified() public function testModified()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -61,6 +72,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($iTime, $oProperties->getModified()); $this->assertEquals($iTime, $oProperties->getModified());
} }
/**
* Title
*/
public function testTitle() public function testTitle()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -71,6 +85,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getTitle()); $this->assertEquals('AAA', $oProperties->getTitle());
} }
/**
* Description
*/
public function testDescription() public function testDescription()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -81,6 +98,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getDescription()); $this->assertEquals('AAA', $oProperties->getDescription());
} }
/**
* Subject
*/
public function testSubject() public function testSubject()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -91,6 +111,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getSubject()); $this->assertEquals('AAA', $oProperties->getSubject());
} }
/**
* Keywords
*/
public function testKeywords() public function testKeywords()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -101,6 +124,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getKeywords()); $this->assertEquals('AAA', $oProperties->getKeywords());
} }
/**
* Category
*/
public function testCategory() public function testCategory()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -111,6 +137,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getCategory()); $this->assertEquals('AAA', $oProperties->getCategory());
} }
/**
* Company
*/
public function testCompany() public function testCompany()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -121,6 +150,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getCompany()); $this->assertEquals('AAA', $oProperties->getCompany());
} }
/**
* Manager
*/
public function testManager() public function testManager()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -131,6 +163,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('AAA', $oProperties->getManager()); $this->assertEquals('AAA', $oProperties->getManager());
} }
/**
* Custom properties
*/
public function testCustomProperty() public function testCustomProperty()
{ {
$oProperties = new DocumentProperties(); $oProperties = new DocumentProperties();
@ -177,6 +212,9 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
), $oProperties->getCustomProperties()); ), $oProperties->getCustomProperties());
} }
/**
* Convert property
*/
public function testConvertProperty() public function testConvertProperty()
{ {
$this->assertEquals('', DocumentProperties::convertProperty('a', 'empty')); $this->assertEquals('', DocumentProperties::convertProperty('a', 'empty'));

View File

@ -0,0 +1,35 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Tests;
use PhpOffice\PhpWord\Footnote;
/**
* Test class for PhpOffice\PhpWord\Footnote
*
* @runTestsInSeparateProcesses
*/
class FootnoteTest extends \PHPUnit_Framework_TestCase
{
/**
* Test add, get, and count footnote elements and links
*/
public function testFootnote()
{
$footnoteElement = new \PhpOffice\PhpWord\Section\Footnote();
$rIdFootnote = Footnote::addFootnoteElement($footnoteElement);
$rIdLink = Footnote::addFootnoteLinkElement('http://test.com');
$this->assertEquals(2, $rIdFootnote);
$this->assertEquals(1, $rIdLink);
$this->assertEquals(1, count(Footnote::getFootnoteElements()));
$this->assertEquals(1, count(Footnote::getFootnoteLinkElements()));
}
}

View File

@ -15,15 +15,14 @@ use PhpOffice\PhpWord\IOFactory;
/** /**
* Test class for PhpOffice\PhpWord\IOFactory * Test class for PhpOffice\PhpWord\IOFactory
* *
* @coversDefaultClass \PhpOffice\PhpWord\IOFactory
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
final class IOFactoryTest extends \PHPUnit_Framework_TestCase class IOFactoryTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @covers ::createWriter * Create existing writer
*/ */
final public function testExistingWriterCanBeCreated() public function testExistingWriterCanBeCreated()
{ {
$this->assertInstanceOf( $this->assertInstanceOf(
'PhpOffice\\PhpWord\\Writer\\Word2007', 'PhpOffice\\PhpWord\\Writer\\Word2007',
@ -32,18 +31,19 @@ final class IOFactoryTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::createWriter * Create non-existing writer
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception *
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
*/ */
final public function testNonexistentWriterCanNotBeCreated() public function testNonexistentWriterCanNotBeCreated()
{ {
IOFactory::createWriter(new PhpWord(), 'Word2006'); IOFactory::createWriter(new PhpWord(), 'Word2006');
} }
/** /**
* @covers ::createReader * Create existing reader
*/ */
final public function testExistingReaderCanBeCreated() public function testExistingReaderCanBeCreated()
{ {
$this->assertInstanceOf( $this->assertInstanceOf(
'PhpOffice\\PhpWord\\Reader\\Word2007', 'PhpOffice\\PhpWord\\Reader\\Word2007',
@ -52,11 +52,24 @@ final class IOFactoryTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::createReader * Create non-existing reader
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception *
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
*/ */
final public function testNonexistentReaderCanNotBeCreated() public function testNonexistentReaderCanNotBeCreated()
{ {
IOFactory::createReader('Word2006'); IOFactory::createReader('Word2006');
} }
/**
* Load document
*/
public function testLoad()
{
$file = __DIR__ . "/_files/templates/blank.docx";
$this->assertInstanceOf(
'PhpOffice\\PhpWord\\PhpWord',
IOFactory::load($file)
);
}
} }

View File

@ -11,25 +11,34 @@ namespace PhpOffice\PhpWord\Tests;
use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Section; use PhpOffice\PhpWord\Section;
use PhpOffice\PhpWord\Section\Image;
/** /**
* Test class for PhpOffice\PhpWord\Media * Test class for PhpOffice\PhpWord\Media
* *
* @coversDefaultClass \PhpOffice\PhpWord\Media
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class MediaTest extends \PHPUnit_Framework_TestCase class MediaTest extends \PHPUnit_Framework_TestCase
{ {
/**
* Get section media elements
*/
public function testGetSectionMediaElementsWithNull() public function testGetSectionMediaElementsWithNull()
{ {
$this->assertEquals(Media::getSectionMediaElements(), array()); $this->assertEquals(Media::getSectionMediaElements(), array());
} }
/**
* Count section media elements
*/
public function testCountSectionMediaElementsWithNull() public function testCountSectionMediaElementsWithNull()
{ {
$this->assertEquals(Media::countSectionMediaElements(), 0); $this->assertEquals(Media::countSectionMediaElements(), 0);
} }
/**
* Get header media elements
*/
public function testGetHeaderMediaElements() public function testGetHeaderMediaElements()
{ {
$this->assertAttributeEquals( $this->assertAttributeEquals(
@ -39,6 +48,9 @@ class MediaTest extends \PHPUnit_Framework_TestCase
); );
} }
/**
* Get footer media elements
*/
public function testGetFooterMediaElements() public function testGetFooterMediaElements()
{ {
$this->assertAttributeEquals( $this->assertAttributeEquals(
@ -49,24 +61,60 @@ class MediaTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Todo: add memory image to this test * Add section media element
*
* @covers \PhpOffice\PhpWord\Media::addSectionMediaElement
*/ */
public function testAddSectionMediaElement() public function testAddSectionMediaElement()
{ {
$section = new Section(0); $local = __DIR__ . "/_files/images/mars.jpg";
$section->addImage(__DIR__ . "/_files/images/mars_noext_jpg"); $object = __DIR__ . "/_files/documents/sheet.xls";
$section->addImage(__DIR__ . "/_files/images/mars.jpg"); $remote = 'http://php.net/images/logos/php-med-trans-light.gif';
$section->addImage(__DIR__ . "/_files/images/mario.gif"); Media::addSectionMediaElement($local, 'image');
$section->addImage(__DIR__ . "/_files/images/firefox.png"); Media::addSectionMediaElement($local, 'image');
$section->addImage(__DIR__ . "/_files/images/duke_nukem.bmp"); Media::addSectionMediaElement($remote, 'image', new Image($remote));
$section->addImage(__DIR__ . "/_files/images/angela_merkel.tif"); Media::addSectionMediaElement($object, 'oleObject');
Media::addSectionMediaElement($object, 'oleObject');
$elements = $section->getElements(); $this->assertEquals(3, Media::countSectionMediaElements());
$this->assertEquals(6, count($elements)); }
foreach ($elements as $element) {
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); /**
} * Add section link
*/
public function testAddSectionLinkElement()
{
$expected = Media::countSectionMediaElements() + 7;
$actual = Media::addSectionLinkElement('http://test.com');
$this->assertEquals($expected, $actual);
$this->assertEquals(1, Media::countSectionMediaElements('links'));
$this->assertEquals(1, count(Media::getSectionMediaElements('links')));
}
/**
* Add header media element
*/
public function testAddHeaderMediaElement()
{
$local = __DIR__ . "/_files/images/mars.jpg";
$remote = 'http://php.net/images/logos/php-med-trans-light.gif';
Media::addHeaderMediaElement(1, $local);
Media::addHeaderMediaElement(1, $local);
Media::addHeaderMediaElement(1, $remote, new Image($remote));
$this->assertEquals(2, Media::countHeaderMediaElements('header1'));
}
/**
* Add footer media element
*/
public function testAddFooterMediaElement()
{
$local = __DIR__ . "/_files/images/mars.jpg";
$remote = 'http://php.net/images/logos/php-med-trans-light.gif';
Media::addFooterMediaElement(1, $local);
Media::addFooterMediaElement(1, $local);
Media::addFooterMediaElement(1, $remote, new Image($remote));
$this->assertEquals(2, Media::countFooterMediaElements('footer1'));
} }
} }

View File

@ -17,17 +17,12 @@ use PhpOffice\PhpWord\Style;
/** /**
* Test class for PhpOffice\PhpWord\PhpWord * Test class for PhpOffice\PhpWord\PhpWord
* *
* @coversDefaultClass \PhpOffice\PhpWord\PhpWord
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class PhpWordTest extends \PHPUnit_Framework_TestCase class PhpWordTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* Test object creation * Test object creation
*
* @covers ::getDocumentProperties
* @covers ::getDefaultFontName
* @covers ::getDefaultFontSize
*/ */
public function testConstruct() public function testConstruct()
{ {
@ -39,9 +34,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test set/get document properties * Test set/get document properties
*
* @covers ::setDocumentProperties
* @covers ::getDocumentProperties
*/ */
public function testSetGetDocumentProperties() public function testSetGetDocumentProperties()
{ {
@ -55,9 +47,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test create/get section * Test create/get section
*
* @covers ::createSection
* @covers ::getSections
*/ */
public function testCreateGetSections() public function testCreateGetSections()
{ {
@ -69,9 +58,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test set/get default font name * Test set/get default font name
*
* @covers ::setDefaultFontName
* @covers ::getDefaultFontName
*/ */
public function testSetGetDefaultFontName() public function testSetGetDefaultFontName()
{ {
@ -84,9 +70,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test set/get default font size * Test set/get default font size
*
* @covers ::setDefaultFontSize
* @covers ::getDefaultFontSize
*/ */
public function testSetGetDefaultFontSize() public function testSetGetDefaultFontSize()
{ {
@ -99,9 +82,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test set default paragraph style * Test set default paragraph style
*
* @covers ::setDefaultParagraphStyle
* @covers ::loadTemplate
*/ */
public function testSetDefaultParagraphStyle() public function testSetDefaultParagraphStyle()
{ {
@ -112,11 +92,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test add styles * Test add styles
*
* @covers ::addParagraphStyle
* @covers ::addFontStyle
* @covers ::addTableStyle
* @covers ::addLinkStyle
*/ */
public function testAddStyles() public function testAddStyles()
{ {
@ -138,8 +113,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test add title style * Test add title style
*
* @covers ::addTitleStyle
*/ */
public function testAddTitleStyle() public function testAddTitleStyle()
{ {
@ -152,8 +125,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test load template * Test load template
*
* @covers ::loadTemplate
*/ */
public function testLoadTemplate() public function testLoadTemplate()
{ {
@ -169,7 +140,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
/** /**
* Test load template exception * Test load template exception
* *
* @covers ::loadTemplate
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception * @expectedException \PhpOffice\PhpWord\Exceptions\Exception
*/ */
public function testLoadTemplateException() public function testLoadTemplateException()

View File

@ -10,17 +10,17 @@
namespace PhpOffice\PhpWord\Tests; namespace PhpOffice\PhpWord\Tests;
use PhpOffice\PhpWord\Section; use PhpOffice\PhpWord\Section;
use PhpOffice\PhpWord\Style;
/** /**
* Test class for PhpOffice\PhpWord\Section * Test class for PhpOffice\PhpWord\Section
* *
* @coversDefaultClass \PhpOffice\PhpWord\Section
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class SectionTest extends \PHPUnit_Framework_TestCase class SectionTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @covers ::getSettings * Get settings
*/ */
public function testGetSettings() public function testGetSettings()
{ {
@ -29,7 +29,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::getElements * Get elements
*/ */
public function testGetElements() public function testGetElements()
{ {
@ -38,7 +38,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::getFooter * Get footer
*/ */
public function testGetFooter() public function testGetFooter()
{ {
@ -47,7 +47,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::getHeaders * Get headers
*/ */
public function testGetHeaders() public function testGetHeaders()
{ {
@ -56,7 +56,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::setSettings * Set settings
*/ */
public function testSetSettings() public function testSetSettings()
{ {
@ -67,22 +67,11 @@ class SectionTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::addText * Add elements
* @covers ::addLink
* @covers ::addTextBreak
* @covers ::addPageBreak
* @covers ::addTable
* @covers ::addListItem
* @covers ::addObject
* @covers ::addImage
* @covers ::addTOC
* @covers ::addTitle
* @covers ::createTextRun
* @covers ::createFootnote
*/ */
public function testAddElements() public function testAddElements()
{ {
$objectSource = __DIR__ . "/_files/documents/sheet.xls"; $objectSource = __DIR__ . "/_files/documents/reader.docx";
$imageSource = __DIR__ . "/_files/images/PhpWord.png"; $imageSource = __DIR__ . "/_files/images/PhpWord.png";
$imageUrl = 'http://php.net//images/logos/php-med-trans-light.gif'; $imageUrl = 'http://php.net//images/logos/php-med-trans-light.gif';
@ -95,36 +84,61 @@ class SectionTest extends \PHPUnit_Framework_TestCase
$section->addListItem(utf8_decode('ä')); $section->addListItem(utf8_decode('ä'));
$section->addObject($objectSource); $section->addObject($objectSource);
$section->addImage($imageSource); $section->addImage($imageSource);
$section->addImage($imageUrl); $section->addMemoryImage($imageUrl);
$section->addTOC();
$section->addTitle(utf8_decode('ä'), 1); $section->addTitle(utf8_decode('ä'), 1);
$section->createTextRun(); $section->createTextRun();
$section->createFootnote(); $section->createFootnote();
$section->addTOC();
$elementCollection = $section->getElements(); $elementCollection = $section->getElements();
$elementType = 'Link'; $elementTypes = array('Text', 'Link', 'TextBreak', 'PageBreak',
$this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$elementType}", $elementCollection[1]); 'Table', 'ListItem', 'Object', 'Image', 'Image',
// $elementTypes = array('Text', 'Link', 'TextBreak', 'PageBreak', 'Title', 'TextRun', 'Footnote');
// 'Table', 'ListItem', 'Object', 'Image', 'Image', 'TOC', $i = 0;
// 'Title', 'TextRun'); foreach ($elementTypes as $elementType) {
// $i = 0; $this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$elementType}", $elementCollection[$i]);
// foreach ($elementTypes as $elementType) { $i++;
// $this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$elementType}", $elementCollection[$i]); }
// $i++; $this->assertInstanceOf("PhpOffice\\PhpWord\\TOC", $elementCollection[$i]);
// }
} }
/** /**
* @covers ::createHeader * Test add object exception
* @covers ::createFooter *
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidObjectException
*/
public function testAddObjectException()
{
$source = __DIR__ . "/_files/xsl/passthrough.xsl";
$section = new Section(0);
$section->addObject($source);
}
/**
* Add title with predefined style
*/
public function testAddTitleWithStyle()
{
Style::addTitleStyle(1, array('size' => 14));
$section = new Section(0);
$section->addTitle('Test', 1);
$elementCollection = $section->getElements();
$this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\Title", $elementCollection[0]);
}
/**
* Create header footer
*/ */
public function testCreateHeaderFooter() public function testCreateHeaderFooter()
{ {
$object = new Section(0); $object = new Section(0);
$elements = array('Header', 'Footer'); $elements = array('Header', 'Footer');
foreach ($elements as $element) { foreach ($elements as $element) {
$method = "create{$element}"; $method = "create{$element}";
$this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$element}", $object->$method()); $this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$element}", $object->$method());
} }
$this->assertFalse($object->hasDifferentFirstPage());
} }
} }

View File

@ -14,14 +14,12 @@ use PhpOffice\PhpWord\Settings;
/** /**
* Test class for PhpOffice\PhpWord\Settings * Test class for PhpOffice\PhpWord\Settings
* *
* @coversDefaultClass \PhpOffice\PhpWord\Settings
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class SettingsTest extends \PHPUnit_Framework_TestCase class SettingsTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @covers ::setCompatibility * Get and set compatibity option
* @covers ::getCompatibility
*/ */
public function testGetSetCompatibility() public function testGetSetCompatibility()
{ {

View File

@ -55,6 +55,7 @@ class FontTest extends \PHPUnit_Framework_TestCase
'strikethrough' => false, 'strikethrough' => false,
'color' => PhpWord::DEFAULT_FONT_COLOR, 'color' => PhpWord::DEFAULT_FONT_COLOR,
'fgColor' => null, 'fgColor' => null,
'hint' => PhpWord::DEFAULT_FONT_CONTENT_TYPE,
); );
foreach ($attributes as $key => $default) { foreach ($attributes as $key => $default) {
$get = "get{$key}"; $get = "get{$key}";
@ -78,19 +79,23 @@ class FontTest extends \PHPUnit_Framework_TestCase
'bold' => true, 'bold' => true,
'italic' => true, 'italic' => true,
'superScript' => true, 'superScript' => true,
'subScript' => true, 'subScript' => false,
'underline' => Font::UNDERLINE_HEAVY, 'underline' => Font::UNDERLINE_HEAVY,
'strikethrough' => true, 'strikethrough' => true,
'color' => '999999', 'color' => '999999',
'fgColor' => '999999', 'fgColor' => '999999',
'hint' => 'eastAsia',
); );
$object->setArrayStyle($attributes);
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
$get = "get{$key}"; $get = "get{$key}";
$object->setStyleValue("_$key", $value);
$this->assertEquals($value, $object->$get()); $this->assertEquals($value, $object->$get());
} }
} }
/**
* Test set line height
*/
public function testLineHeight() public function testLineHeight()
{ {
$phpWord = new PhpWord(); $phpWord = new PhpWord();
@ -121,4 +126,25 @@ class FontTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(720, $lineHeight); $this->assertEquals(720, $lineHeight);
$this->assertEquals('auto', $lineRule); $this->assertEquals('auto', $lineRule);
} }
/**
* Test line height floatval
*/
public function testLineHeightFloatval()
{
$object = new Font(null, array('align' => 'center'));
$object->setLineHeight('1.5pt');
$this->assertEquals(1.5, $object->getLineHeight());
}
/**
* Test line height exception by using nonnumeric value
*
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidStyleException
*/
public function testLineHeightException()
{
$object = new Font();
$object->setLineHeight('a');
}
} }

View File

@ -137,4 +137,15 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
$object->setLineHeight('12.5pt'); $object->setLineHeight('12.5pt');
$this->assertEquals(12.5, $object->getLineHeight()); $this->assertEquals(12.5, $object->getLineHeight());
} }
/**
* Test line height exception by using nonnumeric value
*
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidStyleException
*/
public function testLineHeightException()
{
$object = new Paragraph();
$object->setLineHeight('a');
}
} }

View File

@ -20,9 +20,9 @@ use PhpOffice\PhpWord\Style\Row;
class RowTest extends \PHPUnit_Framework_TestCase class RowTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* Test properties with normal value * Test properties with boolean value
*/ */
public function testProperties() public function testBooleanValue()
{ {
$object = new Row(); $object = new Row();
@ -45,4 +45,23 @@ class RowTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $object->$get()); $this->assertEquals($expected, $object->$get());
} }
} }
/**
* Test properties with nonboolean values, which will return default value
*/
public function testNonBooleanValue()
{
$object = new Row();
$properties = array(
'tblHeader' => 'a',
'cantSplit' => 'b',
);
foreach ($properties as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertFalse($object->$get());
}
}
} }

View File

@ -14,7 +14,6 @@ use PhpOffice\PhpWord\Style\Table;
/** /**
* Test class for PhpOffice\PhpWord\Style\Table * Test class for PhpOffice\PhpWord\Style\Table
* *
* @coversDefaultClass \PhpOffice\PhpWord\Style\Table
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class TableTest extends \PHPUnit_Framework_TestCase class TableTest extends \PHPUnit_Framework_TestCase
@ -25,7 +24,6 @@ class TableTest extends \PHPUnit_Framework_TestCase
* There are 3 variables for class constructor: * There are 3 variables for class constructor:
* - $styleTable: Define table styles * - $styleTable: Define table styles
* - $styleFirstRow: Define style for the first row * - $styleFirstRow: Define style for the first row
* - $styleLastRow: Define style for the last row (reserved)
*/ */
public function testConstruct() public function testConstruct()
{ {
@ -138,4 +136,28 @@ class TableTest extends \PHPUnit_Framework_TestCase
} }
$this->assertEquals($values, $object->getCellMargin()); $this->assertEquals($values, $object->getCellMargin());
} }
/**
* Set style value for various special value types
*/
public function testSetStyleValue()
{
$object = new Table();
$object->setStyleValue('_borderSize', 120);
$object->setStyleValue('_cellMargin', 240);
$object->setStyleValue('_borderColor', '999999');
$this->assertEquals(
array(120, 120, 120, 120, 120, 120),
$object->getBorderSize()
);
$this->assertEquals(
array(240, 240, 240, 240),
$object->getCellMargin()
);
$this->assertEquals(
array('999999', '999999', '999999', '999999', '999999', '999999'),
$object->getBorderColor()
);
}
} }

View File

@ -14,16 +14,12 @@ use PhpOffice\PhpWord\Style;
/** /**
* Test class for PhpOffice\PhpWord\Style * Test class for PhpOffice\PhpWord\Style
* *
* @coversDefaultClass \PhpOffice\PhpWord\Style
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class StyleTest extends \PHPUnit_Framework_TestCase class StyleTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @covers ::addParagraphStyle * Add and get paragraph, font, link, title, and table styles
* @covers ::addFontStyle
* @covers ::addLinkStyle
* @covers ::addTitleStyle
*/ */
public function testStyles() public function testStyles()
{ {
@ -34,6 +30,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
'Link' => 'Font', 'Table' => 'Table', 'Link' => 'Font', 'Table' => 'Table',
'Heading_1' => 'Font', 'Normal' => 'Paragraph'); 'Heading_1' => 'Font', 'Normal' => 'Paragraph');
$elementCount = 6; $elementCount = 6;
Style::addParagraphStyle('Paragraph', $paragraph); Style::addParagraphStyle('Paragraph', $paragraph);
Style::addFontStyle('Font', $font); Style::addFontStyle('Font', $font);
Style::addLinkStyle('Link', $font); Style::addLinkStyle('Link', $font);
@ -47,4 +44,16 @@ class StyleTest extends \PHPUnit_Framework_TestCase
} }
$this->assertNull(Style::getStyle('Unknown')); $this->assertNull(Style::getStyle('Unknown'));
} }
/**
* Set default paragraph style
*/
public function testDefaultParagraphStyle()
{
$paragraph = array('align' => 'center');
Style::setDefaultParagraphStyle($paragraph);
$this->assertInstanceOf("PhpOffice\\PhpWord\\Style\\Paragraph", Style::getStyle('Normal'));
}
} }

View File

@ -14,17 +14,14 @@ use PhpOffice\PhpWord\TOC;
/** /**
* Test class for PhpOffice\PhpWord\TOC * Test class for PhpOffice\PhpWord\TOC
* *
* @coversDefaultClass \PhpOffice\PhpWord\TOC
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class TOCTest extends \PHPUnit_Framework_TestCase class TOCTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @covers ::__construct * Construct with font and TOC style in array format
* @covers ::getStyleTOC
* @covers ::getStyleFont
*/ */
public function testConstruct() public function testConstructWithStyleArray()
{ {
$expected = array( $expected = array(
'tabPos' => 9062, 'tabPos' => 9062,
@ -44,12 +41,21 @@ class TOCTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::addTitle * Construct with named font style
* @covers ::getTitles */
public function testConstructWithStyleName()
{
$object = new TOC('Font Style');
$tocStyle = $object->getStyleTOC();
$this->assertEquals('Font Style', $object->getStyleFont());
}
/**
* Add and get title
*/ */
public function testAddAndGetTitle() public function testAddAndGetTitle()
{ {
// Prepare variables
$titleCount = 3; $titleCount = 3;
$anchor = '_Toc' . (252634154 + $titleCount); $anchor = '_Toc' . (252634154 + $titleCount);
$bookmark = $titleCount - 1; $bookmark = $titleCount - 1;
@ -59,14 +65,12 @@ class TOCTest extends \PHPUnit_Framework_TestCase
'Heading 3' => 3, 'Heading 3' => 3,
); );
// @covers ::addTitle
foreach ($titles as $text => $depth) { foreach ($titles as $text => $depth) {
$response = TOC::addTitle($text, $depth); $response = TOC::addTitle($text, $depth);
} }
$this->assertEquals($anchor, $response[0]); $this->assertEquals($anchor, $response[0]);
$this->assertEquals($bookmark, $response[1]); $this->assertEquals($bookmark, $response[1]);
// @covers ::getTitles
$i = 0; $i = 0;
$savedTitles = TOC::getTitles(); $savedTitles = TOC::getTitles();
foreach ($titles as $text => $depth) { foreach ($titles as $text => $depth) {

View File

@ -15,12 +15,15 @@ use PhpOffice\PhpWord\Template;
/** /**
* Test class for PhpOffice\PhpWord\Template * Test class for PhpOffice\PhpWord\Template
* *
* @covers \PhpOffice\PhpWord\Template
* @coversDefaultClass \PhpOffice\PhpWord\Template * @coversDefaultClass \PhpOffice\PhpWord\Template
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
final class TemplateTest extends \PHPUnit_Framework_TestCase final class TemplateTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* Template can be saved in temporary location
*
* @covers ::save * @covers ::save
* @test * @test
*/ */
@ -60,6 +63,8 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* XSL stylesheet can be applied
*
* @covers ::applyXslStyleSheet * @covers ::applyXslStyleSheet
* @depends testTemplateCanBeSavedInTemporaryLocation * @depends testTemplateCanBeSavedInTemporaryLocation
* @test * @test
@ -86,6 +91,8 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* XSL stylesheet cannot be applied on failure in setting parameter value
*
* @covers ::applyXslStyleSheet * @covers ::applyXslStyleSheet
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception * @expectedException \PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage Could not set values for the given XSL style sheet parameters. * @expectedExceptionMessage Could not set values for the given XSL style sheet parameters.
@ -106,6 +113,8 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* XSL stylesheet can be applied on failure of loading XML from template
*
* @covers ::applyXslStyleSheet * @covers ::applyXslStyleSheet
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception * @expectedException \PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage Could not load XML from the given template. * @expectedExceptionMessage Could not load XML from the given template.
@ -126,10 +135,7 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::getVariables * Get variables and clone row
* @covers ::setValue
* @covers ::cloneRow
* @covers ::saveAs
*/ */
public function testCloneRow() public function testCloneRow()
{ {