QA: Additional unit tests and docblock fixes

This commit is contained in:
Ivan Lanin 2014-05-25 22:51:14 +07:00
parent dc6c487cd0
commit 92c7a24c38
27 changed files with 290 additions and 139 deletions

View File

@ -43,6 +43,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
- `Writer\Word2007\Part`: `Numbering::writeNumbering()`, `Settings::writeSettings()`, `WebSettings::writeWebSettings()`, `ContentTypes::writeContentTypes()`, `Styles::writeStyles()`, `Document::writeDocument()` all changed into `write()`
- `Writer\Word2007\Part\DocProps`: Split into `Writer\Word2007\Part\DocPropsCore` and `Writer\Word2007\Part\DocPropsApp`
- `Element\Title::getBookmarkId()` replaced by `Element\Title::getRelationId()`
- `Writer\HTML::writeDocument`: Replaced by `Writer\HTML::getContent`
### Miscellaneous

View File

@ -22,6 +22,7 @@ namespace PhpOffice\PhpWord;
*/
class Autoloader
{
/** @const string */
const NAMESPACE_PREFIX = 'PhpOffice\\PhpWord\\';
/**

View File

@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord;
*/
class DocumentProperties
{
/** Constants */
/** @const string Property type constants */
const PROPERTY_TYPE_BOOLEAN = 'b';
const PROPERTY_TYPE_INTEGER = 'i';
const PROPERTY_TYPE_FLOAT = 'f';

View File

@ -68,9 +68,8 @@ class XMLWriter
// Create temporary filename
$this->tempFile = @tempnam($tempFolder, 'xml');
// Open storage
// Fallback to memory when temporary file cannot be used
if ($this->xmlWriter->openUri($this->tempFile) === false) {
// Fallback to memory...
$this->xmlWriter->openMemory();
}
}
@ -105,9 +104,16 @@ class XMLWriter
*
* @param mixed $function
* @param mixed $args
* @throws \BadMethodCallException
*/
public function __call($function, $args)
{
// Catch exception
if (method_exists($this->xmlWriter, $function) === false) {
throw new \BadMethodCallException("Method '{$function}' does not exists.");
}
// Run method
try {
@call_user_func_array(array($this->xmlWriter, $function), $args);
} catch (\Exception $ex) {

View File

@ -265,7 +265,7 @@ abstract class AbstractStyle
{
if ($value != null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) {
throw new \InvalidArgumentException('Invalid style value.');
} elseif (is_null($value) || trim($value) == '') {
} elseif ($value === null || trim($value) == '') {
$value = $default;
}

View File

@ -144,7 +144,7 @@ class Cell extends Border
*/
public function getBgColor()
{
if (!is_null($this->shading)) {
if ($this->shading !== null) {
return $this->shading->getFill();
} else {
return null;

View File

@ -540,7 +540,7 @@ class Font extends AbstractStyle
*/
public function getBgColor()
{
if (!is_null($this->shading)) {
if ($this->shading !== null) {
return $this->shading->getFill();
} else {
return null;

View File

@ -20,11 +20,12 @@ namespace PhpOffice\PhpWord\Style;
/**
* Line numbering style
*
* @link http://www.schemacentral.com/sc/ooxml/e-w_lnNumType-1.html
* @link http://www.schemacentral.com/sc/ooxml/t-w_CT_LineNumber.html
* @since 0.10.0
*/
class LineNumbering extends AbstractStyle
{
/** @const string Line numbering restart setting http://www.schemacentral.com/sc/ooxml/a-w_restart-1.html */
const LINE_NUMBERING_CONTINUOUS = 'continuous';
const LINE_NUMBERING_NEW_PAGE = 'newPage';
const LINE_NUMBERING_NEW_SECTION = 'newSection';

View File

@ -65,7 +65,7 @@ class ListItem extends AbstractStyle
*/
public function __construct($numStyle = null)
{
if (!is_null($numStyle)) {
if ($numStyle !== null) {
$this->setNumStyle($numStyle);
} else {
$this->setListType();
@ -149,7 +149,7 @@ class ListItem extends AbstractStyle
{
// Check if legacy style already registered in global Style collection
$numStyle = "PHPWordList{$this->listType}";
if (!is_null(Style::getStyle($numStyle))) {
if (Style::getStyle($numStyle) !== null) {
$this->setNumStyle($numStyle);
return;
}

View File

@ -171,7 +171,7 @@ class Paragraph extends AbstractStyle
*/
public function getSpaceBefore()
{
if (!is_null($this->spacing)) {
if ($this->spacing !== null) {
return $this->spacing->getBefore();
} else {
return null;
@ -196,7 +196,7 @@ class Paragraph extends AbstractStyle
*/
public function getSpaceAfter()
{
if (!is_null($this->spacing)) {
if ($this->spacing !== null) {
return $this->spacing->getAfter();
} else {
return null;
@ -221,7 +221,7 @@ class Paragraph extends AbstractStyle
*/
public function getSpacing()
{
if (!is_null($this->spacing)) {
if ($this->spacing !== null) {
return $this->spacing->getLine();
} else {
return null;
@ -278,7 +278,7 @@ class Paragraph extends AbstractStyle
*/
public function getIndent()
{
if (!is_null($this->indentation)) {
if ($this->indentation !== null) {
return $this->indentation->getLeft();
} else {
return null;
@ -303,7 +303,7 @@ class Paragraph extends AbstractStyle
*/
public function getHanging()
{
if (!is_null($this->indentation)) {
if ($this->indentation !== null) {
return $this->indentation->getHanging();
} else {
return null;

View File

@ -169,7 +169,7 @@ class Table extends Border
*/
public function getBgColor()
{
if (!is_null($this->shading)) {
if ($this->shading !== null) {
return $this->shading->getFill();
}

View File

@ -179,7 +179,7 @@ class TextBox extends Image
$hasInnerMargins = false;
$margins = $this->getInnerMargin();
for ($i = 0; $i < count($margins); $i++) {
if (!is_null($margins[$i])) {
if ($margins[$i] !== null) {
$hasInnerMargins = true;
}
}

View File

@ -258,7 +258,7 @@ abstract class AbstractWriter implements WriterInterface
*
* @param string $filename
* @return \PhpOffice\PhpWord\Shared\ZipArchive
* @throws \PhpOffice\PhpWord\Exception\Exception
* @throws \Exception
*/
protected function getZipArchive($filename)
{
@ -271,13 +271,46 @@ abstract class AbstractWriter implements WriterInterface
$zip = new ZipArchive();
if ($zip->open($filename, ZipArchive::OVERWRITE) !== true) {
if ($zip->open($filename, ZipArchive::CREATE) !== true) {
throw new Exception("Could not open " . $filename . " for writing.");
throw new \Exception("Could not open '{$filename}' for writing.");
}
}
return $zip;
}
/**
* Open file for writing
*
* @param string $filename
* @return resource
* @throws \Exception
* @since 0.11.0
*/
protected function openFile($filename)
{
$filename = $this->getTempFile($filename);
$fileHandle = fopen($filename, 'w');
if ($fileHandle === false) {
throw new \Exception("Could not open '{$filename}' for writing.");
}
return $fileHandle;
}
/**
* Write content to file
*
* @param resource $fileHandle
* @param string $content
* @since 0.11.0
*/
protected function writeFile(&$fileHandle, $content)
{
fwrite($fileHandle, $content);
fclose($fileHandle);
$this->cleanupTempFile();
}
/**
* Add files to package
*

View File

@ -17,7 +17,6 @@
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
/**
@ -69,25 +68,20 @@ class HTML extends AbstractWriter implements WriterInterface
*/
public function save($filename = null)
{
$this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/');
$hFile = fopen($filename, 'w');
if ($hFile !== false) {
fwrite($hFile, $this->writeDocument());
fclose($hFile);
} else {
throw new Exception("Can't open file");
}
$this->clearTempDir();
$fileHandle = $this->openFile($filename);
$this->writeFile($fileHandle, $this->getContent());
}
/**
* Get phpWord data
* Get content
*
* @return string
* @since 0.11.0
*/
public function writeDocument()
public function getContent()
{
$content = '';
$content .= '<!DOCTYPE html>' . PHP_EOL;
$content .= '<!-- Generated by PHPWord -->' . PHP_EOL;
$content .= '<html>' . PHP_EOL;
@ -128,4 +122,16 @@ class HTML extends AbstractWriter implements WriterInterface
{
$this->notes[$noteId] = $noteMark;
}
/**
* Write document
*
* @return string
* @deprecated 0.11.0
* @codeCoverageIgnore
*/
public function writeDocument()
{
return $this->getContent();
}
}

View File

@ -65,13 +65,13 @@ class PDF
* @param string $name Renderer library method name
* @param mixed[] $arguments Array of arguments to pass to the renderer method
* @return mixed Returned data from the PDF renderer wrapper method
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function __call($name, $arguments)
{
if ($this->renderer === null) {
throw new Exception("PDF Rendering library has not been defined.");
}
// Note: Commented because all exceptions should already be catched by `__construct`
// if ($this->renderer === null) {
// throw new Exception("PDF Rendering library has not been defined.");
// }
return call_user_func_array(array($this->renderer, $name), $arguments);
}

View File

@ -50,7 +50,7 @@ class DomPDF extends AbstractRenderer implements WriterInterface
// Create PDF
$pdf = new \DOMPDF();
$pdf->set_paper(strtolower($paperSize), $orientation);
$pdf->load_html($this->writeDocument());
$pdf->load_html($this->getContent());
$pdf->render();
// Write to file

View File

@ -61,7 +61,7 @@ class MPDF extends AbstractRenderer implements WriterInterface
$pdf->setKeywords($docProps->getKeywords());
$pdf->setCreator($docProps->getCreator());
$pdf->writeHTML($this->writeDocument());
$pdf->writeHTML($this->getContent());
// Write to file
fwrite($fileHandle, $pdf->output($filename, 'S'));

View File

@ -54,7 +54,7 @@ class TCPDF extends AbstractRenderer implements WriterInterface
$pdf->setPrintFooter(false);
$pdf->addPage();
$pdf->setFont($this->getFont());
$pdf->writeHTML($this->writeDocument());
$pdf->writeHTML($this->getContent());
// Write document properties
$phpWord = $this->getPhpWord();

View File

@ -17,7 +17,6 @@
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
/**
@ -56,29 +55,34 @@ class RTF extends AbstractWriter implements WriterInterface
}
/**
* Save PhpWord to file
* Save content to file
*
* @param string $filename
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function save($filename = null)
{
$content = '';
$filename = $this->getTempFile($filename);
$hFile = fopen($filename, 'w');
if ($hFile !== false) {
$content .= '{';
$content .= '\rtf1' . PHP_EOL;
$content .= $this->getWriterPart('Header')->write();
$content .= $this->getWriterPart('Document')->write();
$content .= '}';
$fileHandle = $this->openFile($filename);
$this->writeFile($fileHandle, $this->getContent());
}
fwrite($hFile, $content);
fclose($hFile);
} else {
throw new Exception("Can't open file");
}
$this->cleanupTempFile();
/**
* Get content
*
* @return string
* @since 0.11.0
*/
private function getContent()
{
$content = '';
$content .= '{';
$content .= '\rtf1' . PHP_EOL;
$content .= $this->getWriterPart('Header')->write();
$content .= $this->getWriterPart('Document')->write();
$content .= '}';
return $content;
}
/**

View File

@ -20,9 +20,9 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\Settings as PhpWordSettings;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\Style\Font as FontStyle;
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
use PhpOffice\PhpWord\Style\Table as TableStyle;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
@ -31,6 +31,7 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
* Word2007 styles part writer: word/styles.xml
*
* @todo Do something with the numbering style introduced in 0.10.0
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) For writeFontStyle, writeParagraphStyle, and writeTableStyle
*/
class Styles extends AbstractPart
{
@ -59,88 +60,11 @@ class Styles extends AbstractPart
continue;
}
// Font style
if ($style instanceof Font) {
$paragraphStyle = $style->getParagraph();
$styleType = $style->getStyleType();
$type = ($styleType == 'title') ? 'paragraph' : 'character';
if (!is_null($paragraphStyle)) {
$type = 'paragraph';
}
$xmlWriter->startElement('w:style');
$xmlWriter->writeAttribute('w:type', $type);
if ($styleType == 'title') {
$arrStyle = explode('_', $styleName);
$styleId = 'Heading' . $arrStyle[1];
$styleName = 'heading ' . $arrStyle[1];
$styleLink = 'Heading' . $arrStyle[1] . 'Char';
$xmlWriter->writeAttribute('w:styleId', $styleId);
$xmlWriter->startElement('w:link');
$xmlWriter->writeAttribute('w:val', $styleLink);
$xmlWriter->endElement();
}
$xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $styleName);
$xmlWriter->endElement();
// Parent style
$xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal');
// w:pPr
if (!is_null($paragraphStyle)) {
$styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
$styleWriter->write();
}
// w:rPr
$styleWriter = new FontStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement();
// Paragraph style
} elseif ($style instanceof Paragraph) {
$xmlWriter->startElement('w:style');
$xmlWriter->writeAttribute('w:type', 'paragraph');
$xmlWriter->writeAttribute('w:customStyle', '1');
$xmlWriter->writeAttribute('w:styleId', $styleName);
$xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $styleName);
$xmlWriter->endElement();
// Parent style
$basedOn = $style->getBasedOn();
$xmlWriter->writeElementIf(!is_null($basedOn), 'w:basedOn', 'w:val', $basedOn);
// Next paragraph style
$next = $style->getNext();
$xmlWriter->writeElementIf(!is_null($next), 'w:next', 'w:val', $next);
// w:pPr
$styleWriter = new ParagraphStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement();
// Table style
} elseif ($style instanceof Table) {
$xmlWriter->startElement('w:style');
$xmlWriter->writeAttribute('w:type', 'table');
$xmlWriter->writeAttribute('w:customStyle', '1');
$xmlWriter->writeAttribute('w:styleId', $styleName);
$xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $styleName);
$xmlWriter->endElement();
$xmlWriter->startElement('w:uiPriority');
$xmlWriter->writeAttribute('w:val', '99');
$xmlWriter->endElement();
$styleWriter = new TableStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement(); // w:style
// Get style class and execute if the private method exists
$styleClass = substr(get_class($style), strrpos(get_class($style), '\\') + 1);
$method = "write{$styleClass}Style";
if (method_exists($this, $method)) {
$this->$method($xmlWriter, $styleName, $style);
}
}
}
@ -213,4 +137,115 @@ class Styles extends AbstractPart
$xmlWriter->endElement(); // w:style
}
}
/**
* Write font style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param string $styleName
* @param \PhpOffice\PhpWord\Style\Font $style
*/
private function writeFontStyle(XMLWriter $xmlWriter, $styleName, FontStyle $style)
{
$paragraphStyle = $style->getParagraph();
$styleType = $style->getStyleType();
$type = ($styleType == 'title') ? 'paragraph' : 'character';
if (!is_null($paragraphStyle)) {
$type = 'paragraph';
}
$xmlWriter->startElement('w:style');
$xmlWriter->writeAttribute('w:type', $type);
// Heading style
if ($styleType == 'title') {
$arrStyle = explode('_', $styleName);
$styleId = 'Heading' . $arrStyle[1];
$styleName = 'heading ' . $arrStyle[1];
$styleLink = 'Heading' . $arrStyle[1] . 'Char';
$xmlWriter->writeAttribute('w:styleId', $styleId);
$xmlWriter->startElement('w:link');
$xmlWriter->writeAttribute('w:val', $styleLink);
$xmlWriter->endElement();
}
// Style name
$xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $styleName);
$xmlWriter->endElement();
// Parent style
$xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal');
// w:pPr
if (!is_null($paragraphStyle)) {
$styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
$styleWriter->write();
}
// w:rPr
$styleWriter = new FontStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement();
}
/**
* Write paragraph style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param string $styleName
* @param \PhpOffice\PhpWord\Style\Paragraph $style
*/
private function writeParagraphStyle(XMLWriter $xmlWriter, $styleName, ParagraphStyle $style)
{
$xmlWriter->startElement('w:style');
$xmlWriter->writeAttribute('w:type', 'paragraph');
$xmlWriter->writeAttribute('w:customStyle', '1');
$xmlWriter->writeAttribute('w:styleId', $styleName);
$xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $styleName);
$xmlWriter->endElement();
// Parent style
$basedOn = $style->getBasedOn();
$xmlWriter->writeElementIf(!is_null($basedOn), 'w:basedOn', 'w:val', $basedOn);
// Next paragraph style
$next = $style->getNext();
$xmlWriter->writeElementIf(!is_null($next), 'w:next', 'w:val', $next);
// w:pPr
$styleWriter = new ParagraphStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement();
}
/**
* Write table style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param string $styleName
* @param \PhpOffice\PhpWord\Style\Table $style
*/
private function writeTableStyle(XMLWriter $xmlWriter, $styleName, TableStyle $style)
{
$xmlWriter->startElement('w:style');
$xmlWriter->writeAttribute('w:type', 'table');
$xmlWriter->writeAttribute('w:customStyle', '1');
$xmlWriter->writeAttribute('w:styleId', $styleName);
$xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $styleName);
$xmlWriter->endElement();
$xmlWriter->startElement('w:uiPriority');
$xmlWriter->writeAttribute('w:val', '99');
$xmlWriter->endElement();
$styleWriter = new TableStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement(); // w:style
}
}

View File

@ -0,0 +1,40 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Shared;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Test class for PhpOffice\PhpWord\Shared\XMLWriter
*/
class XMLWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* Test method exception
*
* @expectedException \BadMethodCallException
* @expectedExceptionMessage Method 'foo' does not exists.
*/
public function testCallException()
{
Settings::setCompatibility(false);
$object = new XMLWriter();
$object->foo();
}
}

View File

@ -45,6 +45,7 @@ class AbstractStyleTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(true, self::callProtectedMethod($stub, 'setBoolVal', array(true, false)));
$this->assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', array(12, 200)));
$this->assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', array(871.1, 2.1)));
$this->assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', array('871.1', 2.1)));
$this->assertEquals('a', self::callProtectedMethod($stub, 'setEnumVal', array('a', array('a', 'b'), 'b')));
}

View File

@ -52,7 +52,11 @@ class CellTest extends \PHPUnit_Framework_TestCase
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$this->assertNull($object->$get()); // Init with null value
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
}

View File

@ -74,6 +74,7 @@ class FontTest extends \PHPUnit_Framework_TestCase
);
foreach ($attributes as $key => $default) {
$get = is_bool($default) ? "is{$key}" : "get{$key}";
$this->assertEquals($default, $object->$get());
$object->setStyleValue("$key", null);
$this->assertEquals($default, $object->$get());
$object->setStyleValue("$key", '');

View File

@ -96,6 +96,20 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
}
}
/**
* Test get null style value
*/
public function testGetNullStyleValue()
{
$object = new Paragraph();
$attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter');
foreach ($attributes as $key) {
$get = "get{$key}";
$this->assertNull($object->$get());
}
}
/**
* Test tabs
*/

View File

@ -74,6 +74,9 @@ class TableTest extends \PHPUnit_Framework_TestCase
'cellMarginLeft' => 240,
'cellMarginRight' => 240,
'cellMarginBottom' => 240,
'align' => 'center',
'width' => 100,
'unit' => 'pct',
);
foreach ($attributes as $key => $value) {
$set = "set{$key}";
@ -146,6 +149,7 @@ class TableTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($value, $object->$get());
}
$this->assertEquals($values, $object->getCellMargin());
$this->assertTrue($object->hasMargin());
}
/**