@@ -223,4 +223,56 @@ public function testCloneDeleteBlock()
223223 unlink ($ docName );
224224 $ this ->assertTrue ($ docFound );
225225 }
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+ $ actualElements = $ sections [0 ]->getElements ();
264+ unlink ($ templatePath );
265+ $ expectedElements = array (
266+ 'Title: Some title ' ,
267+ '123: Some text. ' ,
268+ '456: Some other text. ' ,
269+ );
270+ $ this ->assertCount (count ($ expectedElements ), $ actualElements );
271+ foreach ($ expectedElements as $ i => $ expectedElement ) {
272+ $ this ->assertEquals (
273+ $ expectedElement ,
274+ $ actualElements [$ i ]->getText ()
275+ );
276+ }
277+ }
226278}
0 commit comments