Passed
Pull Request — master (#4207)
by Owen
13:50
created

AutoSizeTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
6
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PhpOffice\PhpSpreadsheet\Style\Alignment;
9
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
10
use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableStyle;
11
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
12
use PHPUnit\Framework\TestCase;
13
14
class AutoSizeTest extends TestCase
15
{
16
    private Spreadsheet $spreadsheet;
17
18
    private Worksheet $worksheet;
19
20
    protected function setUp(): void
21
    {
22
        $this->spreadsheet = new Spreadsheet();
23
        $this->worksheet = $this->spreadsheet->getActiveSheet();
24
25
        $this->worksheet->setCellValue('A1', 'YEAR')
26
            ->setCellValue('B1', 'QUARTER')
27
            ->setCellValue('C1', 'COUNTRY')
28
            ->setCellValue('D1', 'SALES');
29
        $dataArray = [
30
            ['10', 'Q1', 'United States', 790],
31
            ['10', 'Q2', 'United States', 730],
32
            ['10', 'Q3', 'United States', 860],
33
            ['10', 'Q4', 'United States', 850],
34
        ];
35
36
        $this->worksheet->fromArray($dataArray, null, 'A2');
37
38
        $toColumn = $this->worksheet->getHighestColumn();
39
        ++$toColumn;
40
        for ($i = 'A'; $i !== $toColumn; ++$i) {
41
            $this->worksheet->getColumnDimension($i)->setAutoSize(true);
42
        }
43
    }
44
45
    protected function tearDown(): void
46
    {
47
        $this->spreadsheet->disconnectWorksheets();
48
        unset($this->spreadsheet, $this->worksheet);
49
    }
50
51
    private function setTable(): Table
52
    {
53
        $table = new Table('A1:D5', 'Sales_Data');
54
        $tableStyle = new TableStyle();
55
        $tableStyle->setTheme(TableStyle::TABLE_STYLE_MEDIUM2);
56
        $table->setStyle($tableStyle);
57
        $this->worksheet->addTable($table);
58
59
        return $table;
60
    }
61
62
    private function readColumnSizes(): array
63
    {
64
        $columnSizes = [];
65
        $toColumn = $this->worksheet->getHighestColumn();
66
        ++$toColumn;
67
        for ($column = 'A'; $column !== $toColumn; ++$column) {
68
            $columnSizes[$column] = $this->worksheet->getColumnDimension($column)->getWidth();
69
        }
70
71
        return $columnSizes;
72
    }
73
74
    public function testStandardAutoSize(): void
75
    {
76
        $this->worksheet->calculateColumnWidths();
77
        $columnSizes = $this->readColumnSizes();
78
79
        self::assertSame(['A' => 5.856, 'B' => 9.283, 'C' => 16.425, 'D' => 6.998], $columnSizes);
80
    }
81
82
    public function testAutoFilterAutoSize(): void
83
    {
84
        $this->worksheet->setAutoFilter('A1:D5');
85
86
        $this->worksheet->calculateColumnWidths();
87
        $columnSizes = $this->readColumnSizes();
88
89
        self::assertSame(['A' => 8.141, 'B' => 11.569, 'C' => 16.425, 'D' => 9.283], $columnSizes);
90
    }
91
92
    public function testTableWithAutoFilterAutoSize(): void
93
    {
94
        $this->setTable();
95
96
        $this->worksheet->calculateColumnWidths();
97
        $columnSizes = $this->readColumnSizes();
98
99
        self::assertSame(['A' => 8.141, 'B' => 11.569, 'C' => 16.425, 'D' => 9.283], $columnSizes);
100
    }
101
102
    public function testTableWithoutHiddenHeadersAutoSize(): void
103
    {
104
        $table = $this->setTable();
105
        $table->setShowHeaderRow(false);
106
107
        $this->worksheet->calculateColumnWidths();
108
        $columnSizes = $this->readColumnSizes();
109
110
        self::assertSame(['A' => 5.856, 'B' => 9.283, 'C' => 16.425, 'D' => 6.998], $columnSizes);
111
    }
112
113
    public function testTableWithAutoFilterCenterHeaders(): void
114
    {
115
        $this->setTable();
116
        $this->worksheet->getStyle('A1:D1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
117
118
        $this->worksheet->calculateColumnWidths();
119
        $columnSizes = $this->readColumnSizes();
120
121
        self::assertSame(['A' => 10.569, 'B' => 13.997, 'C' => 16.425, 'D' => 11.711], $columnSizes);
122
    }
123
}
124