MovedFunctionsTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use DateTimeImmutable;
  4. use PhpOffice\PhpSpreadsheet\Calculation\DateTime;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * Sanity tests for functions which have been moved out of DateTime
  9. * to their own classes. A deprecated version remains in DateTime;
  10. * this class contains cursory tests to ensure that those work properly.
  11. * If Scrutinizer fails the PR because of these deprecations, I will
  12. * remove this class from the PR.
  13. *
  14. * @covers \PhpOffice\PhpSpreadsheet\Calculation\DateTime
  15. */
  16. class MovedFunctionsTest extends TestCase
  17. {
  18. public function testMovedFunctions(): void
  19. {
  20. self::assertTrue(DateTime::isLeapYear(1904));
  21. self::assertSame('#VALUE!', DateTime::getDateValue('XYZ'));
  22. self::assertSame(61.0, DateTime::getDateValue('1900-03-01'));
  23. self::assertSame(61.0, DateTime::DATE(1900, 3, 1));
  24. self::assertSame(365, DateTime::DATEDIF('2016-01-01', '2016-12-31', 'YD'));
  25. self::assertSame(61.0, DateTime::DATEVALUE('1900-03-01'));
  26. self::assertSame(28, DateTime::DAYOFMONTH('1904-02-28'));
  27. self::assertSame(364, DateTime::DAYS('2007-12-31', '2007-1-1'));
  28. self::assertSame(9, DateTime::DAYS360('2007-1-1', '2007-1-10', false));
  29. self::assertSame(39493.0, DateTime::EDATE('15-Jan-2008', 1));
  30. self::assertSame(39507.0, DateTime::EOMONTH('15-Jan-2008', 1));
  31. self::assertSame(18, DateTime::HOUROFDAY(0.75));
  32. self::assertSame(52, DateTime::ISOWEEKNUM('2000-01-01'));
  33. self::assertSame(24, DateTime::MINUTE(0.6));
  34. self::assertSame(11, DateTime::MONTHOFYEAR('11-Nov-1918'));
  35. self::assertSame(8, DateTime::NETWORKDAYS('1-Jan-2007', '10-Jan-2007'));
  36. self::assertSame(35, DateTime::SECOND('11:15:35'));
  37. self::assertSame(0.5, DateTime::TIME(12, 0, 0));
  38. self::assertSame(0.40625, DateTime::TIMEVALUE('33:45'));
  39. self::assertSame(5, DateTime::WEEKDAY('24-Oct-1968'));
  40. self::assertSame(52, DateTime::WEEKNUM('21-Dec-2000'));
  41. self::assertSame(39094.0, DateTime::WORKDAY('1-Jan-2007', 9));
  42. self::assertSame(1904, DateTime::YEAR('1904-02-28'));
  43. self::assertSame(0.025, DateTime::YEARFRAC('2007-01-10', '2007-01-01', 0));
  44. }
  45. public function testTodayAndNow(): void
  46. {
  47. // Loop to avoid rare edge case where first calculation
  48. // and second do not take place in same second.
  49. do {
  50. $dtStart = new DateTimeImmutable();
  51. $startSecond = $dtStart->format('s');
  52. $nowResult = DateTime::DATETIMENOW();
  53. $todayResult = DateTime::DATENOW();
  54. $dtEnd = new DateTimeImmutable();
  55. $endSecond = $dtEnd->format('s');
  56. } while ($startSecond !== $endSecond);
  57. self::assertSame(DateTime::DAYOFMONTH($nowResult), DateTime::DAYOFMONTH($todayResult));
  58. }
  59. }