Failed Conditions
Pull Request — master (#4412)
by
unknown
22:45 queued 07:48
created

HtmlTableFormatTest::extractCell()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
rs 9.9
cc 3
nc 3
nop 1
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