2014-12-19 16:29:42 -06:00
|
|
|
<?php
|
|
|
|
|
|
2017-01-17 14:30:19 +00:00
|
|
|
namespace Illuminate\Tests\Queue;
|
|
|
|
|
|
2026-02-06 20:32:35 +08:00
|
|
|
use Illuminate\Bus\Batchable;
|
2021-01-19 14:28:18 -05:00
|
|
|
use Illuminate\Container\Container;
|
2018-10-05 14:48:10 +02:00
|
|
|
use Illuminate\Database\Connection;
|
|
|
|
|
use Illuminate\Queue\DatabaseQueue;
|
2019-09-10 17:16:05 +02:00
|
|
|
use Illuminate\Queue\Queue;
|
2026-03-17 21:24:36 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
2020-02-22 08:04:45 +02:00
|
|
|
use Illuminate\Support\Str;
|
2019-09-10 17:16:05 +02:00
|
|
|
use Mockery as m;
|
2023-10-26 22:09:14 +08:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2019-09-10 17:16:05 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use ReflectionClass;
|
|
|
|
|
use stdClass;
|
2014-12-19 16:29:42 -06:00
|
|
|
|
2016-12-30 21:31:11 +01:00
|
|
|
class QueueDatabaseQueueUnitTest extends TestCase
|
2015-06-01 16:26:53 +01:00
|
|
|
{
|
2023-10-26 22:09:14 +08:00
|
|
|
#[DataProvider('pushJobsDataProvider')]
|
2023-03-17 19:04:18 +01:00
|
|
|
public function testPushProperlyPushesJobOntoDatabase($uuid, $job, $displayNameStartsWith, $jobStartsWith)
|
2015-06-01 15:56:31 +01:00
|
|
|
{
|
2020-02-22 08:04:45 +02:00
|
|
|
Str::createUuidsUsing(function () use ($uuid) {
|
|
|
|
|
return $uuid;
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-06 15:39:54 +03:00
|
|
|
$queue = $this->getMockBuilder(DatabaseQueue::class)->onlyMethods(['currentTime'])->setConstructorArgs([$database = m::mock(Connection::class), 'table', 'default'])->getMock();
|
2019-08-11 19:29:02 +02:00
|
|
|
$queue->expects($this->any())->method('currentTime')->willReturn('time');
|
2021-01-19 14:28:18 -05:00
|
|
|
$queue->setContainer($container = m::spy(Container::class));
|
2018-10-05 14:48:10 +02:00
|
|
|
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
|
2023-03-17 19:04:18 +01:00
|
|
|
$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']);
|
|
|
|
|
|
2019-08-27 14:48:17 +02:00
|
|
|
$this->assertSame('default', $array['queue']);
|
2015-06-01 15:56:31 +01:00
|
|
|
$this->assertEquals(0, $array['attempts']);
|
|
|
|
|
$this->assertNull($array['reserved_at']);
|
2018-12-29 17:32:39 +03:30
|
|
|
$this->assertIsInt($array['available_at']);
|
2015-06-01 15:56:31 +01:00
|
|
|
});
|
2014-12-19 16:29:42 -06:00
|
|
|
|
2023-03-17 19:04:18 +01:00
|
|
|
$queue->push($job, ['data']);
|
2020-02-24 15:35:47 +02:00
|
|
|
|
2024-01-17 21:20:57 +00:00
|
|
|
$container->shouldHaveReceived('bound')->with('events')->twice();
|
2021-01-19 14:28:18 -05:00
|
|
|
|
2020-02-24 15:35:47 +02:00
|
|
|
Str::createUuidsNormally();
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
2014-12-19 16:29:42 -06:00
|
|
|
|
2023-03-17 19:04:18 +01:00
|
|
|
public static function pushJobsDataProvider()
|
|
|
|
|
{
|
|
|
|
|
$uuid = Str::uuid()->toString();
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
[$uuid, new MyTestJob, 'MyTestJob', 'CallQueuedHandler'],
|
|
|
|
|
[$uuid, fn () => 0, 'Closure', 'CallQueuedHandler'],
|
|
|
|
|
[$uuid, 'foo', 'foo', 'foo'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testDelayedPushProperlyPushesJobOntoDatabase()
|
|
|
|
|
{
|
2020-02-22 08:04:45 +02:00
|
|
|
$uuid = Str::uuid();
|
|
|
|
|
|
|
|
|
|
Str::createUuidsUsing(function () use ($uuid) {
|
|
|
|
|
return $uuid;
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-24 09:15:22 -05:00
|
|
|
$time = Carbon::now();
|
|
|
|
|
Carbon::setTestNow($time);
|
|
|
|
|
|
2022-11-27 14:41:23 +02:00
|
|
|
$queue = $this->getMockBuilder(DatabaseQueue::class)
|
|
|
|
|
->onlyMethods(['currentTime'])
|
|
|
|
|
->setConstructorArgs([$database = m::mock(Connection::class), 'table', 'default'])
|
|
|
|
|
->getMock();
|
2019-08-11 19:29:02 +02:00
|
|
|
$queue->expects($this->any())->method('currentTime')->willReturn('time');
|
2021-01-19 14:28:18 -05:00
|
|
|
$queue->setContainer($container = m::spy(Container::class));
|
2018-10-05 14:48:10 +02:00
|
|
|
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
|
2025-04-24 09:15:22 -05:00
|
|
|
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid, $time) {
|
2019-08-27 14:48:17 +02:00
|
|
|
$this->assertSame('default', $array['queue']);
|
2025-04-24 09:15:22 -05:00
|
|
|
$this->assertSame(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'delay' => 10]), $array['payload']);
|
2015-06-01 15:56:31 +01:00
|
|
|
$this->assertEquals(0, $array['attempts']);
|
|
|
|
|
$this->assertNull($array['reserved_at']);
|
2018-12-29 17:32:39 +03:30
|
|
|
$this->assertIsInt($array['available_at']);
|
2015-06-01 15:56:31 +01:00
|
|
|
});
|
2014-12-19 16:29:42 -06:00
|
|
|
|
2015-06-01 16:35:44 +01:00
|
|
|
$queue->later(10, 'foo', ['data']);
|
2020-02-24 15:35:47 +02:00
|
|
|
|
2024-01-17 21:20:57 +00:00
|
|
|
$container->shouldHaveReceived('bound')->with('events')->twice();
|
2021-01-19 14:28:18 -05:00
|
|
|
|
2025-04-24 09:15:22 -05:00
|
|
|
Carbon::setTestNow();
|
2020-02-24 15:35:47 +02:00
|
|
|
Str::createUuidsNormally();
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
2014-12-19 16:29:42 -06:00
|
|
|
|
2026-02-06 20:32:35 +08:00
|
|
|
public function testPushIncludesBatchIdInPayloadForBatchableJob()
|
|
|
|
|
{
|
|
|
|
|
$uuid = Str::uuid()->toString();
|
|
|
|
|
|
|
|
|
|
Str::createUuidsUsing(function () use ($uuid) {
|
|
|
|
|
return $uuid;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$job = (new MyBatchableJob)->withBatchId('test-batch-id');
|
|
|
|
|
|
|
|
|
|
$queue = $this->getMockBuilder(DatabaseQueue::class)->onlyMethods(['currentTime'])->setConstructorArgs([$database = m::mock(Connection::class), 'table', 'default'])->getMock();
|
|
|
|
|
$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) {
|
|
|
|
|
$payload = json_decode($array['payload'], true);
|
|
|
|
|
$this->assertSame('test-batch-id', $payload['data']['batchId']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$queue->push($job, ['data']);
|
|
|
|
|
|
|
|
|
|
$container->shouldHaveReceived('bound')->with('events')->twice();
|
|
|
|
|
|
|
|
|
|
Str::createUuidsNormally();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 19:55:15 +02:00
|
|
|
public function testFailureToCreatePayloadFromObject()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException('InvalidArgumentException');
|
|
|
|
|
|
2017-06-19 15:34:26 +02:00
|
|
|
$job = new stdClass;
|
2016-09-05 19:55:15 +02:00
|
|
|
$job->invalid = "\xc3\x28";
|
|
|
|
|
|
2023-10-25 22:29:20 +08:00
|
|
|
$queue = m::mock(Queue::class)->makePartial();
|
2018-10-05 14:48:10 +02:00
|
|
|
$class = new ReflectionClass(Queue::class);
|
2016-09-05 19:55:15 +02:00
|
|
|
|
|
|
|
|
$createPayload = $class->getMethod('createPayload');
|
|
|
|
|
$createPayload->invokeArgs($queue, [
|
|
|
|
|
$job,
|
2018-10-01 15:23:23 -05:00
|
|
|
'queue-name',
|
2016-09-05 19:55:15 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testFailureToCreatePayloadFromArray()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException('InvalidArgumentException');
|
|
|
|
|
|
2023-10-25 22:29:20 +08:00
|
|
|
$queue = m::mock(Queue::class)->makePartial();
|
2018-10-05 14:48:10 +02:00
|
|
|
$class = new ReflectionClass(Queue::class);
|
2016-09-05 19:55:15 +02:00
|
|
|
|
|
|
|
|
$createPayload = $class->getMethod('createPayload');
|
|
|
|
|
$createPayload->invokeArgs($queue, [
|
|
|
|
|
["\xc3\x28"],
|
2018-10-01 15:23:23 -05:00
|
|
|
'queue-name',
|
2016-09-05 19:55:15 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testBulkBatchPushesOntoDatabase()
|
|
|
|
|
{
|
2020-02-22 08:04:45 +02:00
|
|
|
$uuid = Str::uuid();
|
|
|
|
|
|
|
|
|
|
Str::createUuidsUsing(function () use ($uuid) {
|
|
|
|
|
return $uuid;
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-24 09:15:22 -05:00
|
|
|
$time = Carbon::now();
|
|
|
|
|
Carbon::setTestNow($time);
|
|
|
|
|
|
2018-10-05 14:48:10 +02:00
|
|
|
$database = m::mock(Connection::class);
|
2020-10-06 15:39:54 +03:00
|
|
|
$queue = $this->getMockBuilder(DatabaseQueue::class)->onlyMethods(['currentTime', 'availableAt'])->setConstructorArgs([$database, 'table', 'default'])->getMock();
|
2019-08-11 19:29:02 +02:00
|
|
|
$queue->expects($this->any())->method('currentTime')->willReturn('created');
|
|
|
|
|
$queue->expects($this->any())->method('availableAt')->willReturn('available');
|
2018-10-05 14:48:10 +02:00
|
|
|
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
|
2025-04-24 09:15:22 -05:00
|
|
|
$query->shouldReceive('insert')->once()->andReturnUsing(function ($records) use ($uuid, $time) {
|
2015-06-01 15:56:31 +01:00
|
|
|
$this->assertEquals([[
|
|
|
|
|
'queue' => 'queue',
|
2025-04-24 09:15:22 -05:00
|
|
|
'payload' => json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'delay' => null]),
|
2015-06-01 15:56:31 +01:00
|
|
|
'attempts' => 0,
|
|
|
|
|
'reserved_at' => null,
|
|
|
|
|
'available_at' => 'available',
|
|
|
|
|
'created_at' => 'created',
|
2015-06-01 16:26:53 +01:00
|
|
|
], [
|
2015-06-01 15:56:31 +01:00
|
|
|
'queue' => 'queue',
|
2025-04-24 09:15:22 -05:00
|
|
|
'payload' => json_encode(['uuid' => $uuid, 'displayName' => 'bar', 'job' => 'bar', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'delay' => null]),
|
2015-06-01 15:56:31 +01:00
|
|
|
'attempts' => 0,
|
|
|
|
|
'reserved_at' => null,
|
|
|
|
|
'available_at' => 'available',
|
|
|
|
|
'created_at' => 'created',
|
|
|
|
|
]], $records);
|
|
|
|
|
});
|
2015-05-14 20:15:31 -04:00
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
$queue->bulk(['foo', 'bar'], ['data'], 'queue');
|
2020-02-24 15:35:47 +02:00
|
|
|
|
2025-04-24 09:15:22 -05:00
|
|
|
Carbon::setTestNow();
|
2020-02-24 15:35:47 +02:00
|
|
|
Str::createUuidsNormally();
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
2017-12-07 11:37:37 -03:00
|
|
|
|
|
|
|
|
public function testBuildDatabaseRecordWithPayloadAtTheEnd()
|
|
|
|
|
{
|
2018-10-05 14:48:10 +02:00
|
|
|
$queue = m::mock(DatabaseQueue::class);
|
2017-12-07 08:37:49 -06:00
|
|
|
$record = $queue->buildDatabaseRecord('queue', 'any_payload', 0);
|
2017-12-07 11:37:37 -03:00
|
|
|
$this->assertArrayHasKey('payload', $record);
|
2017-12-07 08:37:49 -06:00
|
|
|
$this->assertArrayHasKey('payload', array_slice($record, -1, 1, true));
|
2017-12-07 11:37:37 -03:00
|
|
|
}
|
2014-12-19 16:29:42 -06:00
|
|
|
}
|
2023-03-17 19:04:18 +01:00
|
|
|
|
|
|
|
|
class MyTestJob
|
|
|
|
|
{
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-06 20:32:35 +08:00
|
|
|
|
|
|
|
|
class MyBatchableJob
|
|
|
|
|
{
|
|
|
|
|
use Batchable;
|
|
|
|
|
}
|