Skip to content

Commit e081830

Browse files
authored
[9.x] PHP 8.0 fix for Closure jobs (#46505)
* Fix Closure job and add extra test cases * styleci
1 parent c6bcfc8 commit e081830

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/Illuminate/Queue/Queue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ function () use ($payload, $queue, $delay, $callback, $job) {
325325
*/
326326
protected function shouldDispatchAfterCommit($job)
327327
{
328-
if (is_object($job) && isset($job->afterCommit)) {
328+
if (! $job instanceof Closure && is_object($job) && isset($job->afterCommit)) {
329329
return $job->afterCommit;
330330
}
331331

tests/Queue/QueueDatabaseQueueUnitTest.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ protected function tearDown(): void
1919
m::close();
2020
}
2121

22-
public function testPushProperlyPushesJobOntoDatabase()
22+
/**
23+
* @dataProvider pushJobsDataProvider
24+
*/
25+
public function testPushProperlyPushesJobOntoDatabase($uuid, $job, $displayNameStartsWith, $jobStartsWith)
2326
{
24-
$uuid = Str::uuid();
25-
2627
Str::createUuidsUsing(function () use ($uuid) {
2728
return $uuid;
2829
});
@@ -31,21 +32,36 @@ public function testPushProperlyPushesJobOntoDatabase()
3132
$queue->expects($this->any())->method('currentTime')->willReturn('time');
3233
$queue->setContainer($container = m::spy(Container::class));
3334
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
34-
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid) {
35+
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid, $displayNameStartsWith, $jobStartsWith) {
36+
$payload = json_decode($array['payload'], true);
37+
$this->assertSame($uuid, $payload['uuid']);
38+
$this->assertStringContainsString($displayNameStartsWith, $payload['displayName']);
39+
$this->assertStringContainsString($jobStartsWith, $payload['job']);
40+
3541
$this->assertSame('default', $array['queue']);
36-
$this->assertSame(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]), $array['payload']);
3742
$this->assertEquals(0, $array['attempts']);
3843
$this->assertNull($array['reserved_at']);
3944
$this->assertIsInt($array['available_at']);
4045
});
4146

42-
$queue->push('foo', ['data']);
47+
$queue->push($job, ['data']);
4348

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

4651
Str::createUuidsNormally();
4752
}
4853

54+
public static function pushJobsDataProvider()
55+
{
56+
$uuid = Str::uuid()->toString();
57+
58+
return [
59+
[$uuid, new MyTestJob, 'MyTestJob', 'CallQueuedHandler'],
60+
[$uuid, fn () => 0, 'Closure', 'CallQueuedHandler'],
61+
[$uuid, 'foo', 'foo', 'foo'],
62+
];
63+
}
64+
4965
public function testDelayedPushProperlyPushesJobOntoDatabase()
5066
{
5167
$uuid = Str::uuid();
@@ -153,3 +169,11 @@ public function testBuildDatabaseRecordWithPayloadAtTheEnd()
153169
$this->assertArrayHasKey('payload', array_slice($record, -1, 1, true));
154170
}
155171
}
172+
173+
class MyTestJob
174+
{
175+
public function handle()
176+
{
177+
// ...
178+
}
179+
}

0 commit comments

Comments
 (0)