TimestampFactoryTest::dataBuildTimestamp()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2017 American Express Travel Related Services Company, Inc.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15
 * or implied. See the License for the specific language governing
16
 * permissions and limitations under the License.
17
 */
18
19
declare(strict_types=1);
20
21
namespace AmericanExpressTest\HyperledgerFabricClient\ProtoFactory;
22
23
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\TimestampFactory;
24
use Google\Protobuf\Timestamp;
25
use PHPUnit\Framework\TestCase;
26
27
/**
28
 * @covers \AmericanExpress\HyperledgerFabricClient\ProtoFactory\TimestampFactory
29
 */
30
class TimestampFactoryTest extends TestCase
31
{
32
    public function testBuildCurrentTimestamp()
33
    {
34
        $timestamp = TimestampFactory::fromDateTime();
35
36
        self::assertInstanceOf(Timestamp::class, $timestamp);
37
        self::assertNotNull($timestamp->getSeconds());
38
        self::assertNotNull($timestamp->getNanos());
39
    }
40
41
    /**
42
     * @param \DateTime $dateTime
43
     * @param int $seconds
44
     * @param int $nanos
45
     * @dataProvider dataBuildTimestamp
46
     */
47
    public function testBuildTimestamp(\DateTime $dateTime, int $seconds, int $nanos)
48
    {
49
        $timestamp = TimestampFactory::fromDateTime($dateTime);
50
51
        self::assertInstanceOf(Timestamp::class, $timestamp);
52
        self::assertSame($seconds, $timestamp->getSeconds());
53
        self::assertSame($nanos, $timestamp->getNanos());
54
    }
55
56
    public function dataBuildTimestamp(): array
57
    {
58
        return [
59
            // Now-ish.
60
            [new \DateTime('2017-11-14T11:23:45', timezone_open('UTC')), 1510658625, 0],
0 ignored issues
show
Bug introduced by
It seems like timezone_open('UTC') can also be of type false; however, parameter $timezone of DateTime::__construct() does only seem to accept null|DateTimeZone, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            [new \DateTime('2017-11-14T11:23:45', /** @scrutinizer ignore-type */ timezone_open('UTC')), 1510658625, 0],
Loading history...
61
            [new \DateTime('2017-11-14T11:23:45.6789', timezone_open('UTC')), 1510658625, 678900000],
62
63
            // Epoch
64
            [new \DateTime('1970-01-01T00:00:00', timezone_open('UTC')), 0, 0],
65
            [new \DateTime('1970-01-01T00:00:00.1234', timezone_open('UTC')), 0, 123400000],
66
67
            // Epoch -1
68
69
            [new \DateTime('1969-12-31T23:59:59', timezone_open('UTC')), -1, 0],
70
            [new \DateTime('1969-12-31T23:59:59.1234', timezone_open('UTC')), -1, 123400000],
71
72
            // Epoch +1
73
            [new \DateTime('1970-01-01T00:00:01', timezone_open('UTC')), 1, 0],
74
            [new \DateTime('1970-01-01T00:00:01.1234', timezone_open('UTC')), 1, 123400000],
75
76
            // Proto's lower limit
77
            [new \DateTime('0001-01-01T00:00:00', timezone_open('UTC')), -62135596800, 0],
78
79
            // Proto's upper limit
80
            [new \DateTime('9999-12-31T23:59:59', timezone_open('UTC')), 253402300799, 0],
81
        ];
82
    }
83
}
84