SIGN IN SIGN UP
laravel / framework UNCLAIMED

Laravel is a web application framework with expressive, elegant syntax.

2014-12-19 16:29:42 -06:00
<?php
namespace Illuminate\Tests\Queue;
use Illuminate\Bus\Batchable;
use Illuminate\Container\Container;
use Illuminate\Database\Connection;
use Illuminate\Queue\DatabaseQueue;
use Illuminate\Queue\Queue;
use Illuminate\Support\Carbon;
2020-02-22 08:04:45 +02:00
use Illuminate\Support\Str;
use Mockery as m;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
2014-12-19 16:29:42 -06:00
class QueueDatabaseQueueUnitTest extends TestCase
{
#[DataProvider('pushJobsDataProvider')]
public function testPushProperlyPushesJobOntoDatabase($uuid, $job, $displayNameStartsWith, $jobStartsWith)
{
2020-02-22 08:04:45 +02:00
Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});
$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');
$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, $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->assertEquals(0, $array['attempts']);
$this->assertNull($array['reserved_at']);
$this->assertIsInt($array['available_at']);
});
2014-12-19 16:29:42 -06:00
$queue->push($job, ['data']);
2020-02-24 15:35:47 +02:00
$container->shouldHaveReceived('bound')->with('events')->twice();
2020-02-24 15:35:47 +02:00
Str::createUuidsNormally();
}
2014-12-19 16:29:42 -06:00
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()
{
2020-02-22 08:04:45 +02:00
$uuid = Str::uuid();
Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});
$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');
$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, $time) {
$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'], 'createdAt' => $time->getTimestamp(), 'delay' => 10]), $array['payload']);
$this->assertEquals(0, $array['attempts']);
$this->assertNull($array['reserved_at']);
$this->assertIsInt($array['available_at']);
});
2014-12-19 16:29:42 -06:00
$queue->later(10, 'foo', ['data']);
2020-02-24 15:35:47 +02:00
$container->shouldHaveReceived('bound')->with('events')->twice();
Carbon::setTestNow();
2020-02-24 15:35:47 +02:00
Str::createUuidsNormally();
}
2014-12-19 16:29:42 -06: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();
}
public function testFailureToCreatePayloadFromObject()
{
$this->expectException('InvalidArgumentException');
$job = new stdClass;
$job->invalid = "\xc3\x28";
$queue = m::mock(Queue::class)->makePartial();
$class = new ReflectionClass(Queue::class);
$createPayload = $class->getMethod('createPayload');
$createPayload->invokeArgs($queue, [
$job,
2018-10-01 15:23:23 -05:00
'queue-name',
]);
}
public function testFailureToCreatePayloadFromArray()
{
$this->expectException('InvalidArgumentException');
$queue = m::mock(Queue::class)->makePartial();
$class = new ReflectionClass(Queue::class);
$createPayload = $class->getMethod('createPayload');
$createPayload->invokeArgs($queue, [
["\xc3\x28"],
2018-10-01 15:23:23 -05:00
'queue-name',
]);
}
public function testBulkBatchPushesOntoDatabase()
{
2020-02-22 08:04:45 +02:00
$uuid = Str::uuid();
Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});
$time = Carbon::now();
Carbon::setTestNow($time);
$database = m::mock(Connection::class);
$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');
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
$query->shouldReceive('insert')->once()->andReturnUsing(function ($records) use ($uuid, $time) {
$this->assertEquals([[
'queue' => 'queue',
'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]),
'attempts' => 0,
'reserved_at' => null,
'available_at' => 'available',
'created_at' => 'created',
], [
'queue' => 'queue',
'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]),
'attempts' => 0,
'reserved_at' => null,
'available_at' => 'available',
'created_at' => 'created',
]], $records);
});
$queue->bulk(['foo', 'bar'], ['data'], 'queue');
2020-02-24 15:35:47 +02:00
Carbon::setTestNow();
2020-02-24 15:35:47 +02:00
Str::createUuidsNormally();
}
public function testBuildDatabaseRecordWithPayloadAtTheEnd()
{
$queue = m::mock(DatabaseQueue::class);
2017-12-07 08:37:49 -06:00
$record = $queue->buildDatabaseRecord('queue', 'any_payload', 0);
$this->assertArrayHasKey('payload', $record);
2017-12-07 08:37:49 -06:00
$this->assertArrayHasKey('payload', array_slice($record, -1, 1, true));
}
2014-12-19 16:29:42 -06:00
}
class MyTestJob
{
public function handle()
{
// ...
}
}
class MyBatchableJob
{
use Batchable;
}