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

SpreadsheetCopyCloneTest::tearDown()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 4
nop 0
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('A3', $sheet->getSelectedCells());
53
        $copysheet = $this->spreadsheet2->getActiveSheet();
54
        self::assertSame('A3', $copysheet->getSelectedCells());
55
        self::assertSame('original', $copysheet->getTitle());
56
        $copysheet->setTitle('unoriginal');
57
        self::assertSame('original', $sheet->getTitle());
58
        self::assertSame('unoriginal', $copysheet->getTitle());
59
        $copysheet->getStyle('A2')->getFont()->setName('font12');
60
        $copysheet->getCell('A2')->setValue('this was a2');
61
62
        self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
63
        self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
64
        self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
65
        self::assertSame('font1', $sheet->getStyle('B1')->getFont()->getName());
66
        self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
67
        self::assertSame('this is a1', $sheet->getCell('A1')->getValue());
68
        self::assertSame('this is a2', $sheet->getCell('A2')->getValue());
69
        self::assertSame('this is a3', $sheet->getCell('A3')->getValue());
70
        self::assertSame('this is b1', $sheet->getCell('B1')->getValue());
71
        self::assertSame('this is b2', $sheet->getCell('B2')->getValue());
72
73
        self::assertSame('font1', $copysheet->getStyle('A1')->getFont()->getName());
74
        self::assertSame('font12', $copysheet->getStyle('A2')->getFont()->getName());
75
        self::assertSame('font3', $copysheet->getStyle('A3')->getFont()->getName());
76
        self::assertSame('font1', $copysheet->getStyle('B1')->getFont()->getName());
77
        self::assertSame('font2', $copysheet->getStyle('B2')->getFont()->getName());
78
        self::assertSame('this is a1', $copysheet->getCell('A1')->getValue());
79
        self::assertSame('this was a2', $copysheet->getCell('A2')->getValue());
80
        self::assertSame('this is a3', $copysheet->getCell('A3')->getValue());
81
        self::assertSame('this is b1', $copysheet->getCell('B1')->getValue());
82
        self::assertSame('this is b2', $copysheet->getCell('B2')->getValue());
83
    }
84
85
    #[DataProvider('providerCopyClone')]
86
    public function testCopyCloneActiveSheet(string $type): void
87
    {
88
        $this->spreadsheet = new Spreadsheet();
89
        $sheet0 = $this->spreadsheet->getActiveSheet();
90
        $sheet1 = $this->spreadsheet->createSheet();
91
        $sheet2 = $this->spreadsheet->createSheet();
92
        $sheet3 = $this->spreadsheet->createSheet();
93
        $sheet4 = $this->spreadsheet->createSheet();
94
        $sheet0->getStyle('B1')->getFont()->setName('whatever');
95
        $sheet1->getStyle('B1')->getFont()->setName('whatever');
96
        $sheet2->getStyle('B1')->getFont()->setName('whatever');
97
        $sheet3->getStyle('B1')->getFont()->setName('whatever');
98
        $sheet4->getStyle('B1')->getFont()->setName('whatever');
99
        $this->spreadsheet->setActiveSheetIndex(2);
100
        if ($type === 'copy') {
101
            $this->spreadsheet2 = $this->spreadsheet->copy();
102
        } else {
103
            $this->spreadsheet2 = clone $this->spreadsheet;
104
        }
105
        self::assertSame(2, $this->spreadsheet2->getActiveSheetIndex());
106
    }
107
108
    public static function providerCopyClone(): array
109
    {
110
        return [
111
            ['copy'],
112
            ['clone'],
113
        ];
114
    }
115
}
116