Simple unit tests for ODText, RTF, and Word2007 Writer

This commit is contained in:
Ivan Lanin 2014-03-12 13:32:49 +07:00
parent 92cfa12d71
commit 8f54fa4cc2
3 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,88 @@
<?php
namespace PHPWord\Tests\Writer;
use PHPUnit_Framework_TestCase;
use PHPWord_Writer_ODText;
use PHPWord;
/**
* Class ODTextTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class ODTextTest extends \PHPUnit_Framework_TestCase
{
/**
* Test construct
*/
public function testConstruct()
{
$object = new PHPWord_Writer_ODText(new PHPWord());
$this->assertInstanceOf('PHPWord', $object->getPHPWord());
$this->assertInstanceOf("PHPWord_HashTable", $object->getDrawingHashTable());
$this->assertEquals('./', $object->getDiskCachingDirectory());
$writerParts = array('Content', 'Manifest', 'Meta', 'Mimetype', 'Styles');
foreach ($writerParts as $part) {
$this->assertInstanceOf(
"PHPWord_Writer_ODText_{$part}",
$object->getWriterPart($part)
);
$this->assertInstanceOf(
"PHPWord_Writer_ODText",
$object->getWriterPart($part)->getParentWriter()
);
}
}
/**
* Test construct with null value/without PHPWord
*
* @expectedException Exception
* @expectedExceptionMessage No PHPWord assigned.
*/
public function testConstructWithNull()
{
$object = new PHPWord_Writer_ODText();
$object->getPHPWord();
}
/**
* Test save()
*/
public function testSave()
{
$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->addTextBreak();
$section->addText('Test 2');
$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
*/
public function testSetDiskCaching()
{
$object = new PHPWord_Writer_ODText();
$object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);
$this->assertTrue($object->getUseDiskCaching());
$this->assertEquals(PHPWORD_TESTS_DIR_ROOT, $object->getDiskCachingDirectory());
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace PHPWord\Tests\Writer;
use PHPUnit_Framework_TestCase;
use PHPWord_Writer_RTF;
use PHPWord;
/**
* Class RTFTest
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class RTFTest extends \PHPUnit_Framework_TestCase
{
/**
* Test construct
*/
public function testConstruct()
{
$object = new PHPWord_Writer_RTF(new PHPWord);
$this->assertInstanceOf('PHPWord', $object->getPHPWord());
$this->assertInstanceOf("PHPWord_HashTable", $object->getDrawingHashTable());
}
/**
* Test construct with null value/without PHPWord
*
* @expectedException Exception
* @expectedExceptionMessage No PHPWord assigned.
*/
public function testConstructWithNull()
{
$object = new PHPWord_Writer_RTF();
$object->getPHPWord();
}
/**
* Test save()
*/
public function testSave()
{
$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->addTextBreak();
$section->addText('Test 2');
$section = $phpWord->createSection();
$textrun = $section->createTextRun();
$textrun->addText('Test 3');
$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);
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace PHPWord\Tests\Writer;
use PHPUnit_Framework_TestCase;
use PHPWord_Writer_Word2007;
use PHPWord;
/**
* Class Word2007Test
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class Word2007Test extends \PHPUnit_Framework_TestCase
{
/**
* Test construct
*/
public function testConstruct()
{
$object = new PHPWord_Writer_Word2007(new PHPWord());
$writerParts = array('ContentTypes', 'Rels', 'DocProps',
'DocumentRels', 'Document', 'Styles', 'Header', 'Footer',
'Footnotes', 'FootnotesRels');
foreach ($writerParts as $part) {
$this->assertInstanceOf(
"PHPWord_Writer_Word2007_{$part}",
$object->getWriterPart($part)
);
$this->assertInstanceOf(
"PHPWord_Writer_Word2007",
$object->getWriterPart($part)->getParentWriter()
);
}
}
/**
* Test save()
*/
public function testSave()
{
$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->addTextBreak();
$section->addText('Test 2');
$section = $phpWord->createSection();
$textrun = $section->createTextRun();
$textrun->addText('Test 3');
$writer = new PHPWord_Writer_Word2007($phpWord);
$file = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.docx')
);
$writer->save($file);
$this->assertTrue(file_exists($file));
unlink($file);
}
}