TimeTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Time;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  6. class TimeTest extends AllSetupTeardown
  7. {
  8. /**
  9. * @dataProvider providerTIME
  10. *
  11. * @param mixed $expectedResult
  12. */
  13. public function testTIME($expectedResult, string $formula): void
  14. {
  15. $this->mightHaveException($expectedResult);
  16. $sheet = $this->getSheet();
  17. $sheet->getCell('B1')->setValue('15');
  18. $sheet->getCell('B2')->setValue('32');
  19. $sheet->getCell('B3')->setValue('50');
  20. $sheet->getCell('A1')->setValue("=TIME($formula)");
  21. self::assertEqualsWithDelta($expectedResult, $sheet->getCell('A1')->getCalculatedValue(), 1E-8);
  22. }
  23. public function providerTIME(): array
  24. {
  25. return require 'tests/data/Calculation/DateTime/TIME.php';
  26. }
  27. public function testTIMEtoUnixTimestamp(): void
  28. {
  29. self::setUnixReturn();
  30. $result = Time::fromHMS(7, 30, 20);
  31. self::assertEqualsWithDelta(27020, $result, 1E-8);
  32. }
  33. public function testTIMEtoDateTimeObject(): void
  34. {
  35. self::setObjectReturn();
  36. $result = Time::fromHMS(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. public function testTIME1904(): void
  45. {
  46. self::setMac1904();
  47. $result = Time::fromHMS(0, 0, 0);
  48. self::assertEquals(0, $result);
  49. }
  50. public function testTIME1900(): void
  51. {
  52. $result = Time::fromHMS(0, 0, 0);
  53. self::assertEquals(0, $result);
  54. }
  55. /**
  56. * @dataProvider providerTimeArray
  57. */
  58. public function testTimeArray(array $expectedResult, string $hour, string $minute, string $second): void
  59. {
  60. $calculation = Calculation::getInstance();
  61. $formula = "=TIME({$hour}, {$minute}, {$second})";
  62. $result = $calculation->_calculateFormulaValue($formula);
  63. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  64. }
  65. public function providerTimeArray(): array
  66. {
  67. return [
  68. 'row vector hour' => [[[0.250706018518519, 0.50070601851852, 0.75070601851852]], '{6,12,18}', '1', '1'],
  69. 'column vector hour' => [[[0.250706018518519], [0.50070601851852], [0.75070601851852]], '{6;12;18}', '1', '1'],
  70. 'matrix hour' => [[[0.250706018518519, 0.50070601851852], [0.75070601851852, 0.95903935185185]], '{6,12;18,23}', '1', '1'],
  71. 'row vector minute' => [[[0.96667824074074, 0.97501157407407, 0.98334490740741, 0.99931712962963]], '23', '{12, 24, 36, 59}', '1'],
  72. 'column vector minute' => [[[0.96734953703704], [0.97568287037037], [0.98401620370370], [0.99998842592593]], '23', '{12; 24; 36; 59}', '59'],
  73. 'matrix minute' => [[[0.50833333333333, 0.51666666666667], [0.52083333333333, 0.5]], '12', '{12, 24; 30, 0}', '0'],
  74. 'row vector second' => [[[0.50069444444444, 0.50137731481481]], '12', '1', '{0,59}'],
  75. 'column vector second' => [[[0.99930555555556], [0.99998842592593]], '23', '59', '{0;59}'],
  76. 'vectors hour and minute' => [
  77. [
  78. [0.87570601851852, 0.88473379629630, 0.89376157407407, 0.90626157407407],
  79. [0.91737268518519, 0.92640046296296, 0.93542824074074, 0.94792824074074],
  80. [0.95903935185185, 0.96806712962963, 0.97709490740741, 0.98959490740741],
  81. ],
  82. '{21;22;23}',
  83. '{1, 14, 27, 45}',
  84. '1',
  85. ],
  86. 'vectors hour and second' => [
  87. [
  88. [0.126041666666667, 0.126215277777778],
  89. [0.334375, 0.33454861111111],
  90. [0.584375, 0.58454861111111],
  91. ],
  92. '{3;8;14}',
  93. '1',
  94. '{30,45}',
  95. ],
  96. 'vectors minute and second' => [
  97. [
  98. [0.75833333333333, 0.75834490740741],
  99. [0.76041666666667, 0.76042824074074],
  100. [0.77083333333333, 0.77084490740741],
  101. [0.75, 0.750011574074074],
  102. ],
  103. '18',
  104. '{12; 15; 30; 0}',
  105. '{0,1}',
  106. ],
  107. 'matrices hour and minute' => [
  108. [
  109. [0.25070601851852, 0.50278935185185],
  110. [0.75487268518519, 0.96528935185185],
  111. ],
  112. '{6, 12; 18, 23}',
  113. '{1, 4; 7, 10}',
  114. '1',
  115. ],
  116. ];
  117. }
  118. /**
  119. * @dataProvider providerTimeArrayException
  120. */
  121. public function testTimeArrayException(string $hour, string $minute, string $second): void
  122. {
  123. $calculation = Calculation::getInstance();
  124. $this->expectException(Exception::class);
  125. $this->expectExceptionMessage('Formulae with more than two array arguments are not supported');
  126. $formula = "=TIME({$hour}, {$minute}, {$second})";
  127. $calculation->_calculateFormulaValue($formula);
  128. }
  129. public function providerTimeArrayException(): array
  130. {
  131. return [
  132. 'matrix arguments with 3 array values' => [
  133. '{6, 12; 16, 23}',
  134. '{1, 4; 7, 10}',
  135. '{0,1}',
  136. ],
  137. ];
  138. }
  139. }