Skip to content

Commit

Permalink
Merge pull request #410 from avosalmon/replay-custom-model
Browse files Browse the repository at this point in the history
Replay events with custom stored event model
  • Loading branch information
sebastiandedeyne authored Jul 26, 2023
2 parents d0645c1 + 59f9106 commit 9decc6c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Console/ReplayCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public function handle(Projectionist $projectionist): void
return;
}

if ($model = $this->option('stored-event-model')) {
if (! class_exists($model)) {
throw new Exception("Model {$model} not found. Make sure the model namespace is correct.");
}

config(['event-sourcing.stored_event_model' => $model]);
}

$this->replay($projectors, (int)$this->option('from'), $this->option('aggregate-uuid'));
}

Expand Down
19 changes: 13 additions & 6 deletions tests/Console/ReplayCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\Projectors\AccountProjector;
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneyAddedEvent;
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneySubtractedEvent;
use Spatie\EventSourcing\Tests\TestClasses\FakeUuid;
use Spatie\EventSourcing\Tests\TestClasses\Mailables\AccountBroke;
use Spatie\EventSourcing\Tests\TestClasses\Models\Account;
use Spatie\EventSourcing\Tests\TestClasses\Models\OtherEloquentStoredEvent;
Expand Down Expand Up @@ -106,18 +107,24 @@
});

it('will replay events from a specific store', function () {
$account = AccountAggregateRootWithStoredEventRepositorySpecified::create();
$uuid = FakeUuid::generate();

foreach (range(1, 5) as $i) {
event(new MoneyAddedEvent($account, 2000));
}
AccountAggregateRootWithStoredEventRepositorySpecified::retrieve($uuid)
->addMoney(2000)
->persist();
};

OtherEloquentStoredEvent::truncate();
$projector = app(AccountProjector::class);
Projectionist::addProjector($projector);

$this->artisan('event-sourcing:replay', ['--stored-event-model' => OtherEloquentStoredEvent::class])
$this->artisan('event-sourcing:replay', [
'projector' => [AccountProjector::class],
'--stored-event-model' => OtherEloquentStoredEvent::class,
])
->expectsOutput('Replaying 5 events...')
->assertExitCode(0);
})->skip();
});

it('will replay events for a specific aggregate root uuid', function () {
EloquentStoredEvent::truncate();
Expand Down

0 comments on commit 9decc6c

Please sign in to comment.