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 449c90d

Browse filesBrowse files
author
Denis Brumann
committed
Improvement: Provide setTo() in MockClock
1 parent 4618856 commit 449c90d
Copy full SHA for 449c90d

File tree

3 files changed

+34
-0
lines changed
Filter options

3 files changed

+34
-0
lines changed

‎src/Symfony/Component/Clock/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Clock/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `MockClock::setTo()`
8+
49
6.2
510
---
611

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

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

50+
public function setTo(\DateTimeImmutable $now): void
51+
{
52+
$this->now = $now;
53+
}
54+
5055
public function withTimeZone(\DateTimeZone|string $timezone): static
5156
{
5257
$clone = clone $this;

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

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

66+
public function testSetTo()
67+
{
68+
$clock = new MockClock((new \DateTimeImmutable('2112-09-17 23:53:00.999Z'))->setTimezone(new \DateTimeZone('UTC')));
69+
70+
$clock->setTo((new \DateTimeImmutable('2112-09-17 23:53:03.001Z'))->setTimezone(new \DateTimeZone('UTC')));
71+
$tz = $clock->now()->getTimezone()->getName();
72+
73+
$this->assertSame('2112-09-17 23:53:03.001000', $clock->now()->format('Y-m-d H:i:s.u'));
74+
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
75+
}
76+
77+
public function testSetToWithDifferentTimezone()
78+
{
79+
$clock = new MockClock((new \DateTimeImmutable('2112-09-17 23:53:00.999Z'))->setTimezone(new \DateTimeZone('UTC')));
80+
$previousTz = $clock->now()->getTimezone()->getName();
81+
82+
$clock->setTo((new \DateTimeImmutable('2112-09-17 23:53:03.001Z'))->setTimezone(new \DateTimeZone('Africa/Johannesburg')));
83+
$newTz = $clock->now()->getTimezone()->getName();
84+
85+
$this->assertSame('2112-09-17 23:53:03.001000', $clock->now()->format('Y-m-d H:i:s.u'));
86+
$this->assertNotSame($previousTz, $newTz);
87+
$this->assertSame($newTz, $clock->now()->getTimezone()->getName());
88+
}
89+
6690
public function testWithTimeZone()
6791
{
6892
$clock = new MockClock();

0 commit comments

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