21
21
use Exception ;
22
22
use PhpOffice \PhpWord \Element \Text ;
23
23
use PhpOffice \PhpWord \Element \TextRun ;
24
- use PhpOffice \PhpWord \Exception \Exception as WordException ;
25
24
use PhpOffice \PhpWord \IOFactory ;
26
25
use PhpOffice \PhpWord \PhpWord ;
27
26
use PhpOffice \PhpWord \Settings ;
38
37
*/
39
38
final class TemplateProcessorTest extends \PHPUnit \Framework \TestCase
40
39
{
40
+ /** @var ?TemplateProcessor */
41
+ private $ templateProcessor ;
42
+
43
+ private function getTemplateProcessor (string $ filename ): TemplateProcessor
44
+ {
45
+ $ this ->templateProcessor = new TemplateProcessor ($ filename );
46
+
47
+ return $ this ->templateProcessor ;
48
+ }
49
+
50
+ protected function tearDown (): void
51
+ {
52
+ if ($ this ->templateProcessor !== null ) {
53
+ $ filename = $ this ->templateProcessor ->getTempDocumentFilename ();
54
+ $ this ->templateProcessor = null ;
55
+ if (file_exists ($ filename )) {
56
+ @unlink ($ filename );
57
+ }
58
+ }
59
+ }
60
+
41
61
/**
42
62
* Construct test.
43
63
*
44
64
* @covers ::__construct
65
+ * @covers ::__destruct
45
66
*/
46
67
public function testTheConstruct (): void
47
68
{
48
- $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
69
+ $ object = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
49
70
self ::assertInstanceOf ('PhpOffice \\PhpWord \\TemplateProcessor ' , $ object );
50
71
self ::assertEquals ([], $ object ->getVariables ());
51
72
}
@@ -106,7 +127,7 @@ public function xtestTemplateCanBeSavedInTemporaryLocation(string $templateFqfn,
106
127
public function testXslStyleSheetCanBeApplied (): void
107
128
{
108
129
$ templateFqfn = __DIR__ . '/_files/templates/with_table_macros.docx ' ;
109
- $ templateProcessor = new TemplateProcessor ($ templateFqfn );
130
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templateFqfn );
110
131
111
132
$ actualDocumentFqfn = $ this ->xtestTemplateCanBeSavedInTemporaryLocation ($ templateFqfn , $ templateProcessor );
112
133
$ expectedDocumentFqfn = __DIR__ . '/_files/documents/without_table_macros.docx ' ;
@@ -150,7 +171,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue
150
171
$ this ->expectExceptionMessage ('Could not set values for the given XSL style sheet parameters. ' );
151
172
}
152
173
153
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
174
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
154
175
155
176
$ xslDomDocument = new DOMDocument ();
156
177
$ xslDomDocument ->load (__DIR__ . '/_files/xsl/passthrough.xsl ' );
@@ -171,7 +192,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
171
192
{
172
193
$ this ->expectException (\PhpOffice \PhpWord \Exception \Exception::class);
173
194
$ this ->expectExceptionMessage ('Could not load the given XML document. ' );
174
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/corrupted_main_document_part.docx ' );
195
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/corrupted_main_document_part.docx ' );
175
196
176
197
$ xslDomDocument = new DOMDocument ();
177
198
$ xslDomDocument ->load (__DIR__ . '/_files/xsl/passthrough.xsl ' );
@@ -190,7 +211,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
190
211
*/
191
212
public function testDeleteRow (): void
192
213
{
193
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/delete-row.docx ' );
214
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/delete-row.docx ' );
194
215
195
216
self ::assertEquals (
196
217
['deleteMe ' , 'deleteMeToo ' ],
@@ -216,7 +237,7 @@ public function testDeleteRow(): void
216
237
*/
217
238
public function testCloneRow (): void
218
239
{
219
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' );
240
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' );
220
241
221
242
self ::assertEquals (
222
243
['tableHeader ' , 'userId ' , 'userName ' , 'userLocation ' ],
@@ -240,7 +261,7 @@ public function testCloneRow(): void
240
261
*/
241
262
public function testCloneRowWithCustomMacro (): void
242
263
{
243
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' );
264
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' );
244
265
245
266
$ templateProcessor ->setMacroOpeningChars ('{# ' );
246
267
$ templateProcessor ->setMacroClosingChars ('#} ' );
@@ -397,7 +418,7 @@ public function testCloneRowAndSetValuesWithCustomMacro(): void
397
418
*/
398
419
public function testMacrosCanBeReplacedInHeaderAndFooter (): void
399
420
{
400
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' );
421
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' );
401
422
402
423
self ::assertEquals (['documentContent ' , 'headerValue:100:100 ' , 'footerValue ' ], $ templateProcessor ->getVariables ());
403
424
@@ -418,7 +439,7 @@ public function testMacrosCanBeReplacedInHeaderAndFooter(): void
418
439
*/
419
440
public function testCustomMacrosCanBeReplacedInHeaderAndFooter (): void
420
441
{
421
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx ' );
442
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx ' );
422
443
$ templateProcessor ->setMacroOpeningChars ('{{ ' );
423
444
$ templateProcessor ->setMacroClosingChars ('}} ' );
424
445
@@ -440,7 +461,7 @@ public function testCustomMacrosCanBeReplacedInHeaderAndFooter(): void
440
461
*/
441
462
public function testSetValue (): void
442
463
{
443
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' );
464
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' );
444
465
Settings::setOutputEscapingEnabled (true );
445
466
$ helloworld = "hello \nworld " ;
446
467
$ templateProcessor ->setValue ('userName ' , $ helloworld );
@@ -455,7 +476,7 @@ public function testSetValue(): void
455
476
*/
456
477
public function testSetValueWithCustomMacro (): void
457
478
{
458
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' );
479
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' );
459
480
$ templateProcessor ->setMacroChars ('{# ' , '#} ' );
460
481
Settings::setOutputEscapingEnabled (true );
461
482
$ helloworld = "hello \nworld " ;
@@ -786,7 +807,7 @@ public function testSetCheckboxWithCustomMacro(): void
786
807
*/
787
808
public function testSetImageValue (): void
788
809
{
789
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' );
810
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' );
790
811
$ imagePath = __DIR__ . '/_files/images/earth.jpg ' ;
791
812
792
813
$ variablesReplace = [
@@ -866,7 +887,7 @@ public function testSetImageValue(): void
866
887
*/
867
888
public function testCloneDeleteBlock (): void
868
889
{
869
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-delete-block.docx ' );
890
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-delete-block.docx ' );
870
891
871
892
self ::assertEquals (
872
893
['DELETEME ' , '/DELETEME ' , 'CLONEME ' , 'blockVariable ' , '/CLONEME ' ],
@@ -906,7 +927,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresent()
906
927
$ templatePath = 'test.docx ' ;
907
928
$ objWriter ->save ($ templatePath );
908
929
909
- $ templateProcessor = new TemplateProcessor ($ templatePath );
930
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templatePath );
910
931
$ variableCount = $ templateProcessor ->getVariableCount ();
911
932
unlink ($ templatePath );
912
933
@@ -943,7 +964,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresentWi
943
964
$ templatePath = 'test.docx ' ;
944
965
$ objWriter ->save ($ templatePath );
945
966
946
- $ templateProcessor = new TemplateProcessor ($ templatePath );
967
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templatePath );
947
968
$ templateProcessor ->setMacroChars ('{{ ' , '}} ' );
948
969
$ variableCount = $ templateProcessor ->getVariableCount ();
949
970
unlink ($ templatePath );
@@ -981,7 +1002,7 @@ public function testCloneBlockCanCloneABlockTwice(): void
981
1002
$ objWriter ->save ($ templatePath );
982
1003
983
1004
// replace placeholders and save the file
984
- $ templateProcessor = new TemplateProcessor ($ templatePath );
1005
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templatePath );
985
1006
$ templateProcessor ->setValue ('title ' , 'Some title ' );
986
1007
$ templateProcessor ->cloneBlock ('subreport ' , 2 );
987
1008
$ templateProcessor ->setValue ('subreport.id ' , '123 ' , 1 );
@@ -1034,7 +1055,7 @@ public function testCloneBlockCanCloneABlockTwiceWithCustomMacro(): void
1034
1055
$ objWriter ->save ($ templatePath );
1035
1056
1036
1057
// replace placeholders and save the file
1037
- $ templateProcessor = new TemplateProcessor ($ templatePath );
1058
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templatePath );
1038
1059
$ templateProcessor ->setMacroChars ('{{ ' , '}} ' );
1039
1060
$ templateProcessor ->setValue ('title ' , 'Some title ' );
1040
1061
$ templateProcessor ->cloneBlock ('subreport ' , 2 );
@@ -1323,7 +1344,7 @@ public function testFixBrokenMacrosWithCustomMacro(): void
1323
1344
*/
1324
1345
public function testMainPartNameDetection (): void
1325
1346
{
1326
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/document22-xml.docx ' );
1347
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/document22-xml.docx ' );
1327
1348
1328
1349
$ variables = ['test ' ];
1329
1350
@@ -1335,7 +1356,7 @@ public function testMainPartNameDetection(): void
1335
1356
*/
1336
1357
public function testMainPartNameDetectionWithCustomMacro (): void
1337
1358
{
1338
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx ' );
1359
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx ' );
1339
1360
$ templateProcessor ->setMacroOpeningChars ('{# ' );
1340
1361
$ templateProcessor ->setMacroClosingChars ('#} ' );
1341
1362
$ variables = ['test ' ];
@@ -1595,18 +1616,6 @@ public function testShouldMakeFieldsUpdateOnOpen(): void
1595
1616
self ::assertStringContainsString ('<w:updateFields w:val="false"/> ' , $ templateProcessor ->getSettingsPart ());
1596
1617
}
1597
1618
1598
- /**
1599
- * Should not allow unserialize to avoid malware.
1600
- */
1601
- public function testUnserialize (): void
1602
- {
1603
- $ this ->expectException (WordException::class);
1604
- $ this ->expectExceptionMessage ('unserialize not permitted ' );
1605
- $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
1606
- $ serialized = serialize ($ object );
1607
- $ object2 = unserialize ($ serialized );
1608
- }
1609
-
1610
1619
public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro (): void
1611
1620
{
1612
1621
$ settingsPart = '<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
0 commit comments