8000 [12.x] Option to disable dispatchAfterResponse in a test (#55456) · laravel/framework@6f55dbc · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f55dbc

Browse files
[12.x] Option to disable dispatchAfterResponse in a test (#55456)
* Option to disable/enable dispatchAfterresponse * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 90dc8f9 commit 6f55dbc

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Illuminate/Bus/Dispatcher.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ class Dispatcher implements QueueingDispatcher
5151
*/
5252
protected $queueResolver;
5353

54+
/**
55+
* Indicates if dispatching after response is disabled.
56+
*
57+
* @var bool
58+
*/
59+
protected $allowsDispatchingAfterResponses = true;
60+
5461
/**
5562
* Create a new command dispatcher instance.
5663
*
@@ -252,6 +259,12 @@ protected function pushCommandToQueue($queue, $command)
252259
*/
253260
public function dispatchAfterResponse($command, $handler = null)
254261
{
262+
if (! $this->allowsDispatchingAfterResponses) {
263+
$this->dispatchSync($command);
264+
265+
return;
266+
}
267+
255268
$this->container->terminating(function () use ($command, $handler) {
256269
$this->dispatchSync($command, $handler);
257270
});
@@ -282,4 +295,28 @@ public function map(array $map)
282295

283296
return $this;
284297
}
298+
299+
/**
300+
* Allow dispatching after responses.
301+
*
302+
* @return $this
303+
*/
304+
public function withDispatchingAfterResponses()
305+
{
306+
$this->allowsDispatchingAfterResponses = true;
307+
308+
return $this;
309+
}
310+
311+
/**
312+
* Disable dispatching after responses.
313+
*
314+
* @return $this
315+
*/
316+
public function withoutDispatchingAfterResponses()
317+
{
318+
$this->allowsDispatchingAfterResponses = false;
319+
320+
return $this;
321+
}
285322
}

tests/Integration/Queue/JobDispatchingTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Queue\Events\JobQueued;
1111
use Illuminate\Queue\Events\JobQueueing;
1212
use Illuminate\Queue\InteractsWithQueue;
13+
use Illuminate\Support\Facades\Bus;
1314
use Illuminate\Support\Facades\Config;
1415
use Orchestra\Testbench\Attributes\WithMigration;
1516

@@ -165,6 +166,37 @@ public function testQueueMayBeNullForJobQueueingAndJobQueuedEvent()
165166
$this->assertNull($events[3]->queue);
166167
}
167168

169+
public function testCanDisableDispatchingAfterResponse()
170+
{
171+
Job::dispatchAfterResponse('test');
172+
173+
$this->assertFalse(Job::$ran);
174+
175+
$this->app->terminate();
176+
177+
$this->assertTrue(Job::$ran);
178+
179+
Bus::withoutDispatchingAfterResponses();
180+
181+
Job::$ran = false;
182+
Job::dispatchAfterResponse('test');
183+
184+
$this->assertTrue(Job::$ran);
185+
186+
$this->app->terminate();
187+
188+
Bus::withDispatchingAfterResponses();
189+
190+
Job::$ran = false;
191+
Job::dispatchAfterResponse('test');
192+
193+
$this->assertFalse(Job::$ran);
194+
195+
$this->app->terminate();
196+
197+
$this->assertTrue(Job::$ran);
198+
}
199+
168200
/**
169201
* Helpers.
170202
*/

0 commit comments

Comments
 (0)
0