Skip to content

Commit

Permalink
dispatchIf() and dispatchUnless() for job chains (#49624)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejarrett authored Jan 9, 2024
1 parent 1c496d0 commit 8f48538
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Illuminate/Foundation/Bus/PendingChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function catchCallbacks()
}

/**
* Dispatch the job with the given arguments.
* Dispatch the job chain.
*
* @return \Illuminate\Foundation\Bus\PendingDispatch
*/
Expand Down Expand Up @@ -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;
}
}
48 changes: 48 additions & 0 deletions tests/Integration/Queue/JobChainingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f48538

Please sign in to comment.