Failed Conditions
Pull Request — master (#4320)
by Owen
13:16
created

HideMergeTest::testHideWithMerge()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 114
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 95
c 1
b 0
f 0
dl 0
loc 114
rs 8.109
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Writer\Mpdf;
6
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PhpOffice\PhpSpreadsheet\Style\Alignment;
9
use PhpOffice\PhpSpreadsheet\Style\Border;
10
use PhpOffice\PhpSpreadsheet\Style\Color;
11
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
12
use PHPUnit\Framework\TestCase;
13
14
class HideMergeTest extends TestCase
15
{
16
    public function testHideWithMerge(): void
17
    {
18
        $spreadsheet = new Spreadsheet();
19
        $worksheet = $spreadsheet->getActiveSheet();
20
        // just some labels for better visualisation of the problem
21
        $worksheet->setCellValue('A1', 'A');
22
        $worksheet->setCellValue('B1', 'B');
23
        $worksheet->setCellValue('C1', 'C');
24
        // setting the row height to better visualize the problem
25
        for ($i = 1; $i <= 10; ++$i) {
26
            $worksheet->getRowDimension($i)->setRowHeight(17);
27
        }
28
        // Headline - merged over two cells AND two rows
29
        $worksheet->mergeCells('B2:C3');
30
        $worksheet->setCellValue('B2', 'Hello World Headline');
31
        $worksheet->getStyle('B2:C3')->getFont()->setBold(true);
32
        $worksheet->getStyle('B2:C3')
33
            ->getAlignment()
34
            ->setHorizontal(Alignment::HORIZONTAL_CENTER);
35
        $worksheet->getStyle('B2:C3')
36
            ->getBorders()
37
            ->getAllBorders()
38
            ->setBorderStyle(Border::BORDER_THIN)
39
            ->setColor(new Color(Color::COLOR_BLACK));
40
41
        // Content 1 - merge over two rows
42
        $worksheet->mergeCells('B4:B5');
43
        $worksheet->mergeCells('C4:C5');
44
        $worksheet->setCellValue('B4', 'Label 1');
45
        $worksheet->setCellValue('C4', 'Text 1');
46
        $worksheet->getStyle('B4:B5')->getFont()->setBold(true);
47
        $worksheet->getStyle('B4:C5')
48
            ->getAlignment()
49
            ->setVertical(Alignment::VERTICAL_CENTER);
50
        $worksheet->getStyle('B4:B5')
51
            ->getBorders()
52
            ->getAllBorders()
53
            ->setBorderStyle(Border::BORDER_THIN)
54
            ->setColor(new Color(Color::COLOR_BLACK));
55
        $worksheet->getStyle('C4:C5')
56
            ->getBorders()
57
            ->getAllBorders()
58
            ->setBorderStyle(Border::BORDER_THIN)
59
            ->setColor(new Color(Color::COLOR_BLACK));
60
61
        // Content 2 - merge over two rows
62
        $worksheet->mergeCells('B6:B7');
63
        $worksheet->mergeCells('C6:C7');
64
        $worksheet->setCellValue('B6', 'Label 2');
65
        $worksheet->setCellValue('C6', 'Text 2');
66
        $worksheet->getStyle('B6:B7')->getFont()->setBold(true);
67
        $worksheet->getStyle('B6:C7')
68
            ->getAlignment()
69
            ->setVertical(Alignment::VERTICAL_CENTER);
70
        $worksheet->getStyle('B6:B7')
71
            ->getBorders()
72
            ->getAllBorders()
73
            ->setBorderStyle(Border::BORDER_THIN)
74
            ->setColor(new Color(Color::COLOR_BLACK));
75
        $worksheet->getStyle('C6:C7')
76
            ->getBorders()
77
            ->getAllBorders()
78
            ->setBorderStyle(Border::BORDER_THIN)
79
            ->setColor(new Color(Color::COLOR_BLACK));
80
81
        // This is where the error was introduced (!!!)
82
        $worksheet->getColumnDimension('A')->setVisible(false);
83
        $mpdf = new Mpdf($spreadsheet);
84
        $html = $mpdf->generateHtmlAll();
85
        $html = preg_replace('/^\\s+/m', '', $html) ?? $html;
86
        $html = preg_replace('/[\\n\\r]/', '', $html) ?? $html;
87
        self::assertStringContainsString(
88
            '<tr class="row0">'
89
                . '<td class="column1 style0 s" style="width:42pt; height:17pt">B</td>'
90
                . '<td class="column2 style0 s" style="width:42pt; height:17pt">C</td>'
91
                . '</tr>',
92
            $html
93
        );
94
        self::assertStringContainsString(
95
            '<tr class="row1">'
96
                . '<td class="column1 style1 s style1" style="width:84pt; height:17pt" colspan="2" rowspan="2">Hello World Headline</td>'
97
                . '</tr>',
98
            $html
99
        );
100
        self::assertStringContainsString(
101
            '<tr class="row2">'
102
                . '</tr>',
103
            $html
104
        );
105
        self::assertStringContainsString(
106
            '<tr class="row3">'
107
                . '<td class="column1 style2 s style2" style="width:42pt; height:17pt" rowspan="2">Label 1</td>'
108
                . '<td class="column2 style3 s style3" style="width:42pt; height:17pt" rowspan="2">Text 1</td>'
109
                . '</tr>',
110
            $html
111
        );
112
        self::assertStringContainsString(
113
            '<tr class="row4">'
114
                . '</tr>',
115
            $html
116
        );
117
        self::assertStringContainsString(
118
            '<tr class="row5">'
119
                . '<td class="column1 style2 s style2" style="width:42pt; height:17pt" rowspan="2">Label 2</td>'
120
                . '<td class="column2 style3 s style3" style="width:42pt; height:17pt" rowspan="2">Text 2</td>'
121
                . '</tr>',
122
            $html
123
        );
124
        self::assertStringContainsString(
125
            '<tr class="row6">'
126
                . '</tr>',
127
            $html
128
        );
129
        $spreadsheet->disconnectWorksheets();
130
    }
131
}
132