TodayTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use DateTimeImmutable;
  4. class TodayTest extends AllSetupTeardown
  5. {
  6. public function testToday(): void
  7. {
  8. $sheet = $this->getSheet();
  9. $row = 0;
  10. // Loop to avoid rare edge case where first calculation
  11. // and second do not take place in same second.
  12. do {
  13. ++$row;
  14. $dtStart = new DateTimeImmutable();
  15. $startSecond = $dtStart->format('s');
  16. $sheet->setCellValue("A$row", '=TODAY()');
  17. // cache result for later assertions
  18. $sheet->getCell("A$row")->getCalculatedValue();
  19. $dtEnd = new DateTimeImmutable();
  20. $endSecond = $dtEnd->format('s');
  21. } while ($startSecond !== $endSecond);
  22. $sheet->setCellValue("B$row", "=YEAR(A$row)");
  23. $sheet->setCellValue("C$row", "=MONTH(A$row)");
  24. $sheet->setCellValue("D$row", "=DAY(A$row)");
  25. $sheet->setCellValue("E$row", "=HOUR(A$row)");
  26. $sheet->setCellValue("F$row", "=MINUTE(A$row)");
  27. $sheet->setCellValue("G$row", "=SECOND(A$row)");
  28. self::assertSame((int) $dtStart->format('Y'), $sheet->getCell("B$row")->getCalculatedValue());
  29. self::assertSame((int) $dtStart->format('m'), $sheet->getCell("C$row")->getCalculatedValue());
  30. self::assertSame((int) $dtStart->format('d'), $sheet->getCell("D$row")->getCalculatedValue());
  31. self::assertSame(0, $sheet->getCell("E$row")->getCalculatedValue());
  32. self::assertSame(0, $sheet->getCell("F$row")->getCalculatedValue());
  33. self::assertSame(0, $sheet->getCell("G$row")->getCalculatedValue());
  34. }
  35. }