File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed
src/Symfony/Component/Clock Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1
1
CHANGELOG
2
2
=========
3
3
4
+ 6.3
5
+ ---
6
+
7
+ * Add ` MockClock::setTo() `
8
+
4
9
6.2
5
10
---
6
11
Original file line number Diff line number Diff line change @@ -47,6 +47,11 @@ public function sleep(float|int $seconds): void
47
47
$ this ->now = (new \DateTimeImmutable ($ now , $ timezone ))->setTimezone ($ timezone );
48
48
}
49
49
50
+ public function setTo (\DateTimeImmutable $ now ): void
51
+ {
52
+ $ this ->now = $ now ;
53
+ }
54
+
50
55
public function withTimeZone (\DateTimeZone |string $ timezone ): static
51
56
{
52
57
$ clone = clone $ this ;
Original file line number Diff line number Diff line change @@ -63,6 +63,30 @@ public function testSleep()
63
63
$ this ->assertSame ($ tz , $ clock ->now ()->getTimezone ()->getName ());
64
64
}
65
65
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
+
66
90
public function testWithTimeZone ()
67
91
{
68
92
$ clock = new MockClock ();
You can’t perform that action at this time.
0 commit comments