More unit tests for Template and Writer
This commit is contained in:
parent
d9c3cfcedf
commit
92a7a6b617
|
|
@ -27,6 +27,8 @@
|
|||
|
||||
/**
|
||||
* PHPWord_Reader_Abstract
|
||||
*
|
||||
* @codeCoverageIgnore Abstract class
|
||||
*/
|
||||
abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
|
||||
{
|
||||
|
|
|
|||
|
|
@ -174,6 +174,7 @@ class PHPWord_Writer_ODText implements PHPWord_Writer_IWriter
|
|||
$objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
|
||||
}
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
// Close file
|
||||
if ($objZip->close() === false) {
|
||||
|
|
|
|||
|
|
@ -101,6 +101,7 @@ class PHPWord_Writer_ODText_Manifest extends PHPWord_Writer_ODText_WriterPart
|
|||
$objWriter->endElement();
|
||||
}
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
$objWriter->endElement();
|
||||
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@
|
|||
namespace PHPWord\Tests;
|
||||
|
||||
use PHPWord_Template;
|
||||
use PHPWord;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass PHPWord_Template
|
||||
* @coversDefaultClass PHPWord_Template
|
||||
*/
|
||||
final class TemplateTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
|
@ -143,4 +144,32 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase
|
|||
*/
|
||||
@$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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ use PHPWord;
|
|||
/**
|
||||
* Class ODTextTest
|
||||
*
|
||||
* @package PHPWord\Tests
|
||||
* @package PHPWord\Tests
|
||||
* @coversDefaultClass PHPWord_Writer_ODText
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class ODTextTest extends \PHPUnit_Framework_TestCase
|
||||
|
|
@ -37,10 +38,9 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* Test construct with null value/without PHPWord
|
||||
*
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage No PHPWord assigned.
|
||||
* @covers ::getPHPWord
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage No PHPWord assigned.
|
||||
*/
|
||||
public function testConstructWithNull()
|
||||
{
|
||||
|
|
@ -49,39 +49,106 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* Test save()
|
||||
* @covers ::save
|
||||
*/
|
||||
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->addFontStyle('Font', array('size' => 11));
|
||||
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
|
||||
$section = $phpWord->createSection();
|
||||
$section->addText('Test 1', 'Font', 'Paragraph');
|
||||
$section->addText('Test 1', 'Font');
|
||||
$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();
|
||||
$textrun = $section->createTextRun();
|
||||
$textrun->addText('Test 3');
|
||||
|
||||
$writer = new PHPWord_Writer_ODText($phpWord);
|
||||
$file = join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.odt')
|
||||
);
|
||||
$writer->save($file);
|
||||
|
||||
$this->assertTrue(file_exists($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->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);
|
||||
$this->assertTrue($object->getUseDiskCaching());
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,13 +6,15 @@ use PHPWord;
|
|||
|
||||
/**
|
||||
* Class RTFTest
|
||||
* @package PHPWord\Tests
|
||||
*
|
||||
* @package PHPWord\Tests
|
||||
* @coversDefaultClass PHPWord_Writer_RTF
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class RTFTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Test construct
|
||||
* covers ::construct
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
|
|
@ -23,10 +25,9 @@ class RTFTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* Test construct with null value/without PHPWord
|
||||
*
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage No PHPWord assigned.
|
||||
* covers ::__construct
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage No PHPWord assigned.
|
||||
*/
|
||||
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()
|
||||
{
|
||||
$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->addFontStyle('Font', array('size' => 11));
|
||||
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
|
||||
$section = $phpWord->createSection();
|
||||
$section->addText('Test 1', 'Font', 'Paragraph');
|
||||
$section->addText('Test 1', 'Font');
|
||||
$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();
|
||||
$textrun = $section->createTextRun();
|
||||
$textrun->addText('Test 3');
|
||||
|
||||
$textrun->addTextBreak();
|
||||
$writer = new PHPWord_Writer_RTF($phpWord);
|
||||
$file = join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf')
|
||||
);
|
||||
$writer->save($file);
|
||||
|
||||
$this->assertTrue(file_exists($file));
|
||||
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ use PHPWord\Tests\TestHelperDOCX;
|
|||
/**
|
||||
* Class Word2007Test
|
||||
*
|
||||
* @package PHPWord\Tests
|
||||
* @package PHPWord\Tests
|
||||
* @coversDefaultClass PHPWord_Writer_Word2007
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class Word2007Test extends \PHPUnit_Framework_TestCase
|
||||
|
|
@ -18,6 +19,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
|
|||
TestHelperDOCX::clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* covers ::__construct
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
$object = new PHPWord_Writer_Word2007(new PHPWord());
|
||||
|
|
@ -37,6 +41,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::save
|
||||
*/
|
||||
public function testSave()
|
||||
{
|
||||
$phpWord = new PHPWord();
|
||||
|
|
@ -51,9 +58,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
|
|||
$textrun->addText('Test 3');
|
||||
|
||||
$writer = new PHPWord_Writer_Word2007($phpWord);
|
||||
$file = join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.docx')
|
||||
$file = \join(
|
||||
\DIRECTORY_SEPARATOR,
|
||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.docx')
|
||||
);
|
||||
$writer->save($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()
|
||||
{
|
||||
|
|
@ -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.
Loading…
Reference in New Issue