Fixed bugs with Text class

This commit is contained in:
Gabriel Bull 2014-03-09 15:24:13 -04:00
parent 79f65f04e1
commit 70588f2f8f
4 changed files with 72 additions and 74 deletions

View File

@ -51,7 +51,6 @@ class PHPWord_Section_Text
*/ */
private $paragraphStyle; private $paragraphStyle;
/** /**
* Create a new Text Element * Create a new Text Element
* *
@ -72,19 +71,20 @@ class PHPWord_Section_Text
* @param null|array|\PHPWord_Style_Font $style * @param null|array|\PHPWord_Style_Font $style
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle * @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
* @return PHPWord_Style_Font * @return PHPWord_Style_Font
* @throws \Exception
*/ */
public function setFontStyle($style = null, $paragraphStyle = null) public function setFontStyle($style = null, $paragraphStyle = null)
{ {
if ($style instanceof PHPWord_Style_Font) { if ($style instanceof PHPWord_Style_Font) {
$this->fontStyle = $style; $this->fontStyle = $style;
$this->setParagraphStyle($paragraphStyle);
} elseif (is_array($style)) { } elseif (is_array($style)) {
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle); $this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
$this->fontStyle->setArrayStyle($style); $this->fontStyle->setArrayStyle($style);
} elseif (null === $style) { } elseif (null === $style) {
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle); $this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
} else { } else {
throw new Exception('Expected array or PHPWord_Style_Font'); $this->fontStyle = $style;
$this->setParagraphStyle($paragraphStyle);
} }
return $this->fontStyle; return $this->fontStyle;
} }
@ -104,7 +104,6 @@ class PHPWord_Section_Text
* *
* @param null|array|\PHPWord_Style_Paragraph $style * @param null|array|\PHPWord_Style_Paragraph $style
* @return null|\PHPWord_Style_Paragraph * @return null|\PHPWord_Style_Paragraph
* @throws \Exception
*/ */
public function setParagraphStyle($style = null) public function setParagraphStyle($style = null)
{ {
@ -116,7 +115,7 @@ class PHPWord_Section_Text
} elseif (null === $style) { } elseif (null === $style) {
$this->paragraphStyle = new PHPWord_Style_Paragraph; $this->paragraphStyle = new PHPWord_Style_Paragraph;
} else { } else {
throw new Exception('Expected array or PHPWord_Style_Paragraph'); $this->paragraphStyle = $style;
} }
return $this->paragraphStyle; return $this->paragraphStyle;
} }
@ -150,4 +149,4 @@ class PHPWord_Section_Text
{ {
return $this->text; return $this->text;
} }
} }

View File

@ -163,7 +163,6 @@ class PHPWord_Style_Font
* *
* @param string $type Type of font * @param string $type Type of font
* @param array $paragraphStyle Paragraph styles definition * @param array $paragraphStyle Paragraph styles definition
* @throws \Exception
*/ */
public function __construct($type = 'text', $paragraphStyle = null) public function __construct($type = 'text', $paragraphStyle = null)
{ {
@ -177,7 +176,7 @@ class PHPWord_Style_Font
} elseif (null === $paragraphStyle) { } elseif (null === $paragraphStyle) {
$this->_paragraphStyle = new PHPWord_Style_Paragraph; $this->_paragraphStyle = new PHPWord_Style_Paragraph;
} else { } else {
throw new Exception('Expected array or PHPWord_Style_Paragraph'); $this->_paragraphStyle = $paragraphStyle;
} }
} }

View File

@ -15,33 +15,33 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
$oSettings->setSettingValue('_orientation', 'landscape'); $oSettings->setSettingValue('_orientation', 'landscape');
$this->assertEquals($oSettings->getOrientation(), 'landscape'); $this->assertEquals('landscape', $oSettings->getOrientation());
$this->assertEquals($oSettings->getPageSizeW(), 16838); $this->assertEquals(16838, $oSettings->getPageSizeW());
$this->assertEquals($oSettings->getPageSizeH(), 11906); $this->assertEquals(11906, $oSettings->getPageSizeH());
$oSettings->setSettingValue('_orientation', null); $oSettings->setSettingValue('_orientation', null);
$this->assertEquals($oSettings->getOrientation(), null); $this->assertNull($oSettings->getOrientation());
$this->assertEquals($oSettings->getPageSizeW(), 11906); $this->assertEquals(11906, $oSettings->getPageSizeW());
$this->assertEquals($oSettings->getPageSizeH(), 16838); $this->assertEquals(16838, $oSettings->getPageSizeH());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setSettingValue('_borderSize', $iVal); $oSettings->setSettingValue('_borderSize', $iVal);
$this->assertEquals($oSettings->getBorderSize(), array($iVal, $iVal, $iVal, $iVal)); $this->assertEquals(array($iVal, $iVal, $iVal, $iVal), $oSettings->getBorderSize());
$this->assertEquals($oSettings->getBorderBottomSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderBottomSize());
$this->assertEquals($oSettings->getBorderLeftSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderLeftSize());
$this->assertEquals($oSettings->getBorderRightSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderRightSize());
$this->assertEquals($oSettings->getBorderTopSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderTopSize());
$oSettings->setSettingValue('_borderColor', 'FF00AA'); $oSettings->setSettingValue('_borderColor', 'FF00AA');
$this->assertEquals($oSettings->getBorderColor(), array('FF00AA', 'FF00AA', 'FF00AA', 'FF00AA')); $this->assertEquals(array('FF00AA', 'FF00AA', 'FF00AA', 'FF00AA'), $oSettings->getBorderColor());
$this->assertEquals($oSettings->getBorderBottomColor(), 'FF00AA'); $this->assertEquals('FF00AA', $oSettings->getBorderBottomColor());
$this->assertEquals($oSettings->getBorderLeftColor(), 'FF00AA'); $this->assertEquals('FF00AA', $oSettings->getBorderLeftColor());
$this->assertEquals($oSettings->getBorderRightColor(), 'FF00AA'); $this->assertEquals('FF00AA', $oSettings->getBorderRightColor());
$this->assertEquals($oSettings->getBorderTopColor(), 'FF00AA'); $this->assertEquals('FF00AA', $oSettings->getBorderTopColor());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setSettingValue('headerHeight', $iVal); $oSettings->setSettingValue('headerHeight', $iVal);
$this->assertEquals($oSettings->getHeaderHeight(), $iVal); $this->assertEquals($iVal, $oSettings->getHeaderHeight());
} }
public function testMargin() public function testMargin()
@ -51,19 +51,19 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setMarginTop($iVal); $oSettings->setMarginTop($iVal);
$this->assertEquals($oSettings->getMarginTop(), $iVal); $this->assertEquals($iVal, $oSettings->getMarginTop());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setMarginBottom($iVal); $oSettings->setMarginBottom($iVal);
$this->assertEquals($oSettings->getMarginBottom(), $iVal); $this->assertEquals($iVal, $oSettings->getMarginBottom());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setMarginLeft($iVal); $oSettings->setMarginLeft($iVal);
$this->assertEquals($oSettings->getMarginLeft(), $iVal); $this->assertEquals($iVal, $oSettings->getMarginLeft());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setMarginRight($iVal); $oSettings->setMarginRight($iVal);
$this->assertEquals($oSettings->getMarginRight(), $iVal); $this->assertEquals($iVal, $oSettings->getMarginRight());
} }
public function testOrientationLandscape() public function testOrientationLandscape()
@ -72,9 +72,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
$oSettings->setLandscape(); $oSettings->setLandscape();
$this->assertEquals($oSettings->getOrientation(), 'landscape'); $this->assertEquals('landscape', $oSettings->getOrientation());
$this->assertEquals($oSettings->getPageSizeW(), 16838); $this->assertEquals(16838, $oSettings->getPageSizeW());
$this->assertEquals($oSettings->getPageSizeH(), 11906); $this->assertEquals(11906, $oSettings->getPageSizeH());
} }
public function testOrientationPortrait() public function testOrientationPortrait()
@ -83,9 +83,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
$oSettings->setPortrait(); $oSettings->setPortrait();
$this->assertEquals($oSettings->getOrientation(), null); $this->assertNull($oSettings->getOrientation());
$this->assertEquals($oSettings->getPageSizeW(), 11906); $this->assertEquals(11906, $oSettings->getPageSizeW());
$this->assertEquals($oSettings->getPageSizeH(), 16838); $this->assertEquals(16838, $oSettings->getPageSizeH());
} }
public function testBorderSize() public function testBorderSize()
@ -95,27 +95,27 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setBorderSize($iVal); $oSettings->setBorderSize($iVal);
$this->assertEquals($oSettings->getBorderSize(), array($iVal, $iVal, $iVal, $iVal)); $this->assertEquals(array($iVal, $iVal, $iVal, $iVal), $oSettings->getBorderSize());
$this->assertEquals($oSettings->getBorderBottomSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderBottomSize());
$this->assertEquals($oSettings->getBorderLeftSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderLeftSize());
$this->assertEquals($oSettings->getBorderRightSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderRightSize());
$this->assertEquals($oSettings->getBorderTopSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderTopSize());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setBorderBottomSize($iVal); $oSettings->setBorderBottomSize($iVal);
$this->assertEquals($oSettings->getBorderBottomSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderBottomSize());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setBorderLeftSize($iVal); $oSettings->setBorderLeftSize($iVal);
$this->assertEquals($oSettings->getBorderLeftSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderLeftSize());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setBorderRightSize($iVal); $oSettings->setBorderRightSize($iVal);
$this->assertEquals($oSettings->getBorderRightSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderRightSize());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setBorderTopSize($iVal); $oSettings->setBorderTopSize($iVal);
$this->assertEquals($oSettings->getBorderTopSize(), $iVal); $this->assertEquals($iVal, $oSettings->getBorderTopSize());
} }
public function testBorderColor() public function testBorderColor()
@ -124,23 +124,23 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
$oSettings->setBorderColor('FF00AA'); $oSettings->setBorderColor('FF00AA');
$this->assertEquals($oSettings->getBorderColor(), array('FF00AA', 'FF00AA', 'FF00AA', 'FF00AA')); $this->assertEquals(array('FF00AA', 'FF00AA', 'FF00AA', 'FF00AA'), $oSettings->getBorderColor());
$this->assertEquals($oSettings->getBorderBottomColor(), 'FF00AA'); $this->assertEquals('FF00AA', $oSettings->getBorderBottomColor());
$this->assertEquals($oSettings->getBorderLeftColor(), 'FF00AA'); $this->assertEquals('FF00AA', $oSettings->getBorderLeftColor());
$this->assertEquals($oSettings->getBorderRightColor(), 'FF00AA'); $this->assertEquals('FF00AA', $oSettings->getBorderRightColor());
$this->assertEquals($oSettings->getBorderTopColor(), 'FF00AA'); $this->assertEquals('FF00AA', $oSettings->getBorderTopColor());
$oSettings->setBorderBottomColor('BBCCDD'); $oSettings->setBorderBottomColor('BBCCDD');
$this->assertEquals($oSettings->getBorderBottomColor(), 'BBCCDD'); $this->assertEquals('BBCCDD', $oSettings->getBorderBottomColor());
$oSettings->setBorderLeftColor('CCDDEE'); $oSettings->setBorderLeftColor('CCDDEE');
$this->assertEquals($oSettings->getBorderLeftColor(), 'CCDDEE'); $this->assertEquals('CCDDEE', $oSettings->getBorderLeftColor());
$oSettings->setBorderRightColor('11EE22'); $oSettings->setBorderRightColor('11EE22');
$this->assertEquals($oSettings->getBorderRightColor(), '11EE22'); $this->assertEquals('11EE22', $oSettings->getBorderRightColor());
$oSettings->setBorderTopColor('22FF33'); $oSettings->setBorderTopColor('22FF33');
$this->assertEquals($oSettings->getBorderTopColor(), '22FF33'); $this->assertEquals('22FF33', $oSettings->getBorderTopColor());
} }
public function testNumberingStart() public function testNumberingStart()
@ -148,14 +148,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
// Section Settings // Section Settings
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
$this->assertEquals($oSettings->getPageNumberingStart(), null); $this->assertNull($oSettings->getPageNumberingStart());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setPageNumberingStart($iVal); $oSettings->setPageNumberingStart($iVal);
$this->assertEquals($oSettings->getPageNumberingStart(), $iVal); $this->assertEquals($iVal, $oSettings->getPageNumberingStart());
$oSettings->setPageNumberingStart(); $oSettings->setPageNumberingStart();
$this->assertEquals($oSettings->getPageNumberingStart(), null); $this->assertNull($oSettings->getPageNumberingStart());
} }
public function testHeader() public function testHeader()
@ -163,14 +163,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
// Section Settings // Section Settings
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
$this->assertEquals($oSettings->getHeaderHeight(), 720); $this->assertEquals(720, $oSettings->getHeaderHeight());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setHeaderHeight($iVal); $oSettings->setHeaderHeight($iVal);
$this->assertEquals($oSettings->getHeaderHeight(), $iVal); $this->assertEquals($iVal, $oSettings->getHeaderHeight());
$oSettings->setHeaderHeight(); $oSettings->setHeaderHeight();
$this->assertEquals($oSettings->getHeaderHeight(), 720); $this->assertEquals(720, $oSettings->getHeaderHeight());
} }
public function testFooter() public function testFooter()
@ -178,14 +178,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
// Section Settings // Section Settings
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
$this->assertEquals($oSettings->getFooterHeight(), 720); $this->assertEquals(720, $oSettings->getFooterHeight());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setFooterHeight($iVal); $oSettings->setFooterHeight($iVal);
$this->assertEquals($oSettings->getFooterHeight(), $iVal); $this->assertEquals($iVal, $oSettings->getFooterHeight());
$oSettings->setFooterHeight(); $oSettings->setFooterHeight();
$this->assertEquals($oSettings->getFooterHeight(), 720); $this->assertEquals(720, $oSettings->getFooterHeight());
} }
public function testColumnsNum() public function testColumnsNum()
@ -194,14 +194,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
// Default // Default
$this->assertEquals($oSettings->getColsNum(), 1); $this->assertEquals(1, $oSettings->getColsNum());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setColsNum($iVal); $oSettings->setColsNum($iVal);
$this->assertEquals($oSettings->getColsNum(), $iVal); $this->assertEquals($iVal, $oSettings->getColsNum());
$oSettings->setColsNum(); $oSettings->setColsNum();
$this->assertEquals($oSettings->getColsNum(), 1); $this->assertEquals(1, $oSettings->getColsNum());
} }
public function testColumnsSpace() public function testColumnsSpace()
@ -210,14 +210,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
// Default // Default
$this->assertEquals($oSettings->getColsSpace(), 720); $this->assertEquals(720, $oSettings->getColsSpace());
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$this->assertInstanceOf('PHPWord_Section_Settings', $oSettings->setColsSpace($iVal)); $this->assertInstanceOf('PHPWord_Section_Settings', $oSettings->setColsSpace($iVal));
$this->assertEquals($oSettings->getColsSpace(), $iVal); $this->assertEquals($iVal, $oSettings->getColsSpace());
$this->assertInstanceOf('PHPWord_Section_Settings', $oSettings->setColsSpace()); $this->assertInstanceOf('PHPWord_Section_Settings', $oSettings->setColsSpace());
$this->assertEquals($oSettings->getColsSpace(), 1); $this->assertEquals(1, $oSettings->getColsSpace());
} }
public function testBreakType() public function testBreakType()
@ -225,12 +225,12 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
// Section Settings // Section Settings
$oSettings = new PHPWord_Section_Settings(); $oSettings = new PHPWord_Section_Settings();
$this->assertEquals($oSettings->getBreakType(), null); $this->assertNull($oSettings->getBreakType());
$oSettings->setBreakType('continuous'); $oSettings->setBreakType('continuous');
$this->assertEquals($oSettings->getBreakType(), 'continuous'); $this->assertEquals('continuous', $oSettings->getBreakType());
$oSettings->setBreakType(); $oSettings->setBreakType();
$this->assertEquals($oSettings->getBreakType(), null); $this->assertNull($oSettings->getBreakType());
} }
} }

View File

@ -11,9 +11,9 @@ class TextTest extends \PHPUnit_Framework_TestCase
$oText = new PHPWord_Section_Text(); $oText = new PHPWord_Section_Text();
$this->assertInstanceOf('PHPWord_Section_Text', $oText); $this->assertInstanceOf('PHPWord_Section_Text', $oText);
$this->assertEquals($oText->getText(), null); $this->assertEquals(null, $oText->getText());
$this->assertEquals($oText->getFontStyle(), null); $this->assertInstanceOf('PHPWord_Style_Font', $oText->getFontStyle());
$this->assertEquals($oText->getParagraphStyle(), null); $this->assertInstanceOf('PHPWord_Style_Paragraph', $oText->getParagraphStyle());
} }
public function testText() public function testText()