Skip to content

Commit

Permalink
[9.x] Adds closure support to dispatch conditionals. (laravel#43784)
Browse files Browse the repository at this point in the history
* [9.x] Adds closure support to dispatch conditionals.

* Style fixes [skip ci]

* pass entire job

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
DarkGhostHunter and taylorotwell authored Aug 22, 2022
1 parent 8c77d8c commit 8453ed8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Illuminate/Foundation/Bus/Dispatchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Foundation\Bus;

use Closure;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Fluent;

Expand All @@ -20,27 +21,43 @@ public static function dispatch(...$arguments)
/**
* Dispatch the job with the given arguments if the given truth test passes.
*
* @param bool $boolean
* @param bool|\Closure $boolean
* @param mixed ...$arguments
* @return \Illuminate\Foundation\Bus\PendingDispatch|\Illuminate\Support\Fluent
*/
public static function dispatchIf($boolean, ...$arguments)
{
return $boolean
if ($boolean instanceof Closure) {
$dispatchable = new static(...$arguments);

return value($boolean, $dispatchable)
? new PendingDispatch($dispatchable)
: new Fluent;
}

return value($boolean)
? new PendingDispatch(new static(...$arguments))
: new Fluent;
}

/**
* Dispatch the job with the given arguments unless the given truth test passes.
*
* @param bool $boolean
* @param bool|\Closure $boolean
* @param mixed ...$arguments
* @return \Illuminate\Foundation\Bus\PendingDispatch|\Illuminate\Support\Fluent
*/
public static function dispatchUnless($boolean, ...$arguments)
{
return ! $boolean
if ($boolean instanceof Closure) {
$dispatchable = new static(...$arguments);

return ! value($boolean, $dispatchable)
? new PendingDispatch($dispatchable)
: new Fluent;
}

return ! value($boolean)
? new PendingDispatch(new static(...$arguments))
: new Fluent;
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Integration/Queue/JobDispatchingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class JobDispatchingTest extends TestCase
protected function tearDown(): void
{
Job::$ran = false;
Job::$value = null;
}

public function testJobCanUseCustomMethodsAfterDispatch()
Expand All @@ -21,6 +22,54 @@ public function testJobCanUseCustomMethodsAfterDispatch()
$this->assertTrue(Job::$ran);
$this->assertSame('new-test', Job::$value);
}

public function testDispatchesConditionallyWithBoolean()
{
Job::dispatchIf(false, 'test')->replaceValue('new-test');

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

Job::dispatchIf(true, 'test')->replaceValue('new-test');

$this->assertTrue(Job::$ran);
$this->assertSame('new-test', Job::$value);
}

public function testDispatchesConditionallyWithClosure()
{
Job::dispatchIf(fn ($job) => $job instanceof Job ? 0 : 1, 'test')->replaceValue('new-test');

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

Job::dispatchIf(fn ($job) => $job instanceof Job ? 1 : 0, 'test')->replaceValue('new-test');

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

public function testDoesNotDispatchesConditionallyWithBoolean()
{
Job::dispatchUnless(true, 'test')->replaceValue('new-test');

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

Job::dispatchUnless(false, 'test')->replaceValue('new-test');

$this->assertTrue(Job::$ran);
$this->assertSame('new-test', Job::$value);
}

public function testDoesNotDispatchesConditionallyWithClosure()
{
Job::dispatchUnless(fn ($job) => $job instanceof Job ? 1 : 0, 'test')->replaceValue('new-test');

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

Job::dispatchUnless(fn ($job) => $job instanceof Job ? 0 : 1, 'test')->replaceValue('new-test');

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

class Job implements ShouldQueue
Expand Down

0 comments on commit 8453ed8

Please sign in to comment.