Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Replay events from a specific event storeSince #160 added the ability…
Browse files Browse the repository at this point in the history
… to have event stores per aggregate root this fixes the gap with the replay command to replay events from a specific store. Discussed in #160
  • Loading branch information
mfullbrook committed May 7, 2019
1 parent 4da7ca2 commit d94c987
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Console/ReplayCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace Spatie\EventProjector\Console;

use Exception;
use InvalidArgumentException;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Spatie\EventProjector\Projectionist;
use Spatie\EventProjector\Models\StoredEvent;

class ReplayCommand extends Command
{
protected $signature = 'event-projector:replay {projector?*}
{--from=0 : Replay events starting from this event number}';
{--from=0 : Replay events starting from this event number}
{--store= : Replay events from this store}';

protected $description = 'Replay stored events';

Expand All @@ -25,12 +28,12 @@ public function __construct(Projectionist $projectionist)
parent::__construct();

$this->projectionist = $projectionist;

$this->storedEventModelClass = $this->getStoredEventClass();
}

public function handle(): void
{
$this->storedEventModelClass = $this->getStoredEventClass();

$projectors = $this->selectProjectors($this->argument('projector'));

if (is_null($projectors)) {
Expand Down Expand Up @@ -99,6 +102,14 @@ private function emptyLine(int $amount = 1): void

private function getStoredEventClass(): string
{
return config('event-projector.stored_event_model');
if ($store = $this->option('store')) {
if (! is_subclass_of($store, StoredEvent::class)) {
throw new InvalidArgumentException(
"Invalid store value - class `$store` does not implement `".StoredEvent::class."`"
);
}
}

return $store ?? config('event-projector.stored_event_model');
}
}
17 changes: 17 additions & 0 deletions tests/Console/ReplayCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use Spatie\EventProjector\Tests\TestClasses\Reactors\BrokeReactor;
use Spatie\EventProjector\Tests\TestClasses\Events\MoneyAddedEvent;
use Spatie\EventProjector\Tests\TestClasses\Mailables\AccountBroke;
use Spatie\EventProjector\Tests\TestClasses\Models\OtherStoredEvent;
use Spatie\EventProjector\Tests\TestClasses\Events\MoneySubtractedEvent;
use Spatie\EventProjector\Tests\TestClasses\Projectors\BalanceProjector;
use Spatie\EventProjector\Tests\TestClasses\AggregateRoots\AccountAggregateRootWithStoredEventSpecified;

final class ReplayCommandTest extends TestCase
{
Expand Down Expand Up @@ -113,4 +115,19 @@ public function it_will_call_certain_methods_on_the_projector_when_replaying_eve
'projector' => [get_class($projector)],
]);
}

public function it_will_replay_events_from_a_specific_store()
{
$account = AccountAggregateRootWithStoredEventSpecified::create();

foreach (range(1, 5) as $i) {
event(new MoneyAddedEvent($account, 2000));
}

OtherStoredEvent::truncate();

$this->artisan('event-projector:replay', ['--store' => OtherStoredEvent::class])
->expectsOutput('Replaying 5 events...')
->assertExitCode(0);
}
}

0 comments on commit d94c987

Please sign in to comment.