mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-30 03:59:11 +00:00
a91dd60a98
Also enforce more rigorous Excel Function implementation by testing the underlying implementation, call via the Calc Engine, and execution from in a worksheet. Separate out unhappy path (exception) checks into a separate test, so that a single test isn't made overcomplex checking for every potentiality. Scrutinizer may dislike variadics, for variable number of arguments of mixed type; but tough. It's 100% valid PHP, accepted by phpstan, and makes life a lot easier. Initial work here covers all the database and datetime unit tests for Excel function implementations.
112 lines
3.4 KiB
PHP
112 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\DateParts;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class YearTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider providerYEAR
|
|
*
|
|
* @param mixed $expectedResultExcel
|
|
*/
|
|
public function testDirectCallToYEAR($expectedResultExcel, ...$args): void
|
|
{
|
|
$result = DateParts::year(...$args);
|
|
self::assertSame($expectedResultExcel, $result);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerYEAR
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testYEARAsFormula($expectedResult, ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$calculation = Calculation::getInstance();
|
|
$formula = "=YEAR({$arguments})";
|
|
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerYEAR
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testYEARInWorksheet($expectedResult, ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$argumentCells = $arguments->populateWorksheet($worksheet);
|
|
$formula = "=YEAR({$argumentCells})";
|
|
|
|
$result = $worksheet->setCellValue('A1', $formula)
|
|
->getCell('A1')
|
|
->getCalculatedValue();
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
public function providerYEAR(): array
|
|
{
|
|
return require 'tests/data/Calculation/DateTime/YEAR.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerUnhappyYEAR
|
|
*/
|
|
public function testYEARUnhappyPath(string $expectedException, ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$argumentCells = $arguments->populateWorksheet($worksheet);
|
|
$formula = "=YEAR({$argumentCells})";
|
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Calculation\Exception::class);
|
|
$this->expectExceptionMessage($expectedException);
|
|
$worksheet->setCellValue('A1', $formula)
|
|
->getCell('A1')
|
|
->getCalculatedValue();
|
|
}
|
|
|
|
public function providerUnhappyYEAR(): array
|
|
{
|
|
return [
|
|
['Formula Error: Wrong number of arguments for YEAR() function'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerYearArray
|
|
*/
|
|
public function testYearArray(array $expectedResult, string $array): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=YEAR({$array})";
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
|
|
}
|
|
|
|
public function providerYearArray(): array
|
|
{
|
|
return [
|
|
'row vector' => [[[2021, 2022, 2023]], '{"2021-01-01", "2022-01-01", "2023-01-01"}'],
|
|
'column vector' => [[[2021], [2022], [2023]], '{"2021-01-01"; "2022-01-01"; "2023-01-01"}'],
|
|
'matrix' => [[[2021, 2022], [2023, 1999]], '{"2021-01-01", "2022-01-01"; "2023-01-01", "1999-12-31"}'],
|
|
];
|
|
}
|
|
}
|