TimeValueTest.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\TimeValue;
  5. class TimeValueTest extends AllSetupTeardown
  6. {
  7. /**
  8. * @dataProvider providerTIMEVALUE
  9. *
  10. * @param mixed $expectedResult
  11. * @param mixed $timeValue
  12. */
  13. public function testTIMEVALUE($expectedResult, $timeValue): void
  14. {
  15. $this->mightHaveException($expectedResult);
  16. $sheet = $this->getSheet();
  17. $sheet->getCell('B1')->setValue('03:45:52');
  18. $sheet->getCell('A1')->setValue("=TIMEVALUE($timeValue)");
  19. $result = $sheet->getCell('A1')->getCalculatedValue();
  20. self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
  21. }
  22. public function providerTIMEVALUE(): array
  23. {
  24. return require 'tests/data/Calculation/DateTime/TIMEVALUE.php';
  25. }
  26. public function testTIMEVALUEtoUnixTimestamp(): void
  27. {
  28. self::setUnixReturn();
  29. $result = TimeValue::fromString('7:30:20');
  30. self::assertEquals(23420, $result);
  31. self::assertEqualsWithDelta(23420, $result, 1E-8);
  32. }
  33. public function testTIMEVALUEtoDateTimeObject(): void
  34. {
  35. self::setObjectReturn();
  36. $result = TimeValue::fromString('7:30:20');
  37. // Must return an object...
  38. self::assertIsObject($result);
  39. // ... of the correct type
  40. self::assertTrue(is_a($result, 'DateTimeInterface'));
  41. // ... with the correct value
  42. self::assertEquals($result->format('H:i:s'), '07:30:20');
  43. }
  44. /**
  45. * @dataProvider providerTimeValueArray
  46. */
  47. public function testTimeValueArray(array $expectedResult, string $array): void
  48. {
  49. $calculation = Calculation::getInstance();
  50. $formula = "=TIMEVALUE({$array})";
  51. $result = $calculation->_calculateFormulaValue($formula);
  52. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  53. }
  54. public function providerTimeValueArray(): array
  55. {
  56. return [
  57. 'row vector' => [[[0.04309027777777, 0.5515625, 0.80579861111111]], '{"2022-02-09 01:02:03", "2022-02-09 13:14:15", "2022-02-09 19:20:21"}'],
  58. 'column vector' => [[[0.04309027777777], [0.5515625], [0.80579861111111]], '{"2022-02-09 01:02:03"; "2022-02-09 13:14:15"; "2022-02-09 19:20:21"}'],
  59. 'matrix' => [[[0.04309027777777, 0.5515625], [0.80579861111111, 0.99998842592592]], '{"2022-02-09 01:02:03", "2022-02-09 13:14:15"; "2022-02-09 19:20:21", "1999-12-31 23:59:59"}'],
  60. ];
  61. }
  62. }