Skip to content

Commit

Permalink
Merge pull request #474 from inmanturbo/main
Browse files Browse the repository at this point in the history
add force option to replay command
  • Loading branch information
sebastiandedeyne authored Jul 10, 2024
2 parents b3bb62a + cf7c74f commit d1654ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Console/ReplayCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class ReplayCommand extends Command
protected $signature = 'event-sourcing:replay {projector?*}
{--from=0 : Replay events starting from this event number}
{--stored-event-model= : Replay events from this store}
{--aggregate-uuid= : Replay events for this aggregate only}';
{--aggregate-uuid= : Replay events for this aggregate only}
{--force : Replay events without asking for confirmation}';

protected $description = 'Replay stored events';

Expand Down Expand Up @@ -45,7 +46,7 @@ public function handle(Projectionist $projectionist): void
public function selectProjectors(array $projectorClassNames): ?Collection
{
if (count($projectorClassNames) === 0) {
if (! $this->confirm('Are you sure you want to replay events to all projectors?', true)) {
if (! $this->option('force') && ! $this->confirm('Are you sure you want to replay events to all projectors?', true)) {
return null;
}

Expand Down Expand Up @@ -96,4 +97,9 @@ protected function emptyLine(int $amount = 1): void
$this->line('');
}
}

protected function isRunningInteractively(): bool
{
return false === $this->option('force');
}
}
26 changes: 26 additions & 0 deletions tests/Console/ReplayCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\EventSourcing\Tests\Console;

use BadMethodCallException;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
Expand Down Expand Up @@ -35,6 +36,31 @@
Mail::fake();
});

it('will run without confirmation when given the force option', function () {
Projectionist::addProjector(BalanceProjector::class);

$this->artisan('event-sourcing:replay', ['--force' => true])
->expectsOutput('Replaying 3 events...')
->assertExitCode(0);
});

it('will not run without confirmation when not given the force option', function () {
$this->expectException(BadMethodCallException::class);

Projectionist::addProjector(BalanceProjector::class);

$this->artisan('event-sourcing:replay');
});

it('will not replay events when the user does not confirm', function () {
Projectionist::addProjector(BalanceProjector::class);

$this->artisan('event-sourcing:replay')
->expectsConfirmation('Are you sure you want to replay events to all projectors?', 'no')
->expectsOutput('No events replayed!')
->assertExitCode(0);
});

it('will replay events to the given projectors', function () {
Event::fake([FinishedEventReplay::class, StartingEventReplay::class]);

Expand Down

0 comments on commit d1654ad

Please sign in to comment.