38
38
*/
39
39
final class TemplateProcessorTest extends \PHPUnit \Framework \TestCase
40
40
{
41
+ private const DELETE_AT_DESTRUCT = true ;
42
+
41
43
/**
42
44
* Construct test.
43
45
*
44
46
* @covers ::__construct
47
+ * @covers ::__destruct
45
48
*/
46
49
public function testTheConstruct (): void
47
50
{
48
- $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
51
+ $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' , self :: DELETE_AT_DESTRUCT );
49
52
self ::assertInstanceOf ('PhpOffice \\PhpWord \\TemplateProcessor ' , $ object );
50
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 );
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Do not delete tempfile test.
67
+ *
68
+ * @covers ::__construct
69
+ * @covers ::__destruct
70
+ */
71
+ public function testDoNotDeleteTempfile (): void
72
+ {
73
+ $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
74
+ $ filename1 = $ object ->save ();
75
+ self ::assertFileExists ($ filename1 );
76
+ unset($ object );
77
+ gc_collect_cycles ();
78
+ self ::assertFileExists ($ filename1 );
79
+ @unlink ($ filename1 );
51
80
}
52
81
53
82
/**
@@ -106,7 +135,7 @@ public function xtestTemplateCanBeSavedInTemporaryLocation(string $templateFqfn,
106
135
public function testXslStyleSheetCanBeApplied (): void
107
136
{
108
137
$ templateFqfn = __DIR__ . '/_files/templates/with_table_macros.docx ' ;
109
- $ templateProcessor = new TemplateProcessor ($ templateFqfn );
138
+ $ templateProcessor = new TemplateProcessor ($ templateFqfn, self :: DELETE_AT_DESTRUCT );
110
139
111
140
$ actualDocumentFqfn = $ this ->xtestTemplateCanBeSavedInTemporaryLocation ($ templateFqfn , $ templateProcessor );
112
141
$ expectedDocumentFqfn = __DIR__ . '/_files/documents/without_table_macros.docx ' ;
@@ -150,7 +179,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue
150
179
$ this ->expectExceptionMessage ('Could not set values for the given XSL style sheet parameters. ' );
151
180
}
152
181
153
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
182
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' , self :: DELETE_AT_DESTRUCT );
154
183
155
184
$ xslDomDocument = new DOMDocument ();
156
185
$ xslDomDocument ->load (__DIR__ . '/_files/xsl/passthrough.xsl ' );
@@ -171,7 +200,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
171
200
{
172
201
$ this ->expectException (\PhpOffice \PhpWord \Exception \Exception::class);
173
202
$ this ->expectExceptionMessage ('Could not load the given XML document. ' );
174
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/corrupted_main_document_part.docx ' );
203
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/corrupted_main_document_part.docx ' , self :: DELETE_AT_DESTRUCT );
175
204
176
205
$ xslDomDocument = new DOMDocument ();
177
206
$ xslDomDocument ->load (__DIR__ . '/_files/xsl/passthrough.xsl ' );
@@ -190,7 +219,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
190
219
*/
191
220
public function testDeleteRow (): void
192
221
{
193
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/delete-row.docx ' );
222
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/delete-row.docx ' , self :: DELETE_AT_DESTRUCT );
194
223
195
224
self ::assertEquals (
196
225
['deleteMe ' , 'deleteMeToo ' ],
@@ -216,7 +245,7 @@ public function testDeleteRow(): void
216
245
*/
217
246
public function testCloneRow (): void
218
247
{
219
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' );
248
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' , self :: DELETE_AT_DESTRUCT );
220
249
221
250
self ::assertEquals (
222
251
['tableHeader ' , 'userId ' , 'userName ' , 'userLocation ' ],
@@ -240,7 +269,7 @@ public function testCloneRow(): void
240
269
*/
241
270
public function testCloneRowWithCustomMacro (): void
242
271
{
243
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' );
272
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' , self :: DELETE_AT_DESTRUCT );
244
273
245
274
$ templateProcessor ->setMacroOpeningChars ('{# ' );
246
275
$ templateProcessor ->setMacroClosingChars ('#} ' );
@@ -397,7 +426,7 @@ public function testCloneRowAndSetValuesWithCustomMacro(): void
397
426
*/
398
427
public function testMacrosCanBeReplacedInHeaderAndFooter (): void
399
428
{
400
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' );
429
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' , self :: DELETE_AT_DESTRUCT );
401
430
402
431
self ::assertEquals (['documentContent ' , 'headerValue:100:100 ' , 'footerValue ' ], $ templateProcessor ->getVariables ());
403
432
@@ -418,7 +447,7 @@ public function testMacrosCanBeReplacedInHeaderAndFooter(): void
418
447
*/
419
448
public function testCustomMacrosCanBeReplacedInHeaderAndFooter (): void
420
449
{
421
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx ' );
450
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx ' , self :: DELETE_AT_DESTRUCT );
422
451
$ templateProcessor ->setMacroOpeningChars ('{{ ' );
423
452
$ templateProcessor ->setMacroClosingChars ('}} ' );
424
453
@@ -440,7 +469,7 @@ public function testCustomMacrosCanBeReplacedInHeaderAndFooter(): void
440
469
*/
441
470
public function testSetValue (): void
442
471
{
443
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' );
472
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge.docx ' , self :: DELETE_AT_DESTRUCT );
444
473
Settings::setOutputEscapingEnabled (true );
445
474
$ helloworld = "hello \nworld " ;
446
475
$ templateProcessor ->setValue ('userName ' , $ helloworld );
@@ -455,7 +484,7 @@ public function testSetValue(): void
455
484
*/
456
485
public function testSetValueWithCustomMacro (): void
457
486
{
458
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' );
487
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx ' , self :: DELETE_AT_DESTRUCT );
459
488
$ templateProcessor ->setMacroChars ('{# ' , '#} ' );
460
489
Settings::setOutputEscapingEnabled (true );
461
490
$ helloworld = "hello \nworld " ;
@@ -615,7 +644,7 @@ public function testSetValuesWithCustomMacro(): void
615
644
*/
616
645
public function testSetImageValue (): void
617
646
{
618
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' );
647
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/header-footer.docx ' , self :: DELETE_AT_DESTRUCT );
619
648
$ imagePath = __DIR__ . '/_files/images/earth.jpg ' ;
620
649
621
650
$ variablesReplace = [
@@ -695,7 +724,7 @@ public function testSetImageValue(): void
695
724
*/
696
725
public function testCloneDeleteBlock (): void
697
726
{
698
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-delete-block.docx ' );
727
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/clone-delete-block.docx ' , self :: DELETE_AT_DESTRUCT );
699
728
700
729
self ::assertEquals (
701
730
['DELETEME ' , '/DELETEME ' , 'CLONEME ' , 'blockVariable ' , '/CLONEME ' ],
@@ -735,7 +764,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresent()
735
764
$ templatePath = 'test.docx ' ;
736
765
$ objWriter ->save ($ templatePath );
737
766
738
- $ templateProcessor = new TemplateProcessor ($ templatePath );
767
+ $ templateProcessor = new TemplateProcessor ($ templatePath, self :: DELETE_AT_DESTRUCT );
739
768
$ variableCount = $ templateProcessor ->getVariableCount ();
740
769
unlink ($ templatePath );
741
770
@@ -772,7 +801,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresentWi
772
801
$ templatePath = 'test.docx ' ;
773
802
$ objWriter ->save ($ templatePath );
774
803
775
- $ templateProcessor = new TemplateProcessor ($ templatePath );
804
+ $ templateProcessor = new TemplateProcessor ($ templatePath, self :: DELETE_AT_DESTRUCT );
776
805
$ templateProcessor ->setMacroChars ('{{ ' , '}} ' );
777
806
$ variableCount = $ templateProcessor ->getVariableCount ();
778
807
unlink ($ templatePath );
@@ -810,7 +839,7 @@ public function testCloneBlockCanCloneABlockTwice(): void
810
839
$ objWriter ->save ($ templatePath );
811
840
812
841
// replace placeholders and save the file
813
- $ templateProcessor = new TemplateProcessor ($ templatePath );
842
+ $ templateProcessor = new TemplateProcessor ($ templatePath, self :: DELETE_AT_DESTRUCT );
814
843
$ templateProcessor ->setValue ('title ' , 'Some title ' );
815
844
$ templateProcessor ->cloneBlock ('subreport ' , 2 );
816
845
$ templateProcessor ->setValue ('subreport.id ' , '123 ' , 1 );
@@ -863,7 +892,7 @@ public function testCloneBlockCanCloneABlockTwiceWithCustomMacro(): void
863
892
$ objWriter ->save ($ templatePath );
864
893
865
894
// replace placeholders and save the file
866
- $ templateProcessor = new TemplateProcessor ($ templatePath );
895
+ $ templateProcessor = new TemplateProcessor ($ templatePath, self :: DELETE_AT_DESTRUCT );
867
896
$ templateProcessor ->setMacroChars ('{{ ' , '}} ' );
868
897
$ templateProcessor ->setValue ('title ' , 'Some title ' );
869
898
$ templateProcessor ->cloneBlock ('subreport ' , 2 );
@@ -1152,7 +1181,7 @@ public function testFixBrokenMacrosWithCustomMacro(): void
1152
1181
*/
1153
1182
public function testMainPartNameDetection (): void
1154
1183
{
1155
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/document22-xml.docx ' );
1184
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/document22-xml.docx ' , self :: DELETE_AT_DESTRUCT );
1156
1185
1157
1186
$ variables = ['test ' ];
1158
1187
@@ -1164,7 +1193,7 @@ public function testMainPartNameDetection(): void
1164
1193
*/
1165
1194
public function testMainPartNameDetectionWithCustomMacro (): void
1166
1195
{
1167
- $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx ' );
1196
+ $ templateProcessor = new TemplateProcessor (__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx ' , self :: DELETE_AT_DESTRUCT );
1168
1197
$ templateProcessor ->setMacroOpeningChars ('{# ' );
1169
1198
$ templateProcessor ->setMacroClosingChars ('#} ' );
1170
1199
$ variables = ['test ' ];
@@ -1431,7 +1460,7 @@ public function testUnserialize(): void
1431
1460
{
1432
1461
$ this ->expectException (WordException::class);
1433
1462
$ this ->expectExceptionMessage ('unserialize not permitted ' );
1434
- $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' );
1463
+ $ object = new TemplateProcessor (__DIR__ . '/_files/templates/blank.docx ' , self :: DELETE_AT_DESTRUCT );
1435
1464
$ serialized = serialize ($ object );
1436
1465
$ object2 = unserialize ($ serialized );
1437
1466
}
0 commit comments