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

[9.x] PHP 8.0 fix for Closure jobs #46505

Merged
merged 2 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
{
// ...
}
}