DaysTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use DateTime;
  4. use DateTimeImmutable;
  5. use Exception;
  6. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  7. use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Days;
  8. class DaysTest extends AllSetupTeardown
  9. {
  10. /**
  11. * @dataProvider providerDAYS
  12. *
  13. * @param mixed $expectedResult
  14. */
  15. public function testDAYS($expectedResult, string $formula): void
  16. {
  17. $this->mightHaveException($expectedResult);
  18. $sheet = $this->getSheet();
  19. $sheet->getCell('B1')->setValue('1954-11-23');
  20. $sheet->getCell('C1')->setValue('1954-11-30');
  21. $sheet->getCell('A1')->setValue("=DAYS($formula)");
  22. self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
  23. }
  24. public function providerDAYS(): array
  25. {
  26. return require 'tests/data/Calculation/DateTime/DAYS.php';
  27. }
  28. public function testObject(): void
  29. {
  30. $obj1 = new DateTime('2000-3-31');
  31. $obj2 = new DateTimeImmutable('2000-2-29');
  32. self::assertSame(31, Days::between($obj1, $obj2));
  33. }
  34. public function testNonDateObject(): void
  35. {
  36. $obj1 = new Exception();
  37. $obj2 = new DateTimeImmutable('2000-2-29');
  38. self::assertSame('#VALUE!', Days::between($obj1, $obj2));
  39. }
  40. /**
  41. * @dataProvider providerDaysArray
  42. */
  43. public function testDaysArray(array $expectedResult, string $startDate, string $endDate): void
  44. {
  45. $calculation = Calculation::getInstance();
  46. $formula = "=DAYS({$startDate}, {$endDate})";
  47. $result = $calculation->_calculateFormulaValue($formula);
  48. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  49. }
  50. public function providerDaysArray(): array
  51. {
  52. return [
  53. 'row vector #1' => [[[-364, -202, 203]], '{"2022-01-01", "2022-06-12", "2023-07-22"}', '"2022-12-31"'],
  54. 'column vector #1' => [[[-364], [-362], [-359]], '{"2022-01-01"; "2022-01-03"; "2022-01-06"}', '"2022-12-31"'],
  55. 'matrix #1' => [[[1, 10], [227, 365]], '{"2022-01-01", "2022-01-10"; "2022-08-15", "2022-12-31"}', '"2021-12-31"'],
  56. ];
  57. }
  58. }