From acfe64bb5323fbb014c40c2e6a61fe6a893e0015 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 13 Mar 2014 19:09:35 +0700 Subject: [PATCH] More unit tests for Writer\Word2007\Base --- Classes/PHPWord/Style/Font.php | 2 - Classes/PHPWord/Style/Row.php | 22 ++- Classes/PHPWord/Writer/Word2007/Base.php | 47 +++++- README.md | 3 +- Tests/PHPWord/Writer/Word2007/BaseTest.php | 170 ++++++++++++++++++++- Tests/_inc/XmlDocument.php | 43 +++++- samples/Sample_01_SimpleText.php | 36 ++++- 7 files changed, 294 insertions(+), 29 deletions(-) diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index bb22a61b..cbd7fc94 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -173,8 +173,6 @@ class PHPWord_Style_Font } elseif (is_array($paragraphStyle)) { $this->_paragraphStyle = new PHPWord_Style_Paragraph; $this->_paragraphStyle->setArrayStyle($paragraphStyle); - } elseif (null === $paragraphStyle) { - $this->_paragraphStyle = new PHPWord_Style_Paragraph; } else { $this->_paragraphStyle = $paragraphStyle; } diff --git a/Classes/PHPWord/Style/Row.php b/Classes/PHPWord/Style/Row.php index 66d5505f..c7140d58 100644 --- a/Classes/PHPWord/Style/Row.php +++ b/Classes/PHPWord/Style/Row.php @@ -36,22 +36,20 @@ class PHPWord_Style_Row * * @var bool */ - private $_tblHeader; + private $_tblHeader = false; /** * Table row cannot break across pages * * @var bool */ - private $_cantSplit; + private $_cantSplit = false; /** * Create a new row style */ public function __construct() { - $this->_tblHeader = null; - $this->_cantSplit = null; } /** @@ -62,23 +60,31 @@ class PHPWord_Style_Row $this->$key = $value; } - public function setTblHeader($pValue = null) + public function setTblHeader($pValue = false) { + if (!is_bool($pValue)) { + $pValue = false; + } $this->_tblHeader = $pValue; + return $this; } public function getTblHeader() { - return $this->_tblHeader ? 1 : 0; + return $this->_tblHeader; } - public function setCantSplit($pValue = null) + public function setCantSplit($pValue = false) { + if (!is_bool($pValue)) { + $pValue = false; + } $this->_cantSplit = $pValue; + return $this; } public function getCantSplit() { - return $this->_cantSplit ? 1 : 0; + return $this->_cantSplit; } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 4e18fdd8..e6af2722 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -31,6 +31,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { + /** + * Write text + */ protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) { $styleFont = $text->getFontStyle(); @@ -81,6 +84,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } } + /** + * Write text run + */ protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) { $elements = $textrun->getElements(); @@ -222,6 +228,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } } + /** + * Write table + */ protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false) { $rID = $link->getRelationId(); @@ -278,6 +287,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } } + /** + * Write preserve text + */ protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun) { $styleFont = $textrun->getFontStyle(); @@ -370,6 +382,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); // p } + /** + * Write text style + */ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style) { $font = $style->getName(); @@ -451,11 +466,17 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } + /** + * Write text break + */ protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) { $objWriter->writeElement('w:p', null); } + /** + * Write table + */ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) { $_rows = $table->getRows(); @@ -499,14 +520,14 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->writeAttribute('w:val', $height); $objWriter->endElement(); } - if (!is_null($tblHeader)) { + if ($tblHeader) { $objWriter->startElement('w:tblHeader'); - $objWriter->writeAttribute('w:val', $tblHeader); + $objWriter->writeAttribute('w:val', '1'); $objWriter->endElement(); } - if (!is_null($cantSplit)) { + if ($cantSplit) { $objWriter->startElement('w:cantSplit'); - $objWriter->writeAttribute('w:val', $cantSplit); + $objWriter->writeAttribute('w:val', '1'); $objWriter->endElement(); } $objWriter->endElement(); @@ -565,6 +586,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } } + /** + * Write table style + */ protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null) { $margins = $style->getCellMargin(); @@ -610,6 +634,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } } + /** + * Write cell style + */ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) { $bgColor = $style->getBgColor(); @@ -794,6 +821,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } } + /** + * Write watermark + */ protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) { $rId = $image->getRelationId(); @@ -838,6 +868,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } + /** + * Write title + */ protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title) { $text = htmlspecialchars($title->getText()); @@ -880,6 +913,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'); @@ -915,6 +951,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/README.md b/README.md index 1003a29a..91405292 100755 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ __Want to contribute?__ Fork us! ## Requirements * PHP version 5.3.0 or higher -* PHP extension [php_zip](http://php.net/manual/en/book.zip.php) enabled +* PHP extension [ZipArchive](http://php.net/manual/en/book.zip.php) +* PHP extension [XMLWriter](http://php.net/manual/en/book.xmlwriter.php) ## Installation diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index cd1f6dbd..866cd5d3 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -7,7 +7,9 @@ use PHPWord\Tests\TestHelperDOCX; /** * Class BaseTest - * @package PHPWord\Tests + * + * @package PHPWord\Tests + * @coversDefaultClass PHPWord_Writer_Word2007_Base * @runTestsInSeparateProcesses */ class BaseTest extends \PHPUnit_Framework_TestCase @@ -20,6 +22,54 @@ class BaseTest extends \PHPUnit_Framework_TestCase TestHelperDOCX::clear(); } + public function testWriteText() + { + $rStyle = 'rStyle'; + $pStyle = 'pStyle'; + + $PHPWord = new PHPWord(); + $PHPWord->addFontStyle($rStyle, array('bold' => true)); + $PHPWord->addParagraphStyle($pStyle, array('align' => 'justify')); + $section = $PHPWord->createSection(); + $section->addText('Test', $rStyle, $pStyle); + $doc = TestHelperDOCX::getDocument($PHPWord); + + $element = "/w:document/w:body/w:p/w:r/w:rPr/w:rStyle"; + $this->assertEquals($rStyle, $doc->getElementAttribute($element, 'w:val')); + $element = "/w:document/w:body/w:p/w:pPr/w:pStyle"; + $this->assertEquals($pStyle, $doc->getElementAttribute($element, 'w:val')); + } + + /** + * Write text run + */ + public function testWriteTextRun() + { + $pStyle = 'pStyle'; + $aStyle = array('align' => 'justify'); + $imageSrc = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'earth.jpg') + ); + + $PHPWord = new PHPWord(); + $PHPWord->addParagraphStyle($pStyle, $aStyle); + $section = $PHPWord->createSection('Test'); + $textrun = $section->createTextRun($pStyle); + $textrun->addText('Test'); + $textrun->addTextBreak(); + $textrun = $section->createTextRun($aStyle); + $textrun->addLink('http://test.com'); + $textrun->addImage($imageSrc); + $doc = TestHelperDOCX::getDocument($PHPWord); + + $parent = "/w:document/w:body/w:p"; + $this->assertTrue($doc->elementExists("{$parent}/w:pPr/w:pStyle[@w:val='{$pStyle}']")); + } + + /** + * Write paragraph style: Alignment + */ public function testWriteParagraphStyleAlign() { $PHPWord = new PHPWord(); @@ -34,7 +84,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * Test write paragraph pagination + * Write paragraph style: Pagination */ public function testWriteParagraphStylePagination() { @@ -63,6 +113,116 @@ class BaseTest extends \PHPUnit_Framework_TestCase } } + /** + * covers ::_writeTextStyle + */ + public function testWriteFontStyle() + { + $PHPWord = new PHPWord(); + $styles['name'] = 'Verdana'; + $styles['size'] = 14; + $styles['bold'] = true; + $styles['italic'] = true; + $styles['underline'] = 'dash'; + $styles['strikethrough'] = true; + $styles['superScript'] = true; + $styles['color'] = 'FF0000'; + $styles['fgColor'] = 'yellow'; + + $section = $PHPWord->createSection(); + $section->addText('Test', $styles); + $doc = TestHelperDOCX::getDocument($PHPWord); + + $parent = '/w:document/w:body/w:p/w:r/w:rPr'; + $this->assertEquals($styles['name'], $doc->getElementAttribute("{$parent}/w:rFonts", 'w:ascii')); + $this->assertEquals($styles['size'] * 2, $doc->getElementAttribute("{$parent}/w:sz", 'w:val')); + $this->assertTrue($doc->elementExists("{$parent}/w:b")); + $this->assertTrue($doc->elementExists("{$parent}/w:i")); + $this->assertEquals($styles['underline'], $doc->getElementAttribute("{$parent}/w:u", 'w:val')); + $this->assertTrue($doc->elementExists("{$parent}/w:strike")); + $this->assertEquals('superscript', $doc->getElementAttribute("{$parent}/w:vertAlign", 'w:val')); + $this->assertEquals($styles['color'], $doc->getElementAttribute("{$parent}/w:color", '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 + */ + public function testWriteTableStyle() + { + $PHPWord = new PHPWord(); + $tWidth = 120; + $rHeight = 120; + $cWidth = 120; + $tStyles["cellMarginTop"] = 120; + $tStyles["cellMarginRight"] = 120; + $tStyles["cellMarginBottom"] = 120; + $tStyles["cellMarginLeft"] = 120; + $rStyles["tblHeader"] = true; + $rStyles["cantSplit"] = true; + $cStyles["valign"] = 'top'; + $cStyles["textDirection"] = 'btLr'; + $cStyles["bgColor"] = 'FF0000'; + $cStyles["borderTopSize"] = 120; + $cStyles["borderBottomSize"] = 120; + $cStyles["borderLeftSize"] = 120; + $cStyles["borderRightSize"] = 120; + $cStyles["borderTopColor"] = 'FF0000'; + $cStyles["borderBottomColor"] = 'FF0000'; + $cStyles["borderLeftColor"] = 'FF0000'; + $cStyles["borderRightColor"] = 'FF0000'; + + $section = $PHPWord->createSection(); + $table = $section->addTable($tStyles); + $table->setWidth = 100; + $table->addRow($rHeight, $rStyles); + $cell = $table->addCell($cWidth, $cStyles); + $cell->addText('Test'); + $cell->addTextBreak(); + $cell->addLink('http://google.com'); + $cell->addListItem('Test'); + $textrun = $cell->createTextRun(); + $textrun->addText('Test'); + + $doc = TestHelperDOCX::getDocument($PHPWord); + + $parent = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellMar'; + $this->assertEquals($tStyles['cellMarginTop'], $doc->getElementAttribute("{$parent}/w:top", 'w:w')); + $this->assertEquals($tStyles['cellMarginRight'], $doc->getElementAttribute("{$parent}/w:right", 'w:w')); + $this->assertEquals($tStyles['cellMarginBottom'], $doc->getElementAttribute("{$parent}/w:bottom", 'w:w')); + $this->assertEquals($tStyles['cellMarginLeft'], $doc->getElementAttribute("{$parent}/w:right", 'w:w')); + + $parent = '/w:document/w:body/w:tbl/w:tr/w:trPr'; + $this->assertEquals($rHeight, $doc->getElementAttribute("{$parent}/w:trHeight", 'w:val')); + $this->assertEquals($rStyles['tblHeader'], $doc->getElementAttribute("{$parent}/w:tblHeader", 'w:val')); + $this->assertEquals($rStyles['cantSplit'], $doc->getElementAttribute("{$parent}/w:cantSplit", 'w:val')); + + $parent = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr'; + $this->assertEquals($cWidth, $doc->getElementAttribute("{$parent}/w:tcW", 'w:w')); + $this->assertEquals($cStyles['valign'], $doc->getElementAttribute("{$parent}/w:vAlign", 'w:val')); + $this->assertEquals($cStyles['textDirection'], $doc->getElementAttribute("{$parent}/w:textDirection", 'w:val')); + } + + /** + * Write cell style + */ public function testWriteCellStyleCellGridSpan() { $PHPWord = new PHPWord(); @@ -87,6 +247,9 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals(5, $element->getAttribute('w:val')); } + /** + * Write image + */ public function testWriteImagePosition() { $PHPWord = new PHPWord(); @@ -109,6 +272,9 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertRegExp('/position:absolute;/', $style); } + /** + * Write preserve text + */ public function testWritePreserveText() { $PHPWord = new PHPWord(); diff --git a/Tests/_inc/XmlDocument.php b/Tests/_inc/XmlDocument.php index f16bd6d8..4d3d0034 100644 --- a/Tests/_inc/XmlDocument.php +++ b/Tests/_inc/XmlDocument.php @@ -45,11 +45,11 @@ class XmlDocument } /** - * @param string $path - * @param string $file - * @return \DOMElement + * @param string $path + * @param string $file + * @return \DOMNodeList */ - public function getElement($path, $file = 'word/document.xml') + public function getNodeList($path, $file = 'word/document.xml') { if ($this->dom === null || $file !== $this->file) { $this->getFileDom($file); @@ -60,7 +60,18 @@ class XmlDocument } - $elements = $this->xpath->query($path); + return $this->xpath->query($path); + } + + /** + * @param string $path + * @param string $file + * @return \DOMElement + */ + public function getElement($path, $file = 'word/document.xml') + { + $elements = $this->getNodeList($path, $file); + return $elements->item(0); } @@ -79,4 +90,26 @@ class XmlDocument { return $this->path; } + + /** + * @param string $path + * @param string $attribute + * @param string $file + * @return string + */ + public function getElementAttribute($path, $attribute, $file = 'word/document.xml') + { + return $this->getElement($path, $file)->getAttribute($attribute); + } + + /** + * @param string $path + * @param string $file + * @return string + */ + public function elementExists($path, $file = 'word/document.xml') + { + $nodeList = $this->getNodeList($path, $file); + return !($nodeList->length == 0); + } } diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 2a251c96..add71ea9 100755 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -7,21 +7,43 @@ 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)); // New portrait section $section = $PHPWord->createSection(); -// Add text elements +// Simple text $section->addText('Hello World!'); + +// Two text break $section->addTextBreak(2); -$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699')); -$section->addTextBreak(2); +// Defined style +$section->addText('I am styled by a font style definition.', 'rStyle'); +$section->addText('I am styled by a paragraph style definition.', null, 'pStyle'); +$section->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle'); +$section->addTextBreak(); -$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16)); -$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100)); -$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle'); -$section->addText('I have only a paragraph style definition.', null, 'pStyle'); +// Inline font style +$fontStyle['name'] = 'Times New Roman'; +$fontStyle['size'] = 20; +$fontStyle['bold'] = true; +$fontStyle['italic'] = true; +$fontStyle['underline'] = 'dash'; +$fontStyle['strikethrough'] = true; +$fontStyle['superScript'] = true; +$fontStyle['color'] = 'FF0000'; +$fontStyle['fgColor'] = 'yellow'; +$section->addText('I am inline styled.', $fontStyle); +$section->addTextBreak(); + +// Link +$section->addLink('http://www.google.com', null, 'NLink'); +$section->addTextBreak(); + +// Image +$section->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18)); // Save file $name = basename(__FILE__, '.php');