Skip to content

Commit 63a535b

Browse files
committed
Merge pull request #94 from ivanlanin/develop
Unit tests for Style_Font and Style_Cell
2 parents 9bae85e + 712e09a commit 63a535b

File tree

11 files changed

+262
-44
lines changed

11 files changed

+262
-44
lines changed

Classes/PHPWord/Shared/Drawing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static function pixelsToCentimeters($pValue = 0)
128128
public static function centimetersToPixels($pValue = 0)
129129
{
130130
if ($pValue != 0) {
131-
return $pValue * 0.028;
131+
return $pValue / 0.028;
132132
} else {
133133
return 0;
134134
}

Classes/PHPWord/Style/Cell.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,6 @@ public function setBgColor($pValue = null)
198198
$this->_bgColor = $pValue;
199199
}
200200

201-
public function setHeight($pValue = null)
202-
{
203-
$this->_height = $pValue;
204-
}
205-
206201
public function setBorderSize($pValue = null)
207202
{
208203
$this->_borderTopSize = $pValue;

Classes/PHPWord/Style/Font.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function __construct($type = 'text', $styleParagraph = null)
192192
*/
193193
public function setStyleValue($key, $value)
194194
{
195-
$method = 'set' . ucwords(substr($key, 1));
195+
$method = 'set' . substr($key, 1);
196196
if (method_exists($this, $method)) {
197197
$this->$method($value);
198198
}

Classes/PHPWord/Style/Paragraph.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,21 @@ public function __construct()
145145
/**
146146
* Set Style value
147147
*
148-
* @param string $key
149-
* @param mixed $value
148+
* @param string $key
149+
* @param mixed $value
150150
*/
151151
public function setStyleValue($key, $value)
152152
{
153-
if ($key == '_indent') {
154-
$value = $value * 720; // 720 twips per indent
155-
}
156-
if ($key == '_hanging') {
153+
if ($key == '_indent' || $key == '_hanging') {
157154
$value = $value * 720;
158155
}
159156
if ($key == '_spacing') {
160157
$value += 240; // because line height of 1 matches 240 twips
161158
}
162-
if ($key === '_tabs') {
163-
$value = new PHPWord_Style_Tabs($value);
159+
$method = 'set' . substr($key, 1);
160+
if (method_exists($this, $method)) {
161+
$this->$method($value);
164162
}
165-
$this->$key = $value;
166163
}
167164

168165
/**
@@ -311,6 +308,20 @@ public function getTabs()
311308
return $this->_tabs;
312309
}
313310

311+
/*
312+
* Set tabs
313+
*
314+
* @param array $pValue
315+
* @return PHPWord_Style_Paragraph
316+
*/
317+
public function setTabs($pValue = null)
318+
{
319+
if (is_array($pValue)) {
320+
$this->_tabs = new PHPWord_Style_Tabs($pValue);
321+
}
322+
return $this;
323+
}
324+
314325
/**
315326
* Get parent style ID
316327
*
@@ -374,7 +385,7 @@ public function getWidowControl()
374385
public function setWidowControl($pValue = true)
375386
{
376387
if (!is_bool($pValue)) {
377-
$pValue = false;
388+
$pValue = true;
378389
}
379390
$this->_widowControl = $pValue;
380391
return $this;
@@ -396,7 +407,7 @@ public function getKeepNext()
396407
* @param bool $pValue
397408
* @return PHPWord_Style_Paragraph
398409
*/
399-
public function setKeepNext($pValue = true)
410+
public function setKeepNext($pValue = false)
400411
{
401412
if (!is_bool($pValue)) {
402413
$pValue = false;
@@ -421,7 +432,7 @@ public function getKeepLines()
421432
* @param bool $pValue
422433
* @return PHPWord_Style_Paragraph
423434
*/
424-
public function setKeepLines($pValue = true)
435+
public function setKeepLines($pValue = false)
425436
{
426437
if (!is_bool($pValue)) {
427438
$pValue = false;
@@ -446,7 +457,7 @@ public function getPageBreakBefore()
446457
* @param bool $pValue
447458
* @return PHPWord_Style_Paragraph
448459
*/
449-
public function setPageBreakBefore($pValue = true)
460+
public function setPageBreakBefore($pValue = false)
450461
{
451462
if (!is_bool($pValue)) {
452463
$pValue = false;

Classes/PHPWord/Writer/ODText/Content.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function writeContent(PHPWord $pPHPWord = null)
115115
$numPStyles++;
116116

117117
$pPHPWord->addParagraphStyle('P' . $numPStyles, array());
118-
$element->setParagraph('P' . $numPStyles);
118+
$element->setParagraphStyle('P' . $numPStyles);
119119
}
120120
}
121121
}
@@ -338,4 +338,15 @@ protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
338338
private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section)
339339
{
340340
}
341+
342+
/**
343+
* Dummy function just to make all samples produce ODT
344+
*
345+
* @todo Create the real function
346+
*/
347+
private function _writeSection(
348+
PHPWord_Shared_XMLWriter $objWriter = null,
349+
PHPWord_Section $section)
350+
{
351+
}
341352
}

README.md

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ the following lines to your ``composer.json``.
3838
2. [Sections](#sections)
3939
* [Section settings](#section-settings)
4040
* [Section page numbering](#section-page-numbering)
41-
3. [Tables](#tables)
41+
3. [Texts](#texts)
42+
4. [Tables](#tables)
4243
* [Cell Style](#tables-cell-style)
43-
4. [Images](#images)
44+
5. [Images](#images)
4445

4546
<a name="basic-usage"></a>
4647
#### Basic usage
@@ -50,29 +51,33 @@ The following is a basic example of the PHPWord library.
5051
```php
5152
$PHPWord = new PHPWord();
5253

53-
// Every element you want to append to the word document is placed in a section. So you need a section:
54+
// Every element you want to append to the word document is placed in a section.
55+
// To create a basic section:
5456
$section = $PHPWord->createSection();
5557

5658
// After creating a section, you can append elements:
5759
$section->addText('Hello world!');
5860

5961
// You can directly style your text by giving the addText function an array:
60-
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
62+
$section->addText('Hello world! I am formatted.',
63+
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
6164

62-
// If you often need the same style again you can create a user defined style to the word document
63-
// and give the addText function the name of the style:
64-
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
65-
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');
65+
// If you often need the same style again you can create a user defined style
66+
// to the word document and give the addText function the name of the style:
67+
$PHPWord->addFontStyle('myOwnStyle',
68+
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
69+
$section->addText('Hello world! I am formatted by a user defined style',
70+
'myOwnStyle');
6671

67-
// You can also putthe appended element to local object an call functions like this:
72+
// You can also put the appended element to local object like this:
6873
$fontStyle = new PHPWord_Style_Font();
6974
$fontStyle->setBold(true);
7075
$fontStyle->setName('Verdana');
7176
$fontStyle->setSize(22);
7277
$myTextElement = $section->addText('Hello World!');
7378
$myTextElement->setFontStyle($fontStyle);
7479

75-
// At least write the document to webspace:
80+
// Finally, write the document:
7681
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
7782
$objWriter->save('helloWorld.docx');
7883
```
@@ -154,6 +159,27 @@ $section = $PHPWord->createSection();
154159
$section->getSettings()->setPageNumberingStart(1);
155160
```
156161

162+
<a name="texts"></a>
163+
#### Texts
164+
165+
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.
166+
167+
`addText` sample:
168+
169+
```php
170+
$fontStyle = array('name' => 'Times New Roman', 'size' => 9);
171+
$paragraphStyle = array('align' => 'both');
172+
$section->addText('I am simple paragraph', $fontStyle, $paragraphStyle);
173+
```
174+
175+
`createTextRun` sample:
176+
177+
```php
178+
$textrun = $section->createTextRun();
179+
$textrun->addText('I am bold', array('bold' => true));
180+
$textrun->addText('I am italic, array('italic' => true));
181+
$textrun->addText('I am colored, array('color' => 'AACC00'));
182+
```
157183
<a name="tables"></a>
158184
#### Tables
159185

Tests/PHPWord/Style/CellTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Style_Cell;
6+
7+
/**
8+
* Class PHPWord_Style_CellTest
9+
*
10+
* @package PHPWord\Tests
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class PHPWord_Style_CellTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* Test setting style with normal value
18+
*/
19+
public function testSetGetNormal()
20+
{
21+
$object = new PHPWord_Style_Cell();
22+
23+
$attributes = array(
24+
'valign' => 'left',
25+
'textDirection' => PHPWord_Style_Cell::TEXT_DIR_BTLR,
26+
'bgColor' => 'FFFF00',
27+
'borderTopSize' => 120,
28+
'borderTopColor' => 'FFFF00',
29+
'borderLeftSize' => 120,
30+
'borderLeftColor' => 'FFFF00',
31+
'borderRightSize' => 120,
32+
'borderRightColor' => 'FFFF00',
33+
'borderBottomSize' => 120,
34+
'borderBottomColor' => 'FFFF00',
35+
'gridSpan' => 2,
36+
'vMerge' => 2,
37+
);
38+
//'defaultBorderColor' => null,
39+
foreach ($attributes as $key => $value) {
40+
$set = "set{$key}";
41+
$get = "get{$key}";
42+
$object->$set($value);
43+
$this->assertEquals($value, $object->$get());
44+
}
45+
}
46+
47+
/**
48+
* Test border color
49+
*/
50+
public function testBorderColor()
51+
{
52+
$object = new PHPWord_Style_Cell();
53+
54+
$default = '000000';
55+
$value = 'FF0000';
56+
57+
$this->assertEquals($default, $object->getDefaultBorderColor());
58+
59+
$object->setStyleValue('_defaultBorderColor', $value);
60+
$this->assertEquals($value, $object->getDefaultBorderColor());
61+
62+
$object->setStyleValue('_borderColor', $value);
63+
$expected = array($value, $value, $value, $value);
64+
$this->assertEquals($expected, $object->getBorderColor());
65+
}
66+
67+
/**
68+
* Test border size
69+
*/
70+
public function testBorderSize()
71+
{
72+
$object = new PHPWord_Style_Cell();
73+
74+
$value = 120;
75+
$expected = array($value, $value, $value, $value);
76+
$object->setStyleValue('_borderSize', $value);
77+
$this->assertEquals($expected, $object->getBorderSize());
78+
}
79+
80+
}

Tests/PHPWord/Style/FontTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public function testSetStyleValueWithNullOrEmpty()
4545
'fgColor' => null,
4646
);
4747
foreach ($attributes as $key => $default) {
48-
$method = 'get' . ucwords($key);
48+
$get = "get{$key}";
4949
$object->setStyleValue("_$key", null);
50-
$this->assertEquals($default, $object->$method());
50+
$this->assertEquals($default, $object->$get());
5151
$object->setStyleValue("_$key", '');
52-
$this->assertEquals($default, $object->$method());
52+
$this->assertEquals($default, $object->$get());
5353
}
5454
}
5555

@@ -73,9 +73,9 @@ public function testSetStyleValueNormal()
7373
'fgColor' => '999999',
7474
);
7575
foreach ($attributes as $key => $value) {
76-
$method = 'get' . ucwords($key);
76+
$get = "get{$key}";
7777
$object->setStyleValue("_$key", $value);
78-
$this->assertEquals($value, $object->$method());
78+
$this->assertEquals($value, $object->$get());
7979
}
8080
}
8181

0 commit comments

Comments
 (0)