Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Clock] Provide modify() in MockClock #48189

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

Merged
merged 1 commit into from
Nov 14, 2022
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
9 changes: 9 additions & 0 deletions 9 src/Symfony/Component/Clock/MockClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function sleep(float|int $seconds): void
$this->now = (new \DateTimeImmutable($now, $timezone))->setTimezone($timezone);
}

public function modify(string $modifier): void
{
if (false === $modifiedNow = @$this->now->modify($modifier)) {
throw new \InvalidArgumentException(sprintf('Invalid modifier: "%s". Could not modify MockClock.', $modifier));
}

$this->now = $modifiedNow;
}

public function withTimeZone(\DateTimeZone|string $timezone): static
{
$clone = clone $this;
Expand Down
53 changes: 53 additions & 0 deletions 53 src/Symfony/Component/Clock/Tests/MockClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,59 @@ public function testSleep()
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
}

public function provideValidModifyStrings(): iterable
{
yield 'absolute datetime value' => [
'2112-09-17 23:53:03.001',
'2112-09-17 23:53:03.001000',
];

yield 'relative modified date' => [
'+2 days',
'2112-09-19 23:53:00.999000',
];
}

/**
* @dataProvider provideValidModifyStrings
*/
public function testModifyWithSpecificDateTime(string $modifiedNow, string $expectedNow)
{
$clock = new MockClock((new \DateTimeImmutable('2112-09-17 23:53:00.999Z'))->setTimezone(new \DateTimeZone('UTC')));
$tz = $clock->now()->getTimezone()->getName();

$clock->modify($modifiedNow);

$this->assertSame($expectedNow, $clock->now()->format('Y-m-d H:i:s.u'));
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
}

public function provideInvalidModifyStrings(): iterable
{
yield 'Named holiday is not recognized' => [
'Halloween',
'Invalid modifier: "Halloween". Could not modify MockClock.',
];

yield 'empty string' => [
'',
'Invalid modifier: "". Could not modify MockClock.',
];
}

/**
* @dataProvider provideInvalidModifyStrings
*/
public function testModifyThrowsOnInvalidString(string $modifiedNow, string $expectedMessage)
{
$clock = new MockClock((new \DateTimeImmutable('2112-09-17 23:53:00.999Z'))->setTimezone(new \DateTimeZone('UTC')));

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($expectedMessage);

$clock->modify($modifiedNow);
}

public function testWithTimeZone()
{
$clock = new MockClock();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.