Skip to content

Commit 296706a

Browse files
committed
add unit tests
1 parent a95c3f8 commit 296706a

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ v0.15.0 (?? ??? 2018)
2222
- Fix parsing of `<w:br/>` tag. @troosan #1274
2323
- Bookmark are not writton as internal link in html writer @troosan #1263
2424
- It should be possible to add a Footnote in a ListItemRun @troosan #1287 #1287
25+
- Fix colspan and rowspan for tables in HTML Writer @mattbolt #1292
2526

2627
### Changed
2728
- Remove zend-stdlib dependency @Trainmaster #1284

samples/Sample_09_Tables.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,20 @@
113113
$table = $section->addTable('Colspan Rowspan');
114114

115115
$row = $table->addRow();
116-
117-
$row->addCell(null, array('vMerge' => 'restart'))->addText('A');
118-
$row->addCell(null, array('gridSpan' => 2, 'vMerge' => 'restart'))->addText('B');
119-
$row->addCell()->addText('1');
116+
$row->addCell(1000, array('vMerge' => 'restart'))->addText('A');
117+
$row->addCell(1000, array('gridSpan' => 2, 'vMerge' => 'restart'))->addText('B');
118+
$row->addCell(1000)->addText('1');
120119

121120
$row = $table->addRow();
122-
$row->addCell(null, array('vMerge' => 'continue'));
123-
$row->addCell(null, array('vMerge' => 'continue', 'gridSpan' => 2));
124-
$row->addCell()->addText('2');
121+
$row->addCell(1000, array('vMerge' => 'continue'));
122+
$row->addCell(1000, array('vMerge' => 'continue', 'gridSpan' => 2));
123+
$row->addCell(1000)->addText('2');
124+
125125
$row = $table->addRow();
126-
$row->addCell(null, array('vMerge' => 'continue'));
127-
$row->addCell()->addText('C');
128-
$row->addCell()->addText('D');
129-
$row->addCell()->addText('3');
126+
$row->addCell(1000, array('vMerge' => 'continue'));
127+
$row->addCell(1000)->addText('C');
128+
$row->addCell(1000)->addText('D');
129+
$row->addCell(1000)->addText('3');
130130

131131
// 5. Nested table
132132

tests/PhpWord/Writer/HTML/ElementTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Writer\HTML;
1919

2020
use PhpOffice\PhpWord\Element\Text as TextElement;
21+
use PhpOffice\PhpWord\PhpWord;
2122
use PhpOffice\PhpWord\Writer\HTML;
2223
use PhpOffice\PhpWord\Writer\HTML\Element\Text;
2324

@@ -54,4 +55,71 @@ public function testWriteTextElement()
5455

5556
$this->assertEquals(htmlspecialchars('-A-', ENT_COMPAT, 'UTF-8'), $object->write());
5657
}
58+
59+
/**
60+
* Tests writing table with col span
61+
*/
62+
public function testWriteColSpan()
63+
{
64+
$phpWord = new PhpWord();
65+
$section = $phpWord->addSection();
66+
$table = $section->addTable();
67+
$row1 = $table->addRow();
68+
$cell11 = $row1->addCell(1000, array('gridSpan' => 2));
69+
$cell11->addText('cell spanning 2 bellow');
70+
$row2 = $table->addRow();
71+
$cell21 = $row2->addCell(500);
72+
$cell21->addText('first cell');
73+
$cell22 = $row2->addCell(500);
74+
$cell22->addText('second cell');
75+
76+
$dom = $this->getAsHTML($phpWord);
77+
echo $dom->saveHTML();
78+
79+
$xpath = new \DOMXpath($dom);
80+
81+
$this->assertTrue($xpath->query('/html/body/table/tr[1]/td')->length == 1);
82+
$this->assertEquals('2', $xpath->query('/html/body/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent);
83+
$this->assertTrue($xpath->query('/html/body/table/tr[2]/td')->length == 2);
84+
}
85+
86+
/**
87+
* Tests writing table with row span
88+
*/
89+
public function testWriteRowSpan()
90+
{
91+
$phpWord = new PhpWord();
92+
$section = $phpWord->addSection();
93+
$table = $section->addTable();
94+
95+
$row1 = $table->addRow();
96+
$row1->addCell(1000, array('vMerge' => 'restart'))->addText('row spanning 3 bellow');
97+
$row1->addCell(500)->addText('first cell being spanned');
98+
99+
$row2 = $table->addRow();
100+
$row2->addCell(null, array('vMerge' => 'continue'));
101+
$row2->addCell(500)->addText('second cell being spanned');
102+
103+
$row3 = $table->addRow();
104+
$row3->addCell(null, array('vMerge' => 'continue'));
105+
$row3->addCell(500)->addText('third cell being spanned');
106+
107+
$dom = $this->getAsHTML($phpWord);
108+
echo $dom->saveHTML();
109+
110+
$xpath = new \DOMXpath($dom);
111+
112+
$this->assertTrue($xpath->query('/html/body/table/tr[1]/td')->length == 2);
113+
$this->assertEquals('3', $xpath->query('/html/body/table/tr[1]/td[1]')->item(0)->attributes->getNamedItem('rowspan')->textContent);
114+
$this->assertTrue($xpath->query('/html/body/table/tr[2]/td')->length == 1);
115+
}
116+
117+
private function getAsHTML(PhpWord $phpWord)
118+
{
119+
$htmlWriter = new HTML($phpWord);
120+
$dom = new \DOMDocument();
121+
$dom->loadHTML($htmlWriter->getContent());
122+
123+
return $dom;
124+
}
57125
}

0 commit comments

Comments
 (0)