Skip to content

Commit

Permalink
[11.x] Name of job set by displayName() must be honoured by Schedule (l…
Browse files Browse the repository at this point in the history
…aravel#50973)

* Name of job set by displayName() must be honoured by Schedule

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
SCIF and taylorotwell authored Apr 9, 2024
1 parent 619e762 commit d63d2b1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
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'],
];
}
}

0 comments on commit d63d2b1

Please sign in to comment.