Unit test for PHPWord_Template.save() method.
This commit is contained in:
parent
5e0fc7a2d8
commit
a2366e9ddf
@ -6,21 +6,20 @@ use PHPWord_Template;
|
|||||||
/**
|
/**
|
||||||
* @coversDefaultClass PHPWord_Template
|
* @coversDefaultClass PHPWord_Template
|
||||||
*/
|
*/
|
||||||
class TemplateTest extends \PHPUnit_Framework_TestCase
|
final class TemplateTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @covers ::applyXslStyleSheet
|
* @covers ::save
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
final public function testXslStyleSheetCanBeApplied()
|
final public function testTemplateCanBeSavedInTemporaryLocation()
|
||||||
{
|
{
|
||||||
$template = new PHPWord_Template(
|
$templateFqfn = \join(
|
||||||
\join(
|
\DIRECTORY_SEPARATOR,
|
||||||
\DIRECTORY_SEPARATOR,
|
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'with_table_macros.docx')
|
||||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'with_table_macros.docx')
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$document = new PHPWord_Template($templateFqfn);
|
||||||
$xslDOMDocument = new \DOMDocument();
|
$xslDOMDocument = new \DOMDocument();
|
||||||
$xslDOMDocument->load(
|
$xslDOMDocument->load(
|
||||||
\join(
|
\join(
|
||||||
@ -28,12 +27,41 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
|
|||||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'remove_tables_by_needle.xsl')
|
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'remove_tables_by_needle.xsl')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach (array('${employee.', '${scoreboard.') as $needle) {
|
foreach (array('${employee.', '${scoreboard.') as $needle) {
|
||||||
$template->applyXslStyleSheet($xslDOMDocument, array('needle' => $needle));
|
$document->applyXslStyleSheet($xslDOMDocument, array('needle' => $needle));
|
||||||
}
|
}
|
||||||
|
|
||||||
$actualDocument = $template->save();
|
$documentFqfn = $document->save();
|
||||||
|
|
||||||
|
$this->assertNotEmpty($documentFqfn, 'FQFN of the saved document is empty.');
|
||||||
|
$this->assertFileExists($documentFqfn, "The saved document \"{$documentFqfn}\" doesn't exist.");
|
||||||
|
|
||||||
|
$templateZip = new \ZipArchive();
|
||||||
|
$templateZip->open($templateFqfn);
|
||||||
|
$templateXml = $templateZip->getFromName('word/document.xml');
|
||||||
|
if ($templateZip->close() === false) {
|
||||||
|
throw new \Exception("Could not close zip file \"{$templateZip}\".");
|
||||||
|
}
|
||||||
|
|
||||||
|
$documentZip = new \ZipArchive();
|
||||||
|
$documentZip->open($documentFqfn);
|
||||||
|
$documentXml = $documentZip->getFromName('word/document.xml');
|
||||||
|
if ($documentZip->close() === false) {
|
||||||
|
throw new \Exception("Could not close zip file \"{$documentZip}\".");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertNotEquals($documentXml, $templateXml);
|
||||||
|
|
||||||
|
return $document;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::applyXslStyleSheet
|
||||||
|
* @depends testTemplateCanBeSavedInTemporaryLocation
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
final public function testXslStyleSheetCanBeApplied(&$actualDocument)
|
||||||
|
{
|
||||||
$expectedDocument = \join(
|
$expectedDocument = \join(
|
||||||
\DIRECTORY_SEPARATOR,
|
\DIRECTORY_SEPARATOR,
|
||||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'without_table_macros.docx')
|
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'without_table_macros.docx')
|
||||||
@ -43,14 +71,14 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
|
|||||||
$actualZip->open($actualDocument);
|
$actualZip->open($actualDocument);
|
||||||
$actualXml = $actualZip->getFromName('word/document.xml');
|
$actualXml = $actualZip->getFromName('word/document.xml');
|
||||||
if ($actualZip->close() === false) {
|
if ($actualZip->close() === false) {
|
||||||
throw new \Exception('Could not close zip file "' . $actualDocument . '".');
|
throw new \Exception("Could not close zip file \"{$actualDocument}\".");
|
||||||
}
|
}
|
||||||
|
|
||||||
$expectedZip = new \ZipArchive();
|
$expectedZip = new \ZipArchive();
|
||||||
$expectedZip->open($expectedDocument);
|
$expectedZip->open($expectedDocument);
|
||||||
$expectedXml = $expectedZip->getFromName('word/document.xml');
|
$expectedXml = $expectedZip->getFromName('word/document.xml');
|
||||||
if ($expectedZip->close() === false) {
|
if ($expectedZip->close() === false) {
|
||||||
throw new \Exception('Could not close zip file "' . $expectedDocument . '".');
|
throw new \Exception("Could not close zip file \"{$expectedDocument}\".");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
|
$this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user