From 3d28bdc86c8ee1850113ecd69916fd72452d45f7 Mon Sep 17 00:00:00 2001 From: James Hulse Date: Thu, 20 Apr 2023 18:37:59 +0100 Subject: [PATCH] [10.x] Allow pruning all cancelled and unfinished queue batches (#46833) * Allow pruning all cancelled and unfinished batches * Apply fixes from StyleCI --- .../Queue/Console/PruneBatchesCommand.php | 4 +- tests/Queue/PruneBatchesCommandTest.php | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/Queue/PruneBatchesCommandTest.php diff --git a/src/Illuminate/Queue/Console/PruneBatchesCommand.php b/src/Illuminate/Queue/Console/PruneBatchesCommand.php index 993ed17ccdd6..586b4dd6bac3 100644 --- a/src/Illuminate/Queue/Console/PruneBatchesCommand.php +++ b/src/Illuminate/Queue/Console/PruneBatchesCommand.php @@ -46,7 +46,7 @@ public function handle() $this->components->info("{$count} entries deleted."); - if ($this->option('unfinished')) { + if ($this->option('unfinished') !== null) { $count = 0; if ($repository instanceof DatabaseBatchRepository) { @@ -56,7 +56,7 @@ public function handle() $this->components->info("{$count} unfinished entries deleted."); } - if ($this->option('cancelled')) { + if ($this->option('cancelled') !== null) { $count = 0; if ($repository instanceof DatabaseBatchRepository) { diff --git a/tests/Queue/PruneBatchesCommandTest.php b/tests/Queue/PruneBatchesCommandTest.php new file mode 100644 index 000000000000..1878373ea8b8 --- /dev/null +++ b/tests/Queue/PruneBatchesCommandTest.php @@ -0,0 +1,46 @@ +instance(BatchRepository::class, $repo = m::spy(DatabaseBatchRepository::class)); + + $command = new PruneBatchesCommand; + $command->setLaravel($container); + + $command->run(new ArrayInput(['--unfinished' => 0]), new NullOutput()); + + $repo->shouldHaveReceived('pruneUnfinished')->once(); + } + + public function testAllowPruningAllCancelledBatches() + { + $container = new Container; + $container->instance(BatchRepository::class, $repo = m::spy(DatabaseBatchRepository::class)); + + $command = new PruneBatchesCommand; + $command->setLaravel($container); + + $command->run(new ArrayInput(['--cancelled' => 0]), new NullOutput()); + + $repo->shouldHaveReceived('pruneCancelled')->once(); + } +}