From 4758c1945c622331dce4ca8e98004701016e59bf Mon Sep 17 00:00:00 2001 From: Kevin Bui Date: Tue, 5 Nov 2024 10:41:13 +1000 Subject: [PATCH] [11.x] Refactor and add remaining test cases for the DatabaseUuidFailedJobProviderTest 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. --- .../DatabaseUuidFailedJobProviderTest.php | 165 ++++++++++++------ 1 file changed, 111 insertions(+), 54 deletions(-) diff --git a/tests/Queue/DatabaseUuidFailedJobProviderTest.php b/tests/Queue/DatabaseUuidFailedJobProviderTest.php index d99284a89986..3ced7361255c 100644 --- a/tests/Queue/DatabaseUuidFailedJobProviderTest.php +++ b/tests/Queue/DatabaseUuidFailedJobProviderTest.php @@ -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()); @@ -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()); @@ -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()); @@ -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([ @@ -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); } }