Skip to content

Commit bc448ae

Browse files
committed
improve code coverage
1 parent d862b1f commit bc448ae

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,47 +1108,16 @@ protected function replaceXmlBlock($macro, $block, $blockType = 'w:p')
11081108
protected function findContainingXmlBlockForMacro($macro, $blockType = 'w:p')
11091109
{
11101110
$macroPos = $this->findMacro($macro);
1111-
if (false === $macroPos) {
1111+
if (0 > $macroPos) {
11121112
return false;
11131113
}
11141114
$start = $this->findXmlBlockStart($macroPos, $blockType);
11151115
if (0 > $start) {
11161116
return false;
11171117
}
11181118
$end = $this->findXmlBlockEnd($start, $blockType);
1119-
if (0 > $end) {
1120-
return false;
1121-
}
1122-
1123-
return array('start' => $start, 'end' => $end);
1124-
}
1125-
1126-
/**
1127-
* Find start and end of XML block containing the given block macro
1128-
* e.g. <w:p>...${macro}...${/macro}...</w:p>
1129-
*
1130-
* Note that only the first instance of the macro will be found
1131-
*
1132-
* @param string $macro Name of macro
1133-
* @param string $blockType XML tag for block
1134-
* @return bool|int[] FALSE if not found, otherwise array with start and end
1135-
*/
1136-
protected function findContainingXmlBlockForBlockMacro($macro, $blockType = 'w:p')
1137-
{
1138-
$macroStartPos = $this->findMacro($macro);
1139-
if (0 > $macroStartPos) {
1140-
return false;
1141-
}
1142-
$macroEndPos = $this->findMacro('/' . $macro, $macroStartPos);
1143-
if (0 > $macroEndPos) {
1144-
return false;
1145-
}
1146-
$start = $this->findXmlBlockStart($macroStartPos, $blockType);
1147-
if (0 > $start) {
1148-
return false;
1149-
}
1150-
$end = $this->findXmlBlockEnd($macroEndPos, $blockType);
1151-
if (0 > $end) {
1119+
//if not found or if resulting string does not contain the macro we are searching for
1120+
if (0 > $end || strstr($this->getSlice($start, $end), $macro) === false) {
11521121
return false;
11531122
}
11541123

@@ -1183,12 +1152,13 @@ protected function findMacro($search, $offset = 0)
11831152
*/
11841153
protected function findXmlBlockStart($offset, $blockType)
11851154
{
1155+
$reverseOffset = (strlen($this->tempDocumentMainPart) - $offset) * -1;
11861156
// first try XML tag with attributes
1187-
$blockStart = strrpos($this->tempDocumentMainPart, '<' . $blockType . ' ', ((strlen($this->tempDocumentMainPart) - $offset) * -1));
1157+
$blockStart = strrpos($this->tempDocumentMainPart, '<' . $blockType . ' ', $reverseOffset);
11881158
// if not found, or if found but contains the XML tag without attribute
11891159
if (false === $blockStart || strrpos($this->getSlice($blockStart, $offset), '<' . $blockType . '>')) {
11901160
// also try XML tag without attributes
1191-
$blockStart = strrpos($this->tempDocumentMainPart, '<' . $blockType . '>', ((strlen($this->tempDocumentMainPart) - $offset) * -1));
1161+
$blockStart = strrpos($this->tempDocumentMainPart, '<' . $blockType . '>', $reverseOffset);
11921162
}
11931163

11941164
return ($blockStart === false) ? -1 : $blockStart;

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,4 +803,31 @@ public function testFindXmlBlockStart()
803803

804804
$this->assertEquals($toFind, $templateProcessor->getSlice($position['start'], $position['end']));
805805
}
806+
807+
public function testShouldReturnFalseIfXmlBlockNotFound()
808+
{
809+
$mainPart = '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
810+
<w:p>
811+
<w:r>
812+
<w:rPr>
813+
<w:lang w:val="en-GB"/>
814+
</w:rPr>
815+
<w:t xml:space="preserve">this is my text containing a ${macro}</w:t>
816+
</w:r>
817+
</w:p>
818+
</w:document>';
819+
$templateProcessor = new TestableTemplateProcesor($mainPart);
820+
821+
//non-existing macro
822+
$result = $templateProcessor->findContainingXmlBlockForMacro('${fake-macro}', 'w:p');
823+
$this->assertFalse($result);
824+
825+
//existing macro but not inside node looked for
826+
$result = $templateProcessor->findContainingXmlBlockForMacro('${macro}', 'w:fake-node');
827+
$this->assertFalse($result);
828+
829+
//existing macro but end tag not found after macro
830+
$result = $templateProcessor->findContainingXmlBlockForMacro('${macro}', 'w:rPr');
831+
$this->assertFalse($result);
832+
}
806833
}

0 commit comments

Comments
 (0)