Skip to content

Unit tests for Style_Font and Style_Cell #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 9, 2014
Merged
2 changes: 1 addition & 1 deletion Classes/PHPWord/Shared/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static function pixelsToCentimeters($pValue = 0)
public static function centimetersToPixels($pValue = 0)
{
if ($pValue != 0) {
return $pValue * 0.028;
return $pValue / 0.028;
} else {
return 0;
}
Expand Down
5 changes: 0 additions & 5 deletions Classes/PHPWord/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,6 @@ public function setBgColor($pValue = null)
$this->_bgColor = $pValue;
}

public function setHeight($pValue = null)
{
$this->_height = $pValue;
}

public function setBorderSize($pValue = null)
{
$this->_borderTopSize = $pValue;
Expand Down
2 changes: 1 addition & 1 deletion Classes/PHPWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function __construct($type = 'text', $styleParagraph = null)
*/
public function setStyleValue($key, $value)
{
$method = 'set' . ucwords(substr($key, 1));
$method = 'set' . substr($key, 1);
if (method_exists($this, $method)) {
$this->$method($value);
}
Expand Down
37 changes: 24 additions & 13 deletions Classes/PHPWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,21 @@ public function __construct()
/**
* 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;
}

/**
Expand Down Expand Up @@ -311,6 +308,20 @@ public function getTabs()
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
*
Expand Down Expand Up @@ -374,7 +385,7 @@ public function getWidowControl()
public function setWidowControl($pValue = true)
{
if (!is_bool($pValue)) {
$pValue = false;
$pValue = true;
}
$this->_widowControl = $pValue;
return $this;
Expand All @@ -396,7 +407,7 @@ public function getKeepNext()
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setKeepNext($pValue = true)
public function setKeepNext($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;
Expand All @@ -421,7 +432,7 @@ public function getKeepLines()
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setKeepLines($pValue = true)
public function setKeepLines($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;
Expand All @@ -446,7 +457,7 @@ public function getPageBreakBefore()
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setPageBreakBefore($pValue = true)
public function setPageBreakBefore($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;
Expand Down
13 changes: 12 additions & 1 deletion Classes/PHPWord/Writer/ODText/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function writeContent(PHPWord $pPHPWord = null)
$numPStyles++;

$pPHPWord->addParagraphStyle('P' . $numPStyles, array());
$element->setParagraph('P' . $numPStyles);
$element->setParagraphStyle('P' . $numPStyles);
}
}
}
Expand Down Expand Up @@ -338,4 +338,15 @@ protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
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)
{
}
}
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,29 +51,33 @@ 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');
$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');
```
Expand Down Expand Up @@ -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

Expand Down
80 changes: 80 additions & 0 deletions Tests/PHPWord/Style/CellTest.php
Original file line number Diff line number Diff line change
@@ -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());
}

}
10 changes: 5 additions & 5 deletions Tests/PHPWord/Style/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public function testSetStyleValueWithNullOrEmpty()
'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());
}
}

Expand All @@ -73,9 +73,9 @@ public function testSetStyleValueNormal()
'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());
}
}

Expand Down
Loading