Skip to content

Commit a2366e9

Browse files
author
Roman Syroeshko
committed
https://github.com/PHPOffice/PHPWord/issues/43
Unit test for PHPWord_Template.save() method.
1 parent 5e0fc7a commit a2366e9

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

Tests/PHPWord/TemplateTest.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,62 @@
66
/**
77
* @coversDefaultClass PHPWord_Template
88
*/
9-
class TemplateTest extends \PHPUnit_Framework_TestCase
9+
final class TemplateTest extends \PHPUnit_Framework_TestCase
1010
{
1111
/**
12-
* @covers ::applyXslStyleSheet
12+
* @covers ::save
1313
* @test
1414
*/
15-
final public function testXslStyleSheetCanBeApplied()
15+
final public function testTemplateCanBeSavedInTemporaryLocation()
1616
{
17-
$template = new PHPWord_Template(
18-
\join(
19-
\DIRECTORY_SEPARATOR,
20-
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'with_table_macros.docx')
21-
)
17+
$templateFqfn = \join(
18+
\DIRECTORY_SEPARATOR,
19+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'with_table_macros.docx')
2220
);
2321

22+
$document = new PHPWord_Template($templateFqfn);
2423
$xslDOMDocument = new \DOMDocument();
2524
$xslDOMDocument->load(
2625
\join(
2726
\DIRECTORY_SEPARATOR,
2827
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'remove_tables_by_needle.xsl')
2928
)
3029
);
31-
3230
foreach (array('${employee.', '${scoreboard.') as $needle) {
33-
$template->applyXslStyleSheet($xslDOMDocument, array('needle' => $needle));
31+
$document->applyXslStyleSheet($xslDOMDocument, array('needle' => $needle));
32+
}
33+
34+
$documentFqfn = $document->save();
35+
36+
$this->assertNotEmpty($documentFqfn, 'FQFN of the saved document is empty.');
37+
$this->assertFileExists($documentFqfn, "The saved document \"{$documentFqfn}\" doesn't exist.");
38+
39+
$templateZip = new \ZipArchive();
40+
$templateZip->open($templateFqfn);
41+
$templateXml = $templateZip->getFromName('word/document.xml');
42+
if ($templateZip->close() === false) {
43+
throw new \Exception("Could not close zip file \"{$templateZip}\".");
3444
}
3545

36-
$actualDocument = $template->save();
46+
$documentZip = new \ZipArchive();
47+
$documentZip->open($documentFqfn);
48+
$documentXml = $documentZip->getFromName('word/document.xml');
49+
if ($documentZip->close() === false) {
50+
throw new \Exception("Could not close zip file \"{$documentZip}\".");
51+
}
52+
53+
$this->assertNotEquals($documentXml, $templateXml);
54+
55+
return $document;
56+
}
57+
58+
/**
59+
* @covers ::applyXslStyleSheet
60+
* @depends testTemplateCanBeSavedInTemporaryLocation
61+
* @test
62+
*/
63+
final public function testXslStyleSheetCanBeApplied(&$actualDocument)
64+
{
3765
$expectedDocument = \join(
3866
\DIRECTORY_SEPARATOR,
3967
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'without_table_macros.docx')
@@ -43,14 +71,14 @@ final public function testXslStyleSheetCanBeApplied()
4371
$actualZip->open($actualDocument);
4472
$actualXml = $actualZip->getFromName('word/document.xml');
4573
if ($actualZip->close() === false) {
46-
throw new \Exception('Could not close zip file "' . $actualDocument . '".');
74+
throw new \Exception("Could not close zip file \"{$actualDocument}\".");
4775
}
4876

4977
$expectedZip = new \ZipArchive();
5078
$expectedZip->open($expectedDocument);
5179
$expectedXml = $expectedZip->getFromName('word/document.xml');
5280
if ($expectedZip->close() === false) {
53-
throw new \Exception('Could not close zip file "' . $expectedDocument . '".');
81+
throw new \Exception("Could not close zip file \"{$expectedDocument}\".");
5482
}
5583

5684
$this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);

0 commit comments

Comments
 (0)