Skip to content

Commit 1e3fc53

Browse files
committed
Update test to cover more code
1 parent 181d539 commit 1e3fc53

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

docs/usage/template.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ Where:
106106
- [width] and [height] can be just numbers or numbers with measure, which supported by Word (cm, mm, in, pt, pc, px, %, em, ex)
107107
- [ratio] uses only for ``false``, ``-`` or ``f`` to turn off respect aspect ration of image. By default template image size uses as 'container' size.
108108

109+
You can use an array as first argument to replace all search patterns with the same file. If you use an indexed array as second argument,
110+
the first item in the first argument will be replaced by the first item in the second argument.
111+
109112
Example:
110113

111114
``` clean
@@ -128,6 +131,15 @@ $templateProcessor->setImageValue('FeatureImage', function () {
128131

129132
return array('path' => SlowFeatureImageGenerator::make(), 'width' => 100, 'height' => 100, 'ratio' => false);
130133
});
134+
135+
// use array to replace multiple values
136+
$templateProcessor->setImageValue(
137+
array('CompanyLogo', 'UserLogo'),
138+
array(
139+
'path/to/company/logo.svg',
140+
array('path' => 'path/to/logo.png', 'width' => 100, 'height' => 100, 'ratio' => false)
141+
)
142+
);
131143
```
132144

133145
## cloneBlock

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ public function testSetImageValue(): void
861861
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');
862862
$imageJpg = __DIR__ . '/_files/images/earth.jpg';
863863
$imageGif = __DIR__ . '/_files/images/mario.gif';
864+
$imagePng = __DIR__ . '/_files/images/firefox.png';
864865
$imageSvg = __DIR__ . '/_files/images/phpword.svg';
865866

866867
$variablesReplace = [
@@ -909,16 +910,17 @@ public function testSetImageValue(): void
909910
$phpWord = new PhpWord();
910911
$section = $phpWord->addSection();
911912
$section->addText('${Test0:width=100:ratio=true}');
912-
$section->addText('${Test1:height=50:ratio=true}');
913-
$section->addText('${Test2:width=10cm:height=7cm:ratio=false}');
913+
$section->addText('${Test1::50:true}');
914+
$section->addText('${Test2:size=10cmx7cm:ratio=false}');
915+
$section->addText('${Test3}');
914916
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
915917
$objWriter->save($testFileName);
916918
self::assertFileExists($testFileName, "Generated file '{$testFileName}' not found!");
917919

918920
$resultFileName = 'images-test-result.docx';
919921
$templateProcessor = new TemplateProcessor($testFileName);
920922
unlink($testFileName);
921-
$templateProcessor->setImageValue(['Test0', 'Test1', 'Test2'], [$imageJpg, $imageGif, $imageSvg]);
923+
$templateProcessor->setImageValue(['Test0', 'Test1', 'Test2', 'Test3'], [$imageJpg, $imageGif, $imageSvg, $imagePng]);
922924
$templateProcessor->saveAs($resultFileName);
923925
self::assertFileExists($resultFileName, "Generated file '{$resultFileName}' not found!");
924926

@@ -930,7 +932,7 @@ public function testSetImageValue(): void
930932
}
931933
unlink($resultFileName);
932934

933-
self::assertStringNotContainsString('${Test}', $expectedMainPartXml, 'word/document.xml has no image.');
935+
self::assertStringNotContainsString('${Test', $expectedMainPartXml, 'word/document.xml has no image.');
934936
}
935937

936938
/**

0 commit comments

Comments
 (0)