Skip to content

Commit

Permalink
[11.x] Refactor and add remaining test cases for the DatabaseUuidFail…
Browse files Browse the repository at this point in the history
…edJobProviderTest class (#53408)

* extract a separate method to create the database uuid failed job provider.

* test get all uuids of failed jobs.

* add test cases for the all and find methods of the DatabaseUuidFailedJobProvider class.

* add test cases for removing failed jobs.

* add a test case for pruning failed jobs.
  • Loading branch information
kevinb1989 authored Nov 5, 2024
1 parent cdf07b6 commit 4758c19
Showing 1 changed file with 111 additions and 54 deletions.
165 changes: 111 additions & 54 deletions tests/Queue/DatabaseUuidFailedJobProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,106 @@
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Queue\Failed\DatabaseUuidFailedJobProvider;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use PHPUnit\Framework\TestCase;
use RuntimeException;

class DatabaseUuidFailedJobProviderTest extends TestCase
{
public function testGettingIdsOfAllFailedJobs()
{
$provider = $this->getFailedJobProvider();

$provider->log('connection-1', 'queue-1', json_encode(['uuid' => 'uuid-1']), new RuntimeException());
$provider->log('connection-1', 'queue-1', json_encode(['uuid' => 'uuid-2']), new RuntimeException());
$provider->log('connection-2', 'queue-2', json_encode(['uuid' => 'uuid-3']), new RuntimeException());
$provider->log('connection-2', 'queue-2', json_encode(['uuid' => 'uuid-4']), new RuntimeException());

$this->assertSame(['uuid-1', 'uuid-2', 'uuid-3', 'uuid-4'], $provider->ids());
$this->assertSame(['uuid-1', 'uuid-2'], $provider->ids('queue-1'));
$this->assertSame(['uuid-3', 'uuid-4'], $provider->ids('queue-2'));
}

public function testGettingAllFailedJobs()
{
$provider = $this->getFailedJobProvider();

$this->assertEmpty($provider->all());

$provider->log('connection-1', 'queue-1', json_encode(['uuid' => 'uuid-1']), new RuntimeException());
$provider->log('connection-1', 'queue-1', json_encode(['uuid' => 'uuid-2']), new RuntimeException());
$provider->log('connection-2', 'queue-2', json_encode(['uuid' => 'uuid-3']), new RuntimeException());
$provider->log('connection-2', 'queue-2', json_encode(['uuid' => 'uuid-4']), new RuntimeException());

$this->assertCount(4, $provider->all());

$this->assertSame(
['uuid-1', 'uuid-2', 'uuid-3', 'uuid-4'],
array_column($provider->all(), 'id')
);
}

public function testFindingFailedJobsById()
{
$provider = $this->getFailedJobProvider();

$provider->log('connection-1', 'queue-1', json_encode(['uuid' => 'uuid-1']), new RuntimeException());

$this->assertNull($provider->find('uuid-2'));
$this->assertEquals('uuid-1', $provider->find('uuid-1')->id);
$this->assertEquals('queue-1', $provider->find('uuid-1')->queue);
$this->assertEquals('connection-1', $provider->find('uuid-1')->connection);
}

public function testRemovingJobsById()
{
$provider = $this->getFailedJobProvider();

$provider->log('connection-1', 'queue-1', json_encode(['uuid' => 'uuid-1']), new RuntimeException());

$this->assertNotNull($provider->find('uuid-1'));

$provider->forget('uuid-1');

$this->assertNull($provider->find('uuid-1'));
}

public function testRemovingAllFailedJobs()
{
$provider = $this->getFailedJobProvider();

$provider->log('connection-1', 'queue-1', json_encode(['uuid' => 'uuid-1']), new RuntimeException());
$provider->log('connection-2', 'queue-2', json_encode(['uuid' => 'uuid-2']), new RuntimeException());

$this->assertCount(2, $provider->all());

$provider->flush();

$this->assertEmpty($provider->all());
}

public function testPruningFailedJobs()
{
$provider = $this->getFailedJobProvider();

Carbon::setTestNow(Carbon::createFromDate(2024, 4, 28));

$provider->log('connection-1', 'queue-1', json_encode(['uuid' => 'uuid-1']), new RuntimeException());
$provider->log('connection-2', 'queue-2', json_encode(['uuid' => 'uuid-2']), new RuntimeException());

$provider->prune(Carbon::createFromDate(2024, 4, 26));

$this->assertCount(2, $provider->all());

$provider->prune(Carbon::createFromDate(2024, 4, 30));

$this->assertEmpty($provider->all());
}

public function testJobsCanBeCounted()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
$table->uuid();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
$provider = new DatabaseUuidFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
$provider = $this->getFailedJobProvider();

$this->assertSame(0, $provider->count());

Expand All @@ -40,20 +118,7 @@ public function testJobsCanBeCounted()

public function testJobsCanBeCountedByConnection()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
$table->uuid();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
$provider = new DatabaseUuidFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
$provider = $this->getFailedJobProvider();

$provider->log('connection-1', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-2', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
Expand All @@ -67,20 +132,7 @@ public function testJobsCanBeCountedByConnection()

public function testJobsCanBeCountedByQueue()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
$table->uuid();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
$provider = new DatabaseUuidFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
$provider = $this->getFailedJobProvider();

$provider->log('database', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('database', 'queue-2', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
Expand All @@ -93,6 +145,22 @@ public function testJobsCanBeCountedByQueue()
}

public function testJobsCanBeCountedByQueueAndConnection()
{
$provider = $this->getFailedJobProvider();

$provider->log('connection-1', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-1', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-2', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-1', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-2', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-2', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$this->assertSame(2, $provider->count('connection-1', 'queue-99'));
$this->assertSame(1, $provider->count('connection-2', 'queue-99'));
$this->assertSame(1, $provider->count('connection-1', 'queue-1'));
$this->assertSame(2, $provider->count('connection-2', 'queue-1'));
}

protected function getFailedJobProvider(string $database = 'default', string $table = 'failed_jobs')
{
$db = new DB;
$db->addConnection([
Expand All @@ -107,17 +175,6 @@ public function testJobsCanBeCountedByQueueAndConnection()
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
$provider = new DatabaseUuidFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');

$provider->log('connection-1', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-1', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-2', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-1', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-2', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('connection-2', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$this->assertSame(2, $provider->count('connection-1', 'queue-99'));
$this->assertSame(1, $provider->count('connection-2', 'queue-99'));
$this->assertSame(1, $provider->count('connection-1', 'queue-1'));
$this->assertSame(2, $provider->count('connection-2', 'queue-1'));
return new DatabaseUuidFailedJobProvider($db->getDatabaseManager(), $database, $table);
}
}

0 comments on commit 4758c19

Please sign in to comment.