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 HtmlColourScaleTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private string $data = ''; |
15
|
|
|
|
16
|
|
|
protected function setUp(): void |
17
|
|
|
{ |
18
|
|
|
$file = 'samples/templates/ColourScale.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 testColourScaleHtmlOutput(): void |
48
|
|
|
{ |
49
|
|
|
$expectedMatches = [ |
50
|
|
|
['E1', 'background-color:#B4CA76;">5<', 'cell E1'], |
51
|
|
|
['F1', 'background-color:#CBCD71;">6<', 'cell F1'], |
52
|
|
|
['G1', 'background-color:#E3D16C;">7<', 'cell G1'], |
53
|
|
|
['D2', 'background-color:#57BB8A;">4<', 'cell D2'], |
54
|
|
|
['E2', 'background-color:#A1C77A;">5<', 'cell E2'], |
55
|
|
|
['F2', 'background-color:#F1A36D;">6<', 'cell F2'], |
56
|
|
|
['D3', 'background-color:#FFD666;">4<', 'cell D3'], |
57
|
|
|
['G3', 'background-color:#EC926F;">7<', 'cell G3'], |
58
|
|
|
['H3', 'background-color:#E67C73;">8<', 'cell H3'], |
59
|
|
|
['A4', 'background-color:#57BB8A;">1<', 'cell A4'], |
60
|
|
|
['I4', 'null"><', 'empty cell I4'], |
61
|
|
|
['J4', 'background-color:#E67C73;">10<', 'cell J4'], |
62
|
|
|
]; |
63
|
|
|
foreach ($expectedMatches as $expected) { |
64
|
|
|
[$coordinate, $expectedString, $message] = $expected; |
65
|
|
|
$string = $this->extractCell($coordinate); |
66
|
|
|
self::assertStringContainsString($expectedString, $string, $message); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|