Passed
Push — master ( e65bc8...3bb10f )
by
unknown
17:18 queued 09:44
created

HyperlinkTest::testReadAndWriteHyperlinks()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 84
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
dl 0
loc 84
rs 8.6763
c 1
b 0
f 0
cc 2
nc 2
nop 0

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\Reader\Xlsx;
6
7
use PhpOffice\PhpSpreadsheet\NamedRange;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
10
11
class HyperlinkTest extends AbstractFunctional
12
{
13
    public function testReadAndWriteHyperlinks(): void
14
    {
15
        $spreadsheet = new Spreadsheet();
16
        $sheet1 = $spreadsheet->getActiveSheet();
17
        $sheet1->setTitle('Sheet One');
18
        $sheet1->getCell('A1')->setValue(100);
19
        $sheet1->getCell('B1')->setValue('this is b1');
20
        $spreadsheet->addNamedRange(new NamedRange('namedb1', $sheet1, '$B$1'));
21
        $sheet1->setCellValue('A2', 'link to same sheet');
22
        $sheet1->getCell('A2')->getHyperlink()
23
            ->setUrl("sheet://'Sheet One'!A1");
24
        $sheet1->getStyle('A2')->getFont()->setHyperlinkTheme();
25
        $sheet1->setCellValue('A3', 'link to defined name');
26
        $sheet1->getCell('A3')->getHyperlink()
27
            ->setUrl('sheet://namedb1');
28
        $sheet1->getStyle('A3')->getFont()->setHyperlinkTheme();
29
30
        $sheet2 = $spreadsheet->createSheet();
31
        $sheet2->setTitle('Sheet Two');
32
        $sheet2->setCellValue('A2', 'link to other sheet');
33
        $sheet2->getCell('A2')->getHyperlink()
34
            ->setUrl("sheet://'Sheet One'!A1");
35
        $sheet2->getStyle('A2')->getFont()->setHyperlinkTheme();
36
        $sheet2->setCellValue('A3', 'external link');
37
        $sheet2->getCell('A3')->getHyperlink()
38
            ->setUrl('https://www.example.com');
39
        $sheet2->getStyle('A3')->getFont()->setHyperlinkTheme();
40
        $sheet2->setCellValue('A4', 'external link with anchor');
41
        $sheet2->getCell('A4')->getHyperlink()
42
            ->setUrl('https://www.example.com#anchor');
43
        $sheet2->getCell('A4')->getHyperlink()->setTooltip('go to anchor tag on example.com');
44
        $sheet2->getStyle('A4')->getFont()->setHyperlinkTheme();
45
46
        $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
47
        $spreadsheet->disconnectWorksheets();
48
        $rsheet1 = $reloadedSpreadsheet->getSheet(0);
49
        self::assertSame('link to same sheet', $rsheet1->getCell('A2')->getValue());
50
        self::assertSame("sheet://'Sheet One'!A1", $rsheet1->getCell('A2')->getHyperlink()->getUrl());
51
52
        self::assertSame('link to defined name', $rsheet1->getCell('A3')->getValue());
53
        self::assertSame('sheet://namedb1', $rsheet1->getCell('A3')->getHyperlink()->getUrl());
54
55
        $rsheet2 = $reloadedSpreadsheet->getSheet(1);
56
        self::assertSame('link to other sheet', $rsheet2->getCell('A2')->getValue());
57
        self::assertSame("sheet://'Sheet One'!A1", $rsheet2->getCell('A2')->getHyperlink()->getUrl());
58
         self::assertSame(
59
             'FF0000FF',
60
             $rsheet2
61
                 ->getStyle('A2')
62
                 ->getFont()->getColor()->getARGB(),
63
             'argb is set in addition to theme'
64
         );
65
66
        self::assertSame('external link', $rsheet2->getCell('A3')->getValue());
67
        self::assertSame('https://www.example.com', $rsheet2->getCell('A3')->getHyperlink()->getUrl());
68
69
        self::assertSame('https://www.example.com#anchor', $rsheet2->getCell('A4')->getHyperlink()->getUrl());
70
        self::assertSame('external link with anchor', $rsheet2->getCell('A4')->getValue());
71
        self::assertSame('go to anchor tag on example.com', $rsheet2->getCell('A4')->getHyperlink()->getToolTip());
72
73
        $testCells = [
74
            [0, 'A2'],
75
            [0, 'A3'],
76
            [0, 'A2'],
77
            [1, 'A3'],
78
            [1, 'A4'],
79
        ];
80
        foreach ($testCells as $sheetAndCell) {
81
            [$sheetIndex, $cell] = $sheetAndCell;
82
            $rsheet = $reloadedSpreadsheet->getSheet($sheetIndex);
83
            self::assertSame(
84
                10,
85
                $rsheet->getStyle($cell)
86
                    ->getFont()->getColor()->getTheme(),
87
                "theme sheet $sheetIndex cell $cell"
88
            );
89
            self::assertSame(
90
                'single',
91
                $rsheet->getStyle('A2')->getFont()->getUnderline(),
92
                "underline sheet $sheetIndex cell $cell"
93
            );
94
        }
95
96
        $reloadedSpreadsheet->disconnectWorksheets();
97
    }
98
}
99