diff --git a/src/Illuminate/Foundation/Bus/Dispatchable.php b/src/Illuminate/Foundation/Bus/Dispatchable.php index 3e90e412026d..295c5fd44769 100644 --- a/src/Illuminate/Foundation/Bus/Dispatchable.php +++ b/src/Illuminate/Foundation/Bus/Dispatchable.php @@ -2,6 +2,7 @@ namespace Illuminate\Foundation\Bus; +use Closure; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Support\Fluent; @@ -20,13 +21,21 @@ 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; } @@ -34,13 +43,21 @@ public static function dispatchIf($boolean, ...$arguments) /** * 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; } diff --git a/tests/Integration/Queue/JobDispatchingTest.php b/tests/Integration/Queue/JobDispatchingTest.php index a910999aeda1..1ad23bd0f020 100644 --- a/tests/Integration/Queue/JobDispatchingTest.php +++ b/tests/Integration/Queue/JobDispatchingTest.php @@ -12,6 +12,7 @@ class JobDispatchingTest extends TestCase protected function tearDown(): void { Job::$ran = false; + Job::$value = null; } public function testJobCanUseCustomMethodsAfterDispatch() @@ -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