Skip to content

Additional unit tests #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Classes/PHPWord/Writer/ODText/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
/**
* Write content file to XML format
*
* @param PHPWord $pPHPWord
* @return string XML Output
* @throws Exception
* @param PHPWord $pPHPWord
* @return string XML Output
* @throws Exception
*/
public function writeContent(PHPWord $pPHPWord = null)
{
Expand Down
7 changes: 4 additions & 3 deletions Classes/PHPWord/Writer/ODText/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public function writeManifest(PHPWord $pPHPWord = null)
/**
* Get image mime type
*
* @param string $pFile Filename
* @return string Mime Type
* @throws Exception
* @param string $pFile Filename
* @return string Mime Type
* @throws Exception
* @codeCoverageIgnore Image is not yet handled by ODText
*/
private function _getImageMimeType($pFile = '')
{
Expand Down
10 changes: 10 additions & 0 deletions Classes/PHPWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,14 @@ private function _addFileToPackage($objZip, $element)
$this->checkContentTypes($element['source']);
}
}

/**
* Get disk caching directory
*
* @return string
*/
public function getDiskCachingDirectory()
{
return $this->_diskCachingDirectory;
}
}
30 changes: 28 additions & 2 deletions Tests/PHPWord/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,43 @@ final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromT
@$template->applyXslStyleSheet($xslDOMDocument);
}

/**
* @covers PHPWord_Template
*/
public function testConstruct()
{
$template = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'clone-row.docx')
);
$expectedVar = array('tableHeader', 'userId', 'userName');
$document = new PHPWord_Template($template);
$actualVar = $document->getVariables();
$document->cloneRow('userId', 9);
$document->setValue('userId#1', utf8_decode('ééé'));
$document->setValue('userId#2', 'a');
$document->setValue('userId#3', 'a');
$document->setValue('userId#3', 'a');
$document->setValue('userId#4', 'a');
$document->setValue('userId#5', 'a');
$document->setValue('userId#6', 'a');
$document->setValue('userId#7', 'a');
$document->setValue('userId#8', 'a');
$document->cloneRow('userId#9', 'a');
$this->assertEquals($expectedVar, $actualVar);
}

/**
* @covers ::setValue
* @covers ::getVariables
* @covers ::cloneRow
* @covers ::saveAs
*/
public function testCloneRow()
public function testCloneMergedRow()
{
$template = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'clone-row.docx')
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'clone-merge.docx')
);
$expectedVar = array('tableHeader', 'userId', 'userName', 'userLocation');
$docName = 'clone-test-result.docx';
Expand Down
45 changes: 45 additions & 0 deletions Tests/PHPWord/Writer/ODText/WriterPartTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace PHPWord\Tests\Writer\ODText;

use PHPWord_Writer_ODText_WriterPart;
use PHPWord_Writer_ODText;
use PHPWord\Tests\TestHelperDOCX;

/**
* Class WriterPartTest
*
* @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_ODText_WriterPart
* @runTestsInSeparateProcesses
*/
class WriterPartTest extends \PHPUnit_Framework_TestCase
{
/**
* covers ::setParentWriter
* covers ::getParentWriter
*/
public function testSetGetParentWriter()
{
$object = $this->getMockForAbstractClass(
'PHPWord_Writer_Word2007_WriterPart'
);
$object->setParentWriter(new PHPWord_Writer_ODText());
$this->assertEquals(
new PHPWord_Writer_ODText(),
$object->getParentWriter()
);
}

/**
* covers ::getParentWriter
* @expectedException Exception
* @expectedExceptionMessage No parent PHPWord_Writer_IWriter assigned.
*/
public function testSetGetParentWriterNull()
{
$object = $this->getMockForAbstractClass(
'PHPWord_Writer_Word2007_WriterPart'
);
$object->getParentWriter();
}
}
13 changes: 0 additions & 13 deletions Tests/PHPWord/Writer/ODTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,6 @@ public function testSave()
unlink($file);
}

/**
* @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_ODText($phpWord);
$writer->save('php://output');
}

/**
* @covers ::save
* @expectedException Exception
Expand Down
21 changes: 5 additions & 16 deletions Tests/PHPWord/Writer/RTFTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ public function testConstructWithNull()
$object->getPHPWord();
}

/**
* @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
Expand Down Expand Up @@ -78,13 +65,15 @@ public function testSave()
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf')
);

$fontStyle = array('name' => 'Verdana', 'size' => 11, 'bold' => true, 'italic' => true, 'color' => 'F29101', 'fgColor' => '123456');
$paragraphStyle = array('align' => 'center', 'spaceAfter' => 120);
$phpWord = new PHPWord();
$phpWord->addFontStyle('Font', array('size' => 11));
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
$phpWord->addFontStyle('Font', $fontStyle);
$phpWord->addParagraphStyle('Paragraph', $paragraphStyle);
$section = $phpWord->createSection();
$section->addText('Test 1', 'Font');
$section->addText('Test 2', array('name' => 'Tahoma'), 'Paragraph');
$section->addTextBreak();
$section->addText('Test 2', null, 'Paragraph');
$section->addLink('http://test.com');
$section->addTitle('Test', 1);
$section->addPageBreak();
Expand Down
44 changes: 44 additions & 0 deletions Tests/PHPWord/Writer/Word2007/FooterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace PHPWord\Tests\Writer\Word2007;

use PHPWord_Writer_Word2007_Footer;
use PHPWord_Writer_Word2007;
use PHPWord_Section_Footer;
use PHPWord\Tests\TestHelperDOCX;

/**
* Class FooterTest
*
* @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_Word2007_Footer
* @runTestsInSeparateProcesses
*/
class FooterTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers ::writeFooter
*/
public function testWriteFooter()
{
$imageSrc = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png')
);
$container = new PHPWord_Section_Footer(1);
$container->addText('');
$container->addPreserveText('');
$container->addTextBreak();
$container->createTextRun();
$container->addTable()->addRow()->addCell()->addText('');
$container->addImage($imageSrc);

$writer = new PHPWord_Writer_Word2007();
$object = new PHPWord_Writer_Word2007_Footer();
$object->setParentWriter($writer);
$object->writeFooter($container);
$writer->setUseDiskCaching(true);
$xml = simplexml_load_string($object->writeFooter($container));

$this->assertInstanceOf('SimpleXMLElement', $xml);
}
}
46 changes: 46 additions & 0 deletions Tests/PHPWord/Writer/Word2007/HeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace PHPWord\Tests\Writer\Word2007;

use PHPWord_Writer_Word2007_Header;
use PHPWord_Writer_Word2007;
use PHPWord_Section_Header;
use PHPWord\Tests\TestHelperDOCX;

/**
* Class HeaderTest
*
* @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_Word2007_Header
* @runTestsInSeparateProcesses
*/
class HeaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers ::writeHeader
*/
public function testWriteHeader()
{
$imageSrc = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png')
);

$container = new PHPWord_Section_Header(1);
$container->addText('Test');
$container->addPreserveText('');
$container->addTextBreak();
$container->createTextRun();
$container->addTable()->addRow()->addCell()->addText('');
$container->addImage($imageSrc);
$container->addWatermark($imageSrc);

$writer = new PHPWord_Writer_Word2007();
$object = new PHPWord_Writer_Word2007_Header();
$object->setParentWriter($writer);
$object->writeHeader($container);
$writer->setUseDiskCaching(true);
$xml = simplexml_load_string($object->writeHeader($container));

$this->assertInstanceOf('SimpleXMLElement', $xml);
}
}
45 changes: 45 additions & 0 deletions Tests/PHPWord/Writer/Word2007/WriterPartTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace PHPWord\Tests\Writer\Word2007;

use PHPWord_Writer_Word2007_WriterPart;
use PHPWord_Writer_Word2007;
use PHPWord\Tests\TestHelperDOCX;

/**
* Class WriterPartTest
*
* @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_Word2007_WriterPart
* @runTestsInSeparateProcesses
*/
class WriterPartTest extends \PHPUnit_Framework_TestCase
{
/**
* covers ::setParentWriter
* covers ::getParentWriter
*/
public function testSetGetParentWriter()
{
$object = $this->getMockForAbstractClass(
'PHPWord_Writer_Word2007_WriterPart'
);
$object->setParentWriter(new PHPWord_Writer_Word2007());
$this->assertEquals(
new PHPWord_Writer_Word2007(),
$object->getParentWriter()
);
}

/**
* covers ::getParentWriter
* @expectedException Exception
* @expectedExceptionMessage No parent PHPWord_Writer_IWriter assigned.
*/
public function testSetGetParentWriterNull()
{
$object = $this->getMockForAbstractClass(
'PHPWord_Writer_Word2007_WriterPart'
);
$object->getParentWriter();
}
}
23 changes: 22 additions & 1 deletion Tests/PHPWord/Writer/Word2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ public function testSave()
unlink($file);
}

/**
* @covers ::save
* @expectedException Exception
* @expectedExceptionMessage PHPWord object unassigned.
*/
public function testSaveException()
{
$writer = new PHPWord_Writer_Word2007();
$writer->save();
}

/**
* @covers ::getWriterPart
*/
public function testGetWriterPartNull()
{
$object = new PHPWord_Writer_Word2007();
$this->assertNull($object->getWriterPart('foo'));
}

/**
* @covers ::checkContentTypes
*/
Expand Down Expand Up @@ -100,13 +120,14 @@ public function testCheckContentTypes()
/**
* @covers ::setUseDiskCaching
* @covers ::getUseDiskCaching
* @covers ::getDiskCachingDirectory
*/
public function testSetGetUseDiskCaching()
{
$object = new PHPWord_Writer_Word2007();
$object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);

$this->assertTrue($object->getUseDiskCaching());
$this->assertEquals(PHPWORD_TESTS_DIR_ROOT, $object->getDiskCachingDirectory());
}

/**
Expand Down
Binary file added Tests/_files/templates/clone-merge.docx
Binary file not shown.
Binary file modified Tests/_files/templates/clone-row.docx
Binary file not shown.