From 1aa83b943820aa66a49ea9112f250e0187cb4cf7 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 13 Mar 2014 20:58:24 +0700 Subject: [PATCH] Some more tests for Writer/Word2007/Base.php. We're on 71.84% coverage now :) --- Classes/PHPWord/Section/MemoryImage.php | 2 +- Classes/PHPWord/Settings.php | 15 +++-- Classes/PHPWord/Writer/RTF.php | 16 ++--- Classes/PHPWord/Writer/Word2007/Base.php | 78 ++++++++++++++++++---- Tests/PHPWord/Writer/Word2007/BaseTest.php | 74 ++++++++++++-------- samples/Sample_01_SimpleText.php | 6 +- samples/Sample_10_EastAsianFontStyle.php | 31 ++++----- 7 files changed, 146 insertions(+), 76 deletions(-) diff --git a/Classes/PHPWord/Section/MemoryImage.php b/Classes/PHPWord/Section/MemoryImage.php index 8486a2a3..f1488c45 100755 --- a/Classes/PHPWord/Section/MemoryImage.php +++ b/Classes/PHPWord/Section/MemoryImage.php @@ -229,4 +229,4 @@ class PHPWord_Section_MemoryImage { return $this->_imageExtension; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Settings.php b/Classes/PHPWord/Settings.php index 67ef697e..af400551 100644 --- a/Classes/PHPWord/Settings.php +++ b/Classes/PHPWord/Settings.php @@ -28,7 +28,8 @@ /** * PHPWord_Settings */ -class PHPWord_Settings { +class PHPWord_Settings +{ /** * Compatibility option for XMLWriter * @@ -42,12 +43,13 @@ class PHPWord_Settings { * @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility * @return boolean Success or failure */ - public static function setCompatibility($compatibility) { + public static function setCompatibility($compatibility) + { if (is_bool($compatibility)) { self::$_xmlWriterCompatibility = $compatibility; - return TRUE; + return true; } - return FALSE; + return false; } // function setCompatibility() @@ -56,7 +58,8 @@ class PHPWord_Settings { * * @return boolean Compatibility */ - public static function getCompatibility() { + public static function getCompatibility() + { return self::$_xmlWriterCompatibility; } // function getCompatibility() -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php index 7730e2bd..9104c6fe 100755 --- a/Classes/PHPWord/Writer/RTF.php +++ b/Classes/PHPWord/Writer/RTF.php @@ -314,22 +314,22 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $sRTFBody .= $this->getDataContentTextBreak(); } elseif ($element instanceof PHPWord_Section_TextRun) { $sRTFBody .= $this->getDataContentTextRun($element); - } elseif($element instanceof PHPWord_Section_Link) { + } elseif ($element instanceof PHPWord_Section_Link) { $sRTFBody .= $this->getDataContentUnsupportedElement('link'); - } elseif($element instanceof PHPWord_Section_Title) { + } elseif ($element instanceof PHPWord_Section_Title) { $sRTFBody .= $this->getDataContentUnsupportedElement('title'); - } elseif($element instanceof PHPWord_Section_PageBreak) { + } elseif ($element instanceof PHPWord_Section_PageBreak) { $sRTFBody .= $this->getDataContentUnsupportedElement('page break'); - } elseif($element instanceof PHPWord_Section_Table) { + } elseif ($element instanceof PHPWord_Section_Table) { $sRTFBody .= $this->getDataContentUnsupportedElement('table'); - } elseif($element instanceof PHPWord_Section_ListItem) { + } elseif ($element instanceof PHPWord_Section_ListItem) { $sRTFBody .= $this->getDataContentUnsupportedElement('list item'); - } elseif($element instanceof PHPWord_Section_Image || + } elseif ($element instanceof PHPWord_Section_Image || $element instanceof PHPWord_Section_MemoryImage) { $sRTFBody .= $this->getDataContentUnsupportedElement('image'); - } elseif($element instanceof PHPWord_Section_Object) { + } elseif ($element instanceof PHPWord_Section_Object) { $sRTFBody .= $this->getDataContentUnsupportedElement('object'); - } elseif($element instanceof PHPWord_TOC) { + } elseif ($element instanceof PHPWord_TOC) { $sRTFBody .= $this->getDataContentUnsupportedElement('TOC'); } else { $sRTFBody .= $this->getDataContentUnsupportedElement('other'); diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 0fb54b27..f1324253 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -29,9 +29,14 @@ /** * Class PHPWord_Writer_Word2007_Base */ -class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { +class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart +{ - protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) { + /** + * Write text + */ + protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) + { $styleFont = $text->getFontStyle(); $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; @@ -80,7 +85,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { } } - protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) { + /** + * Write text run + */ + protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) + { $elements = $textrun->getElements(); $styleParagraph = $textrun->getParagraphStyle(); @@ -221,7 +230,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { } } - protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false) { + /** + * Write link + */ + protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false) + { $rID = $link->getRelationId(); $linkName = $link->getLinkName(); if (is_null($linkName)) { @@ -276,7 +289,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { } } - protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun) { + /** + * Write preserve text + */ + protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun) + { $styleFont = $textrun->getFontStyle(); $styleParagraph = $textrun->getParagraphStyle(); @@ -367,7 +384,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $objWriter->endElement(); // p } - protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style) { + /** + * Write text style + */ + protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style) + { $font = $style->getName(); $bold = $style->getBold(); $italic = $style->getItalic(); @@ -454,11 +475,19 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $objWriter->endElement(); } - protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) { + /** + * Write text break + */ + protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) + { $objWriter->writeElement('w:p', null); } - protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) { + /** + * Write table + */ + protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) + { $_rows = $table->getRows(); $_cRows = count($_rows); @@ -566,7 +595,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { } } - protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null) { + /** + * Write table style + */ + protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null) + { $margins = $style->getCellMargin(); $mTop = (!is_null($margins[0])) ? true : false; $mLeft = (!is_null($margins[1])) ? true : false; @@ -610,7 +643,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { } } - protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) { + /** + * Write cell style + */ + protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) + { $bgColor = $style->getBgColor(); $valign = $style->getVAlign(); $textDir = $style->getTextDirection(); @@ -716,7 +753,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { * @param \PHPWord_Shared_XMLWriter $objWriter * @param \PHPWord_Section_Image $image */ - protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false) { + protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false) + { $rId = $image->getRelationId(); $style = $image->getStyle(); @@ -792,7 +830,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { } } - protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) { + /** + * Write watermark + */ + protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) + { $rId = $image->getRelationId(); $style = $image->getStyle(); @@ -835,7 +877,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $objWriter->endElement(); } - protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title) { + /** + * Write title + */ + protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title) + { $text = htmlspecialchars($title->getText()); $text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text); $anchor = $title->getAnchor(); @@ -876,6 +922,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $objWriter->endElement(); } + /** + * Write footnote + */ protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote) { $objWriter->startElement('w:footnote'); @@ -911,6 +960,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $objWriter->endElement(); // w:footnote } + /** + * Write footnote reference + */ protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false) { if (!$withoutP) { diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index 130a2099..56f859d9 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -28,7 +28,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $PHPWord = new PHPWord(); $PHPWord->addFontStyle($rStyle, array('bold' => true)); - $PHPWord->addParagraphStyle($pStyle, array('align' => 'justify')); + $PHPWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120)); $section = $PHPWord->createSection(); $section->addText('Test', $rStyle, $pStyle); $doc = TestHelperDOCX::getDocument($PHPWord); @@ -45,7 +45,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase public function testWriteTextRun() { $pStyle = 'pStyle'; - $aStyle = array('align' => 'justify'); + $aStyle = array('align' => 'justify', 'spaceBefore' => 120, 'spaceAfter' => 120); $imageSrc = join( DIRECTORY_SEPARATOR, array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'earth.jpg') @@ -66,6 +66,41 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertTrue($doc->elementExists("{$parent}/w:pPr/w:pStyle[@w:val='{$pStyle}']")); } + /** + * Write link + */ + public function testWriteLink() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + + $expected = 'PHPWord'; + $section->addLink('http://github.com/phpoffice/phpword', $expected); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t'); + + $this->assertEquals($expected, $element->nodeValue); + } + + /** + * Write preserve text + */ + public function testWritePreserveText() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + $footer = $section->createFooter(); + + $footer->addPreserveText('{PAGE}'); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml'); + + $this->assertEquals('PAGE', $preserve->nodeValue); + $this->assertEquals('preserve', $preserve->getAttribute('xml:space')); + } + /** * Write paragraph style: Alignment */ @@ -144,23 +179,6 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals($styles['fgColor'], $doc->getElementAttribute("{$parent}/w:highlight", 'w:val')); } - /** - * Write link - */ - public function testWriteLink() - { - $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(); - - $expected = 'PHPWord'; - $section->addLink('http://github.com/phpoffice/phpword', $expected); - - $doc = TestHelperDOCX::getDocument($PHPWord); - $element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t'); - - $this->assertEquals($expected, $element->nodeValue); - } - /** * Write table */ @@ -272,20 +290,18 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * Write preserve text + * Write title */ - public function testWritePreserveText() + public function testWriteTitle() { $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(); - $footer = $section->createFooter(); - - $footer->addPreserveText('{PAGE}'); - + $PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); + $PHPWord->createSection()->addTitle('Test', 1); $doc = TestHelperDOCX::getDocument($PHPWord); - $preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml'); - $this->assertEquals('PAGE', $preserve->nodeValue); - $this->assertEquals('preserve', $preserve->getAttribute('xml:space')); + $element = "/w:document/w:body/w:p/w:pPr/w:pStyle"; + $this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val')); + $element = "/w:document/w:body/w:p/w:r/w:fldChar"; + $this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType')); } } diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index add71ea9..55ffaf25 100755 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -7,13 +7,15 @@ require_once '../Classes/PHPWord.php'; // New Word Document echo date('H:i:s') , " Create new PHPWord object" , EOL; $PHPWord = new PHPWord(); -$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16)); -$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100)); +$PHPWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16)); +$PHPWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100)); +$PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); // New portrait section $section = $PHPWord->createSection(); // Simple text +$section->addTitle('Welcome to PHPWord', 1); $section->addText('Hello World!'); // Two text break diff --git a/samples/Sample_10_EastAsianFontStyle.php b/samples/Sample_10_EastAsianFontStyle.php index cc0a7ef6..8be72109 100644 --- a/samples/Sample_10_EastAsianFontStyle.php +++ b/samples/Sample_10_EastAsianFontStyle.php @@ -1,13 +1,7 @@ '); -} - +define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '
'); require_once '../Classes/PHPWord.php'; // New Word Document @@ -18,13 +12,16 @@ $header = array('size' => 16, 'bold' => true); //1.Use EastAisa FontStyle $section->addText('中文楷体样式测试',array('name' => '楷体', 'size' => 16, 'color' => '1B2232')); -// Save File -echo date('H:i:s') , ' Write to Word2007 format' , EOL; -$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); -$objWriter->save(str_replace('.php', '.docx', __FILE__)); +// Save file +$name = basename(__FILE__, '.php'); +$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf'); +foreach ($writers as $writer => $extension) { + echo date('H:i:s'), " Write to {$writer} format", EOL; + $objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer); + $objWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} -// Echo memory peak usage -echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; - -// Echo done -echo date('H:i:s') , ' Done writing file' , EOL; +// Done +echo date('H:i:s'), " Done writing file(s)", EOL; +echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;