New function PHPWord_Style_Paragraph::setTabs() and unit test for PHPWord_Style_Paragraph

This commit is contained in:
Ivan Lanin 2014-03-09 17:47:04 +07:00
parent 537f49eeb4
commit d177b4744b
3 changed files with 115 additions and 13 deletions

View File

@ -150,19 +150,16 @@ class PHPWord_Style_Paragraph
*/
public function setStyleValue($key, $value)
{
if ($key == '_indent') {
$value = $value * 720; // 720 twips per indent
}
if ($key == '_hanging') {
if ($key == '_indent' || $key == '_hanging') {
$value = $value * 720;
}
if ($key == '_spacing') {
$value += 240; // because line height of 1 matches 240 twips
}
if ($key === '_tabs') {
$value = new PHPWord_Style_Tabs($value);
$method = 'set' . ucwords(substr($key, 1));
if (method_exists($this, $method)) {
$this->$method($value);
}
$this->$key = $value;
}
/**
@ -311,6 +308,20 @@ class PHPWord_Style_Paragraph
return $this->_tabs;
}
/*
* Set tabs
*
* @param array $pValue
* @return PHPWord_Style_Paragraph
*/
public function setTabs($pValue = null)
{
if (is_array($pValue)) {
$this->_tabs = new PHPWord_Style_Tabs($pValue);
}
return $this;
}
/**
* Get parent style ID
*
@ -374,7 +385,7 @@ class PHPWord_Style_Paragraph
public function setWidowControl($pValue = true)
{
if (!is_bool($pValue)) {
$pValue = false;
$pValue = true;
}
$this->_widowControl = $pValue;
return $this;
@ -396,7 +407,7 @@ class PHPWord_Style_Paragraph
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setKeepNext($pValue = true)
public function setKeepNext($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;
@ -421,7 +432,7 @@ class PHPWord_Style_Paragraph
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setKeepLines($pValue = true)
public function setKeepLines($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;
@ -446,7 +457,7 @@ class PHPWord_Style_Paragraph
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setPageBreakBefore($pValue = true)
public function setPageBreakBefore($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;

View File

@ -0,0 +1,90 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_Paragraph;
use PHPWord_Style_Tab;
/**
* Class PHPWord_Style_ParagraphTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class PHPWord_Style_ParagraphTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style values with null or empty value
*/
public function testSetStyleValueWithNullOrEmpty()
{
$object = new PHPWord_Style_Paragraph();
$attributes = array(
'tabs' => null,
'widowControl' => true,
'keepNext' => false,
'keepLines' => false,
'pageBreakBefore' => false,
);
foreach ($attributes as $key => $default) {
$method = 'get' . ucwords($key);
$object->setStyleValue("_$key", null);
$this->assertEquals($default, $object->$method());
$object->setStyleValue("_$key", '');
$this->assertEquals($default, $object->$method());
}
}
/**
* Test setting style values with normal value
*/
public function testSetStyleValueNormal()
{
$object = new PHPWord_Style_Paragraph();
$attributes = array(
'align' => 'justify',
'spaceAfter' => 240,
'spaceBefore' => 240,
'indent' => 1,
'hanging' => 1,
'spacing' => 120,
'basedOn' => 'Normal',
'next' => 'Normal',
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
'pageBreakBefore' => true,
);
foreach ($attributes as $key => $value) {
$method = 'get' . ucwords($key);
$object->setStyleValue("_$key", $value);
if ($key == 'align') {
if ($value == 'justify') {
$value = 'both';
}
} elseif ($key == 'indent' || $key == 'hanging') {
$value = $value * 720;
} elseif ($key == 'spacing') {
$value += 240;
}
$this->assertEquals($value, $object->$method());
}
}
/**
* Test tabs
*/
public function testTabs()
{
$object = new PHPWord_Style_Paragraph();
$object->setTabs(array(
new PHPWord_Style_Tab('left', 1550),
new PHPWord_Style_Tab('right', 5300),
));
$this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs());
}
}

View File

@ -48,6 +48,7 @@ Changes in branch for release 0.7.1 :
- General: (ivanlanin) GH-93 - General: PHPWord_Style_Font refactoring
- General: (ivanlanin) GH-93 - Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing.
- Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion
- Feature: (ivanlanin) - Paragraph: setTabs() function
- QA: (Progi1984) - UnitTests
Changes in branch for release 0.7.0 :