Passed
Push — master ( 334365...ef9772 )
by Eric
14:05 queued 02:14
created

testFromUtcCreatesClockFrozenAtCurrentSystemTimeInUtc()   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 DateMalformedStringException;
18
use DateTimeImmutable;
19
use Esi\Clock\FrozenClock;
20
use PHPUnit\Framework\Attributes\CoversClass;
21
use PHPUnit\Framework\Attributes\RequiresPhp;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\RequiresPhp 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...
22
use PHPUnit\Framework\TestCase;
23
24
/**
25
 * @internal
26
 */
27
#[CoversClass(FrozenClock::class)]
28
final class FrozenClockTest extends TestCase
29
{
30
    public function testAdjustToChangesTheObject(): void
31
    {
32
        $oldNow = new DateTimeImmutable();
33
        $newNow = $oldNow->modify('+7 days');
34
35
        $clock = new FrozenClock($oldNow);
36
37
        $clock->adjustTo('+7 days');
38
39
        self::assertNotEquals($oldNow, $clock->now());
40
        self::assertEquals($newNow, $clock->now());
41
42
        $clock->adjustTo('-7 days');
43
44
        self::assertEquals($oldNow, $clock->now());
45
        self::assertNotEquals($newNow, $clock->now());
46
    }
47
48
    #[RequiresPhp('8.3')]
49
    public function testAdjustToThrowsForInvalidModifier(): void
50
    {
51
        $clock = FrozenClock::fromUtc();
52
53
        $this->expectException(DateMalformedStringException::class);
54
        $clock->adjustTo('notValid');
55
    }
56
57
    public function testClockAsStringShouldReturnStringMatchingFormat(): void
58
    {
59
        $clock = FrozenClock::fromUtc();
60
61
        self::assertStringMatchesFormat('[FrozenClock(): unixtime: %s; iso8601: %s;]', (string) $clock);
62
    }
63
64
    public function testFromUtcCreatesClockFrozenAtCurrentSystemTimeInUtc(): void
65
    {
66
        $clock = FrozenClock::fromUtc();
67
        $now   = $clock->now();
68
69
        self::assertSame('UTC', $now->getTimezone()->getName());
70
    }
71
72
    public function testNowShouldAlwaysReturnTheSameObject(): void
73
    {
74
        $now   = new DateTimeImmutable();
75
        $clock = new FrozenClock($now);
76
77
        self::assertSame($now, $clock->now());
78
        self::assertSame($now, $clock->now());
79
    }
80
81
    public function testSetToChangesTheObject(): void
82
    {
83
        $oldNow = new DateTimeImmutable();
84
        $clock  = new FrozenClock($oldNow);
85
86
        $newNow = new DateTimeImmutable();
87
        $clock->setTo($newNow);
88
89
        self::assertNotSame($oldNow, $clock->now());
90
        self::assertSame($newNow, $clock->now());
91
    }
92
}
93