1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Current; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\DateParts; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\TimeParts; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class TodayTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param mixed $result |
16
|
|
|
*/ |
17
|
|
|
private function assertions(DateTimeImmutable $dtStart, $result): void |
18
|
|
|
{ |
19
|
|
|
self::assertEquals($dtStart->format('Y'), DateParts::year($result)); |
20
|
|
|
self::assertEquals($dtStart->format('m'), DateParts::month($result)); |
21
|
|
|
self::assertEquals($dtStart->format('d'), DateParts::day($result)); |
22
|
|
|
self::assertEquals(0, TimeParts::hour($result)); |
23
|
|
|
self::assertEquals(0, TimeParts::minute($result)); |
24
|
|
|
self::assertEquals(0, TimeParts::second($result)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testDirectCallToToday(): void |
28
|
|
|
{ |
29
|
|
|
// Loop to avoid rare edge case where first calculation |
30
|
|
|
// and second do not take place in same second. |
31
|
|
|
do { |
32
|
|
|
$dtStart = new DateTimeImmutable(); |
33
|
|
|
$startSecond = $dtStart->format('s'); |
34
|
|
|
$result = Current::today(); |
35
|
|
|
$endSecond = (new DateTimeImmutable('now'))->format('s'); |
36
|
|
|
} while ($startSecond !== $endSecond); |
37
|
|
|
|
38
|
|
|
$this->assertions($dtStart, $result); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testTodayAsFormula(): void |
42
|
|
|
{ |
43
|
|
|
$calculation = Calculation::getInstance(); |
44
|
|
|
$formula = '=TODAY()'; |
45
|
|
|
|
46
|
|
|
// Loop to avoid rare edge case where first calculation |
47
|
|
|
// and second do not take place in same second. |
48
|
|
|
do { |
49
|
|
|
$dtStart = new DateTimeImmutable(); |
50
|
|
|
$startSecond = $dtStart->format('s'); |
51
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
52
|
|
|
$endSecond = (new DateTimeImmutable('now'))->format('s'); |
53
|
|
|
} while ($startSecond !== $endSecond); |
54
|
|
|
|
55
|
|
|
$this->assertions($dtStart, $result); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|