Skip to content

Commit

Permalink
[10.x] Allow pruning all cancelled and unfinished queue batches (lara…
Browse files Browse the repository at this point in the history
…vel#46833)

* Allow pruning all cancelled and unfinished batches

* Apply fixes from StyleCI
  • Loading branch information
jameshulse authored Apr 20, 2023
1 parent 2b463dd commit 3d28bdc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/Console/PruneBatchesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions tests/Queue/PruneBatchesCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Illuminate\Tests\Queue;

use Illuminate\Bus\BatchRepository;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Container\Container;
use Illuminate\Queue\Console\PruneBatchesCommand;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

class PruneBatchesCommandTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}

public function testAllowPruningAllUnfinishedBatches()
{
$container = new Container;
$container->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();
}
}

0 comments on commit 3d28bdc

Please sign in to comment.