Skip to content

Commit 5f5484f

Browse files
authored
Name of job set by displayName() must be honoured by Schedule (#51038)
1 parent b8028ce commit 5f5484f

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

src/Illuminate/Console/Scheduling/Schedule.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ public function command($command, array $parameters = [])
154154
*/
155155
public function job($job, $queue = null, $connection = null)
156156
{
157+
$jobName = $job;
158+
159+
if (! is_string($job)) {
160+
$jobName = method_exists($job, 'displayName')
161+
? $job->displayName()
162+
: $job::class;
163+
}
164+
157165
return $this->call(function () use ($job, $queue, $connection) {
158166
$job = is_string($job) ? Container::getInstance()->make($job) : $job;
159167

@@ -162,7 +170,7 @@ public function job($job, $queue = null, $connection = null)
162170
} else {
163171
$this->dispatchNow($job);
164172
}
165-
})->name(is_string($job) ? $job : get_class($job));
173+
})->name($jobName);
166174
}
167175

168176
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Illuminate\Tests\Console\Fixtures;
6+
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
9+
final class JobToTestWithSchedule implements ShouldQueue
10+
{
11+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Illuminate\Tests\Console\Scheduling;
6+
7+
use Illuminate\Console\Scheduling\EventMutex;
8+
use Illuminate\Console\Scheduling\Schedule;
9+
use Illuminate\Console\Scheduling\SchedulingMutex;
10+
use Illuminate\Container\Container;
11+
use Illuminate\Contracts\Queue\ShouldQueue;
12+
use Illuminate\Tests\Console\Fixtures\JobToTestWithSchedule;
13+
use Mockery as m;
14+
use Mockery\MockInterface;
15+
use PHPUnit\Framework\Attributes\CoversClass;
16+
use PHPUnit\Framework\Attributes\DataProvider;
17+
use PHPUnit\Framework\TestCase;
18+
19+
#[CoversClass(Schedule::class)]
20+
final class ScheduleTest extends TestCase
21+
{
22+
private Container $container;
23+
private EventMutex&MockInterface $eventMutex;
24+
private SchedulingMutex&MockInterface $schedulingMutex;
25+
26+
protected function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
$this->container = new Container;
31+
Container::setInstance($this->container);
32+
$this->eventMutex = m::mock(EventMutex::class);
33+
$this->container->instance(EventMutex::class, $this->eventMutex);
34+
$this->schedulingMutex = m::mock(SchedulingMutex::class);
35+
$this->container->instance(SchedulingMutex::class, $this->schedulingMutex);
36+
}
37+
38+
#[DataProvider('jobHonoursDisplayNameIfMethodExistsProvider')]
39+
public function testJobHonoursDisplayNameIfMethodExists(object $job, string $jobName): void
40+
{
41+
$schedule = new Schedule();
42+
$scheduledJob = $schedule->job($job);
43+
self::assertSame($jobName, $scheduledJob->description);
44+
self::assertFalse($this->container->resolved(JobToTestWithSchedule::class));
45+
}
46+
47+
public static function jobHonoursDisplayNameIfMethodExistsProvider(): array
48+
{
49+
$job = new class implements ShouldQueue
50+
{
51+
public function displayName(): string
52+
{
53+
return 'testJob-123';
54+
}
55+
};
56+
57+
return [
58+
[new JobToTestWithSchedule, JobToTestWithSchedule::class],
59+
[$job, 'testJob-123'],
60+
];
61+
}
62+
63+
public function testJobIsNotInstantiatedIfSuppliedAsClassname(): void
64+
{
65+
$schedule = new Schedule();
66+
$scheduledJob = $schedule->job(JobToTestWithSchedule::class);
67+
self::assertSame(JobToTestWithSchedule::class, $scheduledJob->description);
68+
self::assertFalse($this->container->resolved(JobToTestWithSchedule::class));
69+
}
70+
}

0 commit comments

Comments
 (0)