SIGN IN SIGN UP
laravel / framework UNCLAIMED

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

0 0 2 PHP
2014-12-19 16:29:42 -06:00
<?php
namespace Illuminate\Tests\Queue;
use Illuminate\Container\Container;
use Illuminate\Database\Connection;
use Illuminate\Queue\DatabaseQueue;
use Illuminate\Queue\Queue;
2020-02-22 08:04:45 +02:00
use Illuminate\Support\Str;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
2014-12-19 16:29:42 -06:00
class QueueDatabaseQueueUnitTest extends TestCase
{
2019-02-08 23:05:58 +01:00
protected function tearDown(): void
{
m::close();
}
2014-12-19 16:29:42 -06:00
public function testPushProperlyPushesJobOntoDatabase()
{
2020-02-22 08:04:45 +02:00
$uuid = Str::uuid();
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));
2020-02-22 08:07:24 +02:00
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid) {
$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']);
});
2014-12-19 16:29:42 -06:00
$queue->push('foo', ['data']);
2020-02-24 15:35:47 +02:00
$container->shouldHaveReceived('bound')->with('events')->once();
2020-02-24 15:35:47 +02:00
Str::createUuidsNormally();
}
2014-12-19 16:29:42 -06:00
public function testDelayedPushProperlyPushesJobOntoDatabase()
{
2020-02-22 08:04:45 +02:00
$uuid = Str::uuid();
Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});
2016-04-29 11:25:57 -05:00
$queue = $this->getMockBuilder(
DatabaseQueue::class)->onlyMethods(
2016-12-29 08:50:55 -06:00
['currentTime'])->setConstructorArgs(
[$database = m::mock(Connection::class), 'table', 'default']
2016-04-29 11:25:57 -05:00
)->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));
2020-02-22 08:04:45 +02:00
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid) {
$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']);
});
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')->once();
2020-02-24 15:35:47 +02:00
Str::createUuidsNormally();
}
2014-12-19 16:29:42 -06:00
public function testFailureToCreatePayloadFromObject()
{
$this->expectException('InvalidArgumentException');
$job = new stdClass;
$job->invalid = "\xc3\x28";
$queue = $this->getMockForAbstractClass(Queue::class);
$class = new ReflectionClass(Queue::class);
$createPayload = $class->getMethod('createPayload');
$createPayload->setAccessible(true);
$createPayload->invokeArgs($queue, [
$job,
2018-10-01 15:23:23 -05:00
'queue-name',
]);
}
public function testFailureToCreatePayloadFromArray()
{
$this->expectException('InvalidArgumentException');
$queue = $this->getMockForAbstractClass(Queue::class);
$class = new ReflectionClass(Queue::class);
$createPayload = $class->getMethod('createPayload');
$createPayload->setAccessible(true);
$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;
});
$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));
2020-02-22 08:04:45 +02:00
$query->shouldReceive('insert')->once()->andReturnUsing(function ($records) use ($uuid) {
$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']]),
'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']]),
'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
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
}