assertEquals('', $object->write()); } } /** * Test write element text */ public function testWriteTextElement() { $object = new Text(new HTML(), new TextElement(htmlspecialchars('A', ENT_COMPAT, 'UTF-8'))); $object->setOpeningText(htmlspecialchars('-', ENT_COMPAT, 'UTF-8')); $object->setClosingText(htmlspecialchars('-', ENT_COMPAT, 'UTF-8')); $object->setWithoutP(true); $this->assertEquals(htmlspecialchars('-A-', ENT_COMPAT, 'UTF-8'), $object->write()); } /** * Test write TrackChange */ public function testWriteTrackChanges() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $text = $section->addText('my dummy text'); $text->setChangeInfo(TrackChange::INSERTED, 'author name'); $text2 = $section->addText('my other text'); $text2->setTrackChange(new TrackChange(TrackChange::DELETED, 'another author', new \DateTime())); $dom = $this->getAsHTML($phpWord); $xpath = new \DOMXPath($dom); $this->assertTrue($xpath->query('/html/body/p[1]/ins')->length == 1); $this->assertTrue($xpath->query('/html/body/p[2]/del')->length == 1); } /** * Tests writing table with col span */ public function testWriteColSpan() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $table = $section->addTable(); $row1 = $table->addRow(); $cell11 = $row1->addCell(1000, array('gridSpan' => 2)); $cell11->addText('cell spanning 2 bellow'); $row2 = $table->addRow(); $cell21 = $row2->addCell(500); $cell21->addText('first cell'); $cell22 = $row2->addCell(500); $cell22->addText('second cell'); $dom = $this->getAsHTML($phpWord); $xpath = new \DOMXPath($dom); $this->assertTrue($xpath->query('/html/body/table/tr[1]/td')->length == 1); $this->assertEquals('2', $xpath->query('/html/body/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent); $this->assertTrue($xpath->query('/html/body/table/tr[2]/td')->length == 2); } /** * Tests writing table with row span */ public function testWriteRowSpan() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $table = $section->addTable(); $row1 = $table->addRow(); $row1->addCell(1000, array('vMerge' => 'restart'))->addText('row spanning 3 bellow'); $row1->addCell(500)->addText('first cell being spanned'); $row2 = $table->addRow(); $row2->addCell(null, array('vMerge' => 'continue')); $row2->addCell(500)->addText('second cell being spanned'); $row3 = $table->addRow(); $row3->addCell(null, array('vMerge' => 'continue')); $row3->addCell(500)->addText('third cell being spanned'); $dom = $this->getAsHTML($phpWord); $xpath = new \DOMXPath($dom); $this->assertTrue($xpath->query('/html/body/table/tr[1]/td')->length == 2); $this->assertEquals('3', $xpath->query('/html/body/table/tr[1]/td[1]')->item(0)->attributes->getNamedItem('rowspan')->textContent); $this->assertTrue($xpath->query('/html/body/table/tr[2]/td')->length == 1); } private function getAsHTML(PhpWord $phpWord) { $htmlWriter = new HTML($phpWord); $dom = new \DOMDocument(); $dom->loadHTML($htmlWriter->getContent()); return $dom; } public function testWriteTitleTextRun() { $expected = 'Title with TextRun'; $phpWord = new PhpWord(); $section = $phpWord->addSection(); $textRun = new TextRun(); $textRun->addText($expected); $section->addTitle($textRun); $htmlWriter = new HTML($phpWord); $content = $htmlWriter->getContent(); $this->assertTrue(strpos($content, $expected) !== false); } /** * Tests writing table with layout */ public function testWriteTableLayout() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $section->addTable(); $table1 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED)); $row1 = $table1->addRow(); $row1->addCell()->addText('fixed layout table'); $table2 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO)); $row2 = $table2->addRow(); $row2->addCell()->addText('auto layout table'); $dom = $this->getAsHTML($phpWord); $xpath = new \DOMXPath($dom); $this->assertEquals('table-layout: fixed;', $xpath->query('/html/body/table[1]')->item(0)->attributes->getNamedItem('style')->textContent); $this->assertEquals('table-layout: auto;', $xpath->query('/html/body/table[2]')->item(0)->attributes->getNamedItem('style')->textContent); } }