QA: Additional tests for Word2007 writer

This commit is contained in:
Ivan Lanin 2014-04-17 16:19:23 +07:00
parent 306c354b2c
commit 4db75c37a9
17 changed files with 174 additions and 20 deletions

View File

@ -9,15 +9,13 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Element\Element;
use PhpOffice\PhpWord\Element\Footnote as FootnoteElement;
use PhpOffice\PhpWord\Endnotes;
use PhpOffice\PhpWord\Exception\InvalidObjectException;
use PhpOffice\PhpWord\Footnotes;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\TOC;
use PhpOffice\PhpWord\Exception\InvalidObjectException;
use PhpOffice\PhpWord\Shared\String;
/**
* Container abstract class
@ -323,13 +321,13 @@ abstract class AbstractElement
* Add footnote element
*
* @param mixed $paragraphStyle
* @return FootnoteElement
* @return Footnote
*/
public function addFootnote($paragraphStyle = null)
{
$this->checkValidity('footnote');
$footnote = new FootnoteElement($paragraphStyle);
$footnote = new Footnote($paragraphStyle);
$rId = Footnotes::addElement($footnote);
$footnote->setDocPart('footnote', $this->getDocPartId());

View File

@ -13,6 +13,8 @@ use PhpOffice\PhpWord\Style\Row as RowStyle;
/**
* Table row element
*
* @since 0.8.0
*/
class Row extends AbstractElement
{

View File

@ -20,7 +20,7 @@ use PhpOffice\PhpWord\Element\Section;
/**
* Reader for Word2007
*
* @since 0.10.0
* @since 0.8.0
* @todo title, list, watermark, checkbox, toc
* @todo Partly done: image, object
*/

View File

@ -10,7 +10,9 @@
namespace PhpOffice\PhpWord;
/**
* Settings
* PHPWord settings class
*
* @since 0.8.0
*/
class Settings
{

View File

@ -11,6 +11,8 @@ namespace PhpOffice\PhpWord\Style;
/**
* Table row style
*
* @since 0.8.0
*/
class Row extends AbstractStyle
{

View File

@ -298,6 +298,7 @@ class Template
* Save XML to defined name
*
* @param string $strFilename
* @since 0.8.0
*/
public function saveAs($strFilename)
{

View File

@ -9,6 +9,10 @@
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Endnotes;
use PhpOffice\PhpWord\Footnotes;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Element\Endnote;
use PhpOffice\PhpWord\Element\Footnote;
use PhpOffice\PhpWord\Element\Image;
@ -22,11 +26,7 @@ use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\Endnotes;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Footnotes;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
@ -703,12 +703,14 @@ class HTML extends AbstractWriter implements WriterInterface
*/
private function getBase64ImageData(Image $element)
{
$imageData = null;
$imageBinary = null;
$source = $element->getSource();
$imageType = $element->getImageType();
$imageData = null;
$imageBinary = null;
$actualSource = null;
// Get actual source from archive image
// Get actual source from archive image or other source
// Return null if not found
if ($element->getSourceType() == Image::SOURCE_ARCHIVE) {
$source = substr($source, 6);
list($zipFilename, $imageFilename) = explode('#', $source);
@ -723,6 +725,9 @@ class HTML extends AbstractWriter implements WriterInterface
} else {
$actualSource = $source;
}
if (is_null($actualSource)) {
return null;
}
// Read image binary data and convert into Base64
if ($element->getSourceType() == Image::SOURCE_GD) {

View File

@ -19,6 +19,8 @@ use PhpOffice\PhpWord\Writer\ODText\Styles;
/**
* ODText writer
*
* @since 0.7.0
*/
class ODText extends AbstractWriter implements WriterInterface
{

View File

@ -28,6 +28,8 @@ use PhpOffice\PhpWord\TOC;
/**
* RTF writer
*
* @since 0.7.0
*/
class RTF extends AbstractWriter implements WriterInterface
{

View File

@ -10,6 +10,7 @@
namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Exception\Exception;
/**
* Word2007 document properties part writer
@ -46,7 +47,7 @@ class DocProps extends AbstractWriterPart
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function writeDocPropsCore(PhpWord $phpWord)
public function writeDocPropsCore(PhpWord $phpWord = null)
{
if (is_null($phpWord)) {
throw new Exception("No PhpWord assigned.");

View File

@ -9,13 +9,13 @@
namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\TOC;
use PhpOffice\PhpWord\Element\PageBreak;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\TOC;
/**
* Word2007 document part writer

View File

@ -110,7 +110,7 @@ class Settings extends AbstractWriterPart
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param string $settingKey
* @param array $settingValue
* @param array|string $settingValue
*/
protected function writeSetting($xmlWriter, $settingKey, $settingValue)
{

View File

@ -10,11 +10,12 @@
namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\Style;
/**
* Word2007 styles part writer

View File

@ -0,0 +1,44 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Tests\Writer\Word2007;
use PhpOffice\PhpWord\Writer\Word2007\DocProps;
/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\DocProps
*
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\DocProps
* @runTestsInSeparateProcesses
*/
class DocPropsTest extends \PHPUnit_Framework_TestCase
{
/**
* Test write docProps/app.xml with no PhpWord
*
* @expectedException \PhpOffice\PhpWord\Exception\Exception
* @expectedExceptionMessage No PhpWord assigned.
*/
public function testWriteDocPropsAppNoPhpWord()
{
$object = new DocProps();
$object->writeDocPropsApp();
}
/**
* Test write docProps/core.xml with no PhpWord
*
* @expectedException \PhpOffice\PhpWord\Exception\Exception
* @expectedExceptionMessage No PhpWord assigned.
*/
public function testWriteDocPropsCoreNoPhpWord()
{
$object = new DocProps();
$object->writeDocPropsCore();
}
}

View File

@ -11,6 +11,7 @@ namespace PhpOffice\PhpWord\Tests\Writer\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Tests\TestHelperDOCX;
use PhpOffice\PhpWord\Writer\Word2007\Document;
/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Document
@ -27,6 +28,18 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
TestHelperDOCX::clear();
}
/**
* Test write word/document.xm with no PhpWord
*
* @expectedException \PhpOffice\PhpWord\Exception\Exception
* @expectedExceptionMessage No PhpWord assigned.
*/
public function testWriteDocumentNoPhpWord()
{
$object = new Document();
$object->writeDocument();
}
/**
* Write end section page numbering
*/

View File

@ -0,0 +1,68 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Tests\Writer\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Numbering as NumberingStyle;
use PhpOffice\PhpWord\Style\NumberingLevel;
use PhpOffice\PhpWord\Tests\TestHelperDOCX;
use PhpOffice\PhpWord\Writer\Word2007\Numbering;
/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Numbering
*
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Numbering
* @runTestsInSeparateProcesses
* @since 0.10.0
*/
class NumberingTest extends \PHPUnit_Framework_TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}
/**
* Write footnotes
*/
public function testWriteNumbering()
{
$xmlFile = 'word/numbering.xml';
$phpWord = new PhpWord();
$phpWord->addNumberingStyle(
'numStyle',
array(
'type' => 'multilevel',
'levels' => array(
array(
'start' => 1,
'format' => 'decimal',
'restart' => 1,
'suffix' => 'space',
'text' => '%1.',
'align' => 'left',
'left' => 360,
'hanging' => 360,
'tabPos' => 360,
'font' => 'Arial',
'hint' => 'default',
),
)
)
);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:numbering/w:abstractNum', $xmlFile));
}
}

View File

@ -9,6 +9,7 @@
namespace PhpOffice\PhpWord\Tests\Writer\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\Word2007\Styles;
use PhpOffice\PhpWord\Tests\TestHelperDOCX;
/**
@ -27,6 +28,18 @@ class StylesTest extends \PHPUnit_Framework_TestCase
TestHelperDOCX::clear();
}
/**
* Test construct with no PhpWord
*
* @expectedException \PhpOffice\PhpWord\Exception\Exception
* @expectedExceptionMessage No PhpWord assigned.
*/
public function testConstructNoPhpWord()
{
$object = new Styles();
$object->writeStyles();
}
/**
* Test write styles
*/