Merge pull request #94 from ivanlanin/develop

Unit tests for Style_Font and Style_Cell
This commit is contained in:
Progi1984 2014-03-09 18:40:44 +01:00
commit 63a535bf1a
11 changed files with 262 additions and 44 deletions

View File

@ -128,7 +128,7 @@ class PHPWord_Shared_Drawing
public static function centimetersToPixels($pValue = 0)
{
if ($pValue != 0) {
return $pValue * 0.028;
return $pValue / 0.028;
} else {
return 0;
}

View File

@ -198,11 +198,6 @@ class PHPWord_Style_Cell
$this->_bgColor = $pValue;
}
public function setHeight($pValue = null)
{
$this->_height = $pValue;
}
public function setBorderSize($pValue = null)
{
$this->_borderTopSize = $pValue;

View File

@ -192,7 +192,7 @@ class PHPWord_Style_Font
*/
public function setStyleValue($key, $value)
{
$method = 'set' . ucwords(substr($key, 1));
$method = 'set' . substr($key, 1);
if (method_exists($this, $method)) {
$this->$method($value);
}

View File

@ -145,24 +145,21 @@ class PHPWord_Style_Paragraph
/**
* Set Style value
*
* @param string $key
* @param mixed $value
* @param string $key
* @param mixed $value
*/
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' . 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

@ -115,7 +115,7 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
$numPStyles++;
$pPHPWord->addParagraphStyle('P' . $numPStyles, array());
$element->setParagraph('P' . $numPStyles);
$element->setParagraphStyle('P' . $numPStyles);
}
}
}
@ -338,4 +338,15 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section)
{
}
/**
* Dummy function just to make all samples produce ODT
*
* @todo Create the real function
*/
private function _writeSection(
PHPWord_Shared_XMLWriter $objWriter = null,
PHPWord_Section $section)
{
}
}

View File

@ -38,9 +38,10 @@ the following lines to your ``composer.json``.
2. [Sections](#sections)
* [Section settings](#section-settings)
* [Section page numbering](#section-page-numbering)
3. [Tables](#tables)
3. [Texts](#texts)
4. [Tables](#tables)
* [Cell Style](#tables-cell-style)
4. [Images](#images)
5. [Images](#images)
<a name="basic-usage"></a>
#### Basic usage
@ -50,21 +51,25 @@ The following is a basic example of the PHPWord library.
```php
$PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section. So you need a section:
// Every element you want to append to the word document is placed in a section.
// To create a basic section:
$section = $PHPWord->createSection();
// After creating a section, you can append elements:
$section->addText('Hello world!');
// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
$section->addText('Hello world! I am formatted.',
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');
// If you often need the same style again you can create a user defined style
// to the word document and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle',
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style',
'myOwnStyle');
// You can also putthe appended element to local object an call functions like this:
// You can also put the appended element to local object like this:
$fontStyle = new PHPWord_Style_Font();
$fontStyle->setBold(true);
$fontStyle->setName('Verdana');
@ -72,7 +77,7 @@ $fontStyle->setSize(22);
$myTextElement = $section->addText('Hello World!');
$myTextElement->setFontStyle($fontStyle);
// At least write the document to webspace:
// Finally, write the document:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');
```
@ -154,6 +159,27 @@ $section = $PHPWord->createSection();
$section->getSettings()->setPageNumberingStart(1);
```
<a name="texts"></a>
#### Texts
Text can be added by using `addText` and `createTextRun` method. `addText` is used for creating simple paragraphs that only contain texts with the same style. `createTextRun` is used for creating complex paragraphs that contain text with different style (some bold, other italics, etc) or other elements, e.g. images or links.
`addText` sample:
```php
$fontStyle = array('name' => 'Times New Roman', 'size' => 9);
$paragraphStyle = array('align' => 'both');
$section->addText('I am simple paragraph', $fontStyle, $paragraphStyle);
```
`createTextRun` sample:
```php
$textrun = $section->createTextRun();
$textrun->addText('I am bold', array('bold' => true));
$textrun->addText('I am italic, array('italic' => true));
$textrun->addText('I am colored, array('color' => 'AACC00'));
```
<a name="tables"></a>
#### Tables

View File

@ -0,0 +1,80 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_Cell;
/**
* Class PHPWord_Style_CellTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class PHPWord_Style_CellTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style with normal value
*/
public function testSetGetNormal()
{
$object = new PHPWord_Style_Cell();
$attributes = array(
'valign' => 'left',
'textDirection' => PHPWord_Style_Cell::TEXT_DIR_BTLR,
'bgColor' => 'FFFF00',
'borderTopSize' => 120,
'borderTopColor' => 'FFFF00',
'borderLeftSize' => 120,
'borderLeftColor' => 'FFFF00',
'borderRightSize' => 120,
'borderRightColor' => 'FFFF00',
'borderBottomSize' => 120,
'borderBottomColor' => 'FFFF00',
'gridSpan' => 2,
'vMerge' => 2,
);
//'defaultBorderColor' => null,
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
}
/**
* Test border color
*/
public function testBorderColor()
{
$object = new PHPWord_Style_Cell();
$default = '000000';
$value = 'FF0000';
$this->assertEquals($default, $object->getDefaultBorderColor());
$object->setStyleValue('_defaultBorderColor', $value);
$this->assertEquals($value, $object->getDefaultBorderColor());
$object->setStyleValue('_borderColor', $value);
$expected = array($value, $value, $value, $value);
$this->assertEquals($expected, $object->getBorderColor());
}
/**
* Test border size
*/
public function testBorderSize()
{
$object = new PHPWord_Style_Cell();
$value = 120;
$expected = array($value, $value, $value, $value);
$object->setStyleValue('_borderSize', $value);
$this->assertEquals($expected, $object->getBorderSize());
}
}

View File

@ -45,11 +45,11 @@ class PHPWord_Style_FontTest extends \PHPUnit_Framework_TestCase
'fgColor' => null,
);
foreach ($attributes as $key => $default) {
$method = 'get' . ucwords($key);
$get = "get{$key}";
$object->setStyleValue("_$key", null);
$this->assertEquals($default, $object->$method());
$this->assertEquals($default, $object->$get());
$object->setStyleValue("_$key", '');
$this->assertEquals($default, $object->$method());
$this->assertEquals($default, $object->$get());
}
}
@ -73,9 +73,9 @@ class PHPWord_Style_FontTest extends \PHPUnit_Framework_TestCase
'fgColor' => '999999',
);
foreach ($attributes as $key => $value) {
$method = 'get' . ucwords($key);
$get = "get{$key}";
$object->setStyleValue("_$key", $value);
$this->assertEquals($value, $object->$method());
$this->assertEquals($value, $object->$get());
}
}

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) {
$get = "get{$key}";
$object->setStyleValue("_$key", null);
$this->assertEquals($default, $object->$get());
$object->setStyleValue("_$key", '');
$this->assertEquals($default, $object->$get());
}
}
/**
* 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) {
$get = "get{$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->$get());
}
}
/**
* 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

@ -88,10 +88,10 @@ class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase {
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$attributes = array(
'widowControl' => 0,
'keepNext' => 1,
'keepLines' => 1,
'pageBreakBefore' => 1,
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
'pageBreakBefore' => true,
);
foreach ($attributes as $attribute => $value) {
$section->addText('Test', null, array($attribute => $value));
@ -100,11 +100,12 @@ class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase {
// Test the attributes
$i = 0;
foreach ($attributes as $attribute => $value) {
foreach ($attributes as $key => $value) {
$i++;
$path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$attribute}";
$path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$key}";
$element = $doc->getElement($path);
$this->assertEquals($value, $element->getAttribute('w:val'));
$expected = $value ? 1 : 0;
$this->assertEquals($expected, $element->getAttribute('w:val'));
}
}

View File

@ -44,7 +44,11 @@ Changes in branch for release 0.7.1 :
- Feature: (ivanlanin) GH-87 - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next)
- Feature: (jeroenmoors) GH-44 GH-88 - Clone table rows on the fly when using a template document
- Feature: (deds) GH-16 - Initial addition of basic footnote support
- Feature: (ivanlanin) GH-91 - Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before
- Feature: (ivanlanin) GH-92 - Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before
- 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 :