Add unit tests

This commit is contained in:
troosan 2018-12-29 22:03:01 +01:00
parent 5057617de7
commit 23407c99dd
2 changed files with 62 additions and 3 deletions

View File

@ -96,12 +96,12 @@ class TemplateProcessor
// Temporary document filename initialization
$this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord');
if (false === $this->tempDocumentFilename) {
throw new CreateTemporaryFileException();
throw new CreateTemporaryFileException(); // @codeCoverageIgnore
}
// Template file cloning
if (false === copy($documentTemplate, $this->tempDocumentFilename)) {
throw new CopyFileException($documentTemplate, $this->tempDocumentFilename);
throw new CopyFileException($documentTemplate, $this->tempDocumentFilename); // @codeCoverageIgnore
}
// Temporary document content extraction
@ -122,6 +122,19 @@ class TemplateProcessor
$this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName());
}
/**
* Expose zip class
*
* To replace an image: $templateProcessor->zip()->AddFromString("word/media/image1.jpg", file_get_contents($file));<br>
* To read a file: $templateProcessor->zip()->getFromName("word/media/image1.jpg");
*
* @return \PhpOffice\PhpWord\Shared\ZipArchive
*/
public function zip()
{
return $this->zipClass;
}
/**
* @param string $fileName
*
@ -729,7 +742,7 @@ class TemplateProcessor
// Close zip file
if (false === $this->zipClass->close()) {
throw new Exception('Could not close zip file.');
throw new Exception('Could not close zip file.'); // @codeCoverageIgnore
}
return $this->tempDocumentFilename;

View File

@ -24,10 +24,24 @@ namespace PhpOffice\PhpWord;
*/
final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
{
/**
* Construct test
*
* @covers ::__construct
* @test
*/
public function testTheConstruct()
{
$object = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
$this->assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessor', $object);
$this->assertEquals(array(), $object->getVariables());
}
/**
* Template can be saved in temporary location.
*
* @covers ::save
* @covers ::zip
* @test
*/
final public function testTemplateCanBeSavedInTemporaryLocation()
@ -41,6 +55,8 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$templateProcessor->applyXslStyleSheet($xslDomDocument, array('needle' => $needle));
}
$embeddingText = 'The quick Brown Fox jumped over the lazy^H^H^H^Htired unitTester';
$templateProcessor->zip()->AddFromString('word/embeddings/fox.bin', $embeddingText);
$documentFqfn = $templateProcessor->save();
$this->assertNotEmpty($documentFqfn, 'FQFN of the saved document is empty.');
@ -60,6 +76,7 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$documentHeaderXml = $documentZip->getFromName('word/header1.xml');
$documentMainPartXml = $documentZip->getFromName('word/document.xml');
$documentFooterXml = $documentZip->getFromName('word/footer1.xml');
$documentEmbedding = $documentZip->getFromName('word/embeddings/fox.bin');
if (false === $documentZip->close()) {
throw new \Exception("Could not close zip file \"{$documentZip}\".");
}
@ -67,6 +84,7 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$this->assertNotEquals($templateHeaderXml, $documentHeaderXml);
$this->assertNotEquals($templateMainPartXml, $documentMainPartXml);
$this->assertNotEquals($templateFooterXml, $documentFooterXml);
$this->assertEquals($embeddingText, $documentEmbedding);
return $documentFqfn;
}
@ -178,6 +196,18 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($docFound);
}
/**
* @expectedException Exception
* @test
*/
public function testCloneNotExistingRowShouldThrowException()
{
$mainPart = '<?xml version="1.0" encoding="UTF-8"?><w:p><w:r><w:rPr></w:rPr><w:t>text</w:t></w:r></w:p>';
$templateProcessor = new TestableTemplateProcesor($mainPart);
$templateProcessor->cloneRow('fake_search', 2);
}
/**
* @covers ::setValue
* @covers ::saveAs
@ -200,6 +230,22 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($docFound);
}
/**
* @covers ::setValue
* @test
*/
public function testSetValue()
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');
Settings::setOutputEscapingEnabled(true);
$helloworld = "hello\nworld";
$templateProcessor->setValue('userName', $helloworld);
$this->assertEquals(
array('tableHeader', 'userId', 'userLocation'),
$templateProcessor->getVariables()
);
}
/**
* @covers ::setImageValue
* @test