Skip to content

Commit

Permalink
Name of job set by displayName() must be honoured by Schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
SCIF committed Apr 12, 2024
1 parent d08a9b0 commit 8f6597d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ public function command($command, array $parameters = [])
*/
public function job($job, $queue = null, $connection = null)
{
$jobName = $job;
$job = is_string($job) ? Container::getInstance()->make($job) : $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;

Expand All @@ -162,7 +171,7 @@ public function job($job, $queue = null, $connection = null)
} else {
$this->dispatchNow($job);
}
})->name(is_string($job) ? $job : get_class($job));
})->name($jobName);
}

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

declare(strict_types=1);

namespace Illuminate\Tests\Console\Fixtures;

use Illuminate\Contracts\Queue\ShouldQueue;

final class JobToTestWithSchedule implements ShouldQueue
{
}
70 changes: 70 additions & 0 deletions tests/Console/Scheduling/ScheduleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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(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));
}
}

0 comments on commit 8f6597d

Please sign in to comment.