Skip to content

Commit f11b2b3

Browse files
committed
set the event description using the command description
1 parent 9c06ef2 commit f11b2b3

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/LaravelScheduleQueueCommandServiceProvider.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,28 @@ class LaravelScheduleQueueCommandServiceProvider extends ServiceProvider
1313
public function boot(): void
1414
{
1515
Schedule::macro('queueCommand', function (
16-
/** @var class-string<Command> $command */
1716
string $command,
1817
array $parameters = [],
1918
?string $queue = null,
2019
?string $connection = null
2120
) {
2221
/** @var Schedule $this */
23-
return $this->call(function () use ($command, $parameters, $queue, $connection) {
22+
$event = $this->call(function () use ($command, $parameters, $queue, $connection) {
2423
Container::getInstance()
2524
->make(Kernel::class)
2625
->queue($command, $parameters)
2726
->onQueue($queue)
2827
->onConnection($connection);
2928
});
29+
30+
if (class_exists($command)) {
31+
/** @var Command $object */
32+
$object = Container::getInstance()->make($command);
33+
34+
$event->description($object->getDescription());
35+
}
36+
37+
return $event;
3038
});
3139
}
3240
}

tests/QueueCommandTest.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
<?php
22

33
use Illuminate\Console\Command;
4+
use Illuminate\Console\Scheduling\CallbackEvent;
45
use Illuminate\Console\Scheduling\Schedule;
56
use Illuminate\Foundation\Console\QueuedCommand;
67
use Illuminate\Support\Facades\Queue;
78

9+
it('does not set the name when provided as a command signature', function () {
10+
Queue::fake();
11+
12+
/** @var Schedule $scheduler */
13+
$scheduler = $this->app->make(Schedule::class);
14+
15+
/** @var CallbackEvent $event */
16+
$event = $scheduler->queueCommand('bar:run');
17+
expect($event->description)
18+
->toBeNull()
19+
->and(fn() => $event->withoutOverlapping())
20+
->toThrow(LogicException::class);
21+
});
22+
23+
it('sets the name from the command description when provided as a class string', function () {
24+
Queue::fake();
25+
26+
/** @var Schedule $scheduler */
27+
$scheduler = $this->app->make(Schedule::class);
28+
29+
/** @var CallbackEvent $event */
30+
$event = $scheduler->queueCommand(FooCommand::class)->withoutOverlapping();
31+
32+
expect($event->description)->toBe('I am description for the foo command');
33+
});
34+
835
it('sends queued command to correct queue', function () {
936
Queue::fake();
1037

1138
/** @var Schedule $scheduler */
1239
$scheduler = $this->app->make(Schedule::class);
13-
$scheduler->queueCommand(FooCommand::class)->name('')->everyMinute();
14-
$scheduler->queueCommand(FooCommand::class, ['foo' => 'bar'], 'test-queue')->name('')->everyMinute();
40+
$scheduler->queueCommand(FooCommand::class)->everyMinute();
41+
$scheduler->queueCommand('bar:test', ['foo' => 'bar'], 'test-queue')->everyMinute();
1542
$scheduler->queueCommand(FooCommand::class, ['foo' => 'bar'], 'another-queue')->name('')->everyMinute();
1643

1744
$events = $scheduler->events();
@@ -29,9 +56,9 @@
2956

3057
/** @var \Illuminate\Console\Scheduling\Schedule $scheduler */
3158
$scheduler = $this->app->make(Schedule::class);
32-
$scheduler->queueCommand(FooCommand::class)->name('')->everyMinute();
59+
$scheduler->queueCommand(FooCommand::class)->name('')->everyMinute()->withoutOverlapping();
3360
$scheduler->queueCommand(FooCommand::class, ['foo' => 'bar'], null, 'foo')->name('')->everyMinute();
34-
$scheduler->queueCommand(FooCommand::class, ['foo' => 'bar'], null, 'bar')->name('')->everyMinute();
61+
$scheduler->queueCommand('bar:test', ['foo' => 'bar'], null, 'bar')->name('')->everyMinute();
3562

3663
$events = $scheduler->events();
3764
foreach ($events as $event) {
@@ -47,6 +74,8 @@ class FooCommand extends Command
4774
{
4875
protected $signature = 'foo:run';
4976

77+
protected $description = 'I am description for the foo command';
78+
5079
public function handle()
5180
{
5281
//

0 commit comments

Comments
 (0)