Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Name of job set by displayName() must be honoured by Schedule #50961

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

@SCIF SCIF Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it will be better to make the signature properly typed as it can be done with php8.2. On one hand this is a BC for those extending this class. On another hand this is internal class and should not be extended.

Let me know what do you think. In case you consider this is as a major BC break, I would revert back signature as don't want to wait v12 to get this fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this as we currently do not fully type method signatures in the framework.

{
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);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Console/Fixtures/JobToTestWithSchedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Illuminate\Tests\Console\Fixtures;

use Illuminate\Contracts\Queue\ShouldQueue;

final class JobToTestWithSchedule implements ShouldQueue
{
public function __invoke(): void
{
}
}
62 changes: 62 additions & 0 deletions tests/Console/Scheduling/ScheduleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Illuminate\Tests\Console\Scheduling;

use Illuminate\Console\Scheduling\EventMutex;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Console\Scheduling\SchedulingMutex;
use Illuminate\Container\Container;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Tests\Console\Fixtures\JobToTestWithSchedule;
use Mockery as m;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(Schedule::class)]
final class ScheduleTest extends TestCase
{
private Container $container;
private EventMutex&MockInterface $eventMutex;
private SchedulingMutex&MockInterface $schedulingMutex;

protected function setUp(): void
{
parent::setUp();

$this->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'],
];
}
}