Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Use wormhole during the apply phase #136

Merged
merged 6 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Lifecycle/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function validate(Container $container, Event $event, ?State $state = nul
public function apply(Container $container, Event $event, State $state): void
{
if ($this->runsInPhase(Phase::Apply)) {
$container->call($this->callback, $this->guessParameters($event, $state));
app(Wormhole::class)->warp($event, fn () => $container->call($this->callback, $this->guessParameters($event, $state)));
}
}

Expand All @@ -111,7 +111,7 @@ public function handle(Container $container, Event $event, StateCollection $stat
public function replay(Container $container, Event $event, StateCollection $states): void
{
if ($this->runsInPhase(Phase::Replay)) {
app(Wormhole::class)->replay($event, fn () => $container->call($this->callback, $this->guessParameters($event, states: $states)));
app(Wormhole::class)->warp($event, fn () => $container->call($this->callback, $this->guessParameters($event, states: $states)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Support/Wormhole.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function realImmutableNow(): CarbonImmutable
return $this->immutable_now ?? CarbonImmutable::instance(new DateTime());
}

public function replay(Event $event, Closure $callback)
public function warp(Event $event, Closure $callback)
{
if ($this->wormholeIsDisabled($event)) {
return $callback();
Expand Down
49 changes: 49 additions & 0 deletions tests/Feature/ReplayCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Carbon\CarbonImmutable;
use Thunk\Verbs\Attributes\Autodiscovery\StateId;
use Thunk\Verbs\Commands\ReplayCommand;
use Thunk\Verbs\Event;
Expand All @@ -11,6 +12,7 @@
beforeEach(function () {
$GLOBALS['replay_test_counts'] = [];
$GLOBALS['handle_count'] = 0;
$GLOBALS['times'] = [];
});

it('can replay events', function () {
Expand Down Expand Up @@ -61,6 +63,30 @@
->and($GLOBALS['handle_count'])->toBe(1337);
});

it('uses the original event times when replaying', function () {
\Illuminate\Support\Facades\Date::setTestNow('2024-04-01 12:00:00');
$state_id = Id::make();
ReplayCommandTestWormholeEvent::fire(state_id: $state_id);

Verbs::commit();

expect(app(StateManager::class)->load($state_id, ReplayCommandTestWormholeState::class)->time->unix())
->toBe(CarbonImmutable::parse('2024-04-01 12:00:00')->unix())
->and($GLOBALS['time'][$state_id]->unix())
->toBe(CarbonImmutable::parse('2024-04-01 12:00:00')->unix());

$GLOBALS['time'] = [];
\Illuminate\Support\Facades\Date::setTestNow('2024-05-15 18:00:00');

config(['app.env' => 'testing']);
$this->artisan(ReplayCommand::class);

expect(app(StateManager::class)->load($state_id, ReplayCommandTestWormholeState::class)->time->unix())
->toBe(CarbonImmutable::parse('2024-04-01 12:00:00')->unix())
->and($GLOBALS['time'][$state_id]->unix())
->toBe(CarbonImmutable::parse('2024-04-01 12:00:00')->unix());
});

class ReplayCommandTestEvent extends Event
{
public function __construct(
Expand Down Expand Up @@ -90,3 +116,26 @@ class ReplayCommandTestState extends State
{
public int $count = 0;
}

class ReplayCommandTestWormholeEvent extends Event
{
public function __construct(
#[StateId(ReplayCommandTestWormholeState::class)] public ?int $state_id = null
) {
}

public function apply(ReplayCommandTestWormholeState $state): void
{
$state->time = CarbonImmutable::now();
}

public function handle(): void
{
$GLOBALS['time'][$this->state_id] = $this->state(ReplayCommandTestWormholeState::class)->time;
}
}

class ReplayCommandTestWormholeState extends State
{
public CarbonImmutable $time;
}
4 changes: 2 additions & 2 deletions tests/Unit/WormholeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
};
app(MetadataManager::class)->setEphemeral($event, 'created_at', Date::parse('2023-01-02 00:00:00'));
app(Wormhole::class)->replay($event, function () use ($now) {
app(Wormhole::class)->warp($event, function () use ($now) {
expect(Carbon::now()->format('Y-m-d'))->toBe('2023-01-02')
->and(CarbonImmutable::now()->format('Y-m-d'))->toBe('2023-01-02')
->and(now()->format('Y-m-d'))->toBe('2023-01-02')
Expand All @@ -29,7 +29,7 @@
{
};
app(MetadataManager::class)->setEphemeral($event, 'created_at', Date::parse('2023-01-02 00:00:00'));
app(Wormhole::class)->replay($event, function () {
app(Wormhole::class)->warp($event, function () {
expect(Carbon::now()->format('Y-m-d'))->toBe('2023-01-02')
->and(CarbonImmutable::now()->format('Y-m-d'))->toBe('2023-01-02')
->and(now()->format('Y-m-d'))->toBe('2023-01-02')
Expand Down
Loading