From 8f48538ed651173d4dcb3ee42c9c90453adc5869 Mon Sep 17 00:00:00 2001 From: Frankie Jarrett Date: Tue, 9 Jan 2024 16:05:18 -0600 Subject: [PATCH] dispatchIf() and dispatchUnless() for job chains (#49624) --- .../Foundation/Bus/PendingChain.php | 24 +++++++++- tests/Integration/Queue/JobChainingTest.php | 48 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Bus/PendingChain.php b/src/Illuminate/Foundation/Bus/PendingChain.php index 8d3c6892615a..2fb14990c56a 100644 --- a/src/Illuminate/Foundation/Bus/PendingChain.php +++ b/src/Illuminate/Foundation/Bus/PendingChain.php @@ -132,7 +132,7 @@ public function catchCallbacks() } /** - * Dispatch the job with the given arguments. + * Dispatch the job chain. * * @return \Illuminate\Foundation\Bus\PendingDispatch */ @@ -165,4 +165,26 @@ public function dispatch() return app(Dispatcher::class)->dispatch($firstJob); } + + /** + * Dispatch the job chain if the given truth test passes. + * + * @param bool|\Closure $boolean + * @return \Illuminate\Foundation\Bus\PendingDispatch|null + */ + public function dispatchIf($boolean) + { + return value($boolean) ? $this->dispatch() : null; + } + + /** + * Dispatch the job chain unless the given truth test passes. + * + * @param bool|\Closure $boolean + * @return \Illuminate\Foundation\Bus\PendingDispatch|null + */ + public function dispatchUnless($boolean) + { + return ! value($boolean) ? $this->dispatch() : null; + } } diff --git a/tests/Integration/Queue/JobChainingTest.php b/tests/Integration/Queue/JobChainingTest.php index 7037625b62bf..a742e7ec142d 100644 --- a/tests/Integration/Queue/JobChainingTest.php +++ b/tests/Integration/Queue/JobChainingTest.php @@ -495,6 +495,54 @@ public function testBatchConditionable() $this->assertEquals('sync1', $batch->connection()); } + + public function testJobsAreChainedWhenDispatchIfIsTrue() + { + JobChainingTestFirstJob::withChain([ + new JobChainingTestSecondJob, + ])->dispatchIf(true); + + $this->runQueueWorkerCommand(['--stop-when-empty' => true]); + + $this->assertTrue(JobChainingTestFirstJob::$ran); + $this->assertTrue(JobChainingTestSecondJob::$ran); + } + + public function testJobsAreNotChainedWhenDispatchIfIsFalse() + { + JobChainingTestFirstJob::withChain([ + new JobChainingTestSecondJob, + ])->dispatchIf(false); + + $this->runQueueWorkerCommand(['--stop-when-empty' => true]); + + $this->assertFalse(JobChainingTestFirstJob::$ran); + $this->assertFalse(JobChainingTestSecondJob::$ran); + } + + public function testJobsAreChainedWhenDispatchUnlessIsFalse() + { + JobChainingTestFirstJob::withChain([ + new JobChainingTestSecondJob, + ])->dispatchUnless(false); + + $this->runQueueWorkerCommand(['--stop-when-empty' => true]); + + $this->assertTrue(JobChainingTestFirstJob::$ran); + $this->assertTrue(JobChainingTestSecondJob::$ran); + } + + public function testJobsAreNotChainedWhenDispatchUnlessIsTrue() + { + JobChainingTestFirstJob::withChain([ + new JobChainingTestSecondJob, + ])->dispatchUnless(true); + + $this->runQueueWorkerCommand(['--stop-when-empty' => true]); + + $this->assertFalse(JobChainingTestFirstJob::$ran); + $this->assertFalse(JobChainingTestSecondJob::$ran); + } } class JobChainingTestFirstJob implements ShouldQueue