@@ -17,13 +17,7 @@ final class MockClock implements Clock
1717
1818 public function __construct (DateTimeImmutable |DateTimeInterface |string $ now = 'now ' )
1919 {
20- if ($ now instanceof DateTimeImmutable) {
21- $ this ->now = DateTime::fromTimestamp (
22- Timestamp::fromParts ($ now ->getTimestamp ()),
23- );
24- } else {
25- $ this ->now = DateTime::parse ($ now );
26- }
20+ $ this ->setNow ($ now );
2721 }
2822
2923 public function toPsrClock (): ClockInterface
@@ -36,53 +30,61 @@ public function now(): DateTimeInterface
3630 return $ this ->now ;
3731 }
3832
39- public function setNow (DateTimeInterface |string $ now ): void
33+ /**
34+ * Globally sets the current time to the specified value.
35+ */
36+ public function setNow (DateTimeImmutable |DateTimeInterface |string $ now ): void
4037 {
41- if ($ now instanceof DateTimeInterface) {
42- $ this ->now = $ now ;
43- } else {
44- $ this ->now = DateTime::parse ($ now );
45- }
38+ $ this ->now = DateTime::parse ($ now );
4639 }
4740
48- public function timestamp (): int
41+ public function timestamp (): Timestamp
42+ {
43+ return $ this ->now ->getTimestamp ();
44+ }
45+
46+ public function seconds (): int
4947 {
5048 return $ this ->now ->getTimestamp ()->getSeconds ();
5149 }
5250
53- public function timestampMs (): int
51+ public function milliseconds (): int
5452 {
5553 return $ this ->now ->getTimestamp ()->getMilliseconds ();
5654 }
5755
5856 public function sleep (int |Duration $ milliseconds ): void
5957 {
6058 if ($ milliseconds instanceof Duration) {
61- $ this ->addInterval ($ milliseconds );
59+ $ this ->plus ($ milliseconds );
6260 return ;
6361 }
6462
6563 $ this ->now = $ this ->now ->plusMilliseconds ($ milliseconds );
6664 }
6765
68- public function addInterval (Duration $ duration ): void
66+ /**
67+ * Adds the given duration. Providing an integer value adds the corresponding seconds to the current time.
68+ */
69+ public function plus (int |Duration $ duration ): void
6970 {
70- $ this ->now = $ this ->now ->plus ($ duration );
71- }
71+ if (is_int ($ duration )) {
72+ $ duration = Duration::seconds ($ duration );
73+ }
7274
73- public function subInternal (Duration $ duration ): void
74- {
75- $ this ->now = $ this ->now ->minus ($ duration );
75+ $ this ->now = $ this ->now ->plus ($ duration );
7676 }
7777
78- public function changeTime (int $ seconds ): void
78+ /**
79+ * Removes the given duration. Providing an integer value removes the corresponding seconds to the current time.
80+ */
81+ public function minus (int |Duration $ duration ): void
7982 {
80- if ($ seconds < 0 ) {
81- $ seconds = abs ($ seconds );
82- $ this ->now = $ this ->now ->minusSeconds ($ seconds );
83- } else {
84- $ this ->now = $ this ->now ->plusSeconds ($ seconds );
83+ if (is_int ($ duration )) {
84+ $ duration = Duration::seconds ($ duration );
8585 }
86+
87+ $ this ->now = $ this ->now ->minus ($ duration );
8688 }
8789
8890 public function dd (): void
0 commit comments