add test to cover get TemplateProcessor::getVariables

This commit is contained in:
troosan 2018-12-26 22:27:17 +01:00
parent 575c5531b8
commit 8ffaa1c8b7
2 changed files with 33 additions and 0 deletions

View File

@ -425,6 +425,9 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>${variable_name}</w:t></w:r>', $fixed); $this->assertEquals('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>${variable_name}</w:t></w:r>', $fixed);
} }
/**
* @covers ::getMainPartName
*/
public function testMainPartNameDetection() public function testMainPartNameDetection()
{ {
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx'); $templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx');
@ -433,4 +436,21 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($variables, $templateProcessor->getVariables()); $this->assertEquals($variables, $templateProcessor->getVariables());
} }
/**
* @covers ::getVariables
*/
public function testGetVariables()
{
$templateProcessor = new TestableTemplateProcesor();
$variables = $templateProcessor->getVariablesForPart('<w:r><w:t>normal text</w:t></w:r>');
$this->assertEquals(array(), $variables);
$variables = $templateProcessor->getVariablesForPart('<w:r><w:t>${documentContent}</w:t></w:r>');
$this->assertEquals(array('documentContent'), $variables);
$variables = $templateProcessor->getVariablesForPart('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>$</w:t></w:r><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>{</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>variable_name</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>}</w:t></w:r>');
$this->assertEquals(array('variable_name'), $variables);
}
} }

View File

@ -17,6 +17,12 @@
namespace PhpOffice\PhpWord; namespace PhpOffice\PhpWord;
/**
* This class is used to expose publicly methods that are otherwise private or protected.
* This makes testing those methods easier
*
* @author troosan
*/
class TestableTemplateProcesor extends TemplateProcessor class TestableTemplateProcesor extends TemplateProcessor
{ {
public function __construct() public function __construct()
@ -27,4 +33,11 @@ class TestableTemplateProcesor extends TemplateProcessor
{ {
return parent::fixBrokenMacros($documentPart); return parent::fixBrokenMacros($documentPart);
} }
public function getVariablesForPart($documentPartXML)
{
$documentPartXML = parent::fixBrokenMacros($documentPartXML);
return parent::getVariablesForPart($documentPartXML);
}
} }