From 8ffaa1c8b736dfe3669cb2258dcb5c5aff1c9973 Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 26 Dec 2018 22:27:17 +0100 Subject: [PATCH] add test to cover get TemplateProcessor::getVariables --- tests/PhpWord/TemplateProcessorTest.php | 20 +++++++++++++++++++ .../_includes/TestableTemplateProcesor.php | 13 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index fa84cf3b..e4789671 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -425,6 +425,9 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase $this->assertEquals('$15,000.00. ${variable_name}', $fixed); } + /** + * @covers ::getMainPartName + */ public function testMainPartNameDetection() { $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()); } + + /** + * @covers ::getVariables + */ + public function testGetVariables() + { + $templateProcessor = new TestableTemplateProcesor(); + + $variables = $templateProcessor->getVariablesForPart('normal text'); + $this->assertEquals(array(), $variables); + + $variables = $templateProcessor->getVariablesForPart('${documentContent}'); + $this->assertEquals(array('documentContent'), $variables); + + $variables = $templateProcessor->getVariablesForPart('$15,000.00. ${variable_name}'); + $this->assertEquals(array('variable_name'), $variables); + } } diff --git a/tests/PhpWord/_includes/TestableTemplateProcesor.php b/tests/PhpWord/_includes/TestableTemplateProcesor.php index f76da417..65e83695 100644 --- a/tests/PhpWord/_includes/TestableTemplateProcesor.php +++ b/tests/PhpWord/_includes/TestableTemplateProcesor.php @@ -17,6 +17,12 @@ 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 { public function __construct() @@ -27,4 +33,11 @@ class TestableTemplateProcesor extends TemplateProcessor { return parent::fixBrokenMacros($documentPart); } + + public function getVariablesForPart($documentPartXML) + { + $documentPartXML = parent::fixBrokenMacros($documentPartXML); + + return parent::getVariablesForPart($documentPartXML); + } }