Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adjustTime to FrozenClock #689

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@ parameters:
paths:
- src
- test

ignoreErrors:
# This is only for PHP 8.2 compatibility
-
message: "#^PHPDoc tag @throws with type DateMalformedStringException\\|InvalidArgumentException is not subtype of Throwable$#"
count: 1
path: src/FrozenClock.php
-
message: "#^Class DateMalformedStringException not found\\.$#"
count: 1
path: test/FrozenClockTest.php
-
message: "#^Parameter \\#1 \\$exception of method PHPUnit\\\\Framework\\\\TestCase\\:\\:expectException\\(\\) expects class\\-string\\<Throwable\\>, string given\\.$#"
count: 1
path: test/FrozenClockTest.php
22 changes: 22 additions & 0 deletions src/FrozenClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace Lcobucci\Clock;

use DateMalformedStringException;
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;

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

/**
* Adjusts the current time by a given modifier.
*
* @param string $modifier @see https://www.php.net/manual/en/datetime.formats.php
*
* @throws InvalidArgumentException When an invalid format string is passed (PHP < 8.3).
* @throws DateMalformedStringException When an invalid date/time string is passed (PHP 8.3+).
*/
public function adjustTime(string $modifier): void
{
$modifiedTime = @$this->now->modify($modifier);

// PHP < 8.3 won't throw exceptions on invalid modifiers
if ($modifiedTime === false) {
throw new InvalidArgumentException('The given modifier is invalid');
}

$this->now = $this->now->modify($modifier);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that I didn't update this 🤦
Silly me...

}

public function now(): DateTimeImmutable
{
return $this->now;
Expand Down
42 changes: 42 additions & 0 deletions test/FrozenClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

namespace Lcobucci\Clock;

use DateMalformedStringException;
use DateTimeImmutable;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

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

#[Test]
public function adjustTimeChangesTheObject(): void
{
$oldNow = new DateTimeImmutable();
$newNow = $oldNow->modify('+1 day');

$clock = new FrozenClock($oldNow);

$clock->adjustTime('+1 day');

self::assertNotEquals($oldNow, $clock->now());
self::assertEquals($newNow, $clock->now());

$clock->adjustTime('-1 day');

self::assertEquals($oldNow, $clock->now());
self::assertNotEquals($newNow, $clock->now());
}

#[Test]
#[RequiresPhp('< 8.3.0')]
public function adjustTimeThrowsForInvalidModifierInPhp82(): void
{
$clock = FrozenClock::fromUTC();

$this->expectException(InvalidArgumentException::class);
$clock->adjustTime('invalid');
}

#[Test]
#[RequiresPhp('>= 8.3.0')]
public function adjustTimeThrowsForInvalidModifier(): void
{
$clock = FrozenClock::fromUTC();

$this->expectException(DateMalformedStringException::class);
$clock->adjustTime('invalid');
}

#[Test]
public function fromUTCCreatesClockFrozenAtCurrentSystemTimeInUTC(): void
{
Expand Down
Loading