Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Adds closure support to dispatch conditionals. #43784

Merged
merged 3 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/Illuminate/Foundation/Bus/Dispatchable.php
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
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