Passed
Pull Request — master (#4315)
by Owen
12:23
created

SpreadsheetDuplicateSheetTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 2
nc 2
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 SpreadsheetDuplicateSheetTest extends TestCase
11
{
12
    private ?Spreadsheet $spreadsheet = null;
13
14
    protected function tearDown(): void
15
    {
16
        if ($this->spreadsheet !== null) {
17
            $this->spreadsheet->disconnectWorksheets();
18
            $this->spreadsheet = null;
19
        }
20
    }
21
22
    public function testDuplicate(): void
23
    {
24
        $this->spreadsheet = new Spreadsheet();
25
        $sheet = $this->spreadsheet->getActiveSheet();
26
        $sheet->setTitle('original');
27
        $sheet->getCell('A1')->setValue('text1');
28
        $sheet->getCell('A2')->setValue('text2');
29
        $sheet->getStyle('A1')
30
            ->getFont()
31
            ->setBold(true);
32
        $sheet3 = $this->spreadsheet->createSheet();
33
        $sheet3->setTitle('added');
34
        $newSheet = $this->spreadsheet
35
            ->duplicateWorksheetByTitle('original');
36
        $this->spreadsheet->duplicateWorksheetByTitle('added');
37
        self::assertSame('original 1', $newSheet->getTitle());
38
        self::assertSame(
39
            'text1',
40
            $newSheet->getCell('A1')->getValue()
41
        );
42
        self::assertSame(
43
            'text2',
44
            $newSheet->getCell('A2')->getValue()
45
        );
46
        self::assertTrue(
47
            $newSheet->getStyle('A1')->getFont()->getBold()
48
        );
49
        self::assertFalse(
50
            $newSheet->getStyle('A2')->getFont()->getBold()
51
        );
52
        $sheetNames = [];
53
        foreach ($this->spreadsheet->getWorksheetIterator() as $worksheet) {
54
            $sheetNames[] = $worksheet->getTitle();
55
        }
56
        $expected = ['original', 'original 1', 'added', 'added 1'];
57
        self::assertSame($expected, $sheetNames);
58
    }
59
}
60