Passed
Pull Request — master (#4419)
by Owen
13:44
created

SpreadsheetCopyCloneTest::testCopyClone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 56
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 51
c 3
b 0
f 0
dl 0
loc 56
rs 9.069
cc 2
nc 2
nop 1

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;
6
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PHPUnit\Framework\Attributes\DataProvider;
9
use PHPUnit\Framework\TestCase;
10
11
class SpreadsheetCopyCloneTest 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
    #[DataProvider('providerCopyClone')]
30
    public function testCopyClone(string $type): void
31
    {
32
        $this->spreadsheet = new Spreadsheet();
33
        $sheet = $this->spreadsheet->getActiveSheet();
34
        $sheet->setTitle('original');
35
        $sheet->getStyle('A1')->getFont()->setName('font1');
36
        $sheet->getStyle('A2')->getFont()->setName('font2');
37
        $sheet->getStyle('A3')->getFont()->setName('font3');
38
        $sheet->getStyle('B1')->getFont()->setName('font1');
39
        $sheet->getStyle('B2')->getFont()->setName('font2');
40
        $sheet->getCell('A1')->setValue('this is a1');
41
        $sheet->getCell('A2')->setValue('this is a2');
42
        $sheet->getCell('A3')->setValue('this is a3');
43
        $sheet->getCell('B1')->setValue('this is b1');
44
        $sheet->getCell('B2')->setValue('this is b2');
45
        self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
46
        $sheet->setSelectedCells('A3');
47
        if ($type === 'copy') {
48
            $this->spreadsheet2 = $this->spreadsheet->copy();
49
        } else {
50
            $this->spreadsheet2 = clone $this->spreadsheet;
51
        }
52
        self::assertSame($this->spreadsheet, $this->spreadsheet->getCalculationEngine()?->getSpreadsheet());
53
        self::assertSame($this->spreadsheet2, $this->spreadsheet2->getCalculationEngine()?->getSpreadsheet());
54
        self::assertSame('A3', $sheet->getSelectedCells());
55
        $copysheet = $this->spreadsheet2->getActiveSheet();
56
        self::assertSame('A3', $copysheet->getSelectedCells());
57
        self::assertSame('original', $copysheet->getTitle());
58
        $copysheet->setTitle('unoriginal');
59
        self::assertSame('original', $sheet->getTitle());
60
        self::assertSame('unoriginal', $copysheet->getTitle());
61
        $copysheet->getStyle('A2')->getFont()->setName('font12');
62
        $copysheet->getCell('A2')->setValue('this was a2');
63
64
        self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
65
        self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
66
        self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
67
        self::assertSame('font1', $sheet->getStyle('B1')->getFont()->getName());
68
        self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
69
        self::assertSame('this is a1', $sheet->getCell('A1')->getValue());
70
        self::assertSame('this is a2', $sheet->getCell('A2')->getValue());
71
        self::assertSame('this is a3', $sheet->getCell('A3')->getValue());
72
        self::assertSame('this is b1', $sheet->getCell('B1')->getValue());
73
        self::assertSame('this is b2', $sheet->getCell('B2')->getValue());
74
75
        self::assertSame('font1', $copysheet->getStyle('A1')->getFont()->getName());
76
        self::assertSame('font12', $copysheet->getStyle('A2')->getFont()->getName());
77
        self::assertSame('font3', $copysheet->getStyle('A3')->getFont()->getName());
78
        self::assertSame('font1', $copysheet->getStyle('B1')->getFont()->getName());
79
        self::assertSame('font2', $copysheet->getStyle('B2')->getFont()->getName());
80
        self::assertSame('this is a1', $copysheet->getCell('A1')->getValue());
81
        self::assertSame('this was a2', $copysheet->getCell('A2')->getValue());
82
        self::assertSame('this is a3', $copysheet->getCell('A3')->getValue());
83
        self::assertSame('this is b1', $copysheet->getCell('B1')->getValue());
84
        self::assertSame('this is b2', $copysheet->getCell('B2')->getValue());
85
    }
86
87
    #[DataProvider('providerCopyClone')]
88
    public function testCopyCloneActiveSheet(string $type): void
89
    {
90
        $this->spreadsheet = new Spreadsheet();
91
        $sheet0 = $this->spreadsheet->getActiveSheet();
92
        $sheet1 = $this->spreadsheet->createSheet();
93
        $sheet2 = $this->spreadsheet->createSheet();
94
        $sheet3 = $this->spreadsheet->createSheet();
95
        $sheet4 = $this->spreadsheet->createSheet();
96
        $sheet0->getStyle('B1')->getFont()->setName('whatever');
97
        $sheet1->getStyle('B1')->getFont()->setName('whatever');
98
        $sheet2->getStyle('B1')->getFont()->setName('whatever');
99
        $sheet3->getStyle('B1')->getFont()->setName('whatever');
100
        $sheet4->getStyle('B1')->getFont()->setName('whatever');
101
        $this->spreadsheet->setActiveSheetIndex(2);
102
        if ($type === 'copy') {
103
            $this->spreadsheet2 = $this->spreadsheet->copy();
104
        } else {
105
            $this->spreadsheet2 = clone $this->spreadsheet;
106
        }
107
        self::assertSame(2, $this->spreadsheet2->getActiveSheetIndex());
108
    }
109
110
    public static function providerCopyClone(): array
111
    {
112
        return [
113
            ['copy'],
114
            ['clone'],
115
        ];
116
    }
117
118
    public static function providerCopyClone2(): array
119
    {
120
        return [
121
            ['copy', true, false, true, 'array'],
122
            ['clone', true, false, true, 'array'],
123
            ['copy', false, true, false, 'value'],
124
            ['clone', false, true, false, 'value'],
125
            ['copy', false, true, true, 'error'],
126
            ['clone', false, true, true, 'error'],
127
        ];
128
    }
129
130
    #[DataProvider('providerCopyClone2')]
131
    public function testCopyClone2(string $type, bool $suppress, bool $cache, bool $pruning, string $return): void
132
    {
133
        $this->spreadsheet = new Spreadsheet();
134
        $calc = $this->spreadsheet->getCalculationEngine();
135
        self::assertNotNull($calc);
136
        $calc->setSuppressFormulaErrors($suppress);
137
        $calc->setCalculationCacheEnabled($cache);
138
        $calc->setBranchPruningEnabled($pruning);
139
        $calc->setInstanceArrayReturnType($return);
140
        if ($type === 'copy') {
141
            $this->spreadsheet2 = $this->spreadsheet->copy();
142
        } else {
143
            $this->spreadsheet2 = clone $this->spreadsheet;
144
        }
145
        $calc2 = $this->spreadsheet2->getCalculationEngine();
146
        self::assertNotNull($calc2);
147
        self::assertSame($suppress, $calc2->getSuppressFormulaErrors());
148
        self::assertSame($cache, $calc2->getCalculationCacheEnabled());
149
        self::assertSame($pruning, $calc2->getBranchPruningEnabled());
150
        self::assertSame($return, $calc2->getInstanceArrayReturnType());
151
    }
152
}
153