Additional unit tests and some code deduplication

This commit is contained in:
Ivan Lanin 2014-04-11 21:16:07 +07:00
parent ae652a6379
commit a3a9af51e5
12 changed files with 96 additions and 74 deletions

View File

@ -66,9 +66,6 @@ class Section extends AbstractElement
if (is_null($value)) {
continue;
}
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$this->settings->setSettingValue($key, $value);
}
}

View File

@ -77,6 +77,23 @@ class String
return $value;
}
/**
* Return name without underscore for < 0.10.0 variable name compatibility
*
* @param string $value
* @return string
*/
public static function removeUnderscorePrefix($value)
{
if (!is_null($value)) {
if (substr($value, 0, 1) == '_') {
$value = substr($value, 1);
}
}
return $value;
}
/**
* Build control characters array
*/

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/**
* Abstract style class
*
@ -50,7 +52,10 @@ abstract class AbstractStyle
/**
* Set style value template method
*
* Some child classes have their own specific overrides
* Some child classes have their own specific overrides.
* Backward compability check for versions < 0.10.0 which use underscore
* prefix for their private properties.
* Check if the set method is exists. Throws an exception?
*
* @param string $key
* @param string $value
@ -58,14 +63,7 @@ abstract class AbstractStyle
*/
public function setStyleValue($key, $value)
{
// Backward compability check for versions < 0.10.0 which use underscore
// prefix for their private properties
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
// Check if the set method is exists. Throws an exception?
$method = 'set' . $key;
$method = 'set' . String::removeUnderscorePrefix($key);
if (method_exists($this, $method)) {
$this->$method($value);
}

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/**
* Table cell style
*/
@ -145,9 +147,7 @@ class Cell extends AbstractStyle
*/
public function setStyleValue($key, $value)
{
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$key = String::removeUnderscorePrefix($key);
if ($key == 'borderSize') {
$this->setBorderSize($value);
} elseif ($key == 'borderColor') {

View File

@ -193,8 +193,6 @@ class Font extends AbstractStyle
if ($key === 'line-height') {
$this->setLineHeight($value);
null;
} elseif (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$this->setStyleValue($key, $value);
}

View File

@ -10,6 +10,7 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Exception\InvalidStyleException;
use PhpOffice\PhpWord\Shared\String;
/**
* Paragraph style
@ -127,8 +128,6 @@ class Paragraph extends AbstractStyle
foreach ($style as $key => $value) {
if ($key === 'line-height') {
null;
} elseif (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$this->setStyleValue($key, $value);
}
@ -144,9 +143,7 @@ class Paragraph extends AbstractStyle
*/
public function setStyleValue($key, $value)
{
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$key = String::removeUnderscorePrefix($key);
if ($key == 'indent' || $key == 'hanging') {
$value = $value * 720;
} elseif ($key == 'spacing') {

View File

@ -45,16 +45,12 @@ class Row extends AbstractStyle
/**
* Set tblHeader
*
* @param boolean $pValue
* @return $this
* @param boolean $value
* @return self
*/
public function setTblHeader($pValue = false)
public function setTblHeader($value = false)
{
if (!is_bool($pValue)) {
$pValue = false;
}
$this->tblHeader = $pValue;
return $this;
$this->tblHeader = $this->setBoolVal($value, $this->tblHeader);
}
/**
@ -70,16 +66,12 @@ class Row extends AbstractStyle
/**
* Set cantSplit
*
* @param boolean $pValue
* @return $this
* @param boolean $value
* @return self
*/
public function setCantSplit($pValue = false)
public function setCantSplit($value = false)
{
if (!is_bool($pValue)) {
$pValue = false;
}
$this->cantSplit = $pValue;
return $this;
$this->cantSplit = $this->setBoolVal($value, $this->cantSplit);
}
/**
@ -95,15 +87,12 @@ class Row extends AbstractStyle
/**
* Set exactHeight
*
* @param bool $pValue
* @return $this
* @param bool $value
* @return self
*/
public function setExactHeight($pValue = false)
public function setExactHeight($value = false)
{
if (!is_bool($pValue)) {
$pValue = false;
}
$this->exactHeight = $pValue;
$this->exactHeight = $this->setBoolVal($value, $this->exactHeight);
return $this;
}

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/**
* Section settings
*/
@ -217,9 +219,7 @@ class Section extends AbstractStyle
*/
public function setSettingValue($key, $value)
{
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$key = String::removeUnderscorePrefix($key);
if ($key == 'orientation' && $value == 'landscape') {
$this->setLandscape();
} elseif ($key == 'orientation' && is_null($value)) {

View File

@ -110,15 +110,4 @@ class TOC extends AbstractStyle
{
$this->indent = $pValue;
}
/**
* Set style value
*
* @param string $key
* @param string $value
*/
public function setStyleValue($key, $value)
{
$this->$key = $value;
}
}

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/**
* Table style
*/
@ -161,18 +163,12 @@ class Table extends AbstractStyle
unset($this->firstRow->borderInsideHColor);
unset($this->firstRow->borderInsideHSize);
foreach ($styleFirstRow as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$this->firstRow->setStyleValue($key, $value);
}
}
if (!is_null($styleTable) && is_array($styleTable)) {
foreach ($styleTable as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$this->setStyleValue($key, $value);
}
}
@ -186,9 +182,7 @@ class Table extends AbstractStyle
*/
public function setStyleValue($key, $value)
{
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$key = String::removeUnderscorePrefix($key);
if ($key == 'borderSize') {
$this->setBorderSize($value);
} elseif ($key == 'borderColor') {

View File

@ -82,9 +82,6 @@ class TOC
if (!is_null($styleTOC) && is_array($styleTOC)) {
foreach ($styleTOC as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
self::$TOCStyle->setStyleValue($key, $value);
}
}
@ -93,9 +90,6 @@ class TOC
if (is_array($styleFont)) {
self::$fontStyle = new Font();
foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
self::$fontStyle->setStyleValue($key, $value);
}
} else {

View File

@ -0,0 +1,49 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Tests\Style;
use PhpOffice\PhpWord\Style\NumberingLevel;
/**
* Test class for PhpOffice\PhpWord\Style\NumberingLevel
*
* @runTestsInSeparateProcesses
*/
class NumberingLevelTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style with normal value
*/
public function testSetGetNormal()
{
$object = new NumberingLevel();
$attributes = array(
'level' => 1,
'start' => 1,
'format' => 'decimal',
'restart' => 1,
'suffix' => 'space',
'text' => '%1.',
'align' => 'left',
'left' => 360,
'hanging' => 360,
'tabPos' => 360,
'font' => 'Arial',
'hint' => 'default',
);
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
}
}