Skip to content

Commit 23407c9

Browse files
committed
Add unit tests
1 parent 5057617 commit 23407c9

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ public function __construct($documentTemplate)
9696
// Temporary document filename initialization
9797
$this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord');
9898
if (false === $this->tempDocumentFilename) {
99-
throw new CreateTemporaryFileException();
99+
throw new CreateTemporaryFileException(); // @codeCoverageIgnore
100100
}
101101

102102
// Template file cloning
103103
if (false === copy($documentTemplate, $this->tempDocumentFilename)) {
104-
throw new CopyFileException($documentTemplate, $this->tempDocumentFilename);
104+
throw new CopyFileException($documentTemplate, $this->tempDocumentFilename); // @codeCoverageIgnore
105105
}
106106

107107
// Temporary document content extraction
@@ -122,6 +122,19 @@ public function __construct($documentTemplate)
122122
$this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName());
123123
}
124124

125+
/**
126+
* Expose zip class
127+
*
128+
* To replace an image: $templateProcessor->zip()->AddFromString("word/media/image1.jpg", file_get_contents($file));<br>
129+
* To read a file: $templateProcessor->zip()->getFromName("word/media/image1.jpg");
130+
*
131+
* @return \PhpOffice\PhpWord\Shared\ZipArchive
132+
*/
133+
public function zip()
134+
{
135+
return $this->zipClass;
136+
}
137+
125138
/**
126139
* @param string $fileName
127140
*
@@ -729,7 +742,7 @@ public function save()
729742

730743
// Close zip file
731744
if (false === $this->zipClass->close()) {
732-
throw new Exception('Could not close zip file.');
745+
throw new Exception('Could not close zip file.'); // @codeCoverageIgnore
733746
}
734747

735748
return $this->tempDocumentFilename;

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,24 @@
2424
*/
2525
final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
2626
{
27+
/**
28+
* Construct test
29+
*
30+
* @covers ::__construct
31+
* @test
32+
*/
33+
public function testTheConstruct()
34+
{
35+
$object = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
36+
$this->assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessor', $object);
37+
$this->assertEquals(array(), $object->getVariables());
38+
}
39+
2740
/**
2841
* Template can be saved in temporary location.
2942
*
3043
* @covers ::save
44+
* @covers ::zip
3145
* @test
3246
*/
3347
final public function testTemplateCanBeSavedInTemporaryLocation()
@@ -41,6 +55,8 @@ final public function testTemplateCanBeSavedInTemporaryLocation()
4155
$templateProcessor->applyXslStyleSheet($xslDomDocument, array('needle' => $needle));
4256
}
4357

58+
$embeddingText = 'The quick Brown Fox jumped over the lazy^H^H^H^Htired unitTester';
59+
$templateProcessor->zip()->AddFromString('word/embeddings/fox.bin', $embeddingText);
4460
$documentFqfn = $templateProcessor->save();
4561

4662
$this->assertNotEmpty($documentFqfn, 'FQFN of the saved document is empty.');
@@ -60,13 +76,15 @@ final public function testTemplateCanBeSavedInTemporaryLocation()
6076
$documentHeaderXml = $documentZip->getFromName('word/header1.xml');
6177
$documentMainPartXml = $documentZip->getFromName('word/document.xml');
6278
$documentFooterXml = $documentZip->getFromName('word/footer1.xml');
79+
$documentEmbedding = $documentZip->getFromName('word/embeddings/fox.bin');
6380
if (false === $documentZip->close()) {
6481
throw new \Exception("Could not close zip file \"{$documentZip}\".");
6582
}
6683

6784
$this->assertNotEquals($templateHeaderXml, $documentHeaderXml);
6885
$this->assertNotEquals($templateMainPartXml, $documentMainPartXml);
6986
$this->assertNotEquals($templateFooterXml, $documentFooterXml);
87+
$this->assertEquals($embeddingText, $documentEmbedding);
7088

7189
return $documentFqfn;
7290
}
@@ -178,6 +196,18 @@ public function testCloneRow()
178196
$this->assertTrue($docFound);
179197
}
180198

199+
/**
200+
* @expectedException Exception
201+
* @test
202+
*/
203+
public function testCloneNotExistingRowShouldThrowException()
204+
{
205+
$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>';
206+
$templateProcessor = new TestableTemplateProcesor($mainPart);
207+
208+
$templateProcessor->cloneRow('fake_search', 2);
209+
}
210+
181211
/**
182212
* @covers ::setValue
183213
* @covers ::saveAs
@@ -200,6 +230,22 @@ public function testMacrosCanBeReplacedInHeaderAndFooter()
200230
$this->assertTrue($docFound);
201231
}
202232

233+
/**
234+
* @covers ::setValue
235+
* @test
236+
*/
237+
public function testSetValue()
238+
{
239+
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');
240+
Settings::setOutputEscapingEnabled(true);
241+
$helloworld = "hello\nworld";
242+
$templateProcessor->setValue('userName', $helloworld);
243+
$this->assertEquals(
244+
array('tableHeader', 'userId', 'userLocation'),
245+
$templateProcessor->getVariables()
246+
);
247+
}
248+
203249
/**
204250
* @covers ::setImageValue
205251
* @test

0 commit comments

Comments
 (0)