Passed
Push — master ( 33e582...680f77 )
by Mr
02:06
created

TimestampTest::testIsEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
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->assertEquals(null, Timestamp::fromNative(null)->toNative());
30 1
    }
31
32 1
    public function testEquals(): void
33
    {
34 1
        $equalTs = Timestamp::fromString('2016-07-04T17:27:07.123000', 'Y-m-d\\TH:i:s.u');
35 1
        $this->assertTrue($this->timestamp->equals($equalTs));
36 1
        $differentTs = Timestamp::fromString('+1 year', 'Y-m-d\\TH:i:s.u');
37 1
        $this->assertFalse($this->timestamp->equals($differentTs));
38 1
    }
39
40 1
    public function testToString(): void
41
    {
42 1
        $this->assertEquals(self::FIXED_TIMESTAMP_UTC, (string)$this->timestamp);
43 1
    }
44
45 1
    public function testToTime(): void
46
    {
47 1
        $this->assertEquals('1578687888', Timestamp::fromNative('1578687888')->toTime());
48 1
    }
49
50 1
    public function testIsEmpty(): void
51
    {
52 1
        $this->assertTrue(Timestamp::fromNative(null)->isEmpty());
53 1
        $this->assertTrue(Timestamp::makeEmpty()->isEmpty());
54 1
    }
55
56 1
    public function testIsBefore(): void
57
    {
58 1
        $nullTs = Timestamp::fromNative(null);
59 1
        $earlyTs = Timestamp::fromString(self::FIXED_TIMESTAMP_UTC);
60 1
        $lateTs = Timestamp::fromString(self::FIXED_LATE_TIMESTAMP_UTC);
61 1
        $this->assertTrue($nullTs->isBefore($earlyTs));
62 1
        $this->assertFalse($earlyTs->isBefore($nullTs));
63 1
        $this->assertTrue($earlyTs->isBefore($lateTs));
64 1
        $this->assertFalse($lateTs->isBefore($earlyTs));
65 1
    }
66
67 1
    public function testIsAfter(): void
68
    {
69 1
        $nullTs = Timestamp::fromNative(null);
70 1
        $earlyTs = Timestamp::fromString(self::FIXED_TIMESTAMP_UTC);
71 1
        $lateTs = Timestamp::fromString(self::FIXED_LATE_TIMESTAMP_UTC);
72 1
        $this->assertFalse($nullTs->isAfter($earlyTs));
73 1
        $this->assertTrue($earlyTs->isAfter($nullTs));
74 1
        $this->assertFalse($earlyTs->isAfter($lateTs));
75 1
        $this->assertTrue($lateTs->isAfter($earlyTs));
76 1
    }
77
78 1
    public function testModify(): void
79
    {
80 1
        $addTs1 = $this->timestamp->modify('+1 day');
81 1
        $addTs2 = $this->timestamp->modify('+24 hours');
82 1
        $this->assertEquals(self::FIXED_LATE_TIMESTAMP_UTC, (string)$addTs1);
83 1
        $this->assertEquals(self::FIXED_LATE_TIMESTAMP_UTC, (string)$addTs2);
84
85 1
        $subTs1 = $this->timestamp->modify('-1 day - 10 minutes - 1 microsecond');
86 1
        $subTs2 = $this->timestamp->modify('-24 hours - 600 seconds - 1 microsecond');
87 1
        $this->assertEquals(self::FIXED_EARLY_TIMESTAMP_UTC, (string)$subTs1);
88 1
        $this->assertEquals(self::FIXED_EARLY_TIMESTAMP_UTC, (string)$subTs2);
89
90 1
        $this->expectWarning();
91 1
        $this->timestamp->modify('bogus');
92
    }
93
94 8
    protected function setUp(): void
95
    {
96 8
        $this->timestamp = Timestamp::fromNative(self::FIXED_TIMESTAMP_EUR);
97 8
    }
98
}
99