Failed Conditions
Push — master ( 8e3417...f52ae2 )
by
unknown
18:26 queued 07:14
created

HtmlTableFormatWithConditionalTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 33
c 1
b 0
f 0
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractCell() 0 18 3
A setUp() 0 10 1
A testHtmlTableFormatOutput() 0 15 2
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 HtmlTableFormatWithConditionalTest extends TestCase
13
{
14
    private string $data = '';
15
16
    protected function setUp(): void
17
    {
18
        $file = 'samples/templates/TableFormat.xlsx';
19
        $reader = new XlsxReader();
20
        $spreadsheet = $reader->load($file);
21
        $writer = new HtmlWriter($spreadsheet);
22
        $writer->setTableFormats(true);
23
        $writer->setConditionalFormatting(true);
24
        $this->data = $writer->generateHtmlAll();
25
        $spreadsheet->disconnectWorksheets();
26
    }
27
28
    private function extractCell(string $coordinate): string
29
    {
30
        [$column, $row] = Coordinate::indexesFromString($coordinate);
31
        --$column;
32
        --$row;
33
        // extract row into $matches
34
        $match = preg_match('~<tr class="row' . $row . '".+?</tr>~s', $this->data, $matches);
35
        if ($match !== 1) {
36
            return 'unable to match row';
37
        }
38
        $rowData = $matches[0];
39
        // extract cell into $matches
40
        $match = preg_match('~<td class="column' . $column . ' .+?</td>~s', $rowData, $matches);
41
        if ($match !== 1) {
42
            return 'unable to match column';
43
        }
44
45
        return $matches[0];
46
    }
47
48
    public function testHtmlTableFormatOutput(): void
49
    {
50
        $expectedMatches = [
51
            ['J1', 'background-color:#145F82;">Sep<', 'table style for header row cell J1'],
52
            ['J2', 'background-color:#C0E4F5;">110<', 'table style for cell J2'],
53
            ['I3', 'background-color:#82CAEB;">70<', 'table style for cell I3'],
54
            ['J3', 'background-color:#B7E1CD;">70<', 'conditional style for cell J3'], // as conditional calculations are on
55
            ['K3', 'background-color:#82CAEB;">70<', 'table style for cell K3'],
56
            ['J4', 'background-color:#C0E4F5;">1<', 'table style for cell J4'],
57
            ['J5', 'background-color:#82CAEB;">1<', 'table style for cell J5'],
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