Skip to content

[12.x] Option to disable dispatchAfterResponse in a test #55456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Illuminate/Bus/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class Dispatcher implements QueueingDispatcher
*/
protected $queueResolver;

/**
* Indicates if dispatching after response is disabled.
*
* @var bool
*/
protected $allowsDispatchingAfterResponses = true;

/**
* Create a new command dispatcher instance.
*
Expand Down Expand Up @@ -252,6 +259,12 @@ protected function pushCommandToQueue($queue, $command)
*/
public function dispatchAfterResponse($command, $handler = null)
{
if (! $this->allowsDispatchingAfterResponses) {
$this->dispatchSync($command);

return;
}

$this->container->terminating(function () use ($command, $handler) {
$this->dispatchSync($command, $handler);
});
Expand Down Expand Up @@ -282,4 +295,28 @@ public function map(array $map)

return $this;
}

/**
* Allow dispatching after responses.
*
* @return $this
*/
public function withDispatchingAfterResponses()
{
$this->allowsDispatchingAfterResponses = true;

return $this;
}

/**
* Disable dispatching after responses.
*
* @return $this
*/
public function withoutDispatchingAfterResponses()
{
$this->allowsDispatchingAfterResponses = false;

return $this;
}
}
32 changes: 32 additions & 0 deletions tests/Integration/Queue/JobDispatchingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Queue\Events\JobQueued;
use Illuminate\Queue\Events\JobQueueing;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Config;
use Orchestra\Testbench\Attributes\WithMigration;

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

public function testCanDisableDispatchingAfterResponse()
{
Job::dispatchAfterResponse('test');

$this->assertFalse(Job::$ran);

$this->app->terminate();

$this->assertTrue(Job::$ran);

Bus::withoutDispatchingAfterResponses();

Job::$ran = false;
Job::dispatchAfterResponse('test');

$this->assertTrue(Job::$ran);

$this->app->terminate();

Bus::withDispatchingAfterResponses();

Job::$ran = false;
Job::dispatchAfterResponse('test');

$this->assertFalse(Job::$ran);

$this->app->terminate();

$this->assertTrue(Job::$ran);
}

/**
* Helpers.
*/
Expand Down