New unit test for ODText\Content and some test fixes

This commit is contained in:
Ivan Lanin 2014-03-15 14:48:26 +07:00
parent c2a6c783a2
commit d04e600300
9 changed files with 279 additions and 160 deletions

View File

@ -254,24 +254,24 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
} elseif ($element instanceof PHPWord_Section_TextBreak) { } elseif ($element instanceof PHPWord_Section_TextBreak) {
$this->_writeTextBreak($objWriter); $this->_writeTextBreak($objWriter);
} elseif ($element instanceof PHPWord_Section_Link) { } elseif ($element instanceof PHPWord_Section_Link) {
$this->writeUnsupportedElement($objWriter, 'link'); $this->writeUnsupportedElement($objWriter, 'Link');
} elseif ($element instanceof PHPWord_Section_Title) { } elseif ($element instanceof PHPWord_Section_Title) {
$this->writeUnsupportedElement($objWriter, 'title'); $this->writeUnsupportedElement($objWriter, 'Title');
} elseif ($element instanceof PHPWord_Section_PageBreak) { } elseif ($element instanceof PHPWord_Section_PageBreak) {
$this->writeUnsupportedElement($objWriter, 'page break'); $this->writeUnsupportedElement($objWriter, 'Page Break');
} elseif ($element instanceof PHPWord_Section_Table) { } elseif ($element instanceof PHPWord_Section_Table) {
$this->writeUnsupportedElement($objWriter, 'table'); $this->writeUnsupportedElement($objWriter, 'Table');
} elseif ($element instanceof PHPWord_Section_ListItem) { } elseif ($element instanceof PHPWord_Section_ListItem) {
$this->writeUnsupportedElement($objWriter, 'list item'); $this->writeUnsupportedElement($objWriter, 'List Item');
} elseif ($element instanceof PHPWord_Section_Image || } elseif ($element instanceof PHPWord_Section_Image ||
$element instanceof PHPWord_Section_MemoryImage) { $element instanceof PHPWord_Section_MemoryImage) {
$this->writeUnsupportedElement($objWriter, 'image'); $this->writeUnsupportedElement($objWriter, 'Image');
} elseif ($element instanceof PHPWord_Section_Object) { } elseif ($element instanceof PHPWord_Section_Object) {
$this->writeUnsupportedElement($objWriter, 'object'); $this->writeUnsupportedElement($objWriter, 'Object');
} elseif ($element instanceof PHPWord_TOC) { } elseif ($element instanceof PHPWord_TOC) {
$this->writeUnsupportedElement($objWriter, 'TOC'); $this->writeUnsupportedElement($objWriter, 'TOC');
} else { } else {
$this->writeUnsupportedElement($objWriter, 'other'); $this->writeUnsupportedElement($objWriter, 'Element');
} }
} }
@ -372,18 +372,15 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
$objWriter->endElement(); $objWriter->endElement();
} }
// @codeCoverageIgnoreStart
private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null) private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null)
{ {
} }
/**
* Dummy function just to make all samples produce ODT
*
* @todo Create the real function
*/
private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null) private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null)
{ {
} }
// @codeCoverageIgnoreEnd
/** /**
* Write unsupported element * Write unsupported element
@ -394,7 +391,7 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
private function writeUnsupportedElement($objWriter, $element) private function writeUnsupportedElement($objWriter, $element)
{ {
$objWriter->startElement('text:p'); $objWriter->startElement('text:p');
$objWriter->writeRaw("Cannot write content. This version of PHPWord has not supported {$element} element in ODText."); $objWriter->writeRaw("{$element}");
$objWriter->endElement(); $objWriter->endElement();
} }
} }

View File

@ -315,24 +315,24 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
} 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');
} }
} }
} }
@ -468,7 +468,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
{ {
$sRTFText = ''; $sRTFText = '';
$sRTFText .= '\pard\nowidctlpar' . PHP_EOL; $sRTFText .= '\pard\nowidctlpar' . PHP_EOL;
$sRTFText .= "Cannot write content. This version of PHPWord has not supported {$element} element in RTF."; $sRTFText .= "{$element}";
$sRTFText .= '\par' . PHP_EOL; $sRTFText .= '\par' . PHP_EOL;
return $sRTFText; return $sRTFText;

View File

@ -79,7 +79,7 @@ We're reorganizing our documentation. Below are some of the most important thing
<a name="basic-usage"></a> <a name="basic-usage"></a>
#### Basic usage #### Basic usage
The following is a basic example of the PHPWord library. More examples are provided in the (sample folder)[samples/]. The following is a basic example of the PHPWord library. More examples are provided in the [samples folder](samples/).
```php ```php
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();

View File

@ -3,6 +3,13 @@ namespace PHPWord\Tests;
use PHPWord_DocumentProperties; use PHPWord_DocumentProperties;
/**
* Class DocumentPropertiesTest
*
* @package PHPWord\Tests
* @coversDefaultClass PHPWord_DocumentProperties
* @runTestsInSeparateProcesses
*/
class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
{ {
public function testCreator() public function testCreator()
@ -125,11 +132,26 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$oProperties->setCustomProperty('key3', 3); $oProperties->setCustomProperty('key3', 3);
$oProperties->setCustomProperty('key4', 4.4); $oProperties->setCustomProperty('key4', 4.4);
$oProperties->setCustomProperty('key5', 'value5'); $oProperties->setCustomProperty('key5', 'value5');
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, $oProperties->getCustomPropertyType('key1')); $this->assertEquals(
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_BOOLEAN, $oProperties->getCustomPropertyType('key2')); PHPWord_DocumentProperties::PROPERTY_TYPE_STRING,
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, $oProperties->getCustomPropertyType('key3')); $oProperties->getCustomPropertyType('key1')
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_FLOAT, $oProperties->getCustomPropertyType('key4')); );
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, $oProperties->getCustomPropertyType('key5')); $this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_BOOLEAN,
$oProperties->getCustomPropertyType('key2')
);
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER,
$oProperties->getCustomPropertyType('key3')
);
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_FLOAT,
$oProperties->getCustomPropertyType('key4')
);
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_STRING,
$oProperties->getCustomPropertyType('key5')
);
$this->assertEquals(null, $oProperties->getCustomPropertyType('key6')); $this->assertEquals(null, $oProperties->getCustomPropertyType('key6'));
$this->assertEquals(null, $oProperties->getCustomPropertyValue('key1')); $this->assertEquals(null, $oProperties->getCustomPropertyValue('key1'));
$this->assertEquals(true, $oProperties->getCustomPropertyValue('key2')); $this->assertEquals(true, $oProperties->getCustomPropertyValue('key2'));
@ -139,7 +161,13 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(null, $oProperties->getCustomPropertyValue('key6')); $this->assertEquals(null, $oProperties->getCustomPropertyValue('key6'));
$this->assertEquals(true, $oProperties->isCustomPropertySet('key5')); $this->assertEquals(true, $oProperties->isCustomPropertySet('key5'));
$this->assertEquals(false, $oProperties->isCustomPropertySet('key6')); $this->assertEquals(false, $oProperties->isCustomPropertySet('key6'));
$this->assertEquals(array('key1', 'key2', 'key3', 'key4', 'key5'), $oProperties->getCustomProperties()); $this->assertEquals(array(
'key1',
'key2',
'key3',
'key4',
'key5'
), $oProperties->getCustomProperties());
} }
public function testConvertProperty() public function testConvertProperty()
@ -157,13 +185,37 @@ class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('1', PHPWord_DocumentProperties::convertProperty('1', '')); $this->assertEquals('1', PHPWord_DocumentProperties::convertProperty('1', ''));
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, PHPWord_DocumentProperties::convertPropertyType('int')); $this->assertEquals(
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, PHPWord_DocumentProperties::convertPropertyType('uint')); PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER,
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_FLOAT, PHPWord_DocumentProperties::convertPropertyType('decimal')); PHPWord_DocumentProperties::convertPropertyType('int')
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, PHPWord_DocumentProperties::convertPropertyType('lpstr')); );
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_DATE, PHPWord_DocumentProperties::convertPropertyType('date')); $this->assertEquals(
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_BOOLEAN, PHPWord_DocumentProperties::convertPropertyType('bool')); PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER,
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_UNKNOWN, PHPWord_DocumentProperties::convertPropertyType('array')); PHPWord_DocumentProperties::convertPropertyType('uint')
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_UNKNOWN, PHPWord_DocumentProperties::convertPropertyType('')); );
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_FLOAT,
PHPWord_DocumentProperties::convertPropertyType('decimal')
);
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_STRING,
PHPWord_DocumentProperties::convertPropertyType('lpstr')
);
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_DATE,
PHPWord_DocumentProperties::convertPropertyType('date')
);
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_BOOLEAN,
PHPWord_DocumentProperties::convertPropertyType('bool')
);
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_UNKNOWN,
PHPWord_DocumentProperties::convertPropertyType('array')
);
$this->assertEquals(
PHPWord_DocumentProperties::PROPERTY_TYPE_UNKNOWN,
PHPWord_DocumentProperties::convertPropertyType('')
);
} }
} }

View File

@ -7,6 +7,7 @@ use PHPWord_Shared_File;
* Class FileTest * Class FileTest
* *
* @package PHPWord\Tests * @package PHPWord\Tests
* @coversDefaultClass PHPWord_Shared_File
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class FileTest extends \PHPUnit_Framework_TestCase class FileTest extends \PHPUnit_Framework_TestCase
@ -16,10 +17,11 @@ class FileTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFileExists() public function testFileExists()
{ {
$dir = join( $dir = join(DIRECTORY_SEPARATOR, array(
DIRECTORY_SEPARATOR, PHPWORD_TESTS_DIR_ROOT,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates') '_files',
); 'templates'
));
chdir($dir); chdir($dir);
$this->assertTrue(PHPWord_Shared_File::file_exists('blank.docx')); $this->assertTrue(PHPWord_Shared_File::file_exists('blank.docx'));
} }
@ -28,10 +30,11 @@ class FileTest extends \PHPUnit_Framework_TestCase
*/ */
public function testNoFileExists() public function testNoFileExists()
{ {
$dir = join( $dir = join(DIRECTORY_SEPARATOR, array(
DIRECTORY_SEPARATOR, PHPWORD_TESTS_DIR_ROOT,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates') '_files',
); 'templates'
));
chdir($dir); chdir($dir);
$this->assertFalse(PHPWord_Shared_File::file_exists('404.docx')); $this->assertFalse(PHPWord_Shared_File::file_exists('404.docx'));
} }
@ -41,10 +44,11 @@ class FileTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRealpath() public function testRealpath()
{ {
$dir = join( $dir = join(DIRECTORY_SEPARATOR, array(
DIRECTORY_SEPARATOR, PHPWORD_TESTS_DIR_ROOT,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates') '_files',
); 'templates'
));
chdir($dir); chdir($dir);
$file = 'blank.docx'; $file = 'blank.docx';
$expected = $dir . DIRECTORY_SEPARATOR . $file; $expected = $dir . DIRECTORY_SEPARATOR . $file;

View File

@ -7,6 +7,7 @@ use PHPWord_Shared_String;
* Class StringTest * Class StringTest
* *
* @package PHPWord\Tests * @package PHPWord\Tests
* @coversDefaultClass PHPWord_Shared_String
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class StringTest extends \PHPUnit_Framework_TestCase class StringTest extends \PHPUnit_Framework_TestCase

View File

@ -0,0 +1,65 @@
<?php
namespace PHPWord\Tests\Writer\ODText;
use PHPWord;
use PHPWord_Style;
use PHPWord\Tests\TestHelperDOCX;
/**
* Class ContentTest
*
* @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_ODText_Content
* @runTestsInSeparateProcesses
*/
class ContentTest extends \PHPUnit_Framework_TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}
/**
* covers ::writeContent
* covers <private>
*/
public function testWriteContent()
{
$imageSrc = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png')
);
$objectSrc = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'sheet.xls')
);
$expected = 'Expected';
$PHPWord = new PHPWord();
$PHPWord->setDefaultFontName('Verdana');
$PHPWord->addFontStyle('Font', array('size' => 11));
$PHPWord->addParagraphStyle('Paragraph', array('align' => 'center'));
$section = $PHPWord->createSection();
$section->addText($expected);
$section->addText('Test font style', 'Font');
$section->addText('Test paragraph style', null, 'Paragraph');
$section->addTextBreak();
$section->addLink('http://test.com', 'Test link');
$section->addTitle('Test title', 1);
$section->addPageBreak();
$section->addTable();
$section->addListItem('Test list item');
$section->addImage($imageSrc);
$section->addObject($objectSrc);
$section->addTOC();
$textrun = $section->createTextRun();
$textrun->addText('Test text run');
$doc = TestHelperDOCX::getDocument($PHPWord, 'ODText');
$element = "/office:document-content/office:body/office:text/text:p";
$this->assertEquals($expected, $doc->getElement($element, 'content.xml')->nodeValue);
}
}

View File

@ -103,7 +103,7 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
*/ */
public function testSetGetUseDiskCaching() public function testSetGetUseDiskCaching()
{ {
$object = new PHPWord_Writer_Word2007($phpWord); $object = new PHPWord_Writer_Word2007();
$object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT); $object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);
$this->assertTrue($object->getUseDiskCaching()); $this->assertTrue($object->getUseDiskCaching());
@ -120,7 +120,7 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
array(\PHPWORD_TESTS_DIR_ROOT, 'foo') array(\PHPWORD_TESTS_DIR_ROOT, 'foo')
); );
$object = new PHPWord_Writer_Word2007($phpWord); $object = new PHPWord_Writer_Word2007();
$object->setUseDiskCaching(true, $dir); $object->setUseDiskCaching(true, $dir);
} }
} }

View File

@ -12,14 +12,14 @@ class TestHelperDOCX
* @param \PHPWord $PHPWord * @param \PHPWord $PHPWord
* @return \PHPWord\Tests\XmlDocument * @return \PHPWord\Tests\XmlDocument
*/ */
public static function getDocument(PHPWord $PHPWord) public static function getDocument(PHPWord $PHPWord, $writer = 'Word2007')
{ {
self::$file = tempnam(sys_get_temp_dir(), 'PHPWord'); self::$file = tempnam(sys_get_temp_dir(), 'PHPWord');
if (!is_dir(sys_get_temp_dir() . '/PHPWord_Unit_Test/')) { if (!is_dir(sys_get_temp_dir() . '/PHPWord_Unit_Test/')) {
mkdir(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); mkdir(sys_get_temp_dir() . '/PHPWord_Unit_Test/');
} }
$objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter = \PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save(self::$file); $objWriter->save(self::$file);
$zip = new \ZipArchive; $zip = new \ZipArchive;