Skip to content

Commit 8ffaa1c

Browse files
committed
add test to cover get TemplateProcessor::getVariables
1 parent 575c553 commit 8ffaa1c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ public function testFixBrokenMacros()
425425
$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);
426426
}
427427

428+
/**
429+
* @covers ::getMainPartName
430+
*/
428431
public function testMainPartNameDetection()
429432
{
430433
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx');
@@ -433,4 +436,21 @@ public function testMainPartNameDetection()
433436

434437
$this->assertEquals($variables, $templateProcessor->getVariables());
435438
}
439+
440+
/**
441+
* @covers ::getVariables
442+
*/
443+
public function testGetVariables()
444+
{
445+
$templateProcessor = new TestableTemplateProcesor();
446+
447+
$variables = $templateProcessor->getVariablesForPart('<w:r><w:t>normal text</w:t></w:r>');
448+
$this->assertEquals(array(), $variables);
449+
450+
$variables = $templateProcessor->getVariablesForPart('<w:r><w:t>${documentContent}</w:t></w:r>');
451+
$this->assertEquals(array('documentContent'), $variables);
452+
453+
$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>');
454+
$this->assertEquals(array('variable_name'), $variables);
455+
}
436456
}

tests/PhpWord/_includes/TestableTemplateProcesor.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
namespace PhpOffice\PhpWord;
1919

20+
/**
21+
* This class is used to expose publicly methods that are otherwise private or protected.
22+
* This makes testing those methods easier
23+
*
24+
* @author troosan
25+
*/
2026
class TestableTemplateProcesor extends TemplateProcessor
2127
{
2228
public function __construct()
@@ -27,4 +33,11 @@ public function fixBrokenMacros($documentPart)
2733
{
2834
return parent::fixBrokenMacros($documentPart);
2935
}
36+
37+
public function getVariablesForPart($documentPartXML)
38+
{
39+
$documentPartXML = parent::fixBrokenMacros($documentPartXML);
40+
41+
return parent::getVariablesForPart($documentPartXML);
42+
}
3043
}

0 commit comments

Comments
 (0)