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

FrozenClockTest::testAdjustToChangesTheObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 9
c 2
b 0
f 1
dl 0
loc 15
rs 9.9666
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;
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
        $clock->adjustTo('+7 days');
37
38
        self::assertNotEquals($oldNow, $clock->now());
39
        self::assertEquals($newNow, $clock->now());
40
41
        $clock->adjustTo('-7 days');
42
43
        self::assertEquals($oldNow, $clock->now());
44
        self::assertNotEquals($newNow, $clock->now());
45
    }
46
47
    #[RequiresPhp('8.3')]
48
    public function testAdjustToThrowsForInvalidModifier(): void
49
    {
50
        $clock = FrozenClock::fromUtc();
51
52
        $this->expectException(DateMalformedStringException::class);
53
        $clock->adjustTo('notValid');
54
    }
55
56
    public function testClockAsStringShouldReturnStringMatchingFormat(): void
57
    {
58
        $clock = FrozenClock::fromUtc();
59
60
        self::assertStringMatchesFormat('[FrozenClock(): unixtime: %s; iso8601: %s;]', (string) $clock);
61
    }
62
63
    public function testFromUtcCreatesClockFrozenAtCurrentSystemTimeInUtc(): void
64
    {
65
        $clock = FrozenClock::fromUtc();
66
        $now   = $clock->now();
67
68
        self::assertSame('UTC', $now->getTimezone()->getName());
69
    }
70
71
    public function testNowShouldAlwaysReturnTheSameObject(): void
72
    {
73
        $now   = new DateTimeImmutable();
74
        $clock = new FrozenClock($now);
75
76
        self::assertSame($now, $clock->now());
77
        self::assertSame($now, $clock->now());
78
    }
79
80
    public function testSetToChangesTheObject(): void
81
    {
82
        $oldNow = new DateTimeImmutable();
83
        $clock  = new FrozenClock($oldNow);
84
85
        $newNow = new DateTimeImmutable();
86
        $clock->setTo($newNow);
87
88
        self::assertNotSame($oldNow, $clock->now());
89
        self::assertSame($newNow, $clock->now());
90
    }
91
}
92