Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/SecondTest.php
T
Adrien Crivelli ec4098c8fd Strict mode for all tests
While we might never be able to have 100% of our code strict, we can at
the very least do it for all of our tests. This ensures that our tests
are using our API with the types as intended by the test author, and not
silently be cast to what our API requires.
2023-09-07 17:44:56 +08:00

113 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
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
*/
public function testDirectCallToSECOND(mixed $expectedResult, mixed ...$args): void
{
/** @scrutinizer ignore-call */
$result = TimeParts::second(...$args);
self::assertSame($expectedResult, $result);
}
/**
* @dataProvider providerSECOND
*/
public function testSECONDAsFormula(mixed $expectedResult, mixed ...$args): void
{
$arguments = new FormulaArguments(...$args);
$calculation = Calculation::getInstance();
$formula = "=SECOND({$arguments})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame($expectedResult, $result);
}
/**
* @dataProvider providerSECOND
*/
public function testSECONDInWorksheet(mixed $expectedResult, mixed ...$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);
$spreadsheet->disconnectWorksheets();
}
public static function providerSECOND(): array
{
return require 'tests/data/Calculation/DateTime/SECOND.php';
}
/**
* @dataProvider providerUnhappySECOND
*/
public function testSECONDUnhappyPath(string $expectedException, mixed ...$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();
$spreadsheet->disconnectWorksheets();
}
public static 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 static 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"}'],
];
}
}