Skip to content

Commit 27dfc99

Browse files
committed
Add test for dispatch of deferred jobs
1 parent 6ecff0f commit 27dfc99

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/Illuminate/Foundation/Bus/Dispatchable.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public static function dispatchAfterResponse(...$arguments)
8787
return self::dispatch(...$arguments)->afterResponse();
8888
}
8989

90+
/**
91+
* Defer dispatching a command to its appropriate handler.
92+
*/
93+
public static function dispatchDefer(...$arguments)
94+
{
95+
return self::dispatch(...$arguments)->defer();
96+
}
97+
9098
/**
9199
* Set the jobs that should run if this job is successful.
92100
*

src/Illuminate/Foundation/Bus/PendingDispatch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PendingDispatch
2828
protected $afterResponse = false;
2929

3030
/**
31-
* Indicates if the job should be dispatched to the queue after the current request.
31+
* Indicates if the job dispatch should be deferred.
3232
*
3333
* @var bool
3434
*/
@@ -171,7 +171,7 @@ public function afterResponse()
171171
}
172172

173173
/**
174-
* Indicate that the job should be dispatched to the queue after the current request.
174+
* Indicate that the job dispatch should be deferred.
175175
*
176176
* @return $this
177177
*/

tests/Integration/Queue/JobDispatchingTest.php

Lines changed: 12 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\Defer\DeferredCallbackCollection;
1314
use Illuminate\Support\Facades\Bus;
1415
use Illuminate\Support\Facades\Config;
1516
use Orchestra\Testbench\Attributes\WithMigration;
@@ -139,6 +140,17 @@ public function testUniqueJobLockIsReleasedForJobDispatchedAfterResponse()
139140
$this->assertFalse(UniqueJob::$ran);
140141
}
141142

143+
public function testDispatchDeferDelaysDispatchingUntilDeferredCallbacksAreRun()
144+
{
145+
$this->assertFalse(Job::$ran);
146+
147+
Job::dispatchDefer('test');
148+
149+
$this->assertFalse(Job::$ran);
150+
$this->app[DeferredCallbackCollection::class]->invoke();
151+
$this->assertTrue(Job::$ran);
152+
}
153+
142154
public function testQueueMayBeNullForJobQueueingAndJobQueuedEvent()
143155
{
144156
Config::set('queue.default', 'database');

0 commit comments

Comments
 (0)