YearFracTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class YearFracTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerYEARFRAC
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $arg1
  11. * @param mixed $arg2
  12. * @param mixed $arg3
  13. */
  14. public function testYEARFRAC($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', $arg3 = 'omitted'): void
  15. {
  16. $this->mightHaveException($expectedResult);
  17. $sheet = $this->getSheet();
  18. if ($arg1 !== null) {
  19. $sheet->getCell('A1')->setValue($arg1);
  20. }
  21. if ($arg2 !== null) {
  22. $sheet->getCell('A2')->setValue($arg2);
  23. }
  24. if ($arg3 !== null) {
  25. $sheet->getCell('A3')->setValue($arg3);
  26. }
  27. if ($arg1 === 'omitted') {
  28. $sheet->getCell('B1')->setValue('=YEARFRAC()');
  29. } elseif ($arg2 === 'omitted') {
  30. $sheet->getCell('B1')->setValue('=YEARFRAC(A1)');
  31. } elseif ($arg3 === 'omitted') {
  32. $sheet->getCell('B1')->setValue('=YEARFRAC(A1, A2)');
  33. } else {
  34. $sheet->getCell('B1')->setValue('=YEARFRAC(A1, A2, A3)');
  35. }
  36. self::assertEqualswithDelta($expectedResult, $sheet->getCell('B1')->getCalculatedValue(), 1E-6);
  37. }
  38. public function providerYEARFRAC(): array
  39. {
  40. return require 'tests/data/Calculation/DateTime/YEARFRAC.php';
  41. }
  42. /**
  43. * @dataProvider providerYearFracArray
  44. */
  45. public function testYearFracArray(array $expectedResult, string $startDate, string $endDate, ?string $methods): void
  46. {
  47. $calculation = Calculation::getInstance();
  48. if ($methods === null) {
  49. $formula = "=YEARFRAC({$startDate}, {$endDate})";
  50. } else {
  51. $formula = "=YEARFRAC({$startDate}, {$endDate}, {$methods})";
  52. }
  53. $result = $calculation->_calculateFormulaValue($formula);
  54. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  55. }
  56. public function providerYearFracArray(): array
  57. {
  58. return [
  59. 'row vector #1' => [[[1.0, 0.55277777777778, 0.56111111111111]], '{"2022-01-01", "2022-06-12", "2023-07-22"}', '"2022-12-31"', null],
  60. 'column vector #1' => [[[1.0], [0.99444444444445], [0.98611111111111]], '{"2022-01-01"; "2022-01-03"; "2022-01-06"}', '"2022-12-31"', null],
  61. 'matrix #1' => [[[0.002777777777778, 0.027777777777778], [0.625, 1.0]], '{"2022-01-01", "2022-01-10"; "2022-08-15", "2022-12-31"}', '"2021-12-31"', null],
  62. 'column vector with methods' => [[[0.99726027397260, 0.99722222222222], [0.99178082191781, 0.99166666666667], [0.98356164383562, 0.98333333333333]], '{"2022-01-01"; "2022-01-03"; "2022-01-06"}', '"2022-12-31"', '{1, 4}'],
  63. ];
  64. }
  65. }