|
4 | 4 |
|
5 | 5 | namespace Ecotone\Lite\Test; |
6 | 6 |
|
| 7 | +use DateTimeImmutable; |
7 | 8 | use DateTimeInterface; |
8 | 9 | use Ecotone\EventSourcing\EventStore; |
9 | 10 | use Ecotone\EventSourcing\ProjectionManager; |
|
18 | 19 | use Ecotone\Messaging\MessagingException; |
19 | 20 | use Ecotone\Messaging\PollableChannel; |
20 | 21 | use Ecotone\Messaging\Scheduling\Clock; |
| 22 | +use Ecotone\Messaging\Scheduling\Duration; |
21 | 23 | use Ecotone\Messaging\Scheduling\EcotoneClockInterface; |
22 | 24 | use Ecotone\Messaging\Scheduling\TimeSpan; |
23 | 25 | use Ecotone\Messaging\Support\Assert; |
|
32 | 34 | use Ecotone\Modelling\EventBus; |
33 | 35 | use Ecotone\Modelling\QueryBus; |
34 | 36 | use Ecotone\Projecting\ProjectionRegistry; |
| 37 | +use Ecotone\Test\StaticPsrClock; |
| 38 | +use InvalidArgumentException; |
35 | 39 |
|
36 | 40 | /** |
37 | 41 | * @template T |
@@ -191,23 +195,65 @@ public function getEventStreamEvents(string $streamName): array |
191 | 195 | return $this->getGateway(EventStore::class)->load($streamName); |
192 | 196 | } |
193 | 197 |
|
194 | | - public function waitTill(TimeSpan|DateTimeInterface $time): self |
| 198 | + public function changeTimeTo(DateTimeImmutable $time): self |
195 | 199 | { |
196 | | - if ($time instanceof DateTimeInterface) { |
197 | | - if ($time < $this->clock->now()) { |
198 | | - throw new MessagingException("Time to wait is in the past. Now: {$this->clock->now()}, time to wait: {$time}"); |
199 | | - } |
| 200 | + $psrClock = $this->getStaticPsrClockFromContainer(); |
| 201 | + |
| 202 | + if ($psrClock->hasBeenChanged() && $time <= $psrClock->now()) { |
| 203 | + throw new InvalidArgumentException( |
| 204 | + \sprintf( |
| 205 | + 'Cannot move time backwards. Current clock time: %s, requested time: %s', |
| 206 | + $psrClock->now()->format('Y-m-d H:i:s.u'), |
| 207 | + $time->format('Y-m-d H:i:s.u') |
| 208 | + ) |
| 209 | + ); |
200 | 210 | } |
201 | 211 |
|
202 | | - $this->clock->sleep( |
203 | | - $time instanceof TimeSpan |
204 | | - ? $time->toDuration() |
205 | | - : TimeSpan::fromDateInterval($time->diff($this->clock->now()))->toDuration() |
| 212 | + $psrClock->setCurrentTime($time); |
| 213 | + |
| 214 | + return $this; |
| 215 | + } |
| 216 | + |
| 217 | + public function advanceTimeTo(Duration $duration): self |
| 218 | + { |
| 219 | + $psrClock = $this->getStaticPsrClockFromContainer(); |
| 220 | + $psrClock->setCurrentTime( |
| 221 | + DateTimeImmutable::createFromInterface($psrClock->now())->modify("+{$duration->inMicroseconds()} microseconds") |
206 | 222 | ); |
207 | 223 |
|
208 | 224 | return $this; |
209 | 225 | } |
210 | 226 |
|
| 227 | + private function getStaticPsrClockFromContainer(): StaticPsrClock |
| 228 | + { |
| 229 | + try { |
| 230 | + /** @var Clock $clock */ |
| 231 | + $clock = $this->configuredMessagingSystem->getServiceFromContainer(EcotoneClockInterface::class); |
| 232 | + } catch (\Throwable) { |
| 233 | + throw new InvalidArgumentException( |
| 234 | + 'Changing time is only possible when using StaticPsrClock as the ClockInterface. ' . |
| 235 | + 'Register ClockInterface::class => new StaticPsrClock() in your container services.' |
| 236 | + ); |
| 237 | + } |
| 238 | + |
| 239 | + if (! $clock instanceof Clock) { |
| 240 | + throw new InvalidArgumentException( |
| 241 | + 'Changing time is only possible when using Clock as the EcotoneClockInterface. ' . |
| 242 | + 'Register EcotoneClockInterface::class => new Clock(new StaticPsrClock()) in your container services.' |
| 243 | + ); |
| 244 | + } |
| 245 | + |
| 246 | + $clock = $clock->internalClock(); |
| 247 | + if (! $clock instanceof StaticPsrClock) { |
| 248 | + throw new InvalidArgumentException( |
| 249 | + 'Changing time is only possible when using StaticPsrClock as the ClockInterface. ' . |
| 250 | + 'Register ClockInterface::class => new StaticPsrClock() in your container services.' |
| 251 | + ); |
| 252 | + } |
| 253 | + |
| 254 | + return $clock; |
| 255 | + } |
| 256 | + |
211 | 257 | /** |
212 | 258 | * @param Event[]|object[]|array[] $events |
213 | 259 | */ |
|
0 commit comments