Passed
Pull Request — master (#3448)
by Mark
22:11
created

NowTest::testNow()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 23
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.552

1 Method

Rating   Name   Duplication   Size   Complexity  
A NowTest::assertions() 0 8 1
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 NowTest 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($dtStart->format('H'), TimeParts::hour($result));
23
        self::assertEquals($dtStart->format('i'), TimeParts::minute($result));
24
        self::assertEquals($dtStart->format('s'), TimeParts::second($result));
25
    }
26
27
    public function testDirectCallToNow(): 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::now();
35
            $endSecond = (new DateTimeImmutable('now'))->format('s');
36
        } while ($startSecond !== $endSecond);
37
38
        $this->assertions($dtStart, $result);
39
    }
40
41
    public function testNowAsFormula(): void
42
    {
43
        $calculation = Calculation::getInstance();
44
        $formula = '=NOW()';
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