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

Commit ea442bd

Browse filesBrowse files
author
Denis Brumann
committed
[Clock] Provide modify() in MockClock
1 parent 4618856 commit ea442bd
Copy full SHA for ea442bd

File tree

2 files changed

+62
-0
lines changed
Filter options

2 files changed

+62
-0
lines changed

‎src/Symfony/Component/Clock/MockClock.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Clock/MockClock.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public function sleep(float|int $seconds): void
4747
$this->now = (new \DateTimeImmutable($now, $timezone))->setTimezone($timezone);
4848
}
4949

50+
public function modify(string $modifier): void
51+
{
52+
if (false === $modifiedNow = @$this->now->modify($modifier)) {
53+
throw new \InvalidArgumentException(sprintf('Invalid modifier: "%s". Could not modify MockClock.', $modifier));
54+
}
55+
56+
$this->now = $modifiedNow;
57+
}
58+
5059
public function withTimeZone(\DateTimeZone|string $timezone): static
5160
{
5261
$clone = clone $this;

‎src/Symfony/Component/Clock/Tests/MockClockTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Clock/Tests/MockClockTest.php
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,59 @@ public function testSleep()
6363
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
6464
}
6565

66+
public function provideValidModifyStrings(): iterable
67+
{
68+
yield 'absolute datetime value' => [
69+
'2112-09-17 23:53:03.001',
70+
'2112-09-17 23:53:03.001000',
71+
];
72+
73+
yield 'relative modified date' => [
74+
'+2 days',
75+
'2112-09-19 23:53:00.999000',
76+
];
77+
}
78+
79+
/**
80+
* @dataProvider provideValidModifyStrings
81+
*/
82+
public function testModifyWithSpecificDateTime(string $modifiedNow, string $expectedNow)
83+
{
84+
$clock = new MockClock((new \DateTimeImmutable('2112-09-17 23:53:00.999Z'))->setTimezone(new \DateTimeZone('UTC')));
85+
$tz = $clock->now()->getTimezone()->getName();
86+
87+
$clock->modify($modifiedNow);
88+
89+
$this->assertSame($expectedNow, $clock->now()->format('Y-m-d H:i:s.u'));
90+
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
91+
}
92+
93+
public function provideInvalidModifyStrings(): iterable
94+
{
95+
yield 'Named holiday is not recognized' => [
96+
'Halloween',
97+
'Invalid format: "Halloween". Could not modify MockClock.',
98+
];
99+
100+
yield 'empty string' => [
101+
'',
102+
'Invalid format: "". Could not modify MockClock.',
103+
];
104+
}
105+
106+
/**
107+
* @dataProvider provideInvalidModifyStrings
108+
*/
109+
public function testModifyThrowsOnInvalidString(string $modifiedNow, string $expectedMessage)
110+
{
111+
$clock = new MockClock((new \DateTimeImmutable('2112-09-17 23:53:00.999Z'))->setTimezone(new \DateTimeZone('UTC')));
112+
113+
$this->expectException(\InvalidArgumentException::class);
114+
$this->expectExceptionMessage($expectedMessage);
115+
116+
$clock->modify($modifiedNow);
117+
}
118+
66119
public function testWithTimeZone()
67120
{
68121
$clock = new MockClock();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.