EDateTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Month;
  5. class EDateTest extends AllSetupTeardown
  6. {
  7. /**
  8. * @dataProvider providerEDATE
  9. *
  10. * @param mixed $expectedResult
  11. */
  12. public function testEDATE($expectedResult, string $formula): void
  13. {
  14. $this->mightHaveException($expectedResult);
  15. $sheet = $this->getSheet();
  16. $sheet->getCell('A1')->setValue("=EDATE($formula)");
  17. $sheet->getCell('B1')->setValue('1954-11-23');
  18. self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
  19. }
  20. public function providerEDATE(): array
  21. {
  22. return require 'tests/data/Calculation/DateTime/EDATE.php';
  23. }
  24. public function testEDATEtoUnixTimestamp(): void
  25. {
  26. self::setUnixReturn();
  27. $result = Month::adjust('2012-1-26', -1);
  28. self::assertEquals(1324857600, $result);
  29. self::assertEqualsWithDelta(1324857600, $result, 1E-8);
  30. }
  31. public function testEDATEtoDateTimeObject(): void
  32. {
  33. self::setObjectReturn();
  34. $result = Month::adjust('2012-1-26', -1);
  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'), '26-Dec-2011');
  41. }
  42. /**
  43. * @dataProvider providerEDateArray
  44. */
  45. public function testEDateArray(array $expectedResult, string $dateValues, string $methods): void
  46. {
  47. $calculation = Calculation::getInstance();
  48. $formula = "=EDATE({$dateValues}, {$methods})";
  49. $result = $calculation->_calculateFormulaValue($formula);
  50. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  51. }
  52. public function providerEDateArray(): array
  53. {
  54. return [
  55. 'row vector #1' => [[[44593, 44632, 45337]], '{"2022-01-01", "2022-02-12", "2024-01-15"}', '1'],
  56. 'column vector #1' => [[[44593], [44632], [45337]], '{"2022-01-01"; "2022-02-12"; "2024-01-15"}', '1'],
  57. 'matrix #1' => [[[44593, 44632], [44652, 45343]], '{"2022-01-01", "2022-02-12"; "2022-03-01", "2024-01-21"}', '1'],
  58. 'row vector #2' => [[[44573, 44604, 44632]], '"2022-02-12"', '{-1, 0, 1}'],
  59. 'column vector #2' => [[[44573], [44604], [44632]], '"2022-02-12"', '{-1; 0; 1}'],
  60. 'matrix #2' => [[[44573, 44604], [44632, 45334]], '"2022-02-12"', '{-1, 0; 1, 24}'],
  61. ];
  62. }
  63. }