|
7 | 7 | use Illuminate\Contracts\Queue\ShouldQueue;
|
8 | 8 | use Illuminate\Events\CallQueuedListener;
|
9 | 9 | use Illuminate\Events\Dispatcher;
|
| 10 | +use Illuminate\Queue\QueueManager; |
10 | 11 | use Illuminate\Support\Testing\Fakes\QueueFake;
|
11 | 12 | use Mockery as m;
|
12 | 13 | use PHPUnit\Framework\TestCase;
|
@@ -84,6 +85,48 @@ public function testQueueIsSetByGetConnection()
|
84 | 85 | $d->dispatch('some.event', ['foo', 'bar']);
|
85 | 86 | }
|
86 | 87 |
|
| 88 | + public function testQueueIsSetByGetQueueDynamically() |
| 89 | + { |
| 90 | + $d = new Dispatcher; |
| 91 | + |
| 92 | + $fakeQueue = new QueueFake(new Container); |
| 93 | + |
| 94 | + $d->setQueueResolver(function () use ($fakeQueue) { |
| 95 | + return $fakeQueue; |
| 96 | + }); |
| 97 | + |
| 98 | + $d->listen('some.event', TestDispatcherGetQueueDynamically::class.'@handle'); |
| 99 | + $d->dispatch('some.event', [['useHighPriorityQueue' => true], 'bar']); |
| 100 | + |
| 101 | + $fakeQueue->assertPushedOn('p0', CallQueuedListener::class); |
| 102 | + } |
| 103 | + |
| 104 | + public function testQueueIsSetByGetConnectionDynamically() |
| 105 | + { |
| 106 | + $d = new Dispatcher; |
| 107 | + $queueManager = $this->createMock(QueueManager::class); |
| 108 | + $queue = $this->createMock(Queue::class); |
| 109 | + |
| 110 | + $queueManager->expects($this->once()) |
| 111 | + ->method('connection') |
| 112 | + ->with('redis') |
| 113 | + ->willReturn($queue); |
| 114 | + |
| 115 | + $queue->expects($this->once()) |
| 116 | + ->method('pushOn') |
| 117 | + ->with(null, $this->isInstanceOf(CallQueuedListener::class)); |
| 118 | + |
| 119 | + $d->setQueueResolver(function () use ($queueManager) { |
| 120 | + return $queueManager; |
| 121 | + }); |
| 122 | + |
| 123 | + $d->listen('some.event', TestDispatcherGetConnectionDynamically::class.'@handle'); |
| 124 | + $d->dispatch('some.event', [ |
| 125 | + ['shouldUseRedisConnection' => true], |
| 126 | + 'bar', |
| 127 | + ]); |
| 128 | + } |
| 129 | + |
87 | 130 | public function testQueuePropagateRetryUntilAndMaxExceptions()
|
88 | 131 | {
|
89 | 132 | $d = new Dispatcher;
|
@@ -220,3 +263,39 @@ public function handle($job, $next)
|
220 | 263 | $next($job);
|
221 | 264 | }
|
222 | 265 | }
|
| 266 | + |
| 267 | +class TestDispatcherGetConnectionDynamically implements ShouldQueue |
| 268 | +{ |
| 269 | + public function handle() |
| 270 | + { |
| 271 | + // |
| 272 | + } |
| 273 | + |
| 274 | + public function viaConnection($event) |
| 275 | + { |
| 276 | + if ($event['shouldUseRedisConnection']) { |
| 277 | + return 'redis'; |
| 278 | + } |
| 279 | + |
| 280 | + return 'sqs'; |
| 281 | + } |
| 282 | +} |
| 283 | + |
| 284 | +class TestDispatcherGetQueueDynamically implements ShouldQueue |
| 285 | +{ |
| 286 | + public $queue = 'my_queue'; |
| 287 | + |
| 288 | + public function handle() |
| 289 | + { |
| 290 | + // |
| 291 | + } |
| 292 | + |
| 293 | + public function viaQueue($event) |
| 294 | + { |
| 295 | + if ($event['useHighPriorityQueue']) { |
| 296 | + return 'p0'; |
| 297 | + } |
| 298 | + |
| 299 | + return 'p99'; |
| 300 | + } |
| 301 | +} |
0 commit comments