mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-22 19:33:44 +00:00
cb8ee73355
My bulletproofing of these tests was not yet sufficient. Although I have never had a failure in probably thousands of tests, one user submitted a PR which did fail testing NOW, fortunately not in a test that is required to pass. The problem is that it is not sufficient merely to set the cell value inside a do-while loop; it is necessary to calculate it in order to cache its result so that results based on that cell will be internally consistent. No source code is changed for this PR, just some tests.
39 lines
1.6 KiB
PHP
39 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
|
|
|
|
use DateTimeImmutable;
|
|
|
|
class TodayTest extends AllSetupTeardown
|
|
{
|
|
public function testToday(): void
|
|
{
|
|
$sheet = $this->getSheet();
|
|
$row = 0;
|
|
// Loop to avoid rare edge case where first calculation
|
|
// and second do not take place in same second.
|
|
do {
|
|
++$row;
|
|
$dtStart = new DateTimeImmutable();
|
|
$startSecond = $dtStart->format('s');
|
|
$sheet->setCellValue("A$row", '=TODAY()');
|
|
// cache result for later assertions
|
|
$sheet->getCell("A$row")->getCalculatedValue();
|
|
$dtEnd = new DateTimeImmutable();
|
|
$endSecond = $dtEnd->format('s');
|
|
} while ($startSecond !== $endSecond);
|
|
$sheet->setCellValue("B$row", "=YEAR(A$row)");
|
|
$sheet->setCellValue("C$row", "=MONTH(A$row)");
|
|
$sheet->setCellValue("D$row", "=DAY(A$row)");
|
|
$sheet->setCellValue("E$row", "=HOUR(A$row)");
|
|
$sheet->setCellValue("F$row", "=MINUTE(A$row)");
|
|
$sheet->setCellValue("G$row", "=SECOND(A$row)");
|
|
self::assertSame((int) $dtStart->format('Y'), $sheet->getCell("B$row")->getCalculatedValue());
|
|
self::assertSame((int) $dtStart->format('m'), $sheet->getCell("C$row")->getCalculatedValue());
|
|
self::assertSame((int) $dtStart->format('d'), $sheet->getCell("D$row")->getCalculatedValue());
|
|
self::assertSame(0, $sheet->getCell("E$row")->getCalculatedValue());
|
|
self::assertSame(0, $sheet->getCell("F$row")->getCalculatedValue());
|
|
self::assertSame(0, $sheet->getCell("G$row")->getCalculatedValue());
|
|
}
|
|
}
|