-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
WorksheetNamedRangesTest.php
143 lines (109 loc) · 4.15 KB
/
WorksheetNamedRangesTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Settings;
use PHPUnit\Framework\TestCase;
class WorksheetNamedRangesTest extends TestCase
{
protected $spreadsheet;
protected function setUp(): void
{
Settings::setLibXmlLoaderOptions(null); // reset to default options
$reader = new Xlsx();
$this->spreadsheet = $reader->load('tests/data/Worksheet/namedRangeTest.xlsx');
}
public function testCellExists(): void
{
$namedCell = 'GREETING';
$worksheet = $this->spreadsheet->getActiveSheet();
$cellExists = $worksheet->cellExists($namedCell);
self::assertTrue($cellExists);
}
public function testCellNotExists(): void
{
$namedCell = 'GOODBYE';
$worksheet = $this->spreadsheet->getActiveSheet();
$cellExists = $worksheet->cellExists($namedCell);
self::assertFalse($cellExists);
}
public function testCellExistsInvalidScope(): void
{
$namedCell = 'Result';
$worksheet = $this->spreadsheet->getActiveSheet();
$cellExists = $worksheet->cellExists($namedCell);
self::assertFalse($cellExists);
}
public function testCellExistsRange(): void
{
$namedRange = 'Range1';
$this->expectException(Exception::class);
$this->expectExceptionMessage('Cell coordinate can not be a range of cells');
$worksheet = $this->spreadsheet->getActiveSheet();
$worksheet->cellExists($namedRange);
}
public function testGetCell(): void
{
$namedCell = 'GREETING';
$worksheet = $this->spreadsheet->getActiveSheet();
$cell = $worksheet->getCell($namedCell);
self::assertSame('Hello', $cell->getValue());
}
public function testGetCellNotExists(): void
{
$namedCell = 'GOODBYE';
$this->expectException(Exception::class);
$this->expectExceptionMessage("Invalid cell coordinate {$namedCell}");
$worksheet = $this->spreadsheet->getActiveSheet();
$worksheet->getCell($namedCell);
}
public function testGetCellInvalidScope(): void
{
$namedCell = 'Result';
$ucNamedCell = strtoupper($namedCell);
$this->expectException(Exception::class);
$this->expectExceptionMessage("Invalid cell coordinate {$ucNamedCell}");
$worksheet = $this->spreadsheet->getActiveSheet();
$worksheet->getCell($namedCell);
}
public function testGetCellLocalScoped(): void
{
$namedCell = 'Result';
$this->spreadsheet->setActiveSheetIndexByName('Sheet2');
$worksheet = $this->spreadsheet->getActiveSheet();
$cell = $worksheet->getCell($namedCell);
self::assertSame(8, $cell->getCalculatedValue());
}
public function testGetCellNamedFormula(): void
{
$namedCell = 'Result';
$this->spreadsheet->setActiveSheetIndexByName('Sheet2');
$worksheet = $this->spreadsheet->getActiveSheet();
$cell = $worksheet->getCell($namedCell);
self::assertSame(8, $cell->getCalculatedValue());
}
public function testGetCellWithNamedRange(): void
{
$namedCell = 'Range1';
$this->expectException(Exception::class);
$this->expectExceptionMessage('Cell coordinate can not be a range of cells');
$worksheet = $this->spreadsheet->getActiveSheet();
$worksheet->getCell($namedCell);
}
public function testNamedRangeToArray(): void
{
$namedRange = 'Range1';
$worksheet = $this->spreadsheet->getActiveSheet();
$rangeData = $worksheet->namedRangeToArray($namedRange);
self::assertSame([[1, 2, 3]], $rangeData);
}
public function testInvalidNamedRangeToArray(): void
{
$namedRange = 'Range2';
$this->expectException(Exception::class);
$this->expectExceptionMessage("Named Range {$namedRange} does not exist");
$worksheet = $this->spreadsheet->getActiveSheet();
$rangeData = $worksheet->namedRangeToArray($namedRange);
self::assertSame([[1, 2, 3]], $rangeData);
}
}