Skip to content

Commit 449c90d

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

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Symfony/Component/Clock/CHANGELOG.md

Lines 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

Lines 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

Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy