|
6 | 6 | use Illuminate\Database\Capsule\Manager as DB; |
7 | 7 | use Illuminate\Database\Schema\Blueprint; |
8 | 8 | use Illuminate\Queue\Failed\DatabaseFailedJobProvider; |
| 9 | +use Illuminate\Support\Carbon; |
9 | 10 | use Illuminate\Support\Facades\Date; |
10 | 11 | use Illuminate\Support\Str; |
11 | 12 | use PHPUnit\Framework\TestCase; |
12 | 13 | use RuntimeException; |
13 | 14 |
|
14 | 15 | class DatabaseFailedJobProviderTest extends TestCase |
15 | 16 | { |
16 | | - public function testCanFlushFailedJobs() |
| 17 | + protected $db; |
| 18 | + |
| 19 | + protected $provider; |
| 20 | + |
| 21 | + public function setUp(): void |
17 | 22 | { |
18 | | - Date::setTestNow(Date::now()); |
| 23 | + parent::setUp(); |
| 24 | + $this->createDatabaseWithFailedJobTable() |
| 25 | + ->createProvider(); |
| 26 | + } |
19 | 27 |
|
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()); |
25 | 31 |
|
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)); |
30 | 33 |
|
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 | + } |
32 | 37 |
|
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()); |
36 | 41 |
|
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)); |
40 | 43 |
|
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); |
44 | 47 | } |
45 | 48 |
|
46 | | - public function testCanProperlyLogFailedJob() |
| 49 | + public function testCanRetrieveFailedJobsById() |
47 | 50 | { |
48 | | - $db = new DB; |
49 | | - $db->addConnection([ |
50 | | - 'driver' => 'sqlite', |
51 | | - 'database' => ':memory:', |
52 | | - ]); |
| 51 | + array_map(fn () => $this->createFailedJobsRecord(), range(1, 2)); |
53 | 52 |
|
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 | + } |
62 | 57 |
|
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)]); |
64 | 74 |
|
| 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(); |
65 | 105 | $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'); |
67 | 106 |
|
68 | | - $provider->log('database', 'default', json_encode(['uuid' => (string) $uuid]), $exception); |
| 107 | + $this->provider->log('database', 'default', json_encode(['uuid' => (string) $uuid]), $exception); |
69 | 108 |
|
70 | 109 | $exception = (string) mb_convert_encoding($exception, 'UTF-8'); |
71 | 110 |
|
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); |
74 | 113 | } |
75 | 114 |
|
76 | 115 | public function testJobsCanBeCounted() |
77 | 116 | { |
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()); |
94 | 118 |
|
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()); |
97 | 121 |
|
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()); |
101 | 125 | } |
102 | 126 |
|
103 | 127 | public function testJobsCanBeCountedByConnection() |
104 | 128 | { |
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 | + } |
119 | 138 |
|
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 | + } |
124 | 150 |
|
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')); |
128 | 164 | } |
129 | 165 |
|
130 | | - public function testJobsCanBeCountedByQueue() |
| 166 | + protected function createSimpleDatabaseWithFailedJobTable() |
131 | 167 | { |
132 | 168 | $db = new DB; |
133 | 169 | $db->addConnection([ |
134 | 170 | 'driver' => 'sqlite', |
135 | 171 | 'database' => ':memory:', |
136 | 172 | ]); |
| 173 | + |
137 | 174 | $db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) { |
138 | 175 | $table->id(); |
139 | | - $table->text('connection'); |
140 | | - $table->text('queue'); |
141 | | - $table->longText('payload'); |
142 | | - $table->longText('exception'); |
143 | 176 | $table->timestamp('failed_at')->useCurrent(); |
144 | 177 | }); |
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')); |
151 | 178 |
|
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; |
155 | 180 | } |
156 | 181 |
|
157 | | - public function testJobsCanBeCountedByQueueAndConnection() |
| 182 | + protected function createDatabaseWithFailedJobTable() |
158 | 183 | { |
159 | | - $db = new DB; |
160 | | - $db->addConnection([ |
| 184 | + $this->db = new DB; |
| 185 | + $this->db->addConnection([ |
161 | 186 | 'driver' => 'sqlite', |
162 | 187 | 'database' => ':memory:', |
163 | 188 | ]); |
164 | | - $db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) { |
| 189 | + |
| 190 | + $this->db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) { |
165 | 191 | $table->id(); |
166 | 192 | $table->text('connection'); |
167 | 193 | $table->text('queue'); |
168 | 194 | $table->longText('payload'); |
169 | 195 | $table->longText('exception'); |
170 | 196 | $table->timestamp('failed_at')->useCurrent(); |
171 | 197 | }); |
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)); |
184 | 224 | } |
185 | 225 | } |
0 commit comments