Writer unit test enhancements

This commit is contained in:
Ivan Lanin 2014-03-29 22:26:00 +07:00
parent 255af437f2
commit c91d6b61c9
6 changed files with 134 additions and 67 deletions

View File

@ -438,7 +438,7 @@ class RTF implements IWriter
if ($styleFont->getBold()) { if ($styleFont->getBold()) {
$sRTFText .= '\b'; $sRTFText .= '\b';
} }
if ($styleFont->getBold()) { if ($styleFont->getItalic()) {
$sRTFText .= '\i'; $sRTFText .= '\i';
} }
if ($styleFont->getSize()) { if ($styleFont->getSize()) {

View File

@ -14,13 +14,12 @@ use PhpOffice\PhpWord\Writer\ODText;
/** /**
* Test class for PhpOffice\PhpWord\Writer\ODText * Test class for PhpOffice\PhpWord\Writer\ODText
* *
* @coversDefaultClass \PhpOffice\PhpWord\Writer\ODText
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class ODTextTest extends \PHPUnit_Framework_TestCase class ODTextTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* Test construct * Construct
*/ */
public function testConstruct() public function testConstruct()
{ {
@ -43,9 +42,10 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::getPhpWord * Construct with null
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception *
* @expectedExceptionMessage No PhpWord assigned. * @expectedException \PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage No PhpWord assigned.
*/ */
public function testConstructWithNull() public function testConstructWithNull()
{ {
@ -54,7 +54,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::save * Save
*/ */
public function testSave() public function testSave()
{ {
@ -89,7 +89,8 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::save * Save php output
*
* @todo Haven't got any method to test this * @todo Haven't got any method to test this
*/ */
public function testSavePhpOutput() public function testSavePhpOutput()
@ -102,8 +103,9 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::save * Save with no PhpWord object assigned
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception *
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage PhpWord object unassigned. * @expectedExceptionMessage PhpWord object unassigned.
*/ */
public function testSaveException() public function testSaveException()
@ -113,7 +115,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::getWriterPart * Get writer part return null value
*/ */
public function testGetWriterPartNull() public function testGetWriterPartNull()
{ {
@ -122,8 +124,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::setUseDiskCaching * Set/get use disk caching
* @covers ::getUseDiskCaching
*/ */
public function testSetGetUseDiskCaching() public function testSetGetUseDiskCaching()
{ {
@ -134,7 +135,8 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::setUseDiskCaching * Use disk caching exception
*
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception * @expectedException \PhpOffice\PhpWord\Exceptions\Exception
*/ */
public function testSetUseDiskCachingException() public function testSetUseDiskCachingException()

View File

@ -14,13 +14,12 @@ use PhpOffice\PhpWord\Writer\RTF;
/** /**
* Test class for PhpOffice\PhpWord\Writer\RTF * Test class for PhpOffice\PhpWord\Writer\RTF
* *
* @coversDefaultClass \PhpOffice\PhpWord\Writer\RTF
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class RTFTest extends \PHPUnit_Framework_TestCase class RTFTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* covers ::construct * Construct
*/ */
public function testConstruct() public function testConstruct()
{ {
@ -31,8 +30,9 @@ class RTFTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* covers ::__construct * Construct with null
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception *
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage No PhpWord assigned. * @expectedExceptionMessage No PhpWord assigned.
*/ */
public function testConstructWithNull() public function testConstructWithNull()
@ -42,32 +42,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::save * Save
* @todo Haven't got any method to test this
*/
public function testSavePhpOutput()
{
$phpWord = new PhpWord();
$section = $phpWord->createSection();
$section->addText('Test');
$writer = new RTF($phpWord);
$writer->save('php://output');
}
/**
* @covers ::save
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage PhpWord object unassigned.
*/
public function testSaveException()
{
$writer = new RTF();
$writer->save();
}
/**
* @covers ::save
* @covers ::<private>
*/ */
public function testSave() public function testSave()
{ {
@ -76,12 +51,12 @@ class RTFTest extends \PHPUnit_Framework_TestCase
$file = __DIR__ . "/../_files/temp.rtf"; $file = __DIR__ . "/../_files/temp.rtf";
$phpWord = new PhpWord(); $phpWord = new PhpWord();
$phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, 'color' => 'FF0000', 'fgColor' => 'FF0000'));
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$section->addText('Test 1', 'Font'); $section->addText('Test 1', 'Font', 'Paragraph');
$section->addTextBreak(); $section->addTextBreak();
$section->addText('Test 2', null, 'Paragraph'); $section->addText('Test 2', array('name' => 'Tahoma', 'bold' => true, 'italic' => true));
$section->addLink('http://test.com'); $section->addLink('http://test.com');
$section->addTitle('Test', 1); $section->addTitle('Test', 1);
$section->addPageBreak(); $section->addPageBreak();
@ -101,4 +76,30 @@ class RTFTest extends \PHPUnit_Framework_TestCase
unlink($file); unlink($file);
} }
/**
* Save
*
* @todo Haven't got any method to test this
*/
public function testSavePhpOutput()
{
$phpWord = new PhpWord();
$section = $phpWord->createSection();
$section->addText('Test');
$writer = new RTF($phpWord);
$writer->save('php://output');
}
/**
* Save with no PhpWord object assigned
*
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage PhpWord object unassigned.
*/
public function testSaveException()
{
$writer = new RTF();
$writer->save();
}
} }

View File

@ -65,7 +65,8 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$textrun->addTextBreak(); $textrun->addTextBreak();
$textrun = $section->createTextRun($aStyle); $textrun = $section->createTextRun($aStyle);
$textrun->addLink('http://test.com'); $textrun->addLink('http://test.com');
$textrun->addImage($imageSrc); $textrun->addImage($imageSrc, array('align' => 'top'));
$textrun->createFootnote();
$doc = TestHelperDOCX::getDocument($phpWord); $doc = TestHelperDOCX::getDocument($phpWord);
$parent = "/w:document/w:body/w:p"; $parent = "/w:document/w:body/w:p";
@ -80,12 +81,14 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$phpWord = new PhpWord(); $phpWord = new PhpWord();
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$fontStyleArray = array('bold' => true); $fontStyleArray = array('bold' => true);
$fontStyleName = 'Test'; $fontStyleName = 'Font Style';
$paragraphStyleArray = array('align' => 'center');
$paragraphStyleName = 'Paragraph Style';
$expected = 'PhpWord'; $expected = 'PhpWord';
$section->addLink('http://github.com/phpoffice/phpword', $expected); $section->addLink('http://github.com/phpoffice/phpword', $expected);
$section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleArray); $section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleArray, $paragraphStyleArray);
$section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleName); $section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleName, $paragraphStyleName);
$doc = TestHelperDOCX::getDocument($phpWord); $doc = TestHelperDOCX::getDocument($phpWord);
$element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t'); $element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t');
@ -230,6 +233,10 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$tWidth = 120; $tWidth = 120;
$rHeight = 120; $rHeight = 120;
$cWidth = 120; $cWidth = 120;
$imageSrc = __DIR__ . "/../../_files/images/earth.jpg";
$objectSrc = __DIR__ . "/../../_files/documents/sheet.xls";
$tStyles["width"] = 50;
$tStyles["cellMarginTop"] = 120; $tStyles["cellMarginTop"] = 120;
$tStyles["cellMarginRight"] = 120; $tStyles["cellMarginRight"] = 120;
$tStyles["cellMarginBottom"] = 120; $tStyles["cellMarginBottom"] = 120;
@ -247,6 +254,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$cStyles["borderBottomColor"] = 'FF0000'; $cStyles["borderBottomColor"] = 'FF0000';
$cStyles["borderLeftColor"] = 'FF0000'; $cStyles["borderLeftColor"] = 'FF0000';
$cStyles["borderRightColor"] = 'FF0000'; $cStyles["borderRightColor"] = 'FF0000';
$cStyles["vMerge"] = 'restart';
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$table = $section->addTable($tStyles); $table = $section->addTable($tStyles);
@ -257,6 +265,8 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$cell->addTextBreak(); $cell->addTextBreak();
$cell->addLink('http://google.com'); $cell->addLink('http://google.com');
$cell->addListItem('Test'); $cell->addListItem('Test');
$cell->addImage($imageSrc);
$cell->addObject($objectSrc);
$textrun = $cell->createTextRun(); $textrun = $cell->createTextRun();
$textrun->addText('Test'); $textrun->addText('Test');

View File

@ -14,7 +14,6 @@ use PhpOffice\PhpWord\Tests\TestHelperDOCX;
/** /**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Document * Test class for PhpOffice\PhpWord\Writer\Word2007\Document
* *
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Document
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class DocumentTest extends \PHPUnit_Framework_TestCase class DocumentTest extends \PHPUnit_Framework_TestCase
@ -27,6 +26,9 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
TestHelperDOCX::clear(); TestHelperDOCX::clear();
} }
/**
* Write end section page numbering
*/
public function testWriteEndSectionPageNumbering() public function testWriteEndSectionPageNumbering()
{ {
$phpWord = new PhpWord(); $phpWord = new PhpWord();
@ -40,11 +42,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* covers ::_writeTOC * Write elements
* covers ::_writePageBreak
* covers ::_writeListItem
* covers ::_writeTitle
* covers ::_writeObject
*/ */
public function testElements() public function testElements()
{ {

View File

@ -15,18 +15,20 @@ use PhpOffice\PhpWord\Tests\TestHelperDOCX;
/** /**
* Test class for PhpOffice\PhpWord\Writer\Word2007 * Test class for PhpOffice\PhpWord\Writer\Word2007
* *
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class Word2007Test extends \PHPUnit_Framework_TestCase class Word2007Test extends \PHPUnit_Framework_TestCase
{ {
/**
* Tear down after each test
*/
public function tearDown() public function tearDown()
{ {
TestHelperDOCX::clear(); TestHelperDOCX::clear();
} }
/** /**
* covers ::__construct * Construct
*/ */
public function testConstruct() public function testConstruct()
{ {
@ -57,10 +59,12 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::save * Save
*/ */
public function testSave() public function testSave()
{ {
$localImage = __DIR__ . '/../_files/images/earth.jpg';
$remoteImage = 'http://php.net//images/logos/php-med-trans-light.gif';
$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'));
@ -71,16 +75,55 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$textrun = $section->createTextRun(); $textrun = $section->createTextRun();
$textrun->addText('Test 3'); $textrun->addText('Test 3');
$footnote = $textrun->createFootnote();
$footnote->addLink('http://test.com');
$header = $section->createHeader();
$header->addImage($localImage);
$footer = $section->createFooter();
$footer->addImage($remoteImage);
$writer = new Word2007($phpWord); $writer = new Word2007($phpWord);
$file = __DIR__ . "/../_files/temp.docx"; $file = __DIR__ . "/../_files/temp.docx";
$writer->save($file); $writer->save($file);
$this->assertTrue(\file_exists($file));
$this->assertTrue(file_exists($file));
unlink($file); unlink($file);
} }
/** /**
* @covers ::checkContentTypes * Save using disk caching
*/
public function testSaveUseDiskCaching()
{
$phpWord = new PhpWord();
$section = $phpWord->createSection();
$section->addText('Test');
$writer = new Word2007($phpWord);
$writer->setUseDiskCaching(true);
$file = __DIR__ . "/../_files/temp.docx";
$writer->save($file);
$this->assertTrue(file_exists($file));
unlink($file);
}
/**
* Save with no PhpWord object assigned
*
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage PhpWord object unassigned.
*/
public function testSaveException()
{
$writer = new Word2007();
$writer->save();
}
/**
* Check content types
*/ */
public function testCheckContentTypes() public function testCheckContentTypes()
{ {
@ -110,20 +153,33 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::setUseDiskCaching * Get writer part return null value
* @covers ::getUseDiskCaching */
public function testGetWriterPartNull()
{
$object = new Word2007();
$this->assertNull($object->getWriterPart());
}
/**
* Set/get use disk caching
*/ */
public function testSetGetUseDiskCaching() public function testSetGetUseDiskCaching()
{ {
$object = new Word2007(); $phpWord = new PhpWord();
$section = $phpWord->createSection();
$object = new Word2007($phpWord);
$object->setUseDiskCaching(true, \PHPWORD_TESTS_BASE_DIR); $object->setUseDiskCaching(true, \PHPWORD_TESTS_BASE_DIR);
$writer = new Word2007($phpWord);
$writer->save('php://output');
$this->assertTrue($object->getUseDiskCaching()); $this->assertTrue($object->getUseDiskCaching());
} }
/** /**
* @covers ::setUseDiskCaching * Use disk caching exception
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception *
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
*/ */
public function testSetUseDiskCachingException() public function testSetUseDiskCachingException()
{ {