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

HideMergeTest::testHideWithMerge()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 110
Code Lines 93

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 93
c 1
b 0
f 0
dl 0
loc 110
rs 8.1527
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\Tcpdf;
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\Tcpdf;
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
        $Tcpdf = new Tcpdf($spreadsheet);
84
        $html = $Tcpdf->generateHtmlAll();
85
        $html = preg_replace('/^\\s+/m', '', $html) ?? $html;
86
        $html = preg_replace('/[\\n\\r]/', '', $html) ?? $html;
87
        self::assertStringContainsString(
88
            '<tbody><tr style="height:17pt">'
89
                . '<td></td>'
90
                . '<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp">B</td>'
91
                . '<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp">C</td>'
92
                . '</tr>',
93
            $html
94
        );
95
        self::assertStringContainsString(
96
            '<tr style="height:17pt">'
97
                . '<td></td>'
98
                . '<td style="vertical-align:bottom; text-align:center; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; width:84pt; height:17pt" class="gridlines gridlinesp" colspan="2" rowspan="2">Hello World Headline</td>'
99
                . '</tr>',
100
            $html
101
        );
102
        $emptyRowCount = substr_count(
103
            $html,
104
            '<tr style="height:17pt">'
105
                . '<td></td>'
106
                . '</tr>'
107
        );
108
        self::assertSame(3, $emptyRowCount);
109
        self::assertStringContainsString(
110
            '<tr style="height:17pt">'
111
                . '<td></td>'
112
                . '<td style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp" rowspan="2">Label 1</td>'
113
                . '<td style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp" rowspan="2">Text 1</td>'
114
                . '</tr>',
115
            $html
116
        );
117
        self::assertStringContainsString(
118
            '<tr style="height:17pt">'
119
                . '<td></td>'
120
                . '<td style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp" rowspan="2">Label 2</td>'
121
                . '<td style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp" rowspan="2">Text 2</td>'
122
                . '</tr>',
123
            $html
124
        );
125
        $spreadsheet->disconnectWorksheets();
126
    }
127
}
128