Passed
Pull Request — master (#4370)
by Owen
14:15
created

SpreadsheetCopyCloneTest::testClone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
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\TestCase;
9
10
class SpreadsheetCopyCloneTest extends TestCase
11
{
12
    private ?Spreadsheet $spreadsheet = null;
13
14
    private ?Spreadsheet $spreadsheet2 = null;
15
16
    protected function tearDown(): void
17
    {
18
        if ($this->spreadsheet !== null) {
19
            $this->spreadsheet->disconnectWorksheets();
20
            $this->spreadsheet = null;
21
        }
22
        if ($this->spreadsheet2 !== null) {
23
            $this->spreadsheet2->disconnectWorksheets();
24
            $this->spreadsheet2 = null;
25
        }
26
    }
27
28
    public function runTests(string $type): void
29
    {
30
        $this->spreadsheet = new Spreadsheet();
31
        $sheet = $this->spreadsheet->getActiveSheet();
32
        $sheet->setTitle('original');
33
        $sheet->getStyle('A1')->getFont()->setName('font1');
34
        $sheet->getStyle('A2')->getFont()->setName('font2');
35
        $sheet->getStyle('A3')->getFont()->setName('font3');
36
        $sheet->getStyle('B1')->getFont()->setName('font1');
37
        $sheet->getStyle('B2')->getFont()->setName('font2');
38
        $sheet->getCell('A1')->setValue('this is a1');
39
        $sheet->getCell('A2')->setValue('this is a2');
40
        $sheet->getCell('A3')->setValue('this is a3');
41
        $sheet->getCell('B1')->setValue('this is b1');
42
        $sheet->getCell('B2')->setValue('this is b2');
43
        self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
44
        if ($type === 'copy') {
45
            $this->spreadsheet2 = $this->spreadsheet->copy();
46
        } else {
47
            $this->spreadsheet2 = clone $this->spreadsheet;
48
        }
49
        $copysheet = $this->spreadsheet2->getActiveSheet();
50
        self::assertSame('original', $copysheet->getTitle());
51
        $copysheet->setTitle('unoriginal');
52
        self::assertSame('original', $sheet->getTitle());
53
        self::assertSame('unoriginal', $copysheet->getTitle());
54
        $copysheet->getStyle('A2')->getFont()->setName('font12');
55
        $copysheet->getCell('A2')->setValue('this was a2');
56
57
        self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
58
        self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
59
        self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
60
        self::assertSame('font1', $sheet->getStyle('B1')->getFont()->getName());
61
        self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
62
        self::assertSame('this is a1', $sheet->getCell('A1')->getValue());
63
        self::assertSame('this is a2', $sheet->getCell('A2')->getValue());
64
        self::assertSame('this is a3', $sheet->getCell('A3')->getValue());
65
        self::assertSame('this is b1', $sheet->getCell('B1')->getValue());
66
        self::assertSame('this is b2', $sheet->getCell('B2')->getValue());
67
68
        self::assertSame('font1', $copysheet->getStyle('A1')->getFont()->getName());
69
        self::assertSame('font12', $copysheet->getStyle('A2')->getFont()->getName());
70
        self::assertSame('font3', $copysheet->getStyle('A3')->getFont()->getName());
71
        self::assertSame('font1', $copysheet->getStyle('B1')->getFont()->getName());
72
        self::assertSame('font2', $copysheet->getStyle('B2')->getFont()->getName());
73
        self::assertSame('this is a1', $copysheet->getCell('A1')->getValue());
74
        self::assertSame('this was a2', $copysheet->getCell('A2')->getValue());
75
        self::assertSame('this is a3', $copysheet->getCell('A3')->getValue());
76
        self::assertSame('this is b1', $copysheet->getCell('B1')->getValue());
77
        self::assertSame('this is b2', $copysheet->getCell('B2')->getValue());
78
    }
79
80
    public function testClone(): void
81
    {
82
        $this->runTests('clone');
83
    }
84
85
    public function testCopy(): void
86
    {
87
        $this->runTests('copy');
88
    }
89
}
90