diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index 684aec58fc4a..78f19a3cd7c8 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -154,6 +154,14 @@ public function command($command, array $parameters = []) */ public function job($job, $queue = null, $connection = null) { + $jobName = $job; + + if (! is_string($job)) { + $jobName = method_exists($job, 'displayName') + ? $job->displayName() + : $job::class; + } + return $this->call(function () use ($job, $queue, $connection) { $job = is_string($job) ? Container::getInstance()->make($job) : $job; @@ -162,7 +170,7 @@ public function job($job, $queue = null, $connection = null) } else { $this->dispatchNow($job); } - })->name(is_string($job) ? $job : get_class($job)); + })->name($jobName); } /** diff --git a/tests/Console/Fixtures/JobToTestWithSchedule.php b/tests/Console/Fixtures/JobToTestWithSchedule.php new file mode 100644 index 000000000000..8b6dae4aa2bb --- /dev/null +++ b/tests/Console/Fixtures/JobToTestWithSchedule.php @@ -0,0 +1,11 @@ +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(object $job, string $jobName): void + { + $schedule = new Schedule(); + $scheduledJob = $schedule->job($job); + self::assertSame($jobName, $scheduledJob->description); + self::assertFalse($this->container->resolved(JobToTestWithSchedule::class)); + } + + public static function jobHonoursDisplayNameIfMethodExistsProvider(): array + { + $job = new class implements ShouldQueue + { + public function displayName(): string + { + return 'testJob-123'; + } + }; + + return [ + [new JobToTestWithSchedule, JobToTestWithSchedule::class], + [$job, 'testJob-123'], + ]; + } + + public function testJobIsNotInstantiatedIfSuppliedAsClassname(): void + { + $schedule = new Schedule(); + $scheduledJob = $schedule->job(JobToTestWithSchedule::class); + self::assertSame(JobToTestWithSchedule::class, $scheduledJob->description); + self::assertFalse($this->container->resolved(JobToTestWithSchedule::class)); + } +}