From 663e9008b38d8f73c446aca7f32cbca6c394a1f7 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 10 Mar 2014 03:20:24 +0700 Subject: [PATCH] Basic unit tests for PHPWord\Style completed --- Classes/PHPWord/Style/TOC.php | 2 +- Classes/PHPWord/Style/TableFull.php | 7 +- Tests/PHPWord/Style/CellTest.php | 2 +- Tests/PHPWord/Style/ImageTest.php | 71 ++++++++++++++ Tests/PHPWord/Style/ListItemTest.php | 51 ++++++++++ Tests/PHPWord/Style/ParagraphTest.php | 11 +++ Tests/PHPWord/Style/RowTest.php | 43 ++++++++ Tests/PHPWord/Style/TOCTest.php | 41 ++++++++ Tests/PHPWord/Style/TableFullTest.php | 136 ++++++++++++++++++++++++++ Tests/PHPWord/Style/TableTest.php | 54 ++++++++++ Tests/PHPWord/Style/TabsTest.php | 46 +++++++++ 11 files changed, 461 insertions(+), 3 deletions(-) create mode 100644 Tests/PHPWord/Style/ImageTest.php create mode 100644 Tests/PHPWord/Style/ListItemTest.php create mode 100644 Tests/PHPWord/Style/RowTest.php create mode 100644 Tests/PHPWord/Style/TOCTest.php create mode 100644 Tests/PHPWord/Style/TableFullTest.php create mode 100644 Tests/PHPWord/Style/TableTest.php create mode 100644 Tests/PHPWord/Style/TabsTest.php diff --git a/Classes/PHPWord/Style/TOC.php b/Classes/PHPWord/Style/TOC.php index 501604b4..e7adfc41 100755 --- a/Classes/PHPWord/Style/TOC.php +++ b/Classes/PHPWord/Style/TOC.php @@ -85,7 +85,7 @@ class PHPWord_Style_TOC */ public function setTabPos($pValue) { - $this->_tabLeader = $pValue; + $this->_tabPos = $pValue; } /** diff --git a/Classes/PHPWord/Style/TableFull.php b/Classes/PHPWord/Style/TableFull.php index 4e2a7686..11f094e0 100755 --- a/Classes/PHPWord/Style/TableFull.php +++ b/Classes/PHPWord/Style/TableFull.php @@ -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; diff --git a/Tests/PHPWord/Style/CellTest.php b/Tests/PHPWord/Style/CellTest.php index 680814c5..759f6e0e 100644 --- a/Tests/PHPWord/Style/CellTest.php +++ b/Tests/PHPWord/Style/CellTest.php @@ -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}"; diff --git a/Tests/PHPWord/Style/ImageTest.php b/Tests/PHPWord/Style/ImageTest.php new file mode 100644 index 00000000..52d0b4c2 --- /dev/null +++ b/Tests/PHPWord/Style/ImageTest.php @@ -0,0 +1,71 @@ + 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'); + } + +} \ No newline at end of file diff --git a/Tests/PHPWord/Style/ListItemTest.php b/Tests/PHPWord/Style/ListItemTest.php new file mode 100644 index 00000000..9b9f6ec9 --- /dev/null +++ b/Tests/PHPWord/Style/ListItemTest.php @@ -0,0 +1,51 @@ +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()); + } + +} \ No newline at end of file diff --git a/Tests/PHPWord/Style/ParagraphTest.php b/Tests/PHPWord/Style/ParagraphTest.php index 9220923b..e61ff8fd 100644 --- a/Tests/PHPWord/Style/ParagraphTest.php +++ b/Tests/PHPWord/Style/ParagraphTest.php @@ -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()); + } + } \ No newline at end of file diff --git a/Tests/PHPWord/Style/RowTest.php b/Tests/PHPWord/Style/RowTest.php new file mode 100644 index 00000000..63c05d61 --- /dev/null +++ b/Tests/PHPWord/Style/RowTest.php @@ -0,0 +1,43 @@ + 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()); + } + } + +} diff --git a/Tests/PHPWord/Style/TOCTest.php b/Tests/PHPWord/Style/TOCTest.php new file mode 100644 index 00000000..78ac6506 --- /dev/null +++ b/Tests/PHPWord/Style/TOCTest.php @@ -0,0 +1,41 @@ + 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()); + } + } + +} diff --git a/Tests/PHPWord/Style/TableFullTest.php b/Tests/PHPWord/Style/TableFullTest.php new file mode 100644 index 00000000..10855d55 --- /dev/null +++ b/Tests/PHPWord/Style/TableFullTest.php @@ -0,0 +1,136 @@ + '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()); + } + +} diff --git a/Tests/PHPWord/Style/TableTest.php b/Tests/PHPWord/Style/TableTest.php new file mode 100644 index 00000000..93f4ebfc --- /dev/null +++ b/Tests/PHPWord/Style/TableTest.php @@ -0,0 +1,54 @@ +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()); + } + +} diff --git a/Tests/PHPWord/Style/TabsTest.php b/Tests/PHPWord/Style/TabsTest.php new file mode 100644 index 00000000..5d318610 --- /dev/null +++ b/Tests/PHPWord/Style/TabsTest.php @@ -0,0 +1,46 @@ +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')); + } + +}