From 23407c99dd547796ebc0cdccec11dd948c9b1535 Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 29 Dec 2018 22:03:01 +0100 Subject: [PATCH] Add unit tests --- src/PhpWord/TemplateProcessor.php | 19 ++++++++-- tests/PhpWord/TemplateProcessorTest.php | 46 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index d230cdf9..daef625a 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -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));
+ * 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; diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 2bca64ef..a8a01403 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -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 = 'text'; + $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