Basic unit tests for PHPWord\Style completed

This commit is contained in:
Ivan Lanin 2014-03-10 03:20:24 +07:00
parent 79ba793294
commit 663e9008b3
11 changed files with 461 additions and 3 deletions

View File

@ -85,7 +85,7 @@ class PHPWord_Style_TOC
*/
public function setTabPos($pValue)
{
$this->_tabLeader = $pValue;
$this->_tabPos = $pValue;
}
/**

View File

@ -247,7 +247,7 @@ class PHPWord_Style_TableFull
/**
* Set TLRBVH Border Size
*
* @param int $pValue
* @param int $pValue Border size in eighths of a point (1/8 point)
*/
public function setBorderSize($pValue = null)
{
@ -466,6 +466,11 @@ class PHPWord_Style_TableFull
return $this->_cellMarginBottom;
}
/**
* Set TLRB cell margin
*
* @param int $pValue Margin in twips
*/
public function setCellMargin($pValue = null)
{
$this->_cellMarginTop = $pValue;

View File

@ -12,6 +12,7 @@ use PHPWord_Style_Cell;
*/
class CellTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style with normal value
*/
@ -34,7 +35,6 @@ class CellTest extends \PHPUnit_Framework_TestCase
'gridSpan' => 2,
'vMerge' => 2,
);
//'defaultBorderColor' => null,
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";

View File

@ -0,0 +1,71 @@
<?php
namespace PHPWord\Tests\Style;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_Image;
/**
* Class ImageTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class ImageTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style with normal value
*/
public function testSetGetNormal()
{
$object = new PHPWord_Style_Image();
$properties = array(
'width' => 200,
'height' => 200,
'align' => 'left',
'marginTop' => 240,
'marginLeft' => 240,
'wrappingStyle' => 'inline',
);
foreach ($properties as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
}
/**
* Test setStyleValue method
*/
public function testSetStyleValue()
{
$object = new PHPWord_Style_Image();
$properties = array(
'width' => 200,
'height' => 200,
'align' => 'left',
'marginTop' => 240,
'marginLeft' => 240,
);
foreach ($properties as $key => $value) {
$get = "get{$key}";
$object->setStyleValue("_{$key}", $value);
$this->assertEquals($value, $object->$get());
}
}
/**
* Test setWrappingStyle exception
*
* @expectedException InvalidArgumentException
*/
public function testSetWrappingStyleException()
{
$object = new PHPWord_Style_Image();
$object->setWrappingStyle('foo');
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace PHPWord\Tests\Style;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_ListItem;
/**
* Class ListItemTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class ListItemTest extends \PHPUnit_Framework_TestCase
{
/**
* Test construct
*/
public function testConstruct()
{
$object = new PHPWord_Style_ListItem();
$value = PHPWord_Style_ListItem::TYPE_BULLET_FILLED;
$this->assertEquals($value, $object->getListType());
}
/**
* Test set style value
*/
public function testSetStyleValue()
{
$object = new PHPWord_Style_ListItem();
$value = PHPWord_Style_ListItem::TYPE_ALPHANUM;
$object->setStyleValue('_listType', $value);
$this->assertEquals($value, $object->getListType());
}
/**
* Test list type
*/
public function testListType()
{
$object = new PHPWord_Style_ListItem();
$value = PHPWord_Style_ListItem::TYPE_ALPHANUM;
$object->setListType($value);
$this->assertEquals($value, $object->getListType());
}
}

View File

@ -123,4 +123,15 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(720, $lineHeight);
$this->assertEquals('auto', $lineRule);
}
/**
* Test setLineHeight validation
*/
public function testLineHeightValidation()
{
$object = new PHPWord_Style_Paragraph();
$object->setLineHeight('12.5pt');
$this->assertEquals(12.5, $object->getLineHeight());
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace PHPWord\Tests\Style;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_Row;
/**
* Class RowTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class RowTest extends \PHPUnit_Framework_TestCase
{
/**
* Test properties with normal value
*/
public function testProperties()
{
$object = new PHPWord_Style_Row();
$properties = array(
'tblHeader' => true,
'cantSplit' => false,
);
foreach ($properties as $key => $value) {
// set/get
$set = "set{$key}";
$get = "get{$key}";
$expected = $value ? 1 : 0;
$object->$set($value);
$this->assertEquals($expected, $object->$get());
// setStyleValue
$value = !$value;
$expected = $value ? 1 : 0;
$object->setStyleValue("_{$key}", $value);
$this->assertEquals($expected, $object->$get());
}
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace PHPWord\Tests\Style;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_TOC;
/**
* Class TOCTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class TOCTest extends \PHPUnit_Framework_TestCase
{
/**
* Test properties with normal value
*/
public function testProperties()
{
$object = new PHPWord_Style_TOC();
$properties = array(
'tabPos' => 9062,
'tabLeader' => PHPWord_Style_TOC::TABLEADER_DOT,
'indent' => 200,
);
foreach ($properties as $key => $value) {
// set/get
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
// setStyleValue
$object->setStyleValue("_{$key}", null);
$this->assertEquals(null, $object->$get());
}
}
}

View File

@ -0,0 +1,136 @@
<?php
namespace PHPWord\Tests\Style;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_TableFull;
/**
* Class TableFullTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class TableFullTest extends \PHPUnit_Framework_TestCase
{
/**
* Test class construction
*
* There are 3 variables for class constructor:
* - $styleTable: Define table styles
* - $styleFirstRow: Define style for the first row
* - $styleLastRow: Define style for the last row (reserved)
*/
public function testConstruct()
{
$styleTable = array('bgColor' => 'FF0000');
$styleFirstRow = array('borderBottomSize' => 3);
$object = new PHPWord_Style_TableFull($styleTable, $styleFirstRow);
$this->assertEquals('FF0000', $object->getBgColor());
$firstRow = $object->getFirstRow();
$this->assertInstanceOf('PHPWord_Style_TableFull', $firstRow);
$this->assertEquals(3, $firstRow->getBorderBottomSize());
}
/**
* Test setting style with normal value
*/
public function testSetGetNormal()
{
$object = new PHPWord_Style_TableFull();
$attributes = array(
'bgColor' => 'FF0000',
'borderTopSize' => 4,
'borderTopColor' => 'FF0000',
'borderLeftSize' => 4,
'borderLeftColor' => 'FF0000',
'borderRightSize' => 4,
'borderRightColor' => 'FF0000',
'borderBottomSize' => 4,
'borderBottomColor' => 'FF0000',
'borderInsideHSize' => 4,
'borderInsideHColor' => 'FF0000',
'borderInsideVSize' => 4,
'borderInsideVColor' => 'FF0000',
'cellMarginTop' => 240,
'cellMarginLeft' => 240,
'cellMarginRight' => 240,
'cellMarginBottom' => 240,
);
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
}
/**
* Test border color
*
* Set border color and test if each part has the same color
* While looping, push values array to be asserted with getBorderColor
*/
public function testBorderColor()
{
$object = new PHPWord_Style_TableFull();
$parts = array('Top', 'Left', 'Right', 'Bottom', 'InsideH', 'InsideV');
$value = 'FF0000';
$object->setBorderColor($value);
foreach ($parts as $part) {
$get = "getBorder{$part}Color";
$values[] = $value;
$this->assertEquals($value, $object->$get());
}
$this->assertEquals($values, $object->getBorderColor());
}
/**
* Test border size
*
* Set border size and test if each part has the same size
* While looping, push values array to be asserted with getBorderSize
* Value is in eights of a point, i.e. 4 / 8 = .5pt
*/
public function testBorderSize()
{
$object = new PHPWord_Style_TableFull();
$parts = array('Top', 'Left', 'Right', 'Bottom', 'InsideH', 'InsideV');
$value = 4;
$object->setBorderSize($value);
foreach ($parts as $part) {
$get = "getBorder{$part}Size";
$values[] = $value;
$this->assertEquals($value, $object->$get());
}
$this->assertEquals($values, $object->getBorderSize());
}
/**
* Test cell margin
*
* Set cell margin and test if each part has the same margin
* While looping, push values array to be asserted with getCellMargin
* Value is in twips
*/
public function testCellMargin()
{
$object = new PHPWord_Style_TableFull();
$parts = array('Top', 'Left', 'Right', 'Bottom');
$value = 240;
$object->setCellMargin($value);
foreach ($parts as $part) {
$get = "getCellMargin{$part}";
$values[] = $value;
$this->assertEquals($value, $object->$get());
}
$this->assertEquals($values, $object->getCellMargin());
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace PHPWord\Tests\Style;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_Table;
/**
* Class TableTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class TableTest extends \PHPUnit_Framework_TestCase
{
/**
* Test set style value
*/
public function testSetStyleValue()
{
$object = new PHPWord_Style_Table();
$parts = array('Top', 'Left', 'Right', 'Bottom');
$value = 240; // In twips
foreach ($parts as $part) {
$property = "_cellMargin{$part}";
$get = "getCellMargin{$part}";
$object->setStyleValue($property, $value);
$this->assertEquals($value, $object->$get());
}
}
/**
* Test cell margin
*/
public function testCellMargin()
{
$object = new PHPWord_Style_Table();
$parts = array('Top', 'Left', 'Right', 'Bottom');
// Set cell margin and test if each part has the same margin
// While looping, push values array to be asserted with getCellMargin
$value = 240; // In twips
foreach ($parts as $part) {
$set = "setCellMargin{$part}";
$get = "getCellMargin{$part}";
$values[] = $value;
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
$this->assertEquals($values, $object->getCellMargin());
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace PHPWord\Tests\Style;
use PHPUnit_Framework_TestCase;
use PHPWord;
use PHPWord_Style_Tab;
use PHPWord_Style_Tabs;
use PHPWord\Tests\TestHelperDOCX;
/**
* Class TabsTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class TabsTest extends \PHPUnit_Framework_TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}
/**
* Test if the tabs has been created properly
*/
public function testTabsStyle()
{
$PHPWord = new PHPWord();
$PHPWord->addParagraphStyle('tabbed', array(
'tabs' => array(
new PHPWord_Style_Tab('left', 1440, 'dot'),
)
));
$doc = TestHelperDOCX::getDocument($PHPWord);
$file = 'word/styles.xml';
$path = '/w:styles/w:style[@w:styleId="tabbed"]/w:pPr/w:tabs/w:tab[1]';
$element = $doc->getElement($path, $file);
$this->assertEquals('left', $element->getAttribute('w:val'));
$this->assertEquals(1440, $element->getAttribute('w:pos'));
$this->assertEquals('dot', $element->getAttribute('w:leader'));
}
}