Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MinuteTest.php
T
MarkBaker a91dd60a98 Refactor unit tests to ensure that assertions are in the actual test, and not in an abstract class; and that setup/teardown are in the test and not an abstract. This means that assertions and setup/teardown are always in the file when reviewing PRs.
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.
2023-03-10 04:15:12 +01:00

113 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\Calculation\Exception as CalculationException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
use PHPUnit\Framework\TestCase;
class MinuteTest extends TestCase
{
/**
* @dataProvider providerMINUTE
*
* @param mixed $expectedResult
*/
public function testDirectCallToMINUTE($expectedResult, ...$args): void
{
$result = TimeParts::MINUTE(...$args);
self::assertSame($expectedResult, $result);
}
/**
* @dataProvider providerMINUTE
*
* @param mixed $expectedResult
*/
public function testMINUTEAsFormula($expectedResult, ...$args): void
{
$arguments = new FormulaArguments(...$args);
$calculation = Calculation::getInstance();
$formula = "=MINUTE({$arguments})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame($expectedResult, $result);
}
/**
* @dataProvider providerMINUTE
*
* @param mixed $expectedResult
*/
public function testMINUTEInWorksheet($expectedResult, ...$args): void
{
$arguments = new FormulaArguments(...$args);
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$argumentCells = $arguments->populateWorksheet($worksheet);
$formula = "=MINUTE({$argumentCells})";
$result = $worksheet->setCellValue('A1', $formula)
->getCell('A1')
->getCalculatedValue();
self::assertSame($expectedResult, $result);
}
public function providerMINUTE(): array
{
return require 'tests/data/Calculation/DateTime/MINUTE.php';
}
/**
* @dataProvider providerUnhappyMINUTE
*/
public function testMINUTEUnhappyPath(string $expectedException, ...$args): void
{
$arguments = new FormulaArguments(...$args);
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$argumentCells = $arguments->populateWorksheet($worksheet);
$formula = "=MINUTE({$argumentCells})";
$this->expectException(CalculationException::class);
$this->expectExceptionMessage($expectedException);
$worksheet->setCellValue('A1', $formula)
->getCell('A1')
->getCalculatedValue();
}
public function providerUnhappyMINUTE(): array
{
return [
['Formula Error: Wrong number of arguments for MINUTE() function'],
];
}
/**
* @dataProvider providerMinuteArray
*/
public function testMinuteArray(array $expectedResult, string $array): void
{
$calculation = Calculation::getInstance();
$formula = "=MINUTE({$array})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerMinuteArray(): array
{
return [
'row vector' => [[[2, 14, 20]], '{"2022-02-09 01:02:03", "2022-02-09 13:14:15", "2022-02-09 19:20:21"}'],
'column vector' => [[[2], [14], [20]], '{"2022-02-09 01:02:03"; "2022-02-09 13:14:15"; "2022-02-09 19:20:21"}'],
'matrix' => [[[2, 14], [20, 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"}'],
];
}
}