Passed
Pull Request — master (#97)
by
unknown
12:55
created

SystemClockTest::testConstructUsesUtcIfPassedNullTimezone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\Clock.
7
 *
8
 * (c) Eric Sizemore <[email protected]>
9
 *
10
 * This source file is subject to the MIT license. For the full copyright and
11
 * license information, please view the LICENSE file that was distributed with
12
 * this source code.
13
 */
14
15
namespace Esi\Clock\Tests;
16
17
use DateInvalidTimeZoneException;
18
use DateTimeImmutable;
19
use DateTimeZone;
20
use Esi\Clock\FrozenClock;
21
use Esi\Clock\SystemClock;
22
use PHPUnit\Framework\Attributes\CoversClass;
23
use PHPUnit\Framework\Attributes\UsesClass;
24
use PHPUnit\Framework\TestCase;
25
26
use function date_default_timezone_get;
27
28
/**
29
 * @internal
30
 */
31
#[CoversClass(SystemClock::class)]
32
#[UsesClass(FrozenClock::class)]
33
final class SystemClockTest extends TestCase
34
{
35
    public function testClockAsStringShouldReturnStringMatchingFormat(): void
36
    {
37
        $timezone = new DateTimeZone('America/New_York');
38
        $clock    = new SystemClock($timezone);
39
40
        self::assertStringMatchesFormat('[SystemClock("%s"): unixtime: %s; iso8601: %s;]', (string) $clock);
41
    }
42
43
    public function testConstructUsesUtcIfPassedEmptyStringTimezone(): void
44
    {
45
        $clock = new SystemClock('');
46
        $now   = $clock->now();
47
48
        self::assertSame('UTC', $now->getTimezone()->getName());
49
    }
50
51
    public function testConstructUsesUtcIfPassedNullTimezone(): void
52
    {
53
        $clock = new SystemClock(null);
54
        $now   = $clock->now();
55
56
        self::assertSame('UTC', $now->getTimezone()->getName());
57
    }
58
59
    public function testConstructWithTimezoneString(): void
60
    {
61
        $clock = new SystemClock('America/New_York');
62
        $now   = $clock->now();
63
64
        self::assertSame('America/New_York', $now->getTimezone()->getName());
65
    }
66
67
    public function testDoesThrowException(): void
68
    {
69
        $this->expectException(DateInvalidTimeZoneException::class);
70
        new SystemClock('invalid/zone');
71
    }
72
73
    public function testFreezeReturnsFrozenClockAndReturnsSameObject(): void
74
    {
75
        $timezone = new DateTimeZone('America/New_York');
76
        $clock    = new SystemClock($timezone);
77
78
        $before      = new DateTimeImmutable('now', $timezone);
79
        $frozenClock = $clock->freeze();
80
        $after       = new DateTimeImmutable('now', $timezone);
81
82
        // @phpstan-ignore-next-line
83
        self::assertInstanceOf(FrozenClock::class, $frozenClock);
84
85
        $now = $frozenClock->now();
86
87
        self::assertGreaterThanOrEqual($before, $now);
88
        self::assertLessThanOrEqual($after, $now);
89
90
        self::assertSame($now, $frozenClock->now());
91
    }
92
93
    public function testFromSystemTimezoneCreatesAnInstanceUsingTheDefaultTimezoneInSystem(): void
94
    {
95
        $clock = SystemClock::fromSystemTimezone();
96
        $now   = $clock->now();
97
98
        self::assertSame(date_default_timezone_get(), $now->getTimezone()->getName());
99
    }
100
101
    public function testFromUtcCreatesAnInstanceUsingUtcAsTimezone(): void
102
    {
103
        $clock = SystemClock::fromUtc();
104
        $now   = $clock->now();
105
106
        self::assertSame('UTC', $now->getTimezone()->getName());
107
    }
108
109
    public function testNowShouldRespectTheProvidedTimezone(): void
110
    {
111
        $timezone = new DateTimeZone('America/New_York');
112
        $clock    = new SystemClock($timezone);
113
114
        $before = new DateTimeImmutable('now', $timezone);
115
        $now    = $clock->now();
116
        $after  = new DateTimeImmutable('now', $timezone);
117
118
        self::assertEquals($timezone, $now->getTimezone());
119
        self::assertSame('America/New_York', $now->getTimezone()->getName());
120
        self::assertGreaterThanOrEqual($before, $now);
121
        self::assertLessThanOrEqual($after, $now);
122
    }
123
}
124