Files
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

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->_calculateFormulaValue($formula);
$endSecond = (new DateTimeImmutable('now'))->format('s');
} while ($startSecond !== $endSecond);
$this->assertions($dtStart, $result);
}
}