Skip to content

Commit ca0617a

Browse files
authored
[11.x] Refactor and add remaining test cases for the DatabaseFailedJobProviderTest class (#53409)
* create a database and a provider in separate methods. * add a method to create records in the failed_jobs table. * add test cases for the remaining methods of the DatabaseFailedJobProvider class. * rename some test cases and refactor to array_map in the DatabaseFailedJobProviderTest class. * attempt to fix according to StyleCI.
1 parent fb0e5c0 commit ca0617a

File tree

1 file changed

+152
-112
lines changed

1 file changed

+152
-112
lines changed

tests/Queue/DatabaseFailedJobProviderTest.php

Lines changed: 152 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -6,180 +6,220 @@
66
use Illuminate\Database\Capsule\Manager as DB;
77
use Illuminate\Database\Schema\Blueprint;
88
use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
9+
use Illuminate\Support\Carbon;
910
use Illuminate\Support\Facades\Date;
1011
use Illuminate\Support\Str;
1112
use PHPUnit\Framework\TestCase;
1213
use RuntimeException;
1314

1415
class DatabaseFailedJobProviderTest extends TestCase
1516
{
16-
public function testCanFlushFailedJobs()
17+
protected $db;
18+
19+
protected $provider;
20+
21+
public function setUp(): void
1722
{
18-
Date::setTestNow(Date::now());
23+
parent::setUp();
24+
$this->createDatabaseWithFailedJobTable()
25+
->createProvider();
26+
}
1927

20-
$db = new DB;
21-
$db->addConnection([
22-
'driver' => 'sqlite',
23-
'database' => ':memory:',
24-
]);
28+
public function testCanGetAllFailedJobIds()
29+
{
30+
$this->assertEmpty($this->provider->ids());
2531

26-
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
27-
$table->id();
28-
$table->timestamp('failed_at')->useCurrent();
29-
});
32+
array_map(fn () => $this->createFailedJobsRecord(), range(1, 4));
3033

31-
$provider = new DatabaseFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
34+
$this->assertCount(4, $this->provider->ids());
35+
$this->assertSame([4, 3, 2, 1], $this->provider->ids());
36+
}
3237

33-
$db->getConnection()->table('failed_jobs')->insert(['failed_at' => Date::now()->subDays(10)]);
34-
$provider->flush();
35-
$this->assertSame(0, $db->getConnection()->table('failed_jobs')->count());
38+
public function testCanGetAllFailedJobs()
39+
{
40+
$this->assertEmpty($this->provider->all());
3641

37-
$db->getConnection()->table('failed_jobs')->insert(['failed_at' => Date::now()->subDays(10)]);
38-
$provider->flush(15 * 24);
39-
$this->assertSame(1, $db->getConnection()->table('failed_jobs')->count());
42+
array_map(fn () => $this->createFailedJobsRecord(), range(1, 4));
4043

41-
$db->getConnection()->table('failed_jobs')->insert(['failed_at' => Date::now()->subDays(10)]);
42-
$provider->flush(10 * 24);
43-
$this->assertSame(0, $db->getConnection()->table('failed_jobs')->count());
44+
$this->assertCount(4, $this->provider->all());
45+
$this->assertSame(3, $this->provider->all()[1]->id);
46+
$this->assertSame('default', $this->provider->all()[1]->queue);
4447
}
4548

46-
public function testCanProperlyLogFailedJob()
49+
public function testCanRetrieveFailedJobsById()
4750
{
48-
$db = new DB;
49-
$db->addConnection([
50-
'driver' => 'sqlite',
51-
'database' => ':memory:',
52-
]);
51+
array_map(fn () => $this->createFailedJobsRecord(), range(1, 2));
5352

54-
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
55-
$table->id();
56-
$table->text('connection');
57-
$table->text('queue');
58-
$table->longText('payload');
59-
$table->longText('exception');
60-
$table->timestamp('failed_at')->useCurrent();
61-
});
53+
$this->assertNotNull($this->provider->find(1));
54+
$this->assertNotNull($this->provider->find(2));
55+
$this->assertNull($this->provider->find(3));
56+
}
6257

63-
$uuid = Str::uuid();
58+
public function testCanRemoveFailedJobsById()
59+
{
60+
$this->createFailedJobsRecord();
61+
62+
$this->assertFalse($this->provider->forget(2));
63+
$this->assertSame(1, $this->failedJobsTable()->count());
64+
$this->assertTrue($this->provider->forget(1));
65+
$this->assertSame(0, $this->failedJobsTable()->count());
66+
}
67+
68+
public function testCanPruneFailedJobs()
69+
{
70+
Carbon::setTestNow(Carbon::createFromDate(2024, 4, 28));
71+
72+
$this->createFailedJobsRecord(['failed_at' => Carbon::createFromDate(2024, 4, 24)]);
73+
$this->createFailedJobsRecord(['failed_at' => Carbon::createFromDate(2024, 4, 26)]);
6474

75+
$this->provider->prune(Carbon::createFromDate(2024, 4, 23));
76+
$this->assertSame(2, $this->failedJobsTable()->count());
77+
78+
$this->provider->prune(Carbon::createFromDate(2024, 4, 25));
79+
$this->assertSame(1, $this->failedJobsTable()->count());
80+
81+
$this->provider->prune(Carbon::createFromDate(2024, 4, 30));
82+
$this->assertSame(0, $this->failedJobsTable()->count());
83+
}
84+
85+
public function testCanFlushFailedJobs()
86+
{
87+
Date::setTestNow(Date::now());
88+
89+
$this->createFailedJobsRecord(['failed_at' => Date::now()->subDays(10)]);
90+
$this->provider->flush();
91+
$this->assertSame(0, $this->failedJobsTable()->count());
92+
93+
$this->createFailedJobsRecord(['failed_at' => Date::now()->subDays(10)]);
94+
$this->provider->flush(15 * 24);
95+
$this->assertSame(1, $this->failedJobsTable()->count());
96+
97+
$this->createFailedJobsRecord(['failed_at' => Date::now()->subDays(10)]);
98+
$this->provider->flush(10 * 24);
99+
$this->assertSame(0, $this->failedJobsTable()->count());
100+
}
101+
102+
public function testCanProperlyLogFailedJob()
103+
{
104+
$uuid = Str::uuid();
65105
$exception = new Exception(mb_convert_encoding('ÐÑÙ0E\xE2\x�98\xA0World��7B¹!þÿ', 'ISO-8859-1', 'UTF-8'));
66-
$provider = new DatabaseFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
67106

68-
$provider->log('database', 'default', json_encode(['uuid' => (string) $uuid]), $exception);
107+
$this->provider->log('database', 'default', json_encode(['uuid' => (string) $uuid]), $exception);
69108

70109
$exception = (string) mb_convert_encoding($exception, 'UTF-8');
71110

72-
$this->assertSame(1, $db->getConnection()->table('failed_jobs')->count());
73-
$this->assertSame($exception, $db->getConnection()->table('failed_jobs')->first()->exception);
111+
$this->assertSame(1, $this->failedJobsTable()->count());
112+
$this->assertSame($exception, $this->failedJobsTable()->first()->exception);
74113
}
75114

76115
public function testJobsCanBeCounted()
77116
{
78-
$db = new DB;
79-
$db->addConnection([
80-
'driver' => 'sqlite',
81-
'database' => ':memory:',
82-
]);
83-
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
84-
$table->id();
85-
$table->text('connection');
86-
$table->text('queue');
87-
$table->longText('payload');
88-
$table->longText('exception');
89-
$table->timestamp('failed_at')->useCurrent();
90-
});
91-
$provider = new DatabaseFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
92-
93-
$this->assertSame(0, $provider->count());
117+
$this->assertSame(0, $this->provider->count());
94118

95-
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
96-
$this->assertSame(1, $provider->count());
119+
$this->provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
120+
$this->assertSame(1, $this->provider->count());
97121

98-
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
99-
$provider->log('another-connection', 'another-queue', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
100-
$this->assertSame(3, $provider->count());
122+
$this->provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
123+
$this->provider->log('another-connection', 'another-queue', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
124+
$this->assertSame(3, $this->provider->count());
101125
}
102126

103127
public function testJobsCanBeCountedByConnection()
104128
{
105-
$db = new DB;
106-
$db->addConnection([
107-
'driver' => 'sqlite',
108-
'database' => ':memory:',
109-
]);
110-
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
111-
$table->id();
112-
$table->text('connection');
113-
$table->text('queue');
114-
$table->longText('payload');
115-
$table->longText('exception');
116-
$table->timestamp('failed_at')->useCurrent();
117-
});
118-
$provider = new DatabaseFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
129+
$this->provider->log('connection-1', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
130+
$this->provider->log('connection-2', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
131+
$this->assertSame(1, $this->provider->count('connection-1'));
132+
$this->assertSame(1, $this->provider->count('connection-2'));
133+
134+
$this->provider->log('connection-1', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
135+
$this->assertSame(2, $this->provider->count('connection-1'));
136+
$this->assertSame(1, $this->provider->count('connection-2'));
137+
}
119138

120-
$provider->log('connection-1', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
121-
$provider->log('connection-2', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
122-
$this->assertSame(1, $provider->count('connection-1'));
123-
$this->assertSame(1, $provider->count('connection-2'));
139+
public function testJobsCanBeCountedByQueue()
140+
{
141+
$this->provider->log('database', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
142+
$this->provider->log('database', 'queue-2', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
143+
$this->assertSame(1, $this->provider->count(queue: 'queue-1'));
144+
$this->assertSame(1, $this->provider->count(queue: 'queue-2'));
145+
146+
$this->provider->log('database', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
147+
$this->assertSame(2, $this->provider->count(queue: 'queue-1'));
148+
$this->assertSame(1, $this->provider->count(queue: 'queue-2'));
149+
}
124150

125-
$provider->log('connection-1', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
126-
$this->assertSame(2, $provider->count('connection-1'));
127-
$this->assertSame(1, $provider->count('connection-2'));
151+
public function testJobsCanBeCountedByQueueAndConnection()
152+
{
153+
$this->provider->log('connection-1', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
154+
$this->provider->log('connection-1', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
155+
$this->provider->log('connection-2', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
156+
$this->provider->log('connection-1', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
157+
$this->provider->log('connection-2', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
158+
$this->provider->log('connection-2', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
159+
160+
$this->assertSame(2, $this->provider->count('connection-1', 'queue-99'));
161+
$this->assertSame(1, $this->provider->count('connection-2', 'queue-99'));
162+
$this->assertSame(1, $this->provider->count('connection-1', 'queue-1'));
163+
$this->assertSame(2, $this->provider->count('connection-2', 'queue-1'));
128164
}
129165

130-
public function testJobsCanBeCountedByQueue()
166+
protected function createSimpleDatabaseWithFailedJobTable()
131167
{
132168
$db = new DB;
133169
$db->addConnection([
134170
'driver' => 'sqlite',
135171
'database' => ':memory:',
136172
]);
173+
137174
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
138175
$table->id();
139-
$table->text('connection');
140-
$table->text('queue');
141-
$table->longText('payload');
142-
$table->longText('exception');
143176
$table->timestamp('failed_at')->useCurrent();
144177
});
145-
$provider = new DatabaseFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
146-
147-
$provider->log('database', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
148-
$provider->log('database', 'queue-2', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
149-
$this->assertSame(1, $provider->count(queue: 'queue-1'));
150-
$this->assertSame(1, $provider->count(queue: 'queue-2'));
151178

152-
$provider->log('database', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
153-
$this->assertSame(2, $provider->count(queue: 'queue-1'));
154-
$this->assertSame(1, $provider->count(queue: 'queue-2'));
179+
return $db;
155180
}
156181

157-
public function testJobsCanBeCountedByQueueAndConnection()
182+
protected function createDatabaseWithFailedJobTable()
158183
{
159-
$db = new DB;
160-
$db->addConnection([
184+
$this->db = new DB;
185+
$this->db->addConnection([
161186
'driver' => 'sqlite',
162187
'database' => ':memory:',
163188
]);
164-
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
189+
190+
$this->db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
165191
$table->id();
166192
$table->text('connection');
167193
$table->text('queue');
168194
$table->longText('payload');
169195
$table->longText('exception');
170196
$table->timestamp('failed_at')->useCurrent();
171197
});
172-
$provider = new DatabaseFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
173-
174-
$provider->log('connection-1', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
175-
$provider->log('connection-1', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
176-
$provider->log('connection-2', 'queue-99', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
177-
$provider->log('connection-1', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
178-
$provider->log('connection-2', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
179-
$provider->log('connection-2', 'queue-1', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
180-
$this->assertSame(2, $provider->count('connection-1', 'queue-99'));
181-
$this->assertSame(1, $provider->count('connection-2', 'queue-99'));
182-
$this->assertSame(1, $provider->count('connection-1', 'queue-1'));
183-
$this->assertSame(2, $provider->count('connection-2', 'queue-1'));
198+
199+
return $this;
200+
}
201+
202+
protected function createProvider(string $database = 'default', string $table = 'failed_jobs')
203+
{
204+
$this->provider = new DatabaseFailedJobProvider($this->db->getDatabaseManager(), $database, $table);
205+
206+
return $this;
207+
}
208+
209+
protected function failedJobsTable()
210+
{
211+
return $this->db->getConnection()->table('failed_jobs');
212+
}
213+
214+
protected function createFailedJobsRecord(array $overrides = [])
215+
{
216+
return $this->failedJobsTable()
217+
->insert(array_merge([
218+
'connection' => 'database',
219+
'queue' => 'default',
220+
'payload' => json_encode(['uuid' => (string) Str::uuid()]),
221+
'exception' => new Exception('Whoops!'),
222+
'failed_at' => Date::now()->subDays(10),
223+
], $overrides));
184224
}
185225
}

0 commit comments

Comments
 (0)