mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-31 12:40:00 +00:00
70b4ecd4d3
They aren't quite interchangeable. Both are used in the test suite, with no indication of why one or the other. I think we'd be best off being consistent. Based on the names, I think `_calculateFormulaValue` was intended as a private, or at least internal, method, so favor `calculateFormula`. I do not intend to rename or re-categorize `_calculateFormulaValue`, just remove its usage when it isn't clearly warranted.
57 lines
2.0 KiB
PHP
57 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
|
|
|
|
use DateTimeImmutable;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Current;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\DateParts;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\TimeParts;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class TodayTest extends TestCase
|
|
{
|
|
private function assertions(DateTimeImmutable $dtStart, mixed $result): void
|
|
{
|
|
self::assertEquals($dtStart->format('Y'), DateParts::year($result));
|
|
self::assertEquals($dtStart->format('m'), DateParts::month($result));
|
|
self::assertEquals($dtStart->format('d'), DateParts::day($result));
|
|
self::assertEquals(0, TimeParts::hour($result));
|
|
self::assertEquals(0, TimeParts::minute($result));
|
|
self::assertEquals(0, TimeParts::second($result));
|
|
}
|
|
|
|
public function testDirectCallToToday(): void
|
|
{
|
|
// Loop to avoid rare edge case where first calculation
|
|
// and second do not take place in same second.
|
|
do {
|
|
$dtStart = new DateTimeImmutable();
|
|
$startSecond = $dtStart->format('s');
|
|
$result = Current::today();
|
|
$endSecond = (new DateTimeImmutable('now'))->format('s');
|
|
} while ($startSecond !== $endSecond);
|
|
|
|
$this->assertions($dtStart, $result);
|
|
}
|
|
|
|
public function testTodayAsFormula(): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
$formula = '=TODAY()';
|
|
|
|
// Loop to avoid rare edge case where first calculation
|
|
// and second do not take place in same second.
|
|
do {
|
|
$dtStart = new DateTimeImmutable();
|
|
$startSecond = $dtStart->format('s');
|
|
$result = $calculation->calculateFormula($formula);
|
|
$endSecond = (new DateTimeImmutable('now'))->format('s');
|
|
} while ($startSecond !== $endSecond);
|
|
|
|
$this->assertions($dtStart, $result);
|
|
}
|
|
}
|