The `WORKDAY()` function accepts 2 "static" arguments that could be passed as arrays; but also accepts a set of trailing date arguments that are accepted as an array by the splat operator. Only the first two arguments should be tested for returning array values; but the logic still needs to work with the full argument set.

Provide a separate "subset" method in the `ArrayEnabled` Trait, that allows a subset of arguments to be tested for array returns.

Set up basic tests for `WORKDAY()`
This commit is contained in:
MarkBaker 2022-02-10 12:31:22 +01:00
parent 782644684b
commit ec2ca1764f
3 changed files with 54 additions and 0 deletions

View File

@ -37,6 +37,27 @@ trait ArrayEnabled
self::initialiseHelper($arguments); self::initialiseHelper($arguments);
$arguments = self::$arrayArgumentHelper->arguments(); $arguments = self::$arrayArgumentHelper->arguments();
return self::processArguments($method, ...$arguments);
}
/**
* @param mixed ...$arguments
*/
protected static function evaluateArrayArgumentsSubset(callable $method, int $limit, ...$arguments): array
{
self::initialiseHelper(array_slice($arguments, 0, $limit));
$trailingArguments = array_slice($arguments, $limit);
$arguments = self::$arrayArgumentHelper->arguments();
$arguments = array_merge($arguments, $trailingArguments);
return self::processArguments($method, ...$arguments);
}
/**
* @param mixed ...$arguments
*/
private static function processArguments(callable $method, ...$arguments): array
{
if (self::$arrayArgumentHelper->hasArrayArgument() === false) { if (self::$arrayArgumentHelper->hasArrayArgument() === false) {
return [$method(...$arguments)]; return [$method(...$arguments)];
} }

View File

@ -151,6 +151,7 @@ class WorkDay
--$endDate; --$endDate;
// Adjust the calculated end date if it falls over a weekend // Adjust the calculated end date if it falls over a weekend
$endDow = self::getWeekDay($endDate, 3); $endDow = self::getWeekDay($endDate, 3);
/** int $endDoW */
if ($endDow >= 5) { if ($endDow >= 5) {
$endDate += 4 - $endDow; $endDate += 4 - $endDow;
} }
@ -183,6 +184,7 @@ class WorkDay
} }
// Adjust the calculated end date if it falls over a weekend // Adjust the calculated end date if it falls over a weekend
$endDoW = self::getWeekDay($endDate, 3); $endDoW = self::getWeekDay($endDate, 3);
/** int $endDoW */
if ($endDoW >= 5) { if ($endDoW >= 5) {
$endDate += -$endDoW + 4; $endDate += -$endDoW + 4;
} }

View File

@ -2,6 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class WorkDayTest extends AllSetupTeardown class WorkDayTest extends AllSetupTeardown
{ {
/** /**
@ -49,4 +51,33 @@ class WorkDayTest extends AllSetupTeardown
{ {
return require 'tests/data/Calculation/DateTime/WORKDAY.php'; return require 'tests/data/Calculation/DateTime/WORKDAY.php';
} }
/**
* @dataProvider providerWorkDayArray
*/
public function testWorkDayArray(array $expectedResult, string $startDate, string $endDays, ?string $holidays): void
{
$calculation = Calculation::getInstance();
if ($holidays === null) {
$formula = "=WORKDAY({$startDate}, {$endDays})";
} else {
$formula = "=WORKDAY({$startDate}, {$endDays}, {$holidays})";
}
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerWorkDayArray(): array
{
return [
'row vector #1' => [[[44595, 44596, 44599]], '{"2022-02-01", "2022-02-02", "2022-02-03"}', '2', null],
'column vector #1' => [[[44595], [44596], [44599]], '{"2022-02-01"; "2022-02-02"; "2022-02-03"}', '2', null],
'matrix #1' => [[[44595, 44596], [44599, 44600]], '{"2022-02-01", "2022-02-02"; "2022-02-03", "2022-02-04"}', '2', null],
'row vector #2' => [[[44595, 44596]], '"2022-02-01"', '{2, 3}', null],
'column vector #2' => [[[44595], [44596]], '"2022-02-01"', '{2; 3}', null],
'row vector with Holiday' => [[[44596, 44599]], '"2022-02-01"', '{2, 3}', '{"2022-02-02"}'],
'row vector with Holidays' => [[[44599, 44600]], '"2022-02-01"', '{2, 3}', '{"2022-02-02", "2022-02-03"}'],
];
}
} }