Skip to content

Commit

Permalink
[9.x] PHP 8.0 fix for Closure jobs (#46505)
Browse files Browse the repository at this point in the history
* Fix Closure job and add extra test cases

* styleci
  • Loading branch information
bert-w authored Mar 17, 2023
1 parent c6bcfc8 commit e081830
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ function () use ($payload, $queue, $delay, $callback, $job) {
*/
protected function shouldDispatchAfterCommit($job)
{
if (is_object($job) && isset($job->afterCommit)) {
if (! $job instanceof Closure && is_object($job) && isset($job->afterCommit)) {
return $job->afterCommit;
}

Expand Down
36 changes: 30 additions & 6 deletions tests/Queue/QueueDatabaseQueueUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ protected function tearDown(): void
m::close();
}

public function testPushProperlyPushesJobOntoDatabase()
/**
* @dataProvider pushJobsDataProvider
*/
public function testPushProperlyPushesJobOntoDatabase($uuid, $job, $displayNameStartsWith, $jobStartsWith)
{
$uuid = Str::uuid();

Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});
Expand All @@ -31,21 +32,36 @@ public function testPushProperlyPushesJobOntoDatabase()
$queue->expects($this->any())->method('currentTime')->willReturn('time');
$queue->setContainer($container = m::spy(Container::class));
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid) {
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid, $displayNameStartsWith, $jobStartsWith) {
$payload = json_decode($array['payload'], true);
$this->assertSame($uuid, $payload['uuid']);
$this->assertStringContainsString($displayNameStartsWith, $payload['displayName']);
$this->assertStringContainsString($jobStartsWith, $payload['job']);

$this->assertSame('default', $array['queue']);
$this->assertSame(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]), $array['payload']);
$this->assertEquals(0, $array['attempts']);
$this->assertNull($array['reserved_at']);
$this->assertIsInt($array['available_at']);
});

$queue->push('foo', ['data']);
$queue->push($job, ['data']);

$container->shouldHaveReceived('bound')->with('events')->once();

Str::createUuidsNormally();
}

public static function pushJobsDataProvider()
{
$uuid = Str::uuid()->toString();

return [
[$uuid, new MyTestJob, 'MyTestJob', 'CallQueuedHandler'],
[$uuid, fn () => 0, 'Closure', 'CallQueuedHandler'],
[$uuid, 'foo', 'foo', 'foo'],
];
}

public function testDelayedPushProperlyPushesJobOntoDatabase()
{
$uuid = Str::uuid();
Expand Down Expand Up @@ -153,3 +169,11 @@ public function testBuildDatabaseRecordWithPayloadAtTheEnd()
$this->assertArrayHasKey('payload', array_slice($record, -1, 1, true));
}
}

class MyTestJob
{
public function handle()
{
// ...
}
}

0 comments on commit e081830

Please sign in to comment.