99use Tempest \DateTime \DateTime ;
1010use Tempest \DateTime \DateTimeInterface ;
1111use Tempest \DateTime \Duration ;
12- use Tempest \DateTime \Timestamp ;
1312
1413final class MockClock implements Clock
1514{
1615 private DateTimeInterface $ now ;
1716
1817 public function __construct (DateTimeImmutable |DateTimeInterface |string $ now = 'now ' )
1918 {
20- if ($ now instanceof DateTimeImmutable) {
21- $ this ->now = DateTime::fromTimestamp ($ now ->getTimestamp ());
22- } else {
23- $ this ->now = DateTime::parse ($ now );
24- }
19+ $ this ->setNow ($ now );
2520 }
2621
2722 public function toPsrClock (): ClockInterface
@@ -34,13 +29,12 @@ public function now(): DateTimeInterface
3429 return $ this ->now ;
3530 }
3631
37- public function setNow (DateTimeInterface |string $ now ): void
32+ /**
33+ * Globally sets the current time to the specified value.
34+ */
35+ public function setNow (DateTimeImmutable |DateTimeInterface |string $ now ): void
3836 {
39- if ($ now instanceof DateTimeInterface) {
40- $ this ->now = $ now ;
41- } else {
42- $ this ->now = DateTime::parse ($ now );
43- }
37+ $ this ->now = DateTime::parse ($ now );
4438 }
4539
4640 public function timestamp (): int
@@ -56,31 +50,35 @@ public function timestampMs(): int
5650 public function sleep (int |Duration $ milliseconds ): void
5751 {
5852 if ($ milliseconds instanceof Duration) {
59- $ this ->addInterval ($ milliseconds );
53+ $ this ->plus ($ milliseconds );
6054 return ;
6155 }
6256
6357 $ this ->now = $ this ->now ->plusMilliseconds ($ milliseconds );
6458 }
6559
66- public function addInterval (Duration $ duration ): void
60+ /**
61+ * Adds the given duration. Providing an integer value adds the corresponding seconds to the current time.
62+ */
63+ public function plus (int |Duration $ duration ): void
6764 {
68- $ this ->now = $ this ->now ->plus ($ duration );
69- }
65+ if (is_int ($ duration )) {
66+ $ duration = Duration::seconds ($ duration );
67+ }
7068
71- public function subInternal (Duration $ duration ): void
72- {
73- $ this ->now = $ this ->now ->minus ($ duration );
69+ $ this ->now = $ this ->now ->plus ($ duration );
7470 }
7571
76- public function changeTime (int $ seconds ): void
72+ /**
73+ * Removes the given duration. Providing an integer value removes the corresponding seconds to the current time.
74+ */
75+ public function minus (int |Duration $ duration ): void
7776 {
78- if ($ seconds < 0 ) {
79- $ seconds = abs ($ seconds );
80- $ this ->now = $ this ->now ->minusSeconds ($ seconds );
81- } else {
82- $ this ->now = $ this ->now ->plusSeconds ($ seconds );
77+ if (is_int ($ duration )) {
78+ $ duration = Duration::seconds ($ duration );
8379 }
80+
81+ $ this ->now = $ this ->now ->minus ($ duration );
8482 }
8583
8684 public function dd (): void
0 commit comments