Skip to content

Commit 8f48538

Browse files
dispatchIf() and dispatchUnless() for job chains (#49624)
1 parent 1c496d0 commit 8f48538

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/Illuminate/Foundation/Bus/PendingChain.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function catchCallbacks()
132132
}
133133

134134
/**
135-
* Dispatch the job with the given arguments.
135+
* Dispatch the job chain.
136136
*
137137
* @return \Illuminate\Foundation\Bus\PendingDispatch
138138
*/
@@ -165,4 +165,26 @@ public function dispatch()
165165

166166
return app(Dispatcher::class)->dispatch($firstJob);
167167
}
168+
169+
/**
170+
* Dispatch the job chain if the given truth test passes.
171+
*
172+
* @param bool|\Closure $boolean
173+
* @return \Illuminate\Foundation\Bus\PendingDispatch|null
174+
*/
175+
public function dispatchIf($boolean)
176+
{
177+
return value($boolean) ? $this->dispatch() : null;
178+
}
179+
180+
/**
181+
* Dispatch the job chain unless the given truth test passes.
182+
*
183+
* @param bool|\Closure $boolean
184+
* @return \Illuminate\Foundation\Bus\PendingDispatch|null
185+
*/
186+
public function dispatchUnless($boolean)
187+
{
188+
return ! value($boolean) ? $this->dispatch() : null;
189+
}
168190
}

tests/Integration/Queue/JobChainingTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,54 @@ public function testBatchConditionable()
495495

496496
$this->assertEquals('sync1', $batch->connection());
497497
}
498+
499+
public function testJobsAreChainedWhenDispatchIfIsTrue()
500+
{
501+
JobChainingTestFirstJob::withChain([
502+
new JobChainingTestSecondJob,
503+
])->dispatchIf(true);
504+
505+
$this->runQueueWorkerCommand(['--stop-when-empty' => true]);
506+
507+
$this->assertTrue(JobChainingTestFirstJob::$ran);
508+
$this->assertTrue(JobChainingTestSecondJob::$ran);
509+
}
510+
511+
public function testJobsAreNotChainedWhenDispatchIfIsFalse()
512+
{
513+
JobChainingTestFirstJob::withChain([
514+
new JobChainingTestSecondJob,
515+
])->dispatchIf(false);
516+
517+
$this->runQueueWorkerCommand(['--stop-when-empty' => true]);
518+
519+
$this->assertFalse(JobChainingTestFirstJob::$ran);
520+
$this->assertFalse(JobChainingTestSecondJob::$ran);
521+
}
522+
523+
public function testJobsAreChainedWhenDispatchUnlessIsFalse()
524+
{
525+
JobChainingTestFirstJob::withChain([
526+
new JobChainingTestSecondJob,
527+
])->dispatchUnless(false);
528+
529+
$this->runQueueWorkerCommand(['--stop-when-empty' => true]);
530+
531+
$this->assertTrue(JobChainingTestFirstJob::$ran);
532+
$this->assertTrue(JobChainingTestSecondJob::$ran);
533+
}
534+
535+
public function testJobsAreNotChainedWhenDispatchUnlessIsTrue()
536+
{
537+
JobChainingTestFirstJob::withChain([
538+
new JobChainingTestSecondJob,
539+
])->dispatchUnless(true);
540+
541+
$this->runQueueWorkerCommand(['--stop-when-empty' => true]);
542+
543+
$this->assertFalse(JobChainingTestFirstJob::$ran);
544+
$this->assertFalse(JobChainingTestSecondJob::$ran);
545+
}
498546
}
499547

500548
class JobChainingTestFirstJob implements ShouldQueue

0 commit comments

Comments
 (0)