Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#1866) * Apply stricter scoping rules to named range/cell access via Worksheet object * Additional unit tests
- Loading branch information
Mark Baker
committed
Feb 19, 2021
1 parent
b0eb272
commit 1318b90
Showing
3 changed files
with
190 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
tests/PhpSpreadsheetTests/Worksheet/WorksheetNamedRangesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} |
Binary file not shown.