Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/DefinedNameFormulaTest.php
T
oleibman d647fe7ee7 Use Php Attributes Rather than Annotations for PhpUnit
With PhpUnit 10 came the ability to use Php attributes rather than doc-block annotations for things like "data provider". PhpUnit 11 deprecates the use of annotations, and PhpUnit 12 will not not permit their use. Since PhpUnit 11 requires Php8.2+, we cannot adopt it as long as we support Php8.1, which will continue to be the case for some time. However, there is no penalty for early adoption.

Php-cs-fixer can use:
```
'php_unit_attributes' => ['keep_annotations' => false],
```
This allows us to run `composer fix` to automate all the needed changes. No manual changes were needed for any of the test members.

With this change, PhpUnit 9 can no longer be used with the test suite. File composer.json is updated to reflect that reality, and phpunit9.xml.dist, which has been supplied in case anyone needed to use PhpUnit 9, is no longer required, and is thus deleted. For now, PhpUnit 11 is not being added as a possibility.

No source code is changed in this PR.
2024-11-23 20:56:26 -08:00

193 lines
8.3 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests;
use PhpOffice\PhpSpreadsheet\DefinedName;
use PhpOffice\PhpSpreadsheet\NamedFormula;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class DefinedNameFormulaTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('providerRangeOrFormula')]
public function testRangeOrFormula(string $value, bool $expectedResult): void
{
$actualResult = DefinedName::testIfFormula($value);
self::assertSame($expectedResult, $actualResult);
}
public function testAddDefinedNames(): void
{
$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();
$definedNamesForTest = $this->providerRangeOrFormula();
foreach ($definedNamesForTest as $key => $definedNameData) {
[$value] = $definedNameData;
$name = str_replace([' ', '-'], '_', $key);
$spreadSheet->addDefinedName(DefinedName::createInstance($name, $workSheet, $value));
}
$allDefinedNames = $spreadSheet->getDefinedNames();
self::assertCount(count($definedNamesForTest), $allDefinedNames);
}
public function testGetNamedRanges(): void
{
$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();
$rangeOrFormula = [];
$definedNamesForTest = $this->providerRangeOrFormula();
foreach ($definedNamesForTest as $key => $definedNameData) {
[$value, $isFormula] = $definedNameData;
$rangeOrFormula[] = !$isFormula;
$name = str_replace([' ', '-'], '_', $key);
$spreadSheet->addDefinedName(DefinedName::createInstance($name, $workSheet, $value));
}
$allNamedRanges = $spreadSheet->getNamedRanges();
self::assertCount(count(array_filter($rangeOrFormula)), $allNamedRanges);
}
public function testGetScopedNamedRange(): void
{
$rangeName = 'NAMED_RANGE';
$globalRangeValue = 'A1';
$localRangeValue = 'A2';
$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();
$spreadSheet->addDefinedName(DefinedName::createInstance($rangeName, $workSheet, $globalRangeValue));
$spreadSheet->addDefinedName(DefinedName::createInstance($rangeName, $workSheet, $localRangeValue, true));
$localScopedRange = $spreadSheet->getNamedRange($rangeName, $workSheet);
self::assertNotNull($localScopedRange);
self::assertSame($localRangeValue, $localScopedRange->getValue());
}
public function testGetGlobalNamedRange(): void
{
$rangeName = 'NAMED_RANGE';
$globalRangeValue = 'A1';
$localRangeValue = 'A2';
$spreadSheet = new Spreadsheet();
$workSheet1 = $spreadSheet->getActiveSheet();
$spreadSheet->createSheet(1);
$workSheet2 = $spreadSheet->getSheet(1);
$spreadSheet->addDefinedName(DefinedName::createInstance($rangeName, $workSheet1, $globalRangeValue));
$spreadSheet->addDefinedName(DefinedName::createInstance($rangeName, $workSheet1, $localRangeValue, true));
$localScopedRange = $spreadSheet->getNamedRange($rangeName, $workSheet2);
self::assertNotNull($localScopedRange);
self::assertSame($globalRangeValue, $localScopedRange->getValue());
}
public function testGetNamedFormulae(): void
{
$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();
$rangeOrFormula = [];
$definedNamesForTest = $this->providerRangeOrFormula();
foreach ($definedNamesForTest as $key => $definedNameData) {
[$value, $isFormula] = $definedNameData;
$rangeOrFormula[] = $isFormula;
$name = str_replace([' ', '-'], '_', $key);
$spreadSheet->addDefinedName(DefinedName::createInstance($name, $workSheet, $value));
}
$allNamedFormulae = $spreadSheet->getNamedFormulae();
self::assertCount(count(array_filter($rangeOrFormula)), $allNamedFormulae);
}
public function testGetScopedNamedFormula(): void
{
$formulaName = 'GERMAN_VAT_RATE';
$globalFormulaValue = '=19.0%';
$localFormulaValue = '=16.0%';
$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();
$spreadSheet->addDefinedName(DefinedName::createInstance($formulaName, $workSheet, $globalFormulaValue));
$spreadSheet->addDefinedName(DefinedName::createInstance($formulaName, $workSheet, $localFormulaValue, true));
$localScopedFormula = $spreadSheet->getNamedFormula($formulaName, $workSheet);
self::assertNotNull($localScopedFormula);
self::assertSame($localFormulaValue, $localScopedFormula->getValue());
}
public function testGetGlobalNamedFormula(): void
{
$formulaName = 'GERMAN_VAT_RATE';
$globalFormulaValue = '=19.0%';
$localFormulaValue = '=16.0%';
$spreadSheet = new Spreadsheet();
$workSheet1 = $spreadSheet->getActiveSheet();
$spreadSheet->createSheet(1);
$workSheet2 = $spreadSheet->getSheet(1);
$spreadSheet->addDefinedName(DefinedName::createInstance($formulaName, $workSheet1, $globalFormulaValue));
$spreadSheet->addDefinedName(DefinedName::createInstance($formulaName, $workSheet1, $localFormulaValue, true));
$localScopedFormula = $spreadSheet->getNamedFormula($formulaName, $workSheet2);
self::assertNotNull($localScopedFormula);
self::assertSame($globalFormulaValue, $localScopedFormula->getValue());
}
public static function providerRangeOrFormula(): array
{
return [
'simple range' => ['A1', false],
'simple absolute range' => ['$A$1', false],
'simple integer value' => ['42', true],
'simple float value' => ['12.5', true],
'simple string value' => ['"HELLO WORLD"', true],
'range with a worksheet name' => ['Sheet2!$A$1', false],
'range with a quoted worksheet name' => ["'Work Sheet #2'!\$A\$1:\$E\$1", false],
'range with a quoted worksheet name containing quotes' => ["'Mark''s WorkSheet'!\$A\$1:\$E\$1", false],
'range with a utf-8 worksheet name' => ['Γειά!$A$1', false],
'range with a quoted utf-8 worksheet name' => ["'Γειά σου Κόσμε'!\$A\$1", false],
'range with a quoted worksheet name with quotes in a formula' => ["'Mark''s WorkSheet'!\$A\$1+5", true],
'range with a quoted worksheet name in a formula' => ["5*'Work Sheet #2'!\$A\$1", true],
'multiple ranges with quoted worksheet names with quotes in a formula' => ["'Mark''s WorkSheet'!\$A\$1+'Mark''s WorkSheet'!\$B\$2", true],
'named range in a formula' => ['NAMED_RANGE_VALUE+12', true],
'named range and range' => ['NAMED_RANGE_VALUE_1,Sheet2!$A$1', false],
'range with quoted utf-8 worksheet name and a named range' => ["NAMED_RANGE_VALUE_1,'Γειά σου Κόσμε'!\$A\$1", false],
'composite named range' => ['NAMED_RANGE_VALUE_1,NAMED_RANGE_VALUE_2 NAMED_RANGE_VALUE_3', false],
'named ranges in a formula' => ['NAMED_RANGE_VALUE_1/NAMED_RANGE_VALUE_2', true],
'utf-8 named range' => ['Γειά', false],
'utf-8 named range in a formula' => ['2*Γειά', true],
'utf-8 named ranges' => ['Γειά,σου Κόσμε', false],
'utf-8 named ranges in a formula' => ['Здравствуй+мир', true],
];
}
public function testEmptyNamedFormula(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$spreadSheet = new Spreadsheet();
$workSheet1 = $spreadSheet->getActiveSheet();
new NamedFormula('namedformula', $workSheet1);
}
public function testChangeFormula(): void
{
$spreadSheet = new Spreadsheet();
$workSheet1 = $spreadSheet->getActiveSheet();
$namedFormula = new NamedFormula('namedformula', $workSheet1, '=1');
self::assertEquals('=1', $namedFormula->getFormula());
$namedFormula->setFormula('=2');
self::assertEquals('=2', $namedFormula->getFormula());
$namedFormula->setFormula('');
self::assertEquals('=2', $namedFormula->getFormula());
}
}