1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class HtmlTableFormatTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private string $data = ''; |
15
|
|
|
|
16
|
|
|
protected function setUp(): void |
17
|
|
|
{ |
18
|
|
|
$file = 'tests/data/Writer/Html/HtmlTableFormatTest.xlsx'; |
19
|
|
|
$reader = new XlsxReader(); |
20
|
|
|
$spreadsheet = $reader->load($file); |
21
|
|
|
$writer = new HtmlWriter($spreadsheet); |
22
|
|
|
$writer->setTableFormats(true); |
23
|
|
|
$this->data = $writer->generateHtmlAll(); |
24
|
|
|
$spreadsheet->disconnectWorksheets(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function extractCell(string $coordinate): string |
28
|
|
|
{ |
29
|
|
|
[$column, $row] = Coordinate::indexesFromString($coordinate); |
30
|
|
|
--$column; |
31
|
|
|
--$row; |
32
|
|
|
// extract row into $matches |
33
|
|
|
$match = preg_match('~<tr class="row' . $row . '".+?</tr>~s', $this->data, $matches); |
34
|
|
|
if ($match !== 1) { |
35
|
|
|
return 'unable to match row'; |
36
|
|
|
} |
37
|
|
|
$rowData = $matches[0]; |
38
|
|
|
// extract cell into $matches |
39
|
|
|
$match = preg_match('~<td class="column' . $column . ' .+?</td>~s', $rowData, $matches); |
40
|
|
|
if ($match !== 1) { |
41
|
|
|
return 'unable to match column'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $matches[0]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testHtmlTableFormatOutput(): void |
48
|
|
|
{ |
49
|
|
|
$expectedMatches = [ |
50
|
|
|
['J1', 'background-color:#145F82;">Sep<', 'table style for header row cell J1'], |
51
|
|
|
['J2', 'background-color:#C0E4F5;">110<', 'table style for cell J2'], |
52
|
|
|
['I3', 'background-color:#82CAEB;">70<', 'table style for cell I3'], |
53
|
|
|
['J3', 'background-color:#82CAEB;">70<', 'table style for cell J3'], // as conditional calculations are off |
54
|
|
|
['K3', 'background-color:#82CAEB;">70<', 'table style for cell K3'], |
55
|
|
|
['J4', 'background-color:#C0E4F5;">1<', 'table style for cell J4'], |
56
|
|
|
['J5', 'background-color:#82CAEB;">1<', 'table style for cell J5'], |
57
|
|
|
]; |
58
|
|
|
foreach ($expectedMatches as $expected) { |
59
|
|
|
[$coordinate, $expectedString, $message] = $expected; |
60
|
|
|
$string = $this->extractCell($coordinate); |
61
|
|
|
self::assertStringContainsString($expectedString, $string, $message); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|