Merge pull request #112 from ivanlanin/develop
Unit test for PHPWord main class
This commit is contained in:
commit
9c0e70a198
|
|
@ -26,12 +26,13 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** PHPWORD_BASE_PATH */
|
/** PHPWORD_BASE_PATH */
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
if (!defined('PHPWORD_BASE_PATH')) {
|
if (!defined('PHPWORD_BASE_PATH')) {
|
||||||
define('PHPWORD_BASE_PATH', dirname(__FILE__) . '/');
|
define('PHPWORD_BASE_PATH', dirname(__FILE__) . '/');
|
||||||
require PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php';
|
require PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php';
|
||||||
PHPWord_Autoloader::Register();
|
PHPWord_Autoloader::Register();
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHPWord
|
* PHPWord
|
||||||
|
|
@ -151,7 +152,7 @@ class PHPWord
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get default Font size
|
* Get default Font size (in points)
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getDefaultFontSize()
|
public function getDefaultFontSize()
|
||||||
|
|
@ -160,15 +161,24 @@ class PHPWord
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set default Font size
|
* Set default Font size (in points)
|
||||||
* @param int $pValue
|
* @param int $pValue
|
||||||
*/
|
*/
|
||||||
public function setDefaultFontSize($pValue)
|
public function setDefaultFontSize($pValue)
|
||||||
{
|
{
|
||||||
$pValue = $pValue * 2;
|
|
||||||
$this->_defaultFontSize = $pValue;
|
$this->_defaultFontSize = $pValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set default paragraph style definition to styles.xml
|
||||||
|
*
|
||||||
|
* @param array $styles Paragraph style definition
|
||||||
|
*/
|
||||||
|
public function setDefaultParagraphStyle($styles)
|
||||||
|
{
|
||||||
|
PHPWord_Style::setDefaultParagraphStyle($styles);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a paragraph style definition to styles.xml
|
* Adds a paragraph style definition to styles.xml
|
||||||
*
|
*
|
||||||
|
|
@ -213,16 +223,6 @@ class PHPWord
|
||||||
PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
|
PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set default paragraph style definition to styles.xml
|
|
||||||
*
|
|
||||||
* @param array $styles Paragraph style definition
|
|
||||||
*/
|
|
||||||
public function setDefaultParagraphStyle($styles)
|
|
||||||
{
|
|
||||||
PHPWord_Style::setDefaultParagraphStyle($styles);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a hyperlink style to styles.xml
|
* Adds a hyperlink style to styles.xml
|
||||||
*
|
*
|
||||||
|
|
@ -243,15 +243,6 @@ class PHPWord
|
||||||
return $this->_sectionCollection;
|
return $this->_sectionCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get section count
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function _countSections()
|
|
||||||
{
|
|
||||||
return count($this->_sectionCollection);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load a Template File
|
* Load a Template File
|
||||||
*
|
*
|
||||||
|
|
@ -264,7 +255,18 @@ class PHPWord
|
||||||
$template = new PHPWord_Template($strFilename);
|
$template = new PHPWord_Template($strFilename);
|
||||||
return $template;
|
return $template;
|
||||||
} else {
|
} else {
|
||||||
trigger_error('Template file ' . $strFilename . ' not found.', E_USER_ERROR);
|
throw new PHPWord_Exception(
|
||||||
|
"Template file {$strFilename} not found."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get section count
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private function _countSections()
|
||||||
|
{
|
||||||
|
return count($this->_sectionCollection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,183 @@
|
||||||
|
<?php
|
||||||
|
namespace PHPWord\Tests;
|
||||||
|
|
||||||
|
use PHPUnit_Framework_TestCase;
|
||||||
|
use PHPWord;
|
||||||
|
use PHPWord_DocumentProperties;
|
||||||
|
use PHPWord_Section;
|
||||||
|
use PHPWord_Style;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord
|
||||||
|
*/
|
||||||
|
class PHPWordTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var PHPWord
|
||||||
|
*/
|
||||||
|
protected $object;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::__construct
|
||||||
|
* @covers PHPWord::getProperties
|
||||||
|
* @covers PHPWord::getDefaultFontName
|
||||||
|
* @covers PHPWord::getDefaultFontSize
|
||||||
|
*/
|
||||||
|
public function testConstruct()
|
||||||
|
{
|
||||||
|
$object = new PHPWord();
|
||||||
|
$this->assertEquals(
|
||||||
|
new PHPWord_DocumentProperties(),
|
||||||
|
$object->getProperties()
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
PHPWord::DEFAULT_FONT_NAME,
|
||||||
|
$object->getDefaultFontName()
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
PHPWord::DEFAULT_FONT_SIZE,
|
||||||
|
$object->getDefaultFontSize()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::setProperties
|
||||||
|
* @covers PHPWord::getProperties
|
||||||
|
*/
|
||||||
|
public function testSetGetProperties()
|
||||||
|
{
|
||||||
|
$object = new PHPWord();
|
||||||
|
$creator = 'PHPWord';
|
||||||
|
$properties = $object->getProperties();
|
||||||
|
$properties->setCreator($creator);
|
||||||
|
$object->setProperties($properties);
|
||||||
|
$this->assertEquals($creator, $object->getProperties()->getCreator());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::createSection
|
||||||
|
* @covers PHPWord::getSections
|
||||||
|
*/
|
||||||
|
public function testCreateGetSections()
|
||||||
|
{
|
||||||
|
$object = new PHPWord();
|
||||||
|
$this->assertEquals(new PHPWord_Section(1), $object->createSection());
|
||||||
|
$object->createSection();
|
||||||
|
$this->assertEquals(2, count($object->getSections()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::setDefaultFontName
|
||||||
|
* @covers PHPWord::getDefaultFontName
|
||||||
|
*/
|
||||||
|
public function testSetGetDefaultFontName()
|
||||||
|
{
|
||||||
|
$object = new PHPWord();
|
||||||
|
$fontName = 'Times New Roman';
|
||||||
|
$this->assertEquals(
|
||||||
|
PHPWord::DEFAULT_FONT_NAME,
|
||||||
|
$object->getDefaultFontName()
|
||||||
|
);
|
||||||
|
$object->setDefaultFontName($fontName);
|
||||||
|
$this->assertEquals($fontName, $object->getDefaultFontName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::setDefaultFontSize
|
||||||
|
* @covers PHPWord::getDefaultFontSize
|
||||||
|
*/
|
||||||
|
public function testSetGetDefaultFontSize()
|
||||||
|
{
|
||||||
|
$object = new PHPWord();
|
||||||
|
$fontSize = 16;
|
||||||
|
$this->assertEquals(
|
||||||
|
PHPWord::DEFAULT_FONT_SIZE,
|
||||||
|
$object->getDefaultFontSize()
|
||||||
|
);
|
||||||
|
$object->setDefaultFontSize($fontSize);
|
||||||
|
$this->assertEquals($fontSize, $object->getDefaultFontSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::setDefaultParagraphStyle
|
||||||
|
* @covers PHPWord::loadTemplate
|
||||||
|
*/
|
||||||
|
public function testSetDefaultParagraphStyle()
|
||||||
|
{
|
||||||
|
$object = new PHPWord();
|
||||||
|
$object->setDefaultParagraphStyle(array());
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'PHPWord_Style_Paragraph',
|
||||||
|
PHPWord_Style::getStyle('Normal')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::addParagraphStyle
|
||||||
|
* @covers PHPWord::addFontStyle
|
||||||
|
* @covers PHPWord::addTableStyle
|
||||||
|
* @covers PHPWord::addLinkStyle
|
||||||
|
*/
|
||||||
|
public function testAddStyles()
|
||||||
|
{
|
||||||
|
$object = new PHPWord();
|
||||||
|
$styles = array('Paragraph' => 'Paragraph', 'Font' => 'Font',
|
||||||
|
'Table' => 'TableFull', 'Link' => 'Font');
|
||||||
|
foreach ($styles as $key => $value) {
|
||||||
|
$method = "add{$key}Style";
|
||||||
|
$styleId = "{$key} Style";
|
||||||
|
$styleType = "PHPWord_Style_{$value}";
|
||||||
|
$object->$method($styleId, array());
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
$styleType,
|
||||||
|
PHPWord_Style::getStyle($styleId)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::addTitleStyle
|
||||||
|
*/
|
||||||
|
public function testAddTitleStyle()
|
||||||
|
{
|
||||||
|
$object = new PHPWord();
|
||||||
|
$titleLevel = 1;
|
||||||
|
$titleName = "Heading_{$titleLevel}";
|
||||||
|
$object->addTitleStyle($titleLevel, array());
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'PHPWord_Style_Font',
|
||||||
|
PHPWord_Style::getStyle($titleName)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::loadTemplate
|
||||||
|
*/
|
||||||
|
public function testLoadTemplate()
|
||||||
|
{
|
||||||
|
$file = join(
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx')
|
||||||
|
);
|
||||||
|
$object = new PHPWord();
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'PHPWord_Template',
|
||||||
|
$object->loadTemplate($file)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord::loadTemplate
|
||||||
|
* @expectedException PHPWord_Exception
|
||||||
|
*/
|
||||||
|
public function testLoadTemplateException()
|
||||||
|
{
|
||||||
|
$file = join(
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blanks.docx')
|
||||||
|
);
|
||||||
|
$object = new PHPWord();
|
||||||
|
$object->loadTemplate($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue