More unit tests for Template and Writer

This commit is contained in:
Ivan Lanin 2014-03-15 13:25:42 +07:00
parent d9c3cfcedf
commit 92a7a6b617
8 changed files with 214 additions and 35 deletions

View File

@ -27,6 +27,8 @@
/** /**
* PHPWord_Reader_Abstract * PHPWord_Reader_Abstract
*
* @codeCoverageIgnore Abstract class
*/ */
abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
{ {

View File

@ -174,6 +174,7 @@ class PHPWord_Writer_ODText implements PHPWord_Writer_IWriter
$objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
} }
} }
// @codeCoverageIgnoreEnd
// Close file // Close file
if ($objZip->close() === false) { if ($objZip->close() === false) {

View File

@ -101,6 +101,7 @@ class PHPWord_Writer_ODText_Manifest extends PHPWord_Writer_ODText_WriterPart
$objWriter->endElement(); $objWriter->endElement();
} }
} }
// @codeCoverageIgnoreEnd
$objWriter->endElement(); $objWriter->endElement();

View File

@ -2,9 +2,10 @@
namespace PHPWord\Tests; namespace PHPWord\Tests;
use PHPWord_Template; use PHPWord_Template;
use PHPWord;
/** /**
* @coversDefaultClass PHPWord_Template * @coversDefaultClass PHPWord_Template
*/ */
final class TemplateTest extends \PHPUnit_Framework_TestCase final class TemplateTest extends \PHPUnit_Framework_TestCase
{ {
@ -143,4 +144,32 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase
*/ */
@$template->applyXslStyleSheet($xslDOMDocument); @$template->applyXslStyleSheet($xslDOMDocument);
} }
/**
* @covers ::setValue
* @covers ::getVariables
* @covers ::cloneRow
* @covers ::saveAs
*/
public function testCloneRow()
{
$template = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'clone-row.docx')
);
$expectedVar = array('tableHeader', 'userId', 'userName', 'userLocation');
$docName = 'clone-test-result.docx';
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate($template);
$actualVar = $document->getVariables();
$document->cloneRow('userId', 1);
$document->setValue('userId#1', 'Test');
$document->saveAs($docName);
$docFound = file_exists($docName);
unlink($docName);
$this->assertEquals($expectedVar, $actualVar);
$this->assertTrue($docFound);
}
} }

View File

@ -7,7 +7,8 @@ use PHPWord;
/** /**
* Class ODTextTest * Class ODTextTest
* *
* @package PHPWord\Tests * @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_ODText
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class ODTextTest extends \PHPUnit_Framework_TestCase class ODTextTest extends \PHPUnit_Framework_TestCase
@ -37,10 +38,9 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Test construct with null value/without PHPWord * @covers ::getPHPWord
* * @expectedException Exception
* @expectedException Exception * @expectedExceptionMessage No PHPWord assigned.
* @expectedExceptionMessage No PHPWord assigned.
*/ */
public function testConstructWithNull() public function testConstructWithNull()
{ {
@ -49,39 +49,106 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Test save() * @covers ::save
*/ */
public function testSave() public function testSave()
{ {
$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')
);
$file = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.odt')
);
$phpWord = new PHPWord(); $phpWord = new PHPWord();
$phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addFontStyle('Font', array('size' => 11));
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$section->addText('Test 1', 'Font', 'Paragraph'); $section->addText('Test 1', 'Font');
$section->addTextBreak(); $section->addTextBreak();
$section->addText('Test 2'); $section->addText('Test 2', null, 'Paragraph');
$section->addLink('http://test.com');
$section->addTitle('Test', 1);
$section->addPageBreak();
$section->addTable();
$section->addListItem('Test');
$section->addImage($imageSrc);
$section->addObject($objectSrc);
$section->addTOC();
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$textrun = $section->createTextRun(); $textrun = $section->createTextRun();
$textrun->addText('Test 3'); $textrun->addText('Test 3');
$writer = new PHPWord_Writer_ODText($phpWord); $writer = new PHPWord_Writer_ODText($phpWord);
$file = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.odt')
);
$writer->save($file); $writer->save($file);
$this->assertTrue(file_exists($file)); $this->assertTrue(file_exists($file));
unlink($file); unlink($file);
} }
/** /**
* Test disk caching parameters * @covers ::save
* @todo Haven't got any method to test this
*/ */
public function testSetDiskCaching() public function testSavePhpOutput()
{
$phpWord = new PHPWord();
$section = $phpWord->createSection();
$section->addText('Test');
$writer = new PHPWord_Writer_ODText($phpWord);
$writer->save('php://output');
}
/**
* @covers ::save
* @expectedException Exception
* @expectedExceptionMessage PHPWord object unassigned.
*/
public function testSaveException()
{
$writer = new PHPWord_Writer_ODText();
$writer->save();
}
/**
* @covers ::getWriterPart
*/
public function testGetWriterPartNull()
{
$object = new PHPWord_Writer_ODText();
$this->assertNull($object->getWriterPart('foo'));
}
/**
* @covers ::setUseDiskCaching
* @covers ::getUseDiskCaching
*/
public function testSetGetUseDiskCaching()
{ {
$object = new PHPWord_Writer_ODText(); $object = new PHPWord_Writer_ODText();
$object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT); $object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);
$this->assertTrue($object->getUseDiskCaching()); $this->assertTrue($object->getUseDiskCaching());
$this->assertEquals(PHPWORD_TESTS_DIR_ROOT, $object->getDiskCachingDirectory()); $this->assertEquals(PHPWORD_TESTS_DIR_ROOT, $object->getDiskCachingDirectory());
} }
/**
* @covers ::setUseDiskCaching
* @expectedException Exception
*/
public function testSetUseDiskCachingException()
{
$dir = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, 'foo')
);
$object = new PHPWord_Writer_ODText($phpWord);
$object->setUseDiskCaching(true, $dir);
}
} }

View File

@ -6,13 +6,15 @@ use PHPWord;
/** /**
* Class RTFTest * Class RTFTest
* @package PHPWord\Tests *
* @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_RTF
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class RTFTest extends \PHPUnit_Framework_TestCase class RTFTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* Test construct * covers ::construct
*/ */
public function testConstruct() public function testConstruct()
{ {
@ -23,10 +25,9 @@ class RTFTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Test construct with null value/without PHPWord * covers ::__construct
* * @expectedException Exception
* @expectedException Exception * @expectedExceptionMessage No PHPWord assigned.
* @expectedExceptionMessage No PHPWord assigned.
*/ */
public function testConstructWithNull() public function testConstructWithNull()
{ {
@ -35,28 +36,72 @@ class RTFTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Test save() * @covers ::save
* @todo Haven't got any method to test this
*/
public function testSavePhpOutput()
{
$phpWord = new PHPWord();
$section = $phpWord->createSection();
$section->addText('Test');
$writer = new PHPWord_Writer_RTF($phpWord);
$writer->save('php://output');
}
/**
* @covers ::save
* @expectedException Exception
* @expectedExceptionMessage PHPWord object unassigned.
*/
public function testSaveException()
{
$writer = new PHPWord_Writer_RTF();
$writer->save();
}
/**
* @covers ::save
* @covers ::<private>
*/ */
public function testSave() public function testSave()
{ {
$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')
);
$file = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf')
);
$phpWord = new PHPWord(); $phpWord = new PHPWord();
$phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addFontStyle('Font', array('size' => 11));
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$section->addText('Test 1', 'Font', 'Paragraph'); $section->addText('Test 1', 'Font');
$section->addTextBreak(); $section->addTextBreak();
$section->addText('Test 2'); $section->addText('Test 2', null, 'Paragraph');
$section->addLink('http://test.com');
$section->addTitle('Test', 1);
$section->addPageBreak();
$section->addTable();
$section->addListItem('Test');
$section->addImage($imageSrc);
$section->addObject($objectSrc);
$section->addTOC();
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$textrun = $section->createTextRun(); $textrun = $section->createTextRun();
$textrun->addText('Test 3'); $textrun->addText('Test 3');
$textrun->addTextBreak();
$writer = new PHPWord_Writer_RTF($phpWord); $writer = new PHPWord_Writer_RTF($phpWord);
$file = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf')
);
$writer->save($file); $writer->save($file);
$this->assertTrue(file_exists($file)); $this->assertTrue(file_exists($file));
unlink($file); unlink($file);
} }
} }

View File

@ -8,7 +8,8 @@ use PHPWord\Tests\TestHelperDOCX;
/** /**
* Class Word2007Test * Class Word2007Test
* *
* @package PHPWord\Tests * @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_Word2007
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class Word2007Test extends \PHPUnit_Framework_TestCase class Word2007Test extends \PHPUnit_Framework_TestCase
@ -18,6 +19,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
TestHelperDOCX::clear(); TestHelperDOCX::clear();
} }
/**
* covers ::__construct
*/
public function testConstruct() public function testConstruct()
{ {
$object = new PHPWord_Writer_Word2007(new PHPWord()); $object = new PHPWord_Writer_Word2007(new PHPWord());
@ -37,6 +41,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
} }
} }
/**
* @covers ::save
*/
public function testSave() public function testSave()
{ {
$phpWord = new PHPWord(); $phpWord = new PHPWord();
@ -51,9 +58,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
$textrun->addText('Test 3'); $textrun->addText('Test 3');
$writer = new PHPWord_Writer_Word2007($phpWord); $writer = new PHPWord_Writer_Word2007($phpWord);
$file = join( $file = \join(
DIRECTORY_SEPARATOR, \DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.docx') array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.docx')
); );
$writer->save($file); $writer->save($file);
$this->assertTrue(file_exists($file)); $this->assertTrue(file_exists($file));
@ -61,7 +68,7 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers PHPWord_Writer_Word2007::checkContentTypes * @covers ::checkContentTypes
*/ */
public function testCheckContentTypes() public function testCheckContentTypes()
{ {
@ -89,4 +96,31 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
); );
} }
} }
/**
* @covers ::setUseDiskCaching
* @covers ::getUseDiskCaching
*/
public function testSetGetUseDiskCaching()
{
$object = new PHPWord_Writer_Word2007($phpWord);
$object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);
$this->assertTrue($object->getUseDiskCaching());
}
/**
* @covers ::setUseDiskCaching
* @expectedException Exception
*/
public function testSetUseDiskCachingException()
{
$dir = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, 'foo')
);
$object = new PHPWord_Writer_Word2007($phpWord);
$object->setUseDiskCaching(true, $dir);
}
} }

Binary file not shown.