DateTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Date;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  6. class DateTest extends AllSetupTeardown
  7. {
  8. /**
  9. * @dataProvider providerDATE
  10. *
  11. * @param mixed $expectedResult
  12. */
  13. public function testDATE($expectedResult, string $formula): void
  14. {
  15. $this->mightHaveException($expectedResult);
  16. $sheet = $this->getSheet();
  17. $sheet->getCell('B1')->setValue('1954-11-23');
  18. $sheet->getCell('A1')->setValue("=DATE($formula)");
  19. self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
  20. }
  21. public function providerDATE(): array
  22. {
  23. return require 'tests/data/Calculation/DateTime/DATE.php';
  24. }
  25. public function testDATEtoUnixTimestamp(): void
  26. {
  27. self::setUnixReturn();
  28. $result = Date::fromYMD(2012, 1, 31); // 32-bit safe
  29. self::assertEquals(1327968000, $result);
  30. }
  31. public function testDATEtoDateTimeObject(): void
  32. {
  33. self::setObjectReturn();
  34. $result = Date::fromYMD(2012, 1, 31);
  35. // Must return an object...
  36. self::assertIsObject($result);
  37. // ... of the correct type
  38. self::assertTrue(is_a($result, 'DateTimeInterface'));
  39. // ... with the correct value
  40. self::assertEquals($result->format('d-M-Y'), '31-Jan-2012');
  41. }
  42. public function testDATEwith1904Calendar(): void
  43. {
  44. self::setMac1904();
  45. $result = Date::fromYMD(1918, 11, 11);
  46. self::assertEquals($result, 5428);
  47. $result = Date::fromYMD(1901, 1, 31);
  48. self::assertEquals($result, '#NUM!');
  49. }
  50. /**
  51. * @dataProvider providerDateArray
  52. */
  53. public function testDateArray(array $expectedResult, string $year, string $month, string $day): void
  54. {
  55. $calculation = Calculation::getInstance();
  56. $formula = "=DATE({$year}, {$month}, {$day})";
  57. $result = $calculation->_calculateFormulaValue($formula);
  58. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  59. }
  60. public function providerDateArray(): array
  61. {
  62. return [
  63. 'row vector year' => [[[44197, 44562, 44927]], '{2021,2022,2023}', '1', '1'],
  64. 'column vector year' => [[[44197], [44562], [44927]], '{2021;2022;2023}', '1', '1'],
  65. 'matrix year' => [[[43831.00, 44197], [44562, 44927]], '{2020,2021;2022,2023}', '1', '1'],
  66. 'row vector month' => [[[44562, 44652, 44743, 44835]], '2022', '{1, 4, 7, 10}', '1'],
  67. 'column vector month' => [[[44562], [44652], [44743], [44835]], '2022', '{1; 4; 7; 10}', '1'],
  68. 'matrix month' => [[[44562, 44652], [44743, 44835]], '2022', '{1, 4; 7, 10}', '1'],
  69. 'row vector day' => [[[44561, 44562]], '2022', '1', '{0,1}'],
  70. 'column vector day' => [[[44561], [44562]], '2022', '1', '{0;1}'],
  71. 'vectors year and month' => [
  72. [
  73. [44197, 44287, 44378, 44470],
  74. [44562, 44652, 44743, 44835],
  75. [44927, 45017, 45108, 45200],
  76. ],
  77. '{2021;2022;2023}',
  78. '{1, 4, 7, 10}',
  79. '1',
  80. ],
  81. 'vectors year and day' => [
  82. [
  83. [44196, 44197],
  84. [44561, 44562],
  85. [44926, 44927],
  86. ],
  87. '{2021;2022;2023}',
  88. '1',
  89. '{0,1}',
  90. ],
  91. 'vectors month and day' => [
  92. [
  93. [44561, 44562],
  94. [44651, 44652],
  95. [44742, 44743],
  96. [44834, 44835],
  97. ],
  98. '2022',
  99. '{1; 4; 7; 10}',
  100. '{0,1}',
  101. ],
  102. 'matrices year and month' => [
  103. [
  104. [43831, 44287],
  105. [44743, 45200],
  106. ],
  107. '{2020, 2021; 2022, 2023}',
  108. '{1, 4; 7, 10}',
  109. '1',
  110. ],
  111. ];
  112. }
  113. /**
  114. * @dataProvider providerDateArrayException
  115. */
  116. public function testDateArrayException(string $year, string $month, string $day): void
  117. {
  118. $calculation = Calculation::getInstance();
  119. $this->expectException(Exception::class);
  120. $this->expectExceptionMessage('Formulae with more than two array arguments are not supported');
  121. $formula = "=DATE({$year}, {$month}, {$day})";
  122. $calculation->_calculateFormulaValue($formula);
  123. }
  124. public function providerDateArrayException(): array
  125. {
  126. return [
  127. 'matrix arguments with 3 array values' => [
  128. '{2020, 2021; 2022, 2023}',
  129. '{1, 4; 7, 10}',
  130. '{0,1}',
  131. ],
  132. ];
  133. }
  134. }