@@ -223,4 +223,57 @@ public function testCloneDeleteBlock()
223
223
unlink ($ docName );
224
224
$ this ->assertTrue ($ docFound );
225
225
}
226
+
227
+ /**
228
+ * @covers ::cloneBlock
229
+ * @test
230
+ */
231
+ public function cloneBlockCanCloneABlockTwice ()
232
+ {
233
+ // create template with placeholders and block
234
+ $ phpWord = new PhpWord ();
235
+ $ section = $ phpWord ->addSection ();
236
+ $ documentElements = array (
237
+ 'Title: ${title} ' ,
238
+ '${subreport} ' ,
239
+ '${subreport.id}: ${subreport.text}. ' ,
240
+ '${/subreport} ' ,
241
+ );
242
+ foreach ($ documentElements as $ documentElement ) {
243
+ $ section ->addText ($ documentElement );
244
+ }
245
+ $ objWriter = IOFactory::createWriter ($ phpWord );
246
+ $ templatePath = 'test.docx ' ;
247
+ $ objWriter ->save ($ templatePath );
248
+
249
+ // replace placeholders and save the file
250
+ $ templateProcessor = new TemplateProcessor ($ templatePath );
251
+ $ templateProcessor ->setValue ('title ' , 'Some title ' );
252
+ $ templateProcessor ->cloneBlock ('subreport ' , 2 );
253
+ $ templateProcessor ->setValue ('subreport.id ' , '123 ' , 1 );
254
+ $ templateProcessor ->setValue ('subreport.text ' , 'Some text ' , 1 );
255
+ $ templateProcessor ->setValue ('subreport.id ' , '456 ' , 1 );
256
+ $ templateProcessor ->setValue ('subreport.text ' , 'Some other text ' , 1 );
257
+ $ templateProcessor ->saveAs ($ templatePath );
258
+
259
+ // assert the block has been cloned twice
260
+ // and the placeholders have been replaced correctly
261
+ $ phpWord = IOFactory::load ($ templatePath );
262
+ $ sections = $ phpWord ->getSections ();
263
+ /** @var \PhpOffice\PhpWord\Element\TextRun[] $actualElements */
264
+ $ actualElements = $ sections [0 ]->getElements ();
265
+ unlink ($ templatePath );
266
+ $ expectedElements = array (
267
+ 'Title: Some title ' ,
268
+ '123: Some text. ' ,
269
+ '456: Some other text. ' ,
270
+ );
271
+ $ this ->assertCount (count ($ expectedElements ), $ actualElements );
272
+ foreach ($ expectedElements as $ i => $ expectedElement ) {
273
+ $ this ->assertEquals (
274
+ $ expectedElement ,
275
+ $ actualElements [$ i ]->getElement (0 )->getText ()
276
+ );
277
+ }
278
+ }
226
279
}
0 commit comments