add unit tests

This commit is contained in:
troosan 2018-03-20 21:35:06 +01:00
parent a95c3f83bc
commit 296706aa03
3 changed files with 80 additions and 11 deletions

View File

@ -22,6 +22,7 @@ v0.15.0 (?? ??? 2018)
- Fix parsing of `<w:br/>` tag. @troosan #1274 - Fix parsing of `<w:br/>` tag. @troosan #1274
- Bookmark are not writton as internal link in html writer @troosan #1263 - Bookmark are not writton as internal link in html writer @troosan #1263
- It should be possible to add a Footnote in a ListItemRun @troosan #1287 #1287 - It should be possible to add a Footnote in a ListItemRun @troosan #1287 #1287
- Fix colspan and rowspan for tables in HTML Writer @mattbolt #1292
### Changed ### Changed
- Remove zend-stdlib dependency @Trainmaster #1284 - Remove zend-stdlib dependency @Trainmaster #1284

View File

@ -113,20 +113,20 @@ $phpWord->addTableStyle('Colspan Rowspan', $styleTable);
$table = $section->addTable('Colspan Rowspan'); $table = $section->addTable('Colspan Rowspan');
$row = $table->addRow(); $row = $table->addRow();
$row->addCell(1000, array('vMerge' => 'restart'))->addText('A');
$row->addCell(null, array('vMerge' => 'restart'))->addText('A'); $row->addCell(1000, array('gridSpan' => 2, 'vMerge' => 'restart'))->addText('B');
$row->addCell(null, array('gridSpan' => 2, 'vMerge' => 'restart'))->addText('B'); $row->addCell(1000)->addText('1');
$row->addCell()->addText('1');
$row = $table->addRow(); $row = $table->addRow();
$row->addCell(null, array('vMerge' => 'continue')); $row->addCell(1000, array('vMerge' => 'continue'));
$row->addCell(null, array('vMerge' => 'continue', 'gridSpan' => 2)); $row->addCell(1000, array('vMerge' => 'continue', 'gridSpan' => 2));
$row->addCell()->addText('2'); $row->addCell(1000)->addText('2');
$row = $table->addRow(); $row = $table->addRow();
$row->addCell(null, array('vMerge' => 'continue')); $row->addCell(1000, array('vMerge' => 'continue'));
$row->addCell()->addText('C'); $row->addCell(1000)->addText('C');
$row->addCell()->addText('D'); $row->addCell(1000)->addText('D');
$row->addCell()->addText('3'); $row->addCell(1000)->addText('3');
// 5. Nested table // 5. Nested table

View File

@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Writer\HTML; namespace PhpOffice\PhpWord\Writer\HTML;
use PhpOffice\PhpWord\Element\Text as TextElement; use PhpOffice\PhpWord\Element\Text as TextElement;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\HTML; use PhpOffice\PhpWord\Writer\HTML;
use PhpOffice\PhpWord\Writer\HTML\Element\Text; use PhpOffice\PhpWord\Writer\HTML\Element\Text;
@ -54,4 +55,71 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(htmlspecialchars('-A-', ENT_COMPAT, 'UTF-8'), $object->write()); $this->assertEquals(htmlspecialchars('-A-', ENT_COMPAT, 'UTF-8'), $object->write());
} }
/**
* 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);
echo $dom->saveHTML();
$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);
echo $dom->saveHTML();
$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;
}
} }