diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index 684aec58fc4a..65292e2fc16e 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -154,15 +154,19 @@ public function command($command, array $parameters = []) */ public function job($job, $queue = null, $connection = null) { - return $this->call(function () use ($job, $queue, $connection) { - $job = is_string($job) ? Container::getInstance()->make($job) : $job; - - if ($job instanceof ShouldQueue) { - $this->dispatchToQueue($job, $queue ?? $job->queue, $connection ?? $job->connection); - } else { - $this->dispatchNow($job); - } - })->name(is_string($job) ? $job : get_class($job)); + $instance = is_string($job) + ? Container::getInstance()->make($job) + : $job; + + $name = method_exists($instance, 'displayName') + ? $instance->displayName() + : $instance::class; + + return $this->call(function () use ($instance, $queue, $connection) { + $instance instanceof ShouldQueue + ? $this->dispatchToQueue($instance, $queue ?? $instance->queue, $connection ?? $instance->connection) + : $this->dispatchNow($instance); + })->name($name); } /** diff --git a/tests/Console/Fixtures/JobToTestWithSchedule.php b/tests/Console/Fixtures/JobToTestWithSchedule.php new file mode 100644 index 000000000000..78924c79131b --- /dev/null +++ b/tests/Console/Fixtures/JobToTestWithSchedule.php @@ -0,0 +1,14 @@ +container = new Container; + Container::setInstance($this->container); + $this->eventMutex = m::mock(EventMutex::class); + $this->container->instance(EventMutex::class, $this->eventMutex); + $this->schedulingMutex = m::mock(SchedulingMutex::class); + $this->container->instance(SchedulingMutex::class, $this->schedulingMutex); + } + + #[DataProvider('jobHonoursDisplayNameIfMethodExistsProvider')] + public function testJobHonoursDisplayNameIfMethodExists(string|object $job, string $jobName): void + { + $schedule = new Schedule(); + $scheduledJob = $schedule->job($job); + self::assertSame($jobName, $scheduledJob->description); + } + + public static function jobHonoursDisplayNameIfMethodExistsProvider(): array + { + $job = new class implements ShouldQueue + { + public function displayName(): string + { + return 'testJob-123'; + } + }; + + return [ + [JobToTestWithSchedule::class, JobToTestWithSchedule::class], + [new JobToTestWithSchedule, JobToTestWithSchedule::class], + [$job, 'testJob-123'], + ]; + } +}