Skip to content

Commit 1aa83b9

Browse files
committed
Some more tests for Writer/Word2007/Base.php. We're on 71.84% coverage now :)
1 parent fd95a1c commit 1aa83b9

File tree

7 files changed

+146
-76
lines changed

7 files changed

+146
-76
lines changed

Classes/PHPWord/Section/MemoryImage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,4 @@ public function getImageExtension()
229229
{
230230
return $this->_imageExtension;
231231
}
232-
}
232+
}

Classes/PHPWord/Settings.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
/**
2929
* PHPWord_Settings
3030
*/
31-
class PHPWord_Settings {
31+
class PHPWord_Settings
32+
{
3233
/**
3334
* Compatibility option for XMLWriter
3435
*
@@ -42,12 +43,13 @@ class PHPWord_Settings {
4243
* @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility
4344
* @return boolean Success or failure
4445
*/
45-
public static function setCompatibility($compatibility) {
46+
public static function setCompatibility($compatibility)
47+
{
4648
if (is_bool($compatibility)) {
4749
self::$_xmlWriterCompatibility = $compatibility;
48-
return TRUE;
50+
return true;
4951
}
50-
return FALSE;
52+
return false;
5153
} // function setCompatibility()
5254

5355

@@ -56,7 +58,8 @@ public static function setCompatibility($compatibility) {
5658
*
5759
* @return boolean Compatibility
5860
*/
59-
public static function getCompatibility() {
61+
public static function getCompatibility()
62+
{
6063
return self::$_xmlWriterCompatibility;
6164
} // function getCompatibility()
62-
}
65+
}

Classes/PHPWord/Writer/RTF.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,22 @@ private function getDataContent()
314314
$sRTFBody .= $this->getDataContentTextBreak();
315315
} elseif ($element instanceof PHPWord_Section_TextRun) {
316316
$sRTFBody .= $this->getDataContentTextRun($element);
317-
} elseif($element instanceof PHPWord_Section_Link) {
317+
} elseif ($element instanceof PHPWord_Section_Link) {
318318
$sRTFBody .= $this->getDataContentUnsupportedElement('link');
319-
} elseif($element instanceof PHPWord_Section_Title) {
319+
} elseif ($element instanceof PHPWord_Section_Title) {
320320
$sRTFBody .= $this->getDataContentUnsupportedElement('title');
321-
} elseif($element instanceof PHPWord_Section_PageBreak) {
321+
} elseif ($element instanceof PHPWord_Section_PageBreak) {
322322
$sRTFBody .= $this->getDataContentUnsupportedElement('page break');
323-
} elseif($element instanceof PHPWord_Section_Table) {
323+
} elseif ($element instanceof PHPWord_Section_Table) {
324324
$sRTFBody .= $this->getDataContentUnsupportedElement('table');
325-
} elseif($element instanceof PHPWord_Section_ListItem) {
325+
} elseif ($element instanceof PHPWord_Section_ListItem) {
326326
$sRTFBody .= $this->getDataContentUnsupportedElement('list item');
327-
} elseif($element instanceof PHPWord_Section_Image ||
327+
} elseif ($element instanceof PHPWord_Section_Image ||
328328
$element instanceof PHPWord_Section_MemoryImage) {
329329
$sRTFBody .= $this->getDataContentUnsupportedElement('image');
330-
} elseif($element instanceof PHPWord_Section_Object) {
330+
} elseif ($element instanceof PHPWord_Section_Object) {
331331
$sRTFBody .= $this->getDataContentUnsupportedElement('object');
332-
} elseif($element instanceof PHPWord_TOC) {
332+
} elseif ($element instanceof PHPWord_TOC) {
333333
$sRTFBody .= $this->getDataContentUnsupportedElement('TOC');
334334
} else {
335335
$sRTFBody .= $this->getDataContentUnsupportedElement('other');

Classes/PHPWord/Writer/Word2007/Base.php

+65-13
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@
2929
/**
3030
* Class PHPWord_Writer_Word2007_Base
3131
*/
32-
class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
32+
class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
33+
{
3334

34-
protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) {
35+
/**
36+
* Write text
37+
*/
38+
protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false)
39+
{
3540
$styleFont = $text->getFontStyle();
3641

3742
$SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
@@ -80,7 +85,11 @@ protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWor
8085
}
8186
}
8287

83-
protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) {
88+
/**
89+
* Write text run
90+
*/
91+
protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun)
92+
{
8493
$elements = $textrun->getElements();
8594
$styleParagraph = $textrun->getParagraphStyle();
8695

@@ -221,7 +230,11 @@ protected function _writeParagraphStyle(
221230
}
222231
}
223232

224-
protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false) {
233+
/**
234+
* Write link
235+
*/
236+
protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false)
237+
{
225238
$rID = $link->getRelationId();
226239
$linkName = $link->getLinkName();
227240
if (is_null($linkName)) {
@@ -276,7 +289,11 @@ protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWor
276289
}
277290
}
278291

279-
protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun) {
292+
/**
293+
* Write preserve text
294+
*/
295+
protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun)
296+
{
280297
$styleFont = $textrun->getFontStyle();
281298
$styleParagraph = $textrun->getParagraphStyle();
282299

@@ -367,7 +384,11 @@ protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null
367384
$objWriter->endElement(); // p
368385
}
369386

370-
protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style) {
387+
/**
388+
* Write text style
389+
*/
390+
protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style)
391+
{
371392
$font = $style->getName();
372393
$bold = $style->getBold();
373394
$italic = $style->getItalic();
@@ -454,11 +475,19 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
454475
$objWriter->endElement();
455476
}
456477

457-
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) {
478+
/**
479+
* Write text break
480+
*/
481+
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
482+
{
458483
$objWriter->writeElement('w:p', null);
459484
}
460485

461-
protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) {
486+
/**
487+
* Write table
488+
*/
489+
protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table)
490+
{
462491
$_rows = $table->getRows();
463492
$_cRows = count($_rows);
464493

@@ -566,7 +595,11 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
566595
}
567596
}
568597

569-
protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null) {
598+
/**
599+
* Write table style
600+
*/
601+
protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null)
602+
{
570603
$margins = $style->getCellMargin();
571604
$mTop = (!is_null($margins[0])) ? true : false;
572605
$mLeft = (!is_null($margins[1])) ? true : false;
@@ -610,7 +643,11 @@ protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null,
610643
}
611644
}
612645

613-
protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) {
646+
/**
647+
* Write cell style
648+
*/
649+
protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null)
650+
{
614651
$bgColor = $style->getBgColor();
615652
$valign = $style->getVAlign();
616653
$textDir = $style->getTextDirection();
@@ -716,7 +753,8 @@ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
716753
* @param \PHPWord_Shared_XMLWriter $objWriter
717754
* @param \PHPWord_Section_Image $image
718755
*/
719-
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false) {
756+
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false)
757+
{
720758
$rId = $image->getRelationId();
721759

722760
$style = $image->getStyle();
@@ -792,7 +830,11 @@ protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
792830
}
793831
}
794832

795-
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) {
833+
/**
834+
* Write watermark
835+
*/
836+
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image)
837+
{
796838
$rId = $image->getRelationId();
797839

798840
$style = $image->getStyle();
@@ -835,7 +877,11 @@ protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $
835877
$objWriter->endElement();
836878
}
837879

838-
protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title) {
880+
/**
881+
* Write title
882+
*/
883+
protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title)
884+
{
839885
$text = htmlspecialchars($title->getText());
840886
$text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text);
841887
$anchor = $title->getAnchor();
@@ -876,6 +922,9 @@ protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
876922
$objWriter->endElement();
877923
}
878924

925+
/**
926+
* Write footnote
927+
*/
879928
protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote)
880929
{
881930
$objWriter->startElement('w:footnote');
@@ -911,6 +960,9 @@ protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PH
911960
$objWriter->endElement(); // w:footnote
912961
}
913962

963+
/**
964+
* Write footnote reference
965+
*/
914966
protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false)
915967
{
916968
if (!$withoutP) {

Tests/PHPWord/Writer/Word2007/BaseTest.php

+45-29
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testWriteText()
2828

2929
$PHPWord = new PHPWord();
3030
$PHPWord->addFontStyle($rStyle, array('bold' => true));
31-
$PHPWord->addParagraphStyle($pStyle, array('align' => 'justify'));
31+
$PHPWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
3232
$section = $PHPWord->createSection();
3333
$section->addText('Test', $rStyle, $pStyle);
3434
$doc = TestHelperDOCX::getDocument($PHPWord);
@@ -45,7 +45,7 @@ public function testWriteText()
4545
public function testWriteTextRun()
4646
{
4747
$pStyle = 'pStyle';
48-
$aStyle = array('align' => 'justify');
48+
$aStyle = array('align' => 'justify', 'spaceBefore' => 120, 'spaceAfter' => 120);
4949
$imageSrc = join(
5050
DIRECTORY_SEPARATOR,
5151
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'earth.jpg')
@@ -66,6 +66,41 @@ public function testWriteTextRun()
6666
$this->assertTrue($doc->elementExists("{$parent}/w:pPr/w:pStyle[@w:val='{$pStyle}']"));
6767
}
6868

69+
/**
70+
* Write link
71+
*/
72+
public function testWriteLink()
73+
{
74+
$PHPWord = new PHPWord();
75+
$section = $PHPWord->createSection();
76+
77+
$expected = 'PHPWord';
78+
$section->addLink('http://github.com/phpoffice/phpword', $expected);
79+
80+
$doc = TestHelperDOCX::getDocument($PHPWord);
81+
$element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t');
82+
83+
$this->assertEquals($expected, $element->nodeValue);
84+
}
85+
86+
/**
87+
* Write preserve text
88+
*/
89+
public function testWritePreserveText()
90+
{
91+
$PHPWord = new PHPWord();
92+
$section = $PHPWord->createSection();
93+
$footer = $section->createFooter();
94+
95+
$footer->addPreserveText('{PAGE}');
96+
97+
$doc = TestHelperDOCX::getDocument($PHPWord);
98+
$preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml');
99+
100+
$this->assertEquals('PAGE', $preserve->nodeValue);
101+
$this->assertEquals('preserve', $preserve->getAttribute('xml:space'));
102+
}
103+
69104
/**
70105
* Write paragraph style: Alignment
71106
*/
@@ -144,23 +179,6 @@ public function testWriteFontStyle()
144179
$this->assertEquals($styles['fgColor'], $doc->getElementAttribute("{$parent}/w:highlight", 'w:val'));
145180
}
146181

147-
/**
148-
* Write link
149-
*/
150-
public function testWriteLink()
151-
{
152-
$PHPWord = new PHPWord();
153-
$section = $PHPWord->createSection();
154-
155-
$expected = 'PHPWord';
156-
$section->addLink('http://github.com/phpoffice/phpword', $expected);
157-
158-
$doc = TestHelperDOCX::getDocument($PHPWord);
159-
$element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t');
160-
161-
$this->assertEquals($expected, $element->nodeValue);
162-
}
163-
164182
/**
165183
* Write table
166184
*/
@@ -272,20 +290,18 @@ public function testWriteImagePosition()
272290
}
273291

274292
/**
275-
* Write preserve text
293+
* Write title
276294
*/
277-
public function testWritePreserveText()
295+
public function testWriteTitle()
278296
{
279297
$PHPWord = new PHPWord();
280-
$section = $PHPWord->createSection();
281-
$footer = $section->createFooter();
282-
283-
$footer->addPreserveText('{PAGE}');
284-
298+
$PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
299+
$PHPWord->createSection()->addTitle('Test', 1);
285300
$doc = TestHelperDOCX::getDocument($PHPWord);
286-
$preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml');
287301

288-
$this->assertEquals('PAGE', $preserve->nodeValue);
289-
$this->assertEquals('preserve', $preserve->getAttribute('xml:space'));
302+
$element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
303+
$this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
304+
$element = "/w:document/w:body/w:p/w:r/w:fldChar";
305+
$this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType'));
290306
}
291307
}

samples/Sample_01_SimpleText.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
// New Word Document
88
echo date('H:i:s') , " Create new PHPWord object" , EOL;
99
$PHPWord = new PHPWord();
10-
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
11-
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
10+
$PHPWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16));
11+
$PHPWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100));
12+
$PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
1213

1314
// New portrait section
1415
$section = $PHPWord->createSection();
1516

1617
// Simple text
18+
$section->addTitle('Welcome to PHPWord', 1);
1719
$section->addText('Hello World!');
1820

1921
// Two text break

0 commit comments

Comments
 (0)