Failed Conditions
Pull Request — master (#4250)
by Owen
14:50
created

Issue4248Test   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testHtml() 0 22 1
A testStyles() 0 56 3
A tearDown() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6
7
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8
use PhpOffice\PhpSpreadsheet\Shared\File;
9
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
10
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
11
use PHPUnit\Framework\TestCase;
12
13
class Issue4248Test extends TestCase
14
{
15
    private string $outfile = '';
16
17
    protected function tearDown(): void
18
    {
19
        if ($this->outfile !== '') {
20
            unlink($this->outfile);
21
            $this->outfile = '';
22
        }
23
    }
24
25
    public function testStyles(): void
26
    {
27
        $file = 'tests/data/Reader/XLSX/issue.4248.xlsx';
28
        $reader = new XlsxReader();
29
        $spreadsheet = $reader->load($file);
30
        $sheet = $spreadsheet->getActiveSheet();
0 ignored issues
show
Unused Code introduced by
The assignment to $sheet is dead and can be removed.
Loading history...
31
        $writer = new XlsxWriter($spreadsheet);
32
        $this->outfile = File::temporaryFilename();
33
        $writer->save($this->outfile);
34
        $spreadsheet->disconnectWorksheets();
35
36
        $file = 'zip://';
37
        $file .= $this->outfile;
38
        $file .= '#xl/styles.xml';
39
        $data = file_get_contents($file) ?: '';
40
        $expected = '<fill>'
41
            . '<patternFill patternType="darkDown"/>'
42
            . '</fill>';
43
        self::assertStringContainsString($expected, $data, 'neither fgColor nor bgColor');
44
        $expected = '<fill>'
45
            . '<patternFill patternType="darkDown">'
46
            . '<bgColor rgb="FFBDD7EE"/>'
47
            . '</patternFill></fill>';
48
        self::assertStringContainsString($expected, $data, 'bgColor but no fgColor');
49
        $expected = '<dxfs count="15">'
50
            . '<dxf>' // dxfId 1 - fill color for Oui
51
            . '<fill>'
52
            . '<patternFill><bgColor rgb="FF00B050"/></patternFill>'
53
            . '</fill>'
54
            . '<border/>'
55
            . '</dxf>'
56
            . '<dxf>' // dxfId 2 - fill color for Non
57
            . '<font><color rgb="FF9C0006"/></font>'
58
            . '<fill>'
59
            . '<patternFill><bgColor rgb="FFFFC7CE"/></patternFill>'
60
            . '</fill>'
61
            . '<border/>'
62
            . '</dxf>';
63
        self::assertStringContainsString($expected, $data, 'conditional fill styles');
64
65
        $file = 'zip://';
66
        $file .= $this->outfile;
67
        $file .= '#xl/worksheets/sheet1.xml';
68
        $data = file_get_contents($file) ?: '';
69
        $expected = '<conditionalFormatting sqref="C16:C38 E17:H18 I17:J37 D18 J23:J38 E38 I38">'
70
            . '<cfRule type="containsText" dxfId="0" priority="1" operator="containsText" text="Oui">'
71
            . '<formula>NOT(ISERROR(SEARCH(&quot;Oui&quot;,C16)))</formula>'
72
            . '</cfRule>'
73
            . '</conditionalFormatting>';
74
        self::assertStringContainsString($expected, $data, 'first condition for D18');
75
        $expected = '<conditionalFormatting sqref="C16:C38 I17:J37 E17:H18 D18 J23:J38 E38 I38">'
76
            . '<cfRule type="containsText" dxfId="1" priority="2" operator="containsText" text="Non">'
77
            . '<formula>NOT(ISERROR(SEARCH(&quot;Non&quot;,C16)))</formula>'
78
            . '</cfRule>'
79
            . '</conditionalFormatting>';
80
        self::assertStringContainsString($expected, $data, 'second condition for D18');
81
    }
82
83
    public function testHtml(): void
84
    {
85
        $file = 'tests/data/Reader/XLSX/issue.4248.xlsx';
86
        $reader = new XlsxReader();
87
        $spreadsheet = $reader->load($file);
88
        $sheet = $spreadsheet->getActiveSheet();
0 ignored issues
show
Unused Code introduced by
The assignment to $sheet is dead and can be removed.
Loading history...
89
        $writer = new HtmlWriter($spreadsheet);
90
91
        $file = 'zip://';
92
        $file .= $this->outfile;
93
        $file .= '#xl/styles.xml';
94
        $data = str_replace(["\r", "\n"], '', $writer->generateHtmlAll());
95
        $expected = '          <tr class="row17">' // Cell D18
96
            . '            <td class="column0 style0">&nbsp;</td>'
97
            . '            <td class="column1 style28 null"></td>'
98
            . '            <td class="column2 style35 s">Eligible </td>'
99
            . '            <td class="column3 style70 f">Non</td>';
100
        self::assertStringContainsString($expected, $data, 'Cell D18 style');
101
        $expected = '      td.style70, th.style70 { vertical-align:middle; text-align:center; border-bottom:1px solid #000000 !important; border-top:2px solid #000000 !important; border-left:2px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:16pt; background-color:#BDD7EE }';
102
        self::assertStringContainsString($expected, $data, 'background color');
103
104
        $spreadsheet->disconnectWorksheets();
105
    }
106
}
107