1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Html; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class HtmlTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testCsvWithAngleBracket() |
15
|
|
|
{ |
16
|
|
|
$filename = __DIR__ . '/../../data/Reader/HTML/csv_with_angle_bracket.csv'; |
17
|
|
|
$reader = new Html(); |
18
|
|
|
self::assertFalse($reader->canRead($filename)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function providerCanReadVerySmallFile() |
22
|
|
|
{ |
23
|
|
|
$padding = str_repeat('a', 2048); |
24
|
|
|
|
25
|
|
|
return [ |
26
|
|
|
[true, ' <html> ' . $padding . ' </html> '], |
27
|
|
|
[true, ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>' . $padding . '</html>'], |
28
|
|
|
[true, '<html></html>'], |
29
|
|
|
[false, ''], |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @dataProvider providerCanReadVerySmallFile |
35
|
|
|
* |
36
|
|
|
* @param bool $expected |
37
|
|
|
* @param string $content |
38
|
|
|
*/ |
39
|
|
|
public function testCanReadVerySmallFile($expected, $content) |
40
|
|
|
{ |
41
|
|
|
$filename = $this->createHtml($content); |
42
|
|
|
$reader = new Html(); |
43
|
|
|
$actual = $reader->canRead($filename); |
44
|
|
|
|
45
|
|
|
self::assertSame($expected, $actual); |
46
|
|
|
|
47
|
|
|
unlink($filename); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testBackgroundColorInRanding() |
51
|
|
|
{ |
52
|
|
|
$html = '<table> |
53
|
|
|
<tr> |
54
|
|
|
<td style="background-color: #000000;color: #FFFFFF">Blue background</td> |
55
|
|
|
</tr> |
56
|
|
|
</table>'; |
57
|
|
|
$filename = $this->createHtml($html); |
58
|
|
|
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); |
59
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
60
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
61
|
|
|
|
62
|
|
|
self::assertEquals('FFFFFF', $style->getFont()->getColor()->getRGB()); |
63
|
|
|
|
64
|
|
|
unlink($filename); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testCanApplyInlineBordersStyles() |
68
|
|
|
{ |
69
|
|
|
$html = '<table> |
70
|
|
|
<tr> |
71
|
|
|
<td style="border: 1px solid #333333;">Thin border</td> |
72
|
|
|
<td style="border-bottom: 1px solid #333333;">Border bottom</td> |
73
|
|
|
<td style="border-top: 1px solid #333333;">Border top</td> |
74
|
|
|
<td style="border-left: 1px solid #333333;">Border left</td> |
75
|
|
|
<td style="border-right: 1px solid #333333;">Border right</td> |
76
|
|
|
</tr> |
77
|
|
|
</table>'; |
78
|
|
|
$filename = $this->createHtml($html); |
79
|
|
|
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); |
80
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
81
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
82
|
|
|
$borders = $style->getBorders(); |
83
|
|
|
|
84
|
|
|
/** @var Border $border */ |
85
|
|
|
foreach ([$borders->getTop(), $borders->getBottom(), $borders->getLeft(), $borders->getRight()] as $border) { |
86
|
|
|
self::assertEquals('333333', $border->getColor()->getRGB()); |
87
|
|
|
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$style = $firstSheet->getCell('B1')->getStyle(); |
91
|
|
|
$border = $style->getBorders()->getBottom(); |
92
|
|
|
self::assertEquals('333333', $border->getColor()->getRGB()); |
93
|
|
|
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); |
94
|
|
|
|
95
|
|
|
$style = $firstSheet->getCell('C1')->getStyle(); |
96
|
|
|
$border = $style->getBorders()->getTop(); |
97
|
|
|
self::assertEquals('333333', $border->getColor()->getRGB()); |
98
|
|
|
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); |
99
|
|
|
|
100
|
|
|
$style = $firstSheet->getCell('D1')->getStyle(); |
101
|
|
|
$border = $style->getBorders()->getLeft(); |
102
|
|
|
self::assertEquals('333333', $border->getColor()->getRGB()); |
103
|
|
|
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); |
104
|
|
|
|
105
|
|
|
$style = $firstSheet->getCell('E1')->getStyle(); |
106
|
|
|
$border = $style->getBorders()->getRight(); |
107
|
|
|
self::assertEquals('333333', $border->getColor()->getRGB()); |
108
|
|
|
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); |
109
|
|
|
|
110
|
|
|
unlink($filename); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testCanApplyInlineFontStyles() |
114
|
|
|
{ |
115
|
|
|
$html = '<table> |
116
|
|
|
<tr> |
117
|
|
|
<td style="font-size: 16px;">16px</td> |
118
|
|
|
<td style="font-family: \'Times New Roman\'">Times New Roman</td> |
119
|
|
|
<td style="font-weight: bold;">Bold</td> |
120
|
|
|
<td style="font-style: italic;">Italic</td> |
121
|
|
|
<td style="text-decoration: underline;">Underline</td> |
122
|
|
|
<td style="text-decoration: line-through;">Line through</td> |
123
|
|
|
</tr> |
124
|
|
|
</table>'; |
125
|
|
|
$filename = $this->createHtml($html); |
126
|
|
|
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); |
127
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
128
|
|
|
|
129
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
130
|
|
|
self::assertEquals(16, $style->getFont()->getSize()); |
131
|
|
|
|
132
|
|
|
$style = $firstSheet->getCell('B1')->getStyle(); |
133
|
|
|
self::assertEquals('Times New Roman', $style->getFont()->getName()); |
134
|
|
|
|
135
|
|
|
$style = $firstSheet->getCell('C1')->getStyle(); |
136
|
|
|
self::assertTrue($style->getFont()->getBold()); |
137
|
|
|
|
138
|
|
|
$style = $firstSheet->getCell('D1')->getStyle(); |
139
|
|
|
self::assertTrue($style->getFont()->getItalic()); |
140
|
|
|
|
141
|
|
|
$style = $firstSheet->getCell('E1')->getStyle(); |
142
|
|
|
self::assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline()); |
143
|
|
|
|
144
|
|
|
$style = $firstSheet->getCell('F1')->getStyle(); |
145
|
|
|
self::assertTrue($style->getFont()->getStrikethrough()); |
146
|
|
|
|
147
|
|
|
unlink($filename); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testCanApplyInlineWidth() |
151
|
|
|
{ |
152
|
|
|
$html = '<table> |
153
|
|
|
<tr> |
154
|
|
|
<td width="50">50px</td> |
155
|
|
|
<td style="width: 100px;">100px</td> |
156
|
|
|
</tr> |
157
|
|
|
</table>'; |
158
|
|
|
$filename = $this->createHtml($html); |
159
|
|
|
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); |
160
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
161
|
|
|
|
162
|
|
|
$dimension = $firstSheet->getColumnDimension('A'); |
163
|
|
|
self::assertEquals(50, $dimension->getWidth()); |
164
|
|
|
|
165
|
|
|
$dimension = $firstSheet->getColumnDimension('B'); |
166
|
|
|
self::assertEquals(100, $dimension->getWidth()); |
167
|
|
|
|
168
|
|
|
unlink($filename); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testCanApplyInlineHeight() |
172
|
|
|
{ |
173
|
|
|
$html = '<table> |
174
|
|
|
<tr> |
175
|
|
|
<td height="50">1</td> |
176
|
|
|
</tr> |
177
|
|
|
<tr> |
178
|
|
|
<td style="height: 100px;">2</td> |
179
|
|
|
</tr> |
180
|
|
|
</table>'; |
181
|
|
|
$filename = $this->createHtml($html); |
182
|
|
|
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); |
183
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
184
|
|
|
|
185
|
|
|
$dimension = $firstSheet->getRowDimension(1); |
186
|
|
|
self::assertEquals(50, $dimension->getRowHeight()); |
187
|
|
|
|
188
|
|
|
$dimension = $firstSheet->getRowDimension(2); |
189
|
|
|
self::assertEquals(100, $dimension->getRowHeight()); |
190
|
|
|
|
191
|
|
|
unlink($filename); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testCanApplyAlignment() |
195
|
|
|
{ |
196
|
|
|
$html = '<table> |
197
|
|
|
<tr> |
198
|
|
|
<td align="center">Center align</td> |
199
|
|
|
<td valign="center">Center valign</td> |
200
|
|
|
<td style="text-align: center;">Center align</td> |
201
|
|
|
<td style="vertical-align: center;">Center valign</td> |
202
|
|
|
<td style="text-indent: 10px;">Text indent</td> |
203
|
|
|
<td style="word-wrap: break-word;">Wraptext</td> |
204
|
|
|
</tr> |
205
|
|
|
</table>'; |
206
|
|
|
$filename = $this->createHtml($html); |
207
|
|
|
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); |
208
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
209
|
|
|
|
210
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
211
|
|
|
self::assertEquals(Alignment::HORIZONTAL_CENTER, $style->getAlignment()->getHorizontal()); |
212
|
|
|
|
213
|
|
|
$style = $firstSheet->getCell('B1')->getStyle(); |
214
|
|
|
self::assertEquals(Alignment::VERTICAL_CENTER, $style->getAlignment()->getVertical()); |
215
|
|
|
|
216
|
|
|
$style = $firstSheet->getCell('C1')->getStyle(); |
217
|
|
|
self::assertEquals(Alignment::HORIZONTAL_CENTER, $style->getAlignment()->getHorizontal()); |
218
|
|
|
|
219
|
|
|
$style = $firstSheet->getCell('D1')->getStyle(); |
220
|
|
|
self::assertEquals(Alignment::VERTICAL_CENTER, $style->getAlignment()->getVertical()); |
221
|
|
|
|
222
|
|
|
$style = $firstSheet->getCell('E1')->getStyle(); |
223
|
|
|
self::assertEquals(10, $style->getAlignment()->getIndent()); |
224
|
|
|
|
225
|
|
|
$style = $firstSheet->getCell('F1')->getStyle(); |
226
|
|
|
self::assertTrue($style->getAlignment()->getWrapText()); |
227
|
|
|
|
228
|
|
|
unlink($filename); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testCanApplyInlineDataFormat() |
232
|
|
|
{ |
233
|
|
|
$html = '<table> |
234
|
|
|
<tr> |
235
|
|
|
<td data-format="mmm-yy">2019-02-02 12:34:00</td> |
236
|
|
|
</tr> |
237
|
|
|
</table>'; |
238
|
|
|
$filename = $this->createHtml($html); |
239
|
|
|
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); |
240
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
241
|
|
|
|
242
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
243
|
|
|
self::assertEquals('mmm-yy', $style->getNumberFormat()->getFormatCode()); |
244
|
|
|
|
245
|
|
|
unlink($filename); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testCanInsertImage() |
249
|
|
|
{ |
250
|
|
|
$imagePath = realpath(__DIR__ . '/../../data/Reader/HTML/image.jpg'); |
251
|
|
|
|
252
|
|
|
$html = '<table> |
253
|
|
|
<tr> |
254
|
|
|
<td><img src="' . $imagePath . '" alt=""></td> |
255
|
|
|
</tr> |
256
|
|
|
</table>'; |
257
|
|
|
$filename = $this->createHtml($html); |
258
|
|
|
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); |
259
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
260
|
|
|
|
261
|
|
|
/** @var Drawing $drawing */ |
262
|
|
|
$drawing = $firstSheet->getDrawingCollection()[0]; |
263
|
|
|
self::assertEquals($imagePath, $drawing->getPath()); |
264
|
|
|
self::assertEquals('A1', $drawing->getCoordinates()); |
265
|
|
|
|
266
|
|
|
unlink($filename); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param string $html |
271
|
|
|
* |
272
|
|
|
* @return string |
273
|
|
|
*/ |
274
|
|
|
private function createHtml($html) |
275
|
|
|
{ |
276
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'html'); |
277
|
|
|
file_put_contents($filename, $html); |
278
|
|
|
|
279
|
|
|
return $filename; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param $filename |
284
|
|
|
* |
285
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet |
286
|
|
|
*/ |
287
|
|
|
private function loadHtmlIntoSpreadsheet($filename) |
288
|
|
|
{ |
289
|
|
|
return (new Html())->load($filename); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|