Files
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

192 lines
7.0 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Settings;
class RightTest extends AllSetupTeardown
{
/**
* @param mixed $str string from which to extract
* @param mixed $cnt number of characters to extract
*/
#[\PHPUnit\Framework\Attributes\DataProvider('providerRIGHT')]
public function testRIGHT(mixed $expectedResult, mixed $str = 'omitted', mixed $cnt = 'omitted'): void
{
$this->mightHaveException($expectedResult);
$sheet = $this->getSheet();
if ($str === 'omitted') {
$sheet->getCell('B1')->setValue('=RIGHT()');
} elseif ($cnt === 'omitted') {
$this->setCell('A1', $str);
$sheet->getCell('B1')->setValue('=RIGHT(A1)');
} else {
$this->setCell('A1', $str);
$this->setCell('A2', $cnt);
$sheet->getCell('B1')->setValue('=RIGHT(A1, A2)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
public static function providerRIGHT(): array
{
return require 'tests/data/Calculation/TextData/RIGHT.php';
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerLocaleRIGHT')]
public function testLowerWithLocaleBoolean(string $expectedResult, string $locale, mixed $value, mixed $characters): void
{
$newLocale = Settings::setLocale($locale);
if ($newLocale === false) {
self::markTestSkipped('Unable to set locale for locale-specific test');
}
$sheet = $this->getSheet();
$this->setCell('A1', $value);
$this->setCell('A2', $characters);
$sheet->getCell('B1')->setValue('=RIGHT(A1, A2)');
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
public static function providerLocaleRIGHT(): array
{
return [
['RAI', 'fr_FR', true, 3],
['AAR', 'nl_NL', true, 3],
['OSI', 'fi', true, 3],
['ИНА', 'bg', true, 3],
['UX', 'fr_FR', false, 2],
['WAAR', 'nl_NL', false, 4],
['ÄTOSI', 'fi', false, 5],
['ЖЬ', 'bg', false, 2],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerCalculationTypeRIGHTTrue')]
public function testCalculationTypeTrue(string $type, string $resultB1, string $resultB2): void
{
Functions::setCompatibilityMode($type);
$sheet = $this->getSheet();
$this->setCell('A1', true);
$this->setCell('A2', 'Hello');
$this->setCell('B1', '=RIGHT(A1, 1)');
$this->setCell('B2', '=RIGHT(A2, A1)');
self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
}
public static function providerCalculationTypeRIGHTTrue(): array
{
return [
'Excel RIGHT(true, 1) AND RIGHT("hello", true)' => [
Functions::COMPATIBILITY_EXCEL,
'E',
'o',
],
'Gnumeric RIGHT(true, 1) AND RIGHT("hello", true)' => [
Functions::COMPATIBILITY_GNUMERIC,
'E',
'o',
],
'OpenOffice RIGHT(true, 1) AND RIGHT("hello", true)' => [
Functions::COMPATIBILITY_OPENOFFICE,
'1',
'#VALUE!',
],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerCalculationTypeRIGHTFalse')]
public function testCalculationTypeFalse(string $type, string $resultB1, string $resultB2): void
{
Functions::setCompatibilityMode($type);
$sheet = $this->getSheet();
$this->setCell('A1', false);
$this->setCell('A2', 'Hello');
$this->setCell('B1', '=RIGHT(A1, 1)');
$this->setCell('B2', '=RIGHT(A2, A1)');
self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
}
public static function providerCalculationTypeRIGHTFalse(): array
{
return [
'Excel RIGHT(false, 1) AND RIGHT("hello", false)' => [
Functions::COMPATIBILITY_EXCEL,
'E',
'',
],
'Gnumeric RIGHT(false, 1) AND RIGHT("hello", false)' => [
Functions::COMPATIBILITY_GNUMERIC,
'E',
'',
],
'OpenOffice RIGHT(false, 1) AND RIGHT("hello", false)' => [
Functions::COMPATIBILITY_OPENOFFICE,
'0',
'#VALUE!',
],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerCalculationTypeRIGHTNull')]
public function testCalculationTypeNull(string $type, string $resultB1, string $resultB2): void
{
Functions::setCompatibilityMode($type);
$sheet = $this->getSheet();
$this->setCell('A2', 'Hello');
$this->setCell('B1', '=RIGHT(A1, 1)');
$this->setCell('B2', '=RIGHT(A2, A1)');
self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
}
public static function providerCalculationTypeRIGHTNull(): array
{
return [
'Excel RIGHT(null, 1) AND RIGHT("hello", null)' => [
Functions::COMPATIBILITY_EXCEL,
'',
'',
],
'Gnumeric RIGHT(null, 1) AND RIGHT("hello", null)' => [
Functions::COMPATIBILITY_GNUMERIC,
'',
'o',
],
'OpenOffice RIGHT(null, 1) AND RIGHT("hello", null)' => [
Functions::COMPATIBILITY_OPENOFFICE,
'',
'',
],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerRightArray')]
public function testRightArray(array $expectedResult, string $argument1, string $argument2): void
{
$calculation = Calculation::getInstance();
$formula = "=RIGHT({$argument1}, {$argument2})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public static function providerRightArray(): array
{
return [
'row vector #1' => [[['llo', 'rld', 'eet']], '{"Hello", "World", "PhpSpreadsheet"}', '3'],
'column vector #1' => [[['llo'], ['rld'], ['eet']], '{"Hello"; "World"; "PhpSpreadsheet"}', '3'],
'matrix #1' => [[['llo', 'rld'], ['eet', 'cel']], '{"Hello", "World"; "PhpSpreadsheet", "Excel"}', '3'],
'column vector #2' => [[['eet'], ['sheet']], '"PhpSpreadsheet"', '{3; 5}'],
];
}
}