DateDifTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class DateDifTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerDATEDIF
  8. *
  9. * @param mixed $expectedResult
  10. */
  11. public function testDATEDIF($expectedResult, string $formula): void
  12. {
  13. $this->mightHaveException($expectedResult);
  14. $sheet = $this->getSheet();
  15. $sheet->getCell('B1')->setValue('1954-11-23');
  16. $sheet->getCell('A1')->setValue("=DATEDIF($formula)");
  17. self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
  18. }
  19. public function providerDATEDIF(): array
  20. {
  21. return require 'tests/data/Calculation/DateTime/DATEDIF.php';
  22. }
  23. /**
  24. * @dataProvider providerDateDifArray
  25. */
  26. public function testDateDifArray(array $expectedResult, string $startDate, string $endDate, ?string $methods): void
  27. {
  28. $calculation = Calculation::getInstance();
  29. if ($methods === null) {
  30. $formula = "=DATEDIF({$startDate}, {$endDate})";
  31. } else {
  32. $formula = "=DATEDIF({$startDate}, {$endDate}, {$methods})";
  33. }
  34. $result = $calculation->_calculateFormulaValue($formula);
  35. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  36. }
  37. public function providerDateDifArray(): array
  38. {
  39. return [
  40. 'row vector #1' => [[[364, 202, '#NUM!']], '{"2022-01-01", "2022-06-12", "2023-07-22"}', '"2022-12-31"', null],
  41. 'column vector #1' => [[[364], [362], [359]], '{"2022-01-01"; "2022-01-03"; "2022-01-06"}', '"2022-12-31"', null],
  42. 'matrix #1' => [[[365, 266], [139, 1]], '{"2022-01-01", "2022-04-10"; "2022-08-15", "2022-12-31"}', '"2023-01-01"', null],
  43. 'column vector with methods' => [[[364, 11], [242, 7], [173, 5]], '{"2022-01-01"; "2022-05-03"; "2022-07-11"}', '"2022-12-31"', '{"D", "M"}'],
  44. ];
  45. }
  46. }