Skip to content

Commit f52f425

Browse files
committed
add force option to replay command
1 parent b3bb62a commit f52f425

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Console/ReplayCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class ReplayCommand extends Command
1313
protected $signature = 'event-sourcing:replay {projector?*}
1414
{--from=0 : Replay events starting from this event number}
1515
{--stored-event-model= : Replay events from this store}
16-
{--aggregate-uuid= : Replay events for this aggregate only}';
16+
{--aggregate-uuid= : Replay events for this aggregate only}
17+
{--force : Replay events without asking for confirmation}';
1718

1819
protected $description = 'Replay stored events';
1920

@@ -45,7 +46,7 @@ public function handle(Projectionist $projectionist): void
4546
public function selectProjectors(array $projectorClassNames): ?Collection
4647
{
4748
if (count($projectorClassNames) === 0) {
48-
if (! $this->confirm('Are you sure you want to replay events to all projectors?', true)) {
49+
if ($this->isRunningInteractively() && ! $this->confirm('Are you sure you want to replay events to all projectors?', true)) {
4950
return null;
5051
}
5152

@@ -96,4 +97,9 @@ protected function emptyLine(int $amount = 1): void
9697
$this->line('');
9798
}
9899
}
100+
101+
protected function isRunningInteractively(): bool
102+
{
103+
return ! $this->option('force');
104+
}
99105
}

tests/Console/ReplayCommandTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\EventSourcing\Tests\Console;
44

5+
use BadMethodCallException;
56
use Illuminate\Support\Facades\Artisan;
67
use Illuminate\Support\Facades\Event;
78
use Illuminate\Support\Facades\Mail;
@@ -35,6 +36,31 @@
3536
Mail::fake();
3637
});
3738

39+
it('will run without confirmation when given the force option', function () {
40+
Projectionist::addProjector(BalanceProjector::class);
41+
42+
$this->artisan('event-sourcing:replay', ['--force' => true])
43+
->expectsOutput('Replaying 3 events...')
44+
->assertExitCode(0);
45+
});
46+
47+
it('will not run without confirmation when not given the force option', function () {
48+
$this->expectException(BadMethodCallException::class);
49+
50+
Projectionist::addProjector(BalanceProjector::class);
51+
52+
$this->artisan('event-sourcing:replay');
53+
});
54+
55+
it('will not replay events when the user does not confirm', function () {
56+
Projectionist::addProjector(BalanceProjector::class);
57+
58+
$this->artisan('event-sourcing:replay')
59+
->expectsConfirmation('Are you sure you want to replay events to all projectors?', 'no')
60+
->expectsOutput('No events replayed!')
61+
->assertExitCode(0);
62+
});
63+
3864
it('will replay events to the given projectors', function () {
3965
Event::fake([FinishedEventReplay::class, StartingEventReplay::class]);
4066

0 commit comments

Comments
 (0)