From f52f42543fa3223ea6184af3c090f05ac7873225 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Jul 2024 11:59:06 -0400 Subject: [PATCH 1/3] add force option to replay command --- src/Console/ReplayCommand.php | 10 ++++++++-- tests/Console/ReplayCommandTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Console/ReplayCommand.php b/src/Console/ReplayCommand.php index 750962b5..8b50d96a 100644 --- a/src/Console/ReplayCommand.php +++ b/src/Console/ReplayCommand.php @@ -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'; @@ -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->isRunningInteractively() && ! $this->confirm('Are you sure you want to replay events to all projectors?', true)) { return null; } @@ -96,4 +97,9 @@ protected function emptyLine(int $amount = 1): void $this->line(''); } } + + protected function isRunningInteractively(): bool + { + return ! $this->option('force'); + } } diff --git a/tests/Console/ReplayCommandTest.php b/tests/Console/ReplayCommandTest.php index cb6d8741..6fcda472 100644 --- a/tests/Console/ReplayCommandTest.php +++ b/tests/Console/ReplayCommandTest.php @@ -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; @@ -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]); From 1c8d4debbbe026d3e3876b046280a4ec31c218f8 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Jul 2024 12:19:24 -0400 Subject: [PATCH 2/3] use false === instead of bang! --- src/Console/ReplayCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/ReplayCommand.php b/src/Console/ReplayCommand.php index 8b50d96a..0362e099 100644 --- a/src/Console/ReplayCommand.php +++ b/src/Console/ReplayCommand.php @@ -100,6 +100,6 @@ protected function emptyLine(int $amount = 1): void protected function isRunningInteractively(): bool { - return ! $this->option('force'); + return false === $this->option('force'); } } From cf7c74f131194d6138a83a83a572f4510a243d89 Mon Sep 17 00:00:00 2001 From: inmanturbo <47095624+inmanturbo@users.noreply.github.com> Date: Wed, 10 Jul 2024 08:57:09 -0400 Subject: [PATCH 3/3] Check force option directly Co-authored-by: Sebastian De Deyne --- src/Console/ReplayCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/ReplayCommand.php b/src/Console/ReplayCommand.php index 0362e099..0bb15bb0 100644 --- a/src/Console/ReplayCommand.php +++ b/src/Console/ReplayCommand.php @@ -46,7 +46,7 @@ public function handle(Projectionist $projectionist): void public function selectProjectors(array $projectorClassNames): ?Collection { if (count($projectorClassNames) === 0) { - if ($this->isRunningInteractively() && ! $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; }