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], |
|
|
|
|
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
|
|
|
|