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
{
41
- private const DELETE_AT_DESTRUCT = true ;
40
+ /** @var ?TemplateProcessor */
41
+ private $ templateProcessor ;
42
42
43
- /**
44
- * Construct test.
45
- *
46
- * @covers ::__construct
47
- * @covers ::__destruct
48
- */
49
- public function testTheConstruct (): void
43
+ private function getTemplateProcessor (string $ filename ): TemplateProcessor
50
44
{
51
- $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' , self ::DELETE_AT_DESTRUCT );
52
- self ::assertInstanceOf ('PhpOffice \\PhpWord \\TemplateProcessor ' , $ object );
53
- self ::assertEquals ([], $ object ->getVariables ());
54
- $ filename1 = $ object ->save ();
55
- self ::assertFileExists ($ filename1 );
56
- unset($ object );
57
- gc_collect_cycles ();
58
- if (method_exists (self ::class, 'assertFileDoesNotExist ' )) {
59
- self ::assertFileDoesNotExist ($ filename1 );
60
- } else {
61
- self ::assertFileNotExists ($ filename1 );
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
+ }
62
58
}
63
59
}
64
60
65
61
/**
66
- * Do not delete tempfile test.
62
+ * Construct test.
67
63
*
68
64
* @covers ::__construct
69
65
* @covers ::__destruct
70
66
*/
71
- public function testDoNotDeleteTempfile (): void
67
+ public function testTheConstruct (): void
72
68
{
73
- $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
69
+ $ object = $ this ->getTemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
70
+ self ::assertInstanceOf ('PhpOffice \\PhpWord \\TemplateProcessor ' , $ object );
71
+ self ::assertEquals ([], $ object ->getVariables ());
74
72
$ filename1 = $ object ->save ();
75
73
self ::assertFileExists ($ filename1 );
76
- unset($ object );
77
- gc_collect_cycles ();
78
- self ::assertFileExists ($ filename1 );
79
- @unlink ($ filename1 );
80
74
}
81
75
82
76
/**
@@ -135,7 +129,7 @@ public function xtestTemplateCanBeSavedInTemporaryLocation(string $templateFqfn,
135
129
public function testXslStyleSheetCanBeApplied (): void
136
130
{
137
131
$ templateFqfn = __DIR__ . '/_files/templates/with_table_macros.docx ' ;
138
- $ templateProcessor = new TemplateProcessor ($ templateFqfn, self :: DELETE_AT_DESTRUCT );
132
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templateFqfn );
139
133
140
134
$ actualDocumentFqfn = $ this ->xtestTemplateCanBeSavedInTemporaryLocation ($ templateFqfn , $ templateProcessor );
141
135
$ expectedDocumentFqfn = __DIR__ . '/_files/documents/without_table_macros.docx ' ;
@@ -179,7 +173,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue
179
173
$ this ->expectExceptionMessage ('Could not set values for the given XSL style sheet parameters. ' );
180
174
}
181
175
182
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' , self :: DELETE_AT_DESTRUCT );
176
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
183
177
184
178
$ xslDomDocument = new DOMDocument ();
185
179
$ xslDomDocument ->load (__DIR__ . '/_files/xsl/passthrough.xsl ' );
@@ -200,7 +194,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
200
194
{
201
195
$ this ->expectException (\PhpOffice \PhpWord \Exception \Exception::class);
202
196
$ this ->expectExceptionMessage ('Could not load the given XML document. ' );
203
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/corrupted_main_document_part.docx ' , self :: DELETE_AT_DESTRUCT );
197
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/corrupted_main_document_part.docx ' );
204
198
205
199
$ xslDomDocument = new DOMDocument ();
206
200
$ xslDomDocument ->load (__DIR__ . '/_files/xsl/passthrough.xsl ' );
@@ -219,7 +213,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
219
213
*/
220
214
public function testDeleteRow (): void
221
215
{
222
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/delete-row.docx ' , self :: DELETE_AT_DESTRUCT );
216
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/delete-row.docx ' );
223
217
224
218
self ::assertEquals (
225
219
['deleteMe ' , 'deleteMeToo ' ],
@@ -245,7 +239,7 @@ public function testDeleteRow(): void
245
239
*/
246
240
public function testCloneRow (): void
247
241
{
248
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' , self :: DELETE_AT_DESTRUCT );
242
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' );
249
243
250
244
self ::assertEquals (
251
245
['tableHeader ' , 'userId ' , 'userName ' , 'userLocation ' ],
@@ -269,7 +263,7 @@ public function testCloneRow(): void
269
263
*/
270
264
public function testCloneRowWithCustomMacro (): void
271
265
{
272
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' , self :: DELETE_AT_DESTRUCT );
266
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' );
273
267
274
268
$ templateProcessor ->setMacroOpeningChars ('{# ' );
275
269
$ templateProcessor ->setMacroClosingChars ('#} ' );
@@ -426,7 +420,7 @@ public function testCloneRowAndSetValuesWithCustomMacro(): void
426
420
*/
427
421
public function testMacrosCanBeReplacedInHeaderAndFooter (): void
428
422
{
429
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' , self :: DELETE_AT_DESTRUCT );
423
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' );
430
424
431
425
self ::assertEquals (['documentContent ' , 'headerValue:100:100 ' , 'footerValue ' ], $ templateProcessor ->getVariables ());
432
426
@@ -447,7 +441,7 @@ public function testMacrosCanBeReplacedInHeaderAndFooter(): void
447
441
*/
448
442
public function testCustomMacrosCanBeReplacedInHeaderAndFooter (): void
449
443
{
450
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx ' , self :: DELETE_AT_DESTRUCT );
444
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx ' );
451
445
$ templateProcessor ->setMacroOpeningChars ('{{ ' );
452
446
$ templateProcessor ->setMacroClosingChars ('}} ' );
453
447
@@ -469,7 +463,7 @@ public function testCustomMacrosCanBeReplacedInHeaderAndFooter(): void
469
463
*/
470
464
public function testSetValue (): void
471
465
{
472
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' , self :: DELETE_AT_DESTRUCT );
466
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' );
473
467
Settings::setOutputEscapingEnabled (true );
474
468
$ helloworld = "hello \nworld " ;
475
469
$ templateProcessor ->setValue ('userName ' , $ helloworld );
@@ -484,7 +478,7 @@ public function testSetValue(): void
484
478
*/
485
479
public function testSetValueWithCustomMacro (): void
486
480
{
487
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' , self :: DELETE_AT_DESTRUCT );
481
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' );
488
482
$ templateProcessor ->setMacroChars ('{# ' , '#} ' );
489
483
Settings::setOutputEscapingEnabled (true );
490
484
$ helloworld = "hello \nworld " ;
@@ -644,7 +638,7 @@ public function testSetValuesWithCustomMacro(): void
644
638
*/
645
639
public function testSetImageValue (): void
646
640
{
647
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' , self :: DELETE_AT_DESTRUCT );
641
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' );
648
642
$ imagePath = __DIR__ . '/_files/images/earth.jpg ' ;
649
643
650
644
$ variablesReplace = [
@@ -724,7 +718,7 @@ public function testSetImageValue(): void
724
718
*/
725
719
public function testCloneDeleteBlock (): void
726
720
{
727
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-delete-block.docx ' , self :: DELETE_AT_DESTRUCT );
721
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/clone-delete-block.docx ' );
728
722
729
723
self ::assertEquals (
730
724
['DELETEME ' , '/DELETEME ' , 'CLONEME ' , 'blockVariable ' , '/CLONEME ' ],
@@ -764,7 +758,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresent()
764
758
$ templatePath = 'test.docx ' ;
765
759
$ objWriter ->save ($ templatePath );
766
760
767
- $ templateProcessor = new TemplateProcessor ($ templatePath, self :: DELETE_AT_DESTRUCT );
761
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templatePath );
768
762
$ variableCount = $ templateProcessor ->getVariableCount ();
769
763
unlink ($ templatePath );
770
764
@@ -801,7 +795,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresentWi
801
795
$ templatePath = 'test.docx ' ;
802
796
$ objWriter ->save ($ templatePath );
803
797
804
- $ templateProcessor = new TemplateProcessor ($ templatePath, self :: DELETE_AT_DESTRUCT );
798
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templatePath );
805
799
$ templateProcessor ->setMacroChars ('{{ ' , '}} ' );
806
800
$ variableCount = $ templateProcessor ->getVariableCount ();
807
801
unlink ($ templatePath );
@@ -839,7 +833,7 @@ public function testCloneBlockCanCloneABlockTwice(): void
839
833
$ objWriter ->save ($ templatePath );
840
834
841
835
// replace placeholders and save the file
842
- $ templateProcessor = new TemplateProcessor ($ templatePath, self :: DELETE_AT_DESTRUCT );
836
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templatePath );
843
837
$ templateProcessor ->setValue ('title ' , 'Some title ' );
844
838
$ templateProcessor ->cloneBlock ('subreport ' , 2 );
845
839
$ templateProcessor ->setValue ('subreport.id ' , '123 ' , 1 );
@@ -892,7 +886,7 @@ public function testCloneBlockCanCloneABlockTwiceWithCustomMacro(): void
892
886
$ objWriter ->save ($ templatePath );
893
887
894
888
// replace placeholders and save the file
895
- $ templateProcessor = new TemplateProcessor ($ templatePath, self :: DELETE_AT_DESTRUCT );
889
+ $ templateProcessor = $ this -> getTemplateProcessor ($ templatePath );
896
890
$ templateProcessor ->setMacroChars ('{{ ' , '}} ' );
897
891
$ templateProcessor ->setValue ('title ' , 'Some title ' );
898
892
$ templateProcessor ->cloneBlock ('subreport ' , 2 );
@@ -1181,7 +1175,7 @@ public function testFixBrokenMacrosWithCustomMacro(): void
1181
1175
*/
1182
1176
public function testMainPartNameDetection (): void
1183
1177
{
1184
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/document22-xml.docx ' , self :: DELETE_AT_DESTRUCT );
1178
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/document22-xml.docx ' );
1185
1179
1186
1180
$ variables = ['test ' ];
1187
1181
@@ -1193,7 +1187,7 @@ public function testMainPartNameDetection(): void
1193
1187
*/
1194
1188
public function testMainPartNameDetectionWithCustomMacro (): void
1195
1189
{
1196
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx ' , self :: DELETE_AT_DESTRUCT );
1190
+ $ templateProcessor = $ this -> getTemplateProcessor (__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx ' );
1197
1191
$ templateProcessor ->setMacroOpeningChars ('{# ' );
1198
1192
$ templateProcessor ->setMacroClosingChars ('#} ' );
1199
1193
$ variables = ['test ' ];
@@ -1453,18 +1447,6 @@ public function testShouldMakeFieldsUpdateOnOpen(): void
1453
1447
self ::assertStringContainsString ('<w:updateFields w:val="false"/> ' , $ templateProcessor ->getSettingsPart ());
1454
1448
}
1455
1449
1456
- /**
1457
- * Should not allow unserialize to avoid malware.
1458
- */
1459
- public function testUnserialize (): void
1460
- {
1461
- $ this ->expectException (WordException::class);
1462
- $ this ->expectExceptionMessage ('unserialize not permitted ' );
1463
- $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' , self ::DELETE_AT_DESTRUCT );
1464
- $ serialized = serialize ($ object );
1465
- $ object2 = unserialize ($ serialized );
1466
- }
1467
-
1468
1450
public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro (): void
1469
1451
{
1470
1452
$ settingsPart = '<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
0 commit comments