Skip to content

Commit 049ecee

Browse files
authored
Merge pull request #689 from lgrossi/lucas/minor-frozen-clock-helpers
Add adjustTime to FrozenClock
2 parents 0c4702c + 47cb7d3 commit 049ecee

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

phpstan.neon.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,18 @@ parameters:
33
paths:
44
- src
55
- test
6+
7+
ignoreErrors:
8+
# This is only for PHP 8.2 compatibility
9+
-
10+
message: "#^PHPDoc tag @throws with type DateMalformedStringException\\|InvalidArgumentException is not subtype of Throwable$#"
11+
count: 1
12+
path: src/FrozenClock.php
13+
-
14+
message: "#^Class DateMalformedStringException not found\\.$#"
15+
count: 1
16+
path: test/FrozenClockTest.php
17+
-
18+
message: "#^Parameter \\#1 \\$exception of method PHPUnit\\\\Framework\\\\TestCase\\:\\:expectException\\(\\) expects class\\-string\\<Throwable\\>, string given\\.$#"
19+
count: 1
20+
path: test/FrozenClockTest.php

src/FrozenClock.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
namespace Lcobucci\Clock;
55

6+
use DateMalformedStringException;
67
use DateTimeImmutable;
78
use DateTimeZone;
9+
use InvalidArgumentException;
810

911
final class FrozenClock implements Clock
1012
{
@@ -22,6 +24,26 @@ public function setTo(DateTimeImmutable $now): void
2224
$this->now = $now;
2325
}
2426

27+
/**
28+
* Adjusts the current time by a given modifier.
29+
*
30+
* @param string $modifier @see https://www.php.net/manual/en/datetime.formats.php
31+
*
32+
* @throws InvalidArgumentException When an invalid format string is passed (PHP < 8.3).
33+
* @throws DateMalformedStringException When an invalid date/time string is passed (PHP 8.3+).
34+
*/
35+
public function adjustTime(string $modifier): void
36+
{
37+
$modifiedTime = @$this->now->modify($modifier);
38+
39+
// PHP < 8.3 won't throw exceptions on invalid modifiers
40+
if ($modifiedTime === false) {
41+
throw new InvalidArgumentException('The given modifier is invalid');
42+
}
43+
44+
$this->now = $this->now->modify($modifier);
45+
}
46+
2547
public function now(): DateTimeImmutable
2648
{
2749
return $this->now;

test/FrozenClockTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
namespace Lcobucci\Clock;
55

6+
use DateMalformedStringException;
67
use DateTimeImmutable;
8+
use InvalidArgumentException;
79
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\Attributes\RequiresPhp;
811
use PHPUnit\Framework\Attributes\Test;
912
use PHPUnit\Framework\TestCase;
1013

@@ -34,6 +37,45 @@ public function nowSetChangesTheObject(): void
3437
self::assertSame($newNow, $clock->now());
3538
}
3639

40+
#[Test]
41+
public function adjustTimeChangesTheObject(): void
42+
{
43+
$oldNow = new DateTimeImmutable();
44+
$newNow = $oldNow->modify('+1 day');
45+
46+
$clock = new FrozenClock($oldNow);
47+
48+
$clock->adjustTime('+1 day');
49+
50+
self::assertNotEquals($oldNow, $clock->now());
51+
self::assertEquals($newNow, $clock->now());
52+
53+
$clock->adjustTime('-1 day');
54+
55+
self::assertEquals($oldNow, $clock->now());
56+
self::assertNotEquals($newNow, $clock->now());
57+
}
58+
59+
#[Test]
60+
#[RequiresPhp('< 8.3.0')]
61+
public function adjustTimeThrowsForInvalidModifierInPhp82(): void
62+
{
63+
$clock = FrozenClock::fromUTC();
64+
65+
$this->expectException(InvalidArgumentException::class);
66+
$clock->adjustTime('invalid');
67+
}
68+
69+
#[Test]
70+
#[RequiresPhp('>= 8.3.0')]
71+
public function adjustTimeThrowsForInvalidModifier(): void
72+
{
73+
$clock = FrozenClock::fromUTC();
74+
75+
$this->expectException(DateMalformedStringException::class);
76+
$clock->adjustTime('invalid');
77+
}
78+
3779
#[Test]
3880
public function fromUTCCreatesClockFrozenAtCurrentSystemTimeInUTC(): void
3981
{

0 commit comments

Comments
 (0)