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 HtmlConditionalFormattingTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private string $data = ''; |
15
|
|
|
|
16
|
|
|
protected function setUp(): void |
17
|
|
|
{ |
18
|
|
|
$file = 'tests/data/Writer/Html/HtmlConditionalFormattingTest.xlsx'; |
19
|
|
|
$reader = new XlsxReader(); |
20
|
|
|
$spreadsheet = $reader->load($file); |
21
|
|
|
$writer = new HtmlWriter($spreadsheet); |
22
|
|
|
$writer->setConditionalFormatting(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 testConditionalFormattingHtmLOutput(): void |
48
|
|
|
{ |
49
|
|
|
$expectedMatches = [ |
50
|
|
|
['B1', 'class="column1 style1 s">Jan<', 'no conditional styling for B1'], |
51
|
|
|
['F2', '"vertical-align:bottom;border-bottom:1px solid #000000 !important;border-top:1px solid #000000 !important;border-left:1px solid #000000 !important;border-right:1px solid #000000 !important;color:#006100;font-family:\'Arial\';font-size:11pt;background-color:#C6EFCE;">120<', 'conditional style for F2'], |
52
|
|
|
['H2', '"vertical-align:bottom;border-bottom:1px solid #000000 !important;border-top:1px solid #000000 !important;border-left:1px solid #000000 !important;border-right:1px solid #000000 !important;color:#9C5700;font-family:\'Arial\';font-size:11pt;background-color:#FFEB9C;">90<', 'conditional style for H2'], |
53
|
|
|
['F3', '"vertical-align:bottom;border-bottom:1px solid #000000 !important;border-top:1px solid #000000 !important;border-left:1px solid #000000 !important;border-right:1px solid #000000 !important;color:#006100;font-family:\'Arial\';font-size:11pt;background-color:#C6EFCE;">70<', 'conditional style for cell F3'], |
54
|
|
|
['H3', '"vertical-align:bottom;border-bottom:1px solid #000000 !important;border-top:1px solid #000000 !important;border-left:1px solid #000000 !important;border-right:1px solid #000000 !important;color:#9C5700;font-family:\'Arial\';font-size:11pt;background-color:#FFEB9C;">60<', 'conditional style for cell H3'], |
55
|
|
|
['F4', '"vertical-align:bottom;border-bottom:1px solid #000000 !important;border-top:1px solid #000000 !important;border-left:1px solid #000000 !important;border-right:1px solid #000000 !important;color:#006100;font-family:\'Arial\';font-size:11pt;background-color:#C6EFCE;">1<', 'conditional style for cell F4'], |
56
|
|
|
['L4', '"vertical-align:bottom;border-bottom:1px solid #000000 !important;border-top:1px solid #000000 !important;border-left:1px solid #000000 !important;border-right:1px solid #000000 !important;color:#9C0006;font-family:\'Arial\';font-size:11pt;background-color:#FFC7CE;">5<', 'conditional style for cell L4'], |
57
|
|
|
['F5', 'class="column5 style1 n">0<', 'no conditional styling for F5'], |
58
|
|
|
]; |
59
|
|
|
foreach ($expectedMatches as $expected) { |
60
|
|
|
[$coordinate, $expectedString, $message] = $expected; |
61
|
|
|
$string = $this->extractCell($coordinate); |
62
|
|
|
self::assertStringContainsString($expectedString, $string, $message); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|