Passed
Pull Request — master (#4370)
by Owen
12:31
created

SpreadsheetCoverageTest::testCopy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 40
rs 9.344
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;
6
7
use PhpOffice\PhpSpreadsheet\Exception as SSException;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PHPUnit\Framework\TestCase;
10
11
class SpreadsheetCoverageTest extends TestCase
12
{
13
    private ?Spreadsheet $spreadsheet = null;
14
15
    private ?Spreadsheet $spreadsheet2 = null;
16
17
    protected function tearDown(): void
18
    {
19
        if ($this->spreadsheet !== null) {
20
            $this->spreadsheet->disconnectWorksheets();
21
            $this->spreadsheet = null;
22
        }
23
        if ($this->spreadsheet2 !== null) {
24
            $this->spreadsheet2->disconnectWorksheets();
25
            $this->spreadsheet2 = null;
26
        }
27
    }
28
29
    public function testDocumentProperties(): void
30
    {
31
        $this->spreadsheet = new Spreadsheet();
32
        $properties = $this->spreadsheet->getProperties();
33
        $properties->setCreator('Anyone');
34
        $properties->setTitle('Description');
35
        $this->spreadsheet2 = new Spreadsheet();
36
        self::assertNotEquals($properties, $this->spreadsheet2->getProperties());
37
        $properties2 = clone $properties;
38
        $this->spreadsheet2->setProperties($properties2);
39
        self::assertEquals($properties, $this->spreadsheet2->getProperties());
40
    }
41
42
    public function testDocumentSecurity(): void
43
    {
44
        $this->spreadsheet = new Spreadsheet();
45
        $security = $this->spreadsheet->getSecurity();
46
        $security->setLockRevision(true);
47
        $revisionsPassword = 'revpasswd';
48
        $security->setRevisionsPassword($revisionsPassword);
49
        $this->spreadsheet2 = new Spreadsheet();
50
        self::assertNotEquals($security, $this->spreadsheet2->getSecurity());
51
        $security2 = clone $security;
52
        $this->spreadsheet2->setSecurity($security2);
53
        self::assertEquals($security, $this->spreadsheet2->getSecurity());
54
    }
55
56
    public function testCellXfCollection(): void
57
    {
58
        $this->spreadsheet = new Spreadsheet();
59
        $sheet = $this->spreadsheet->getActiveSheet();
60
        $sheet->getStyle('A1')->getFont()->setName('font1');
61
        $sheet->getStyle('A2')->getFont()->setName('font2');
62
        $sheet->getStyle('A3')->getFont()->setName('font3');
63
        $sheet->getStyle('B1')->getFont()->setName('font1');
64
        $sheet->getStyle('B2')->getFont()->setName('font2');
65
        $collection = $this->spreadsheet->getCellXfCollection();
66
        self::assertCount(4, $collection);
67
        $font1Style = $collection[1];
68
        self::assertTrue($this->spreadsheet->cellXfExists($font1Style));
69
        self::assertSame('font1', $this->spreadsheet->getCellXfCollection()[1]->getFont()->getName());
70
        self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
71
        self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
72
        self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
73
        self::assertSame('font1', $sheet->getStyle('B1')->getFont()->getName());
74
        self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
75
76
        $this->spreadsheet->removeCellXfByIndex(1);
77
        self::assertFalse($this->spreadsheet->cellXfExists($font1Style));
78
        self::assertSame('font2', $this->spreadsheet->getCellXfCollection()[1]->getFont()->getName());
79
        self::assertSame('Calibri', $sheet->getStyle('A1')->getFont()->getName());
80
        self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
81
        self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
82
        self::assertSame('Calibri', $sheet->getStyle('B1')->getFont()->getName());
83
        self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
84
    }
85
86
    public function testInvalidRemoveCellXfByIndex(): void
87
    {
88
        $this->expectException(SSException::class);
89
        $this->expectExceptionMessage('CellXf index is out of bounds.');
90
        $this->spreadsheet = new Spreadsheet();
91
        $sheet = $this->spreadsheet->getActiveSheet();
92
        $sheet->getStyle('A1')->getFont()->setName('font1');
93
        $sheet->getStyle('A2')->getFont()->setName('font2');
94
        $sheet->getStyle('A3')->getFont()->setName('font3');
95
        $sheet->getStyle('B1')->getFont()->setName('font1');
96
        $sheet->getStyle('B2')->getFont()->setName('font2');
97
        $this->spreadsheet->removeCellXfByIndex(5);
98
    }
99
100
    public function testInvalidRemoveDefaultStyle(): void
101
    {
102
        $this->expectException(SSException::class);
103
        $this->expectExceptionMessage('No default style found for this workbook');
104
        // Removing default style probably should be disallowed.
105
        $this->spreadsheet = new Spreadsheet();
106
        $this->spreadsheet->removeCellXfByIndex(0);
107
        $this->spreadsheet->getDefaultStyle();
108
    }
109
110
    public function testCellStyleXF(): void
111
    {
112
        $this->spreadsheet = new Spreadsheet();
113
        $collection = $this->spreadsheet->getCellStyleXfCollection();
114
        self::assertCount(1, $collection);
115
        $styleXf = $collection[0];
116
        self::assertSame($styleXf, $this->spreadsheet->getCellStyleXfByIndex(0));
117
        $hash = $styleXf->getHashCode();
118
        self::assertSame($styleXf, $this->spreadsheet->getCellStyleXfByHashCode($hash));
119
        self::assertFalse($this->spreadsheet->getCellStyleXfByHashCode($hash . 'x'));
120
    }
121
122
    public function testInvalidRemoveCellStyleXfByIndex(): void
123
    {
124
        $this->expectException(SSException::class);
125
        $this->expectExceptionMessage('CellStyleXf index is out of bounds.');
126
        $this->spreadsheet = new Spreadsheet();
127
        $this->spreadsheet->removeCellStyleXfByIndex(5);
128
    }
129
130
    public function testInvalidFirstSheetIndex(): void
131
    {
132
        $this->expectException(SSException::class);
133
        $this->expectExceptionMessage('First sheet index must be a positive integer.');
134
        $this->spreadsheet = new Spreadsheet();
135
        $this->spreadsheet->setFirstSheetIndex(-1);
136
    }
137
138
    public function testInvalidVisibility(): void
139
    {
140
        $this->expectException(SSException::class);
141
        $this->expectExceptionMessage('Invalid visibility value.');
142
        $this->spreadsheet = new Spreadsheet();
143
        $this->spreadsheet->setVisibility(Spreadsheet::VISIBILITY_HIDDEN);
144
        self::assertSame(Spreadsheet::VISIBILITY_HIDDEN, $this->spreadsheet->getVisibility());
145
        $this->spreadsheet->setVisibility(null);
146
        self::assertSame(Spreadsheet::VISIBILITY_VISIBLE, $this->spreadsheet->getVisibility());
147
        $this->spreadsheet->setVisibility('badvalue');
148
    }
149
150
    public function testInvalidTabRatio(): void
151
    {
152
        $this->expectException(SSException::class);
153
        $this->expectExceptionMessage('Tab ratio must be between 0 and 1000.');
154
        $this->spreadsheet = new Spreadsheet();
155
        $this->spreadsheet->setTabRatio(2000);
156
    }
157
}
158