mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-25 02:48:16 +00:00
9c2deb125f
Stop a few of scrutinizers complaints about 100% valid use of PHP variadics. Hopefully, this issue will cease to be an issue when we can specify mixed datatype for variadic arguments in the tests
112 lines
3.5 KiB
PHP
112 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\TimeParts;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class SecondTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider providerSECOND
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testDirectCallToSECOND($expectedResult, ...$args): void
|
|
{
|
|
$result = TimeParts::second(/** @scrutinizer ignore-type */ ...$args);
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerSECOND
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testSECONDAsFormula($expectedResult, ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$calculation = Calculation::getInstance();
|
|
$formula = "=SECOND({$arguments})";
|
|
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerSECOND
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testSECONDInWorksheet($expectedResult, ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$argumentCells = $arguments->populateWorksheet($worksheet);
|
|
$formula = "=SECOND({$argumentCells})";
|
|
|
|
$result = $worksheet->setCellValue('A1', $formula)
|
|
->getCell('A1')
|
|
->getCalculatedValue();
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
public function providerSECOND(): array
|
|
{
|
|
return require 'tests/data/Calculation/DateTime/SECOND.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerUnhappySECOND
|
|
*/
|
|
public function testSECONDUnhappyPath(string $expectedException, ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$argumentCells = $arguments->populateWorksheet($worksheet);
|
|
$formula = "=SECOND({$argumentCells})";
|
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Calculation\Exception::class);
|
|
$this->expectExceptionMessage($expectedException);
|
|
$worksheet->setCellValue('A1', $formula)
|
|
->getCell('A1')
|
|
->getCalculatedValue();
|
|
}
|
|
|
|
public function providerUnhappySECOND(): array
|
|
{
|
|
return [
|
|
['Formula Error: Wrong number of arguments for SECOND() function'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerSecondArray
|
|
*/
|
|
public function testSecondArray(array $expectedResult, string $array): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=SECOND({$array})";
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
|
|
}
|
|
|
|
public function providerSecondArray(): array
|
|
{
|
|
return [
|
|
'row vector' => [[[3, 15, 21]], '{"2022-02-09 01:02:03", "2022-02-09 13:14:15", "2022-02-09 19:20:21"}'],
|
|
'column vector' => [[[3], [15], [21]], '{"2022-02-09 01:02:03"; "2022-02-09 13:14:15"; "2022-02-09 19:20:21"}'],
|
|
'matrix' => [[[3, 15], [21, 59]], '{"2022-02-09 01:02:03", "2022-02-09 13:14:15"; "2022-02-09 19:20:21", "1999-12-31 23:59:59"}'],
|
|
];
|
|
}
|
|
}
|