From 1ce13df4c4e2a480a64e55c5f05d89fcfe6bd62d Mon Sep 17 00:00:00 2001 From: Aleksandr Zhuravlev Date: Mon, 8 Apr 2024 21:26:39 +1000 Subject: [PATCH] Name of job set by displayName() must be honoured by Schedule --- .../Console/Scheduling/Schedule.php | 20 +++--- .../Fixtures/JobToTestWithSchedule.php | 14 +++++ tests/Console/Scheduling/ScheduleTest.php | 62 +++++++++++++++++++ 3 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 tests/Console/Fixtures/JobToTestWithSchedule.php create mode 100644 tests/Console/Scheduling/ScheduleTest.php diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index 684aec58fc4a..a81d21e7bf0b 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -146,23 +146,19 @@ public function command($command, array $parameters = []) /** * Add a new job callback event to the schedule. - * - * @param object|string $job - * @param string|null $queue - * @param string|null $connection - * @return \Illuminate\Console\Scheduling\CallbackEvent */ - public function job($job, $queue = null, $connection = null) + public function job(object|string $job, ?string $queue = null, ?string $connection = null): CallbackEvent { - return $this->call(function () use ($job, $queue, $connection) { - $job = is_string($job) ? Container::getInstance()->make($job) : $job; + $jobObject = is_string($job) ? Container::getInstance()->make($job) : $job; + $jobName = method_exists($jobObject, 'displayName') ? $jobObject->displayName() : $jobObject::class; - if ($job instanceof ShouldQueue) { - $this->dispatchToQueue($job, $queue ?? $job->queue, $connection ?? $job->connection); + return $this->call(function () use ($jobObject, $queue, $connection) { + if ($jobObject instanceof ShouldQueue) { + $this->dispatchToQueue($jobObject, $queue ?? $jobObject->queue, $connection ?? $jobObject->connection); } else { - $this->dispatchNow($job); + $this->dispatchNow($jobObject); } - })->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..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'], + ]; + } +}