|
4 | 4 |
|
5 | 5 | use Exception; |
6 | 6 | use Illuminate\Bus\Queueable; |
| 7 | +use Illuminate\Bus\UniqueLock; |
7 | 8 | use Illuminate\Container\Container; |
8 | 9 | use Illuminate\Contracts\Cache\Repository as Cache; |
9 | 10 | use Illuminate\Contracts\Queue\ShouldBeUnique; |
@@ -169,6 +170,62 @@ protected function getLockKey($job) |
169 | 170 | { |
170 | 171 | return 'laravel_unique_job:'.(is_string($job) ? $job : get_class($job)).':'; |
171 | 172 | } |
| 173 | + |
| 174 | + public function testLockUsesDisplayNameWhenAvailable() |
| 175 | + { |
| 176 | + Bus::fake(); |
| 177 | + |
| 178 | + $lockKey = 'laravel_unique_job:App\\Actions\\UniqueTestAction:'; |
| 179 | + |
| 180 | + dispatch(new UniqueTestJobWithDisplayName); |
| 181 | + $this->runQueueWorkerCommand(['--once' => true]); |
| 182 | + Bus::assertDispatched(UniqueTestJobWithDisplayName::class); |
| 183 | + |
| 184 | + $this->assertFalse( |
| 185 | + $this->app->get(Cache::class)->lock($lockKey, 10)->get() |
| 186 | + ); |
| 187 | + |
| 188 | + Bus::assertDispatchedTimes(UniqueTestJobWithDisplayName::class); |
| 189 | + dispatch(new UniqueTestJobWithDisplayName); |
| 190 | + $this->runQueueWorkerCommand(['--once' => true]); |
| 191 | + Bus::assertDispatchedTimes(UniqueTestJobWithDisplayName::class); |
| 192 | + |
| 193 | + $this->assertFalse( |
| 194 | + $this->app->get(Cache::class)->lock($lockKey, 10)->get() |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + public function testUniqueLockCreatesKeyWithClassName() |
| 199 | + { |
| 200 | + $this->assertEquals( |
| 201 | + 'laravel_unique_job:'.UniqueTestJob::class.':', |
| 202 | + UniqueLock::getKey(new UniqueTestJob) |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + public function testUniqueLockCreatesKeyWithIdAndClassName() |
| 207 | + { |
| 208 | + $this->assertEquals( |
| 209 | + 'laravel_unique_job:'.UniqueIdTestJob::class.':unique-id-1', |
| 210 | + UniqueLock::getKey(new UniqueIdTestJob) |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + public function testUniqueLockCreatesKeyWithDisplayNameWhenAvailable() |
| 215 | + { |
| 216 | + $this->assertEquals( |
| 217 | + 'laravel_unique_job:App\\Actions\\UniqueTestAction:unique-id-2', |
| 218 | + UniqueLock::getKey(new UniqueIdTestJobWithDisplayName) |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + public function testUniqueLockCreatesKeyWithIdAndDisplayNameWhenAvailable() |
| 223 | + { |
| 224 | + $this->assertEquals( |
| 225 | + 'laravel_unique_job:App\\Actions\\UniqueTestAction:unique-id-2', |
| 226 | + UniqueLock::getKey(new UniqueIdTestJobWithDisplayName) |
| 227 | + ); |
| 228 | + } |
172 | 229 | } |
173 | 230 |
|
174 | 231 | class UniqueTestJob implements ShouldQueue, ShouldBeUnique |
@@ -239,3 +296,32 @@ public function uniqueVia(): Cache |
239 | 296 | return Container::getInstance()->make(Cache::class); |
240 | 297 | } |
241 | 298 | } |
| 299 | + |
| 300 | +class UniqueIdTestJob extends UniqueTestJob |
| 301 | +{ |
| 302 | + public function uniqueId(): string |
| 303 | + { |
| 304 | + return 'unique-id-1'; |
| 305 | + } |
| 306 | +} |
| 307 | + |
| 308 | +class UniqueTestJobWithDisplayName extends UniqueTestJob |
| 309 | +{ |
| 310 | + public function displayName(): string |
| 311 | + { |
| 312 | + return 'App\\Actions\\UniqueTestAction'; |
| 313 | + } |
| 314 | +} |
| 315 | + |
| 316 | +class UniqueIdTestJobWithDisplayName extends UniqueTestJob |
| 317 | +{ |
| 318 | + public function uniqueId(): string |
| 319 | + { |
| 320 | + return 'unique-id-2'; |
| 321 | + } |
| 322 | + |
| 323 | + public function displayName(): string |
| 324 | + { |
| 325 | + return 'App\\Actions\\UniqueTestAction'; |
| 326 | + } |
| 327 | +} |
0 commit comments