YearTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class YearTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerYEAR
  8. *
  9. * @param mixed $expectedResult
  10. */
  11. public function testYEAR($expectedResult, string $dateTimeValue): void
  12. {
  13. $this->mightHaveException($expectedResult);
  14. $sheet = $this->getSheet();
  15. $sheet->getCell('A1')->setValue("=YEAR($dateTimeValue)");
  16. $sheet->getCell('B1')->setValue('1954-11-23');
  17. self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
  18. }
  19. public function providerYEAR(): array
  20. {
  21. return require 'tests/data/Calculation/DateTime/YEAR.php';
  22. }
  23. /**
  24. * @dataProvider providerYearArray
  25. */
  26. public function testYearArray(array $expectedResult, string $array): void
  27. {
  28. $calculation = Calculation::getInstance();
  29. $formula = "=YEAR({$array})";
  30. $result = $calculation->_calculateFormulaValue($formula);
  31. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  32. }
  33. public function providerYearArray(): array
  34. {
  35. return [
  36. 'row vector' => [[[2021, 2022, 2023]], '{"2021-01-01", "2022-01-01", "2023-01-01"}'],
  37. 'column vector' => [[[2021], [2022], [2023]], '{"2021-01-01"; "2022-01-01"; "2023-01-01"}'],
  38. 'matrix' => [[[2021, 2022], [2023, 1999]], '{"2021-01-01", "2022-01-01"; "2023-01-01", "1999-12-31"}'],
  39. ];
  40. }
  41. }