Add unit tests
This commit is contained in:
parent
5057617de7
commit
23407c99dd
|
|
@ -96,12 +96,12 @@ class TemplateProcessor
|
||||||
// Temporary document filename initialization
|
// Temporary document filename initialization
|
||||||
$this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord');
|
$this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord');
|
||||||
if (false === $this->tempDocumentFilename) {
|
if (false === $this->tempDocumentFilename) {
|
||||||
throw new CreateTemporaryFileException();
|
throw new CreateTemporaryFileException(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template file cloning
|
// Template file cloning
|
||||||
if (false === copy($documentTemplate, $this->tempDocumentFilename)) {
|
if (false === copy($documentTemplate, $this->tempDocumentFilename)) {
|
||||||
throw new CopyFileException($documentTemplate, $this->tempDocumentFilename);
|
throw new CopyFileException($documentTemplate, $this->tempDocumentFilename); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
// Temporary document content extraction
|
// Temporary document content extraction
|
||||||
|
|
@ -122,6 +122,19 @@ class TemplateProcessor
|
||||||
$this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName());
|
$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
|
* @param string $fileName
|
||||||
*
|
*
|
||||||
|
|
@ -729,7 +742,7 @@ class TemplateProcessor
|
||||||
|
|
||||||
// Close zip file
|
// Close zip file
|
||||||
if (false === $this->zipClass->close()) {
|
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;
|
return $this->tempDocumentFilename;
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,24 @@ namespace PhpOffice\PhpWord;
|
||||||
*/
|
*/
|
||||||
final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
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.
|
* Template can be saved in temporary location.
|
||||||
*
|
*
|
||||||
* @covers ::save
|
* @covers ::save
|
||||||
|
* @covers ::zip
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
final public function testTemplateCanBeSavedInTemporaryLocation()
|
final public function testTemplateCanBeSavedInTemporaryLocation()
|
||||||
|
|
@ -41,6 +55,8 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
||||||
$templateProcessor->applyXslStyleSheet($xslDomDocument, array('needle' => $needle));
|
$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();
|
$documentFqfn = $templateProcessor->save();
|
||||||
|
|
||||||
$this->assertNotEmpty($documentFqfn, 'FQFN of the saved document is empty.');
|
$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');
|
$documentHeaderXml = $documentZip->getFromName('word/header1.xml');
|
||||||
$documentMainPartXml = $documentZip->getFromName('word/document.xml');
|
$documentMainPartXml = $documentZip->getFromName('word/document.xml');
|
||||||
$documentFooterXml = $documentZip->getFromName('word/footer1.xml');
|
$documentFooterXml = $documentZip->getFromName('word/footer1.xml');
|
||||||
|
$documentEmbedding = $documentZip->getFromName('word/embeddings/fox.bin');
|
||||||
if (false === $documentZip->close()) {
|
if (false === $documentZip->close()) {
|
||||||
throw new \Exception("Could not close zip file \"{$documentZip}\".");
|
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($templateHeaderXml, $documentHeaderXml);
|
||||||
$this->assertNotEquals($templateMainPartXml, $documentMainPartXml);
|
$this->assertNotEquals($templateMainPartXml, $documentMainPartXml);
|
||||||
$this->assertNotEquals($templateFooterXml, $documentFooterXml);
|
$this->assertNotEquals($templateFooterXml, $documentFooterXml);
|
||||||
|
$this->assertEquals($embeddingText, $documentEmbedding);
|
||||||
|
|
||||||
return $documentFqfn;
|
return $documentFqfn;
|
||||||
}
|
}
|
||||||
|
|
@ -178,6 +196,18 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
||||||
$this->assertTrue($docFound);
|
$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 ::setValue
|
||||||
* @covers ::saveAs
|
* @covers ::saveAs
|
||||||
|
|
@ -200,6 +230,22 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
||||||
$this->assertTrue($docFound);
|
$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
|
* @covers ::setImageValue
|
||||||
* @test
|
* @test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue