Some more tests for Writer/Word2007/Base.php. We're on 71.84% coverage now :)

This commit is contained in:
Ivan Lanin 2014-03-13 20:58:24 +07:00
parent fd95a1c1ec
commit 1aa83b9438
7 changed files with 146 additions and 76 deletions

View File

@ -229,4 +229,4 @@ class PHPWord_Section_MemoryImage
{ {
return $this->_imageExtension; return $this->_imageExtension;
} }
} }

View File

@ -28,7 +28,8 @@
/** /**
* PHPWord_Settings * PHPWord_Settings
*/ */
class PHPWord_Settings { class PHPWord_Settings
{
/** /**
* Compatibility option for XMLWriter * Compatibility option for XMLWriter
* *
@ -42,12 +43,13 @@ class PHPWord_Settings {
* @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility * @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility
* @return boolean Success or failure * @return boolean Success or failure
*/ */
public static function setCompatibility($compatibility) { public static function setCompatibility($compatibility)
{
if (is_bool($compatibility)) { if (is_bool($compatibility)) {
self::$_xmlWriterCompatibility = $compatibility; self::$_xmlWriterCompatibility = $compatibility;
return TRUE; return true;
} }
return FALSE; return false;
} // function setCompatibility() } // function setCompatibility()
@ -56,7 +58,8 @@ class PHPWord_Settings {
* *
* @return boolean Compatibility * @return boolean Compatibility
*/ */
public static function getCompatibility() { public static function getCompatibility()
{
return self::$_xmlWriterCompatibility; return self::$_xmlWriterCompatibility;
} // function getCompatibility() } // function getCompatibility()
} }

View File

@ -314,22 +314,22 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFBody .= $this->getDataContentTextBreak(); $sRTFBody .= $this->getDataContentTextBreak();
} elseif ($element instanceof PHPWord_Section_TextRun) { } elseif ($element instanceof PHPWord_Section_TextRun) {
$sRTFBody .= $this->getDataContentTextRun($element); $sRTFBody .= $this->getDataContentTextRun($element);
} elseif($element instanceof PHPWord_Section_Link) { } elseif ($element instanceof PHPWord_Section_Link) {
$sRTFBody .= $this->getDataContentUnsupportedElement('link'); $sRTFBody .= $this->getDataContentUnsupportedElement('link');
} elseif($element instanceof PHPWord_Section_Title) { } elseif ($element instanceof PHPWord_Section_Title) {
$sRTFBody .= $this->getDataContentUnsupportedElement('title'); $sRTFBody .= $this->getDataContentUnsupportedElement('title');
} elseif($element instanceof PHPWord_Section_PageBreak) { } elseif ($element instanceof PHPWord_Section_PageBreak) {
$sRTFBody .= $this->getDataContentUnsupportedElement('page break'); $sRTFBody .= $this->getDataContentUnsupportedElement('page break');
} elseif($element instanceof PHPWord_Section_Table) { } elseif ($element instanceof PHPWord_Section_Table) {
$sRTFBody .= $this->getDataContentUnsupportedElement('table'); $sRTFBody .= $this->getDataContentUnsupportedElement('table');
} elseif($element instanceof PHPWord_Section_ListItem) { } elseif ($element instanceof PHPWord_Section_ListItem) {
$sRTFBody .= $this->getDataContentUnsupportedElement('list item'); $sRTFBody .= $this->getDataContentUnsupportedElement('list item');
} elseif($element instanceof PHPWord_Section_Image || } elseif ($element instanceof PHPWord_Section_Image ||
$element instanceof PHPWord_Section_MemoryImage) { $element instanceof PHPWord_Section_MemoryImage) {
$sRTFBody .= $this->getDataContentUnsupportedElement('image'); $sRTFBody .= $this->getDataContentUnsupportedElement('image');
} elseif($element instanceof PHPWord_Section_Object) { } elseif ($element instanceof PHPWord_Section_Object) {
$sRTFBody .= $this->getDataContentUnsupportedElement('object'); $sRTFBody .= $this->getDataContentUnsupportedElement('object');
} elseif($element instanceof PHPWord_TOC) { } elseif ($element instanceof PHPWord_TOC) {
$sRTFBody .= $this->getDataContentUnsupportedElement('TOC'); $sRTFBody .= $this->getDataContentUnsupportedElement('TOC');
} else { } else {
$sRTFBody .= $this->getDataContentUnsupportedElement('other'); $sRTFBody .= $this->getDataContentUnsupportedElement('other');

View File

@ -29,9 +29,14 @@
/** /**
* Class PHPWord_Writer_Word2007_Base * 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(); $styleFont = $text->getFontStyle();
$SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; $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(); $elements = $textrun->getElements();
$styleParagraph = $textrun->getParagraphStyle(); $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(); $rID = $link->getRelationId();
$linkName = $link->getLinkName(); $linkName = $link->getLinkName();
if (is_null($linkName)) { 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(); $styleFont = $textrun->getFontStyle();
$styleParagraph = $textrun->getParagraphStyle(); $styleParagraph = $textrun->getParagraphStyle();
@ -367,7 +384,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); // p $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(); $font = $style->getName();
$bold = $style->getBold(); $bold = $style->getBold();
$italic = $style->getItalic(); $italic = $style->getItalic();
@ -454,11 +475,19 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); $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); $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(); $_rows = $table->getRows();
$_cRows = count($_rows); $_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(); $margins = $style->getCellMargin();
$mTop = (!is_null($margins[0])) ? true : false; $mTop = (!is_null($margins[0])) ? true : false;
$mLeft = (!is_null($margins[1])) ? 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(); $bgColor = $style->getBgColor();
$valign = $style->getVAlign(); $valign = $style->getVAlign();
$textDir = $style->getTextDirection(); $textDir = $style->getTextDirection();
@ -716,7 +753,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
* @param \PHPWord_Shared_XMLWriter $objWriter * @param \PHPWord_Shared_XMLWriter $objWriter
* @param \PHPWord_Section_Image $image * @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(); $rId = $image->getRelationId();
$style = $image->getStyle(); $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(); $rId = $image->getRelationId();
$style = $image->getStyle(); $style = $image->getStyle();
@ -835,7 +877,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); $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 = htmlspecialchars($title->getText());
$text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text); $text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text);
$anchor = $title->getAnchor(); $anchor = $title->getAnchor();
@ -876,6 +922,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); $objWriter->endElement();
} }
/**
* Write footnote
*/
protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote) protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote)
{ {
$objWriter->startElement('w:footnote'); $objWriter->startElement('w:footnote');
@ -911,6 +960,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); // w:footnote $objWriter->endElement(); // w:footnote
} }
/**
* Write footnote reference
*/
protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false) protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false)
{ {
if (!$withoutP) { if (!$withoutP) {

View File

@ -28,7 +28,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
$PHPWord->addFontStyle($rStyle, array('bold' => true)); $PHPWord->addFontStyle($rStyle, array('bold' => true));
$PHPWord->addParagraphStyle($pStyle, array('align' => 'justify')); $PHPWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
$section = $PHPWord->createSection(); $section = $PHPWord->createSection();
$section->addText('Test', $rStyle, $pStyle); $section->addText('Test', $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($PHPWord); $doc = TestHelperDOCX::getDocument($PHPWord);
@ -45,7 +45,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
public function testWriteTextRun() public function testWriteTextRun()
{ {
$pStyle = 'pStyle'; $pStyle = 'pStyle';
$aStyle = array('align' => 'justify'); $aStyle = array('align' => 'justify', 'spaceBefore' => 120, 'spaceAfter' => 120);
$imageSrc = join( $imageSrc = join(
DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'earth.jpg') 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}']")); $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 * 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')); $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 * 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(); $PHPWord = new PHPWord();
$section = $PHPWord->createSection(); $PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
$footer = $section->createFooter(); $PHPWord->createSection()->addTitle('Test', 1);
$footer->addPreserveText('{PAGE}');
$doc = TestHelperDOCX::getDocument($PHPWord); $doc = TestHelperDOCX::getDocument($PHPWord);
$preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml');
$this->assertEquals('PAGE', $preserve->nodeValue); $element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
$this->assertEquals('preserve', $preserve->getAttribute('xml:space')); $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'));
} }
} }

View File

@ -7,13 +7,15 @@ require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
echo date('H:i:s') , " Create new PHPWord object" , EOL; echo date('H:i:s') , " Create new PHPWord object" , EOL;
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16)); $PHPWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100)); $PHPWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100));
$PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
// New portrait section // New portrait section
$section = $PHPWord->createSection(); $section = $PHPWord->createSection();
// Simple text // Simple text
$section->addTitle('Welcome to PHPWord', 1);
$section->addText('Hello World!'); $section->addText('Hello World!');
// Two text break // Two text break

View File

@ -1,13 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
} else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
@ -18,13 +12,16 @@ $header = array('size' => 16, 'bold' => true);
//1.Use EastAisa FontStyle //1.Use EastAisa FontStyle
$section->addText('中文楷体样式测试',array('name' => '楷体', 'size' => 16, 'color' => '1B2232')); $section->addText('中文楷体样式测试',array('name' => '楷体', 'size' => 16, 'color' => '1B2232'));
// Save File // Save file
echo date('H:i:s') , ' Write to Word2007 format' , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); 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 // Done
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; 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;
// Echo done
echo date('H:i:s') , ' Done writing file' , EOL;