1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/value-object project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Tests\ValueObject; |
10
|
|
|
|
11
|
|
|
use Daikon\ValueObject\Timestamp; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
final class TimestampTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
private const FIXED_TIMESTAMP_EUR = '2016-07-04T19:27:07.123000+02:00'; |
17
|
|
|
|
18
|
|
|
private const FIXED_TIMESTAMP_UTC = '2016-07-04T17:27:07.123000+00:00'; |
19
|
|
|
|
20
|
|
|
private const FIXED_EARLY_TIMESTAMP_UTC = '2016-07-03T17:17:07.122999+00:00'; |
21
|
|
|
|
22
|
|
|
private const FIXED_LATE_TIMESTAMP_UTC = '2016-07-05T17:27:07.123000+00:00'; |
23
|
|
|
|
24
|
|
|
private Timestamp $timestamp; |
25
|
|
|
|
26
|
1 |
|
public function testToNative(): void |
27
|
|
|
{ |
28
|
1 |
|
$this->assertEquals(self::FIXED_TIMESTAMP_UTC, $this->timestamp->toNative()); |
29
|
1 |
|
$this->assertNull(Timestamp::fromNative(null)->toNative()); |
30
|
1 |
|
$this->assertNull(Timestamp::makeEmpty()->toNative()); |
31
|
1 |
|
} |
32
|
|
|
|
33
|
1 |
|
public function testEquals(): void |
34
|
|
|
{ |
35
|
1 |
|
$equalTs = Timestamp::fromString('2016-07-04T17:27:07.123000', 'Y-m-d\\TH:i:s.u'); |
36
|
1 |
|
$this->assertTrue($this->timestamp->equals($equalTs)); |
37
|
1 |
|
$differentTs = Timestamp::fromString('+1 year', 'Y-m-d\\TH:i:s.u'); |
38
|
1 |
|
$this->assertFalse($this->timestamp->equals($differentTs)); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function testToString(): void |
42
|
|
|
{ |
43
|
1 |
|
$this->assertEquals(self::FIXED_TIMESTAMP_UTC, (string)$this->timestamp); |
44
|
1 |
|
} |
45
|
|
|
|
46
|
1 |
|
public function testToTime(): void |
47
|
|
|
{ |
48
|
1 |
|
$this->assertEquals('1578687888', Timestamp::fromNative('1578687888')->toTime()); |
49
|
1 |
|
$this->assertSame(1578687889, Timestamp::fromNative(1578687889)->toTime()); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
1 |
|
public function testIsEmpty(): void |
53
|
|
|
{ |
54
|
1 |
|
$this->assertTrue(Timestamp::fromNative(null)->isEmpty()); |
55
|
1 |
|
$this->assertTrue(Timestamp::makeEmpty()->isEmpty()); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
public function testIsBefore(): void |
59
|
|
|
{ |
60
|
1 |
|
$nullTs = Timestamp::makeEmpty(); |
61
|
1 |
|
$earlyTs = Timestamp::fromString(self::FIXED_TIMESTAMP_UTC); |
62
|
1 |
|
$lateTs = Timestamp::fromString(self::FIXED_LATE_TIMESTAMP_UTC); |
63
|
1 |
|
$this->assertTrue($nullTs->isBefore($earlyTs)); |
64
|
1 |
|
$this->assertFalse($earlyTs->isBefore($nullTs)); |
65
|
1 |
|
$this->assertTrue($earlyTs->isBefore($lateTs)); |
66
|
1 |
|
$this->assertFalse($lateTs->isBefore($earlyTs)); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
public function testIsAfter(): void |
70
|
|
|
{ |
71
|
1 |
|
$nullTs = Timestamp::makeEmpty(); |
72
|
1 |
|
$earlyTs = Timestamp::fromString(self::FIXED_TIMESTAMP_UTC); |
73
|
1 |
|
$lateTs = Timestamp::fromString(self::FIXED_LATE_TIMESTAMP_UTC); |
74
|
1 |
|
$this->assertFalse($nullTs->isAfter($earlyTs)); |
75
|
1 |
|
$this->assertTrue($earlyTs->isAfter($nullTs)); |
76
|
1 |
|
$this->assertFalse($earlyTs->isAfter($lateTs)); |
77
|
1 |
|
$this->assertTrue($lateTs->isAfter($earlyTs)); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
public function testModify(): void |
81
|
|
|
{ |
82
|
1 |
|
$addTs1 = $this->timestamp->modify('+1 day'); |
83
|
1 |
|
$addTs2 = $this->timestamp->modify('+24 hours'); |
84
|
1 |
|
$this->assertEquals(self::FIXED_LATE_TIMESTAMP_UTC, (string)$addTs1); |
85
|
1 |
|
$this->assertEquals(self::FIXED_LATE_TIMESTAMP_UTC, (string)$addTs2); |
86
|
|
|
|
87
|
1 |
|
$subTs1 = $this->timestamp->modify('-1 day - 10 minutes - 1 microsecond'); |
88
|
1 |
|
$subTs2 = $this->timestamp->modify('-24 hours - 600 seconds - 1 microsecond'); |
89
|
1 |
|
$this->assertEquals(self::FIXED_EARLY_TIMESTAMP_UTC, (string)$subTs1); |
90
|
1 |
|
$this->assertEquals(self::FIXED_EARLY_TIMESTAMP_UTC, (string)$subTs2); |
91
|
|
|
|
92
|
1 |
|
$this->expectWarning(); |
93
|
1 |
|
$this->timestamp->modify('bogus'); |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
protected function setUp(): void |
97
|
|
|
{ |
98
|
8 |
|
$this->timestamp = Timestamp::fromNative(self::FIXED_TIMESTAMP_EUR); |
99
|
8 |
|
} |
100
|
|
|
} |
101
|
|
|
|