Passed
Push — master ( b9f35a...680a43 )
by Eric
02:22
created

testConstructUsesUtcIfPassedEmptyStringTimezone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
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
23
use PHPUnit\Framework\Attributes\CoversClass;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use PHPUnit\Framework\Attributes\UsesClass;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\UsesClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use PHPUnit\Framework\TestCase;
26
27
use function date_default_timezone_get;
28
29
/**
30
 * @internal
31
 */
32
#[CoversClass(SystemClock::class)]
33
#[UsesClass(FrozenClock::class)]
34
final class SystemClockTest extends TestCase
35
{
36
    public function testClockAsStringShouldReturnStringMatchingFormat(): void
37
    {
38
        $timezone = new DateTimeZone('America/New_York');
39
        $clock    = new SystemClock($timezone);
40
41
        self::assertStringMatchesFormat('[SystemClock("%s"): unixtime: %s; iso8601: %s;]', (string) $clock);
42
    }
43
44
    public function testConstructUsesUtcIfPassedEmptyStringTimezone(): void
45
    {
46
        $clock = new SystemClock('');
47
        $now   = $clock->now();
48
49
        self::assertSame('UTC', $now->getTimezone()->getName());
50
    }
51
52
    public function testConstructUsesUtcIfPassedNullTimezone(): void
53
    {
54
        $clock = new SystemClock(null);
55
        $now   = $clock->now();
56
57
        self::assertSame('UTC', $now->getTimezone()->getName());
58
    }
59
60
    public function testConstructWithTimezoneString(): void
61
    {
62
        $clock = new SystemClock('America/New_York');
63
        $now   = $clock->now();
64
65
        self::assertSame('America/New_York', $now->getTimezone()->getName());
66
    }
67
68
    public function testDoesThrowException(): void
69
    {
70
        $this->expectException(DateInvalidTimeZoneException::class);
71
        new SystemClock('invalid/zone');
72
    }
73
74
    public function testFreezeReturnsFrozenClockAndReturnsSameObject(): void
75
    {
76
        $timezone = new DateTimeZone('America/New_York');
77
        $clock    = new SystemClock($timezone);
78
79
        $before      = new DateTimeImmutable('now', $timezone);
80
        $frozenClock = $clock->freeze();
81
        $after       = new DateTimeImmutable('now', $timezone);
82
83
        // @phpstan-ignore-next-line
84
        self::assertInstanceOf(FrozenClock::class, $frozenClock);
85
86
        $now = $frozenClock->now();
87
88
        self::assertGreaterThanOrEqual($before, $now);
89
        self::assertLessThanOrEqual($after, $now);
90
91
        self::assertSame($now, $frozenClock->now());
92
    }
93
94
    public function testFromSystemTimezoneCreatesAnInstanceUsingTheDefaultTimezoneInSystem(): void
95
    {
96
        $clock = SystemClock::fromSystemTimezone();
97
        $now   = $clock->now();
98
99
        self::assertSame(date_default_timezone_get(), $now->getTimezone()->getName());
100
    }
101
102
    public function testFromUtcCreatesAnInstanceUsingUtcAsTimezone(): void
103
    {
104
        $clock = SystemClock::fromUtc();
105
        $now   = $clock->now();
106
107
        self::assertSame('UTC', $now->getTimezone()->getName());
108
    }
109
110
    public function testNowShouldRespectTheProvidedTimezone(): void
111
    {
112
        $timezone = new DateTimeZone('America/New_York');
113
        $clock    = new SystemClock($timezone);
114
115
        $before = new DateTimeImmutable('now', $timezone);
116
        $now    = $clock->now();
117
        $after  = new DateTimeImmutable('now', $timezone);
118
119
        self::assertEquals($timezone, $now->getTimezone());
120
        self::assertSame('America/New_York', $now->getTimezone()->getName());
121
        self::assertGreaterThanOrEqual($before, $now);
122
        self::assertLessThanOrEqual($after, $now);
123
    }
124
}
125