EoMonthTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Month;
  5. class EoMonthTest extends AllSetupTeardown
  6. {
  7. /**
  8. * @dataProvider providerEOMONTH
  9. *
  10. * @param mixed $expectedResult
  11. */
  12. public function testEOMONTH($expectedResult, string $formula): void
  13. {
  14. $this->mightHaveException($expectedResult);
  15. $sheet = $this->getSheet();
  16. $sheet->getCell('A1')->setValue("=EOMONTH($formula)");
  17. $sheet->getCell('B1')->setValue('1954-11-23');
  18. self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
  19. }
  20. public function providerEOMONTH(): array
  21. {
  22. return require 'tests/data/Calculation/DateTime/EOMONTH.php';
  23. }
  24. public function testEOMONTHtoUnixTimestamp(): void
  25. {
  26. self::setUnixReturn();
  27. $result = Month::lastDay('2012-1-26', -1);
  28. self::assertEquals(1325289600, $result);
  29. }
  30. public function testEOMONTHtoDateTimeObject(): void
  31. {
  32. self::setObjectReturn();
  33. $result = Month::lastDay('2012-1-26', -1);
  34. // Must return an object...
  35. self::assertIsObject($result);
  36. // ... of the correct type
  37. self::assertTrue(is_a($result, 'DateTimeInterface'));
  38. // ... with the correct value
  39. self::assertSame($result->format('d-M-Y'), '31-Dec-2011');
  40. }
  41. /**
  42. * @dataProvider providerEoMonthArray
  43. */
  44. public function testEoMonthArray(array $expectedResult, string $dateValues, string $methods): void
  45. {
  46. $calculation = Calculation::getInstance();
  47. $formula = "=EOMONTH({$dateValues}, {$methods})";
  48. $result = $calculation->_calculateFormulaValue($formula);
  49. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  50. }
  51. public function providerEoMonthArray(): array
  52. {
  53. return [
  54. 'row vector #1' => [[[44620, 44651, 45351]], '{"2022-01-01", "2022-02-12", "2024-01-15"}', '1'],
  55. 'column vector #1' => [[[44620], [44651], [45351]], '{"2022-01-01"; "2022-02-12"; "2024-01-15"}', '1'],
  56. 'matrix #1' => [[[44620, 44651], [44681, 45351]], '{"2022-01-01", "2022-02-12"; "2022-03-01", "2024-01-21"}', '1'],
  57. 'row vector #2' => [[[44592, 44620, 44651]], '"2022-02-12"', '{-1, 0, 1}'],
  58. 'column vector #2' => [[[44592], [44620], [44651]], '"2022-02-12"', '{-1; 0; 1}'],
  59. 'matrix #2' => [[[44592, 44620], [44651, 45351]], '"2022-02-12"', '{-1, 0; 1, 24}'],
  60. ];
  61. }
  62. }