From 2abb88f14a4472f62075ac809ba1294c0e2b28c9 Mon Sep 17 00:00:00 2001 From: Brett Bailey Date: Mon, 11 Dec 2023 09:42:46 +1000 Subject: [PATCH 1/3] Add Conditionable to Batched and Chained jobs --- src/Illuminate/Bus/PendingBatch.php | 3 + .../Foundation/Bus/PendingChain.php | 3 + tests/Integration/Queue/JobChainingTest.php | 57 ++++++++++++++++--- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Bus/PendingBatch.php b/src/Illuminate/Bus/PendingBatch.php index 6cd518311d21..463cebd9cf6b 100644 --- a/src/Illuminate/Bus/PendingBatch.php +++ b/src/Illuminate/Bus/PendingBatch.php @@ -8,11 +8,14 @@ use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use Illuminate\Support\Traits\Conditionable; use Laravel\SerializableClosure\SerializableClosure; use Throwable; class PendingBatch { + use Conditionable; + /** * The IoC container instance. * diff --git a/src/Illuminate/Foundation/Bus/PendingChain.php b/src/Illuminate/Foundation/Bus/PendingChain.php index b56b773ea9b8..8d3c6892615a 100644 --- a/src/Illuminate/Foundation/Bus/PendingChain.php +++ b/src/Illuminate/Foundation/Bus/PendingChain.php @@ -5,10 +5,13 @@ use Closure; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Queue\CallQueuedClosure; +use Illuminate\Support\Traits\Conditionable; use Laravel\SerializableClosure\SerializableClosure; class PendingChain { + use Conditionable; + /** * The class name of the job being dispatched. * diff --git a/tests/Integration/Queue/JobChainingTest.php b/tests/Integration/Queue/JobChainingTest.php index 9dec15912fbd..c36481225f93 100644 --- a/tests/Integration/Queue/JobChainingTest.php +++ b/tests/Integration/Queue/JobChainingTest.php @@ -3,9 +3,11 @@ namespace Illuminate\Tests\Integration\Queue; use Illuminate\Bus\Batchable; +use Illuminate\Bus\PendingBatch; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Foundation\Bus\PendingChain; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Carbon; @@ -387,6 +389,43 @@ public function testChainBatchFailureNotAllowed() $this->assertEquals(['c1', 'c2', 'b1', 'b3'], JobRunRecorder::$results); $this->assertEquals(['batch failed', 'chain failed'], JobRunRecorder::$failures); } + + public function testChainConditionable() + { + $chain = Bus::chain([]) + ->onConnection('sync1') + ->when(true, function (PendingChain $chain) { + $chain->onConnection('sync2'); + }); + + $this->assertEquals('sync2', $chain->connection); + + $chain = Bus::chain([]) + ->onConnection('sync1') + ->when(false, function (PendingChain $chain) { + $chain->onConnection('sync2'); + }); + + $this->assertEquals('sync1', $chain->connection); + } + + public function testBatchConditionable() + { + $batch = Bus::batch([]) + ->onConnection('sync1') + ->when(true, function (PendingBatch $batch) { + $batch->onConnection('sync2'); + }); + + $this->assertEquals('sync2', $batch->connection()); + $batch = Bus::batch([]) + ->onConnection('sync1') + ->when(false, function (PendingBatch $batch) { + $batch->onConnection('sync2'); + }); + + $this->assertEquals('sync1', $batch->connection()); + } } class JobChainingTestFirstJob implements ShouldQueue @@ -401,8 +440,8 @@ class JobChainingTestFirstJob implements ShouldQueue public function handle() { - static::$ran = true; - static::$usedQueue = $this->queue; + static::$ran = true; + static::$usedQueue = $this->queue; static::$usedConnection = $this->connection; } } @@ -419,8 +458,8 @@ class JobChainingTestSecondJob implements ShouldQueue public function handle() { - static::$ran = true; - static::$usedQueue = $this->queue; + static::$ran = true; + static::$usedQueue = $this->queue; static::$usedConnection = $this->connection; } } @@ -437,8 +476,8 @@ class JobChainingTestThirdJob implements ShouldQueue public function handle() { - static::$ran = true; - static::$usedQueue = $this->queue; + static::$ran = true; + static::$usedQueue = $this->queue; static::$usedConnection = $this->connection; } } @@ -561,14 +600,14 @@ class JobChainingTestBatchedJob implements ShouldQueue public function __construct(string $id, int $times = 0) { - $this->id = $id; + $this->id = $id; $this->times = $times; } public function handle() { for ($i = 0; $i < $this->times; $i++) { - $this->batch()->add(new JobChainingTestBatchedJob($this->id.'-'.$i)); + $this->batch()->add(new JobChainingTestBatchedJob($this->id . '-' . $i)); } JobRunRecorder::record($this->id); } @@ -604,7 +643,7 @@ public static function recordFailure(string $message) public static function reset() { - self::$results = []; + self::$results = []; self::$failures = []; } } From 349cf5bdf2c040d5cca49a36421a1ec4d7caf00a Mon Sep 17 00:00:00 2001 From: Brett Bailey Date: Mon, 11 Dec 2023 09:46:00 +1000 Subject: [PATCH 2/3] styleci --- tests/Integration/Queue/JobChainingTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Integration/Queue/JobChainingTest.php b/tests/Integration/Queue/JobChainingTest.php index c36481225f93..1ff8b7dee61e 100644 --- a/tests/Integration/Queue/JobChainingTest.php +++ b/tests/Integration/Queue/JobChainingTest.php @@ -440,8 +440,8 @@ class JobChainingTestFirstJob implements ShouldQueue public function handle() { - static::$ran = true; - static::$usedQueue = $this->queue; + static::$ran = true; + static::$usedQueue = $this->queue; static::$usedConnection = $this->connection; } } @@ -458,8 +458,8 @@ class JobChainingTestSecondJob implements ShouldQueue public function handle() { - static::$ran = true; - static::$usedQueue = $this->queue; + static::$ran = true; + static::$usedQueue = $this->queue; static::$usedConnection = $this->connection; } } @@ -476,8 +476,8 @@ class JobChainingTestThirdJob implements ShouldQueue public function handle() { - static::$ran = true; - static::$usedQueue = $this->queue; + static::$ran = true; + static::$usedQueue = $this->queue; static::$usedConnection = $this->connection; } } @@ -600,7 +600,7 @@ class JobChainingTestBatchedJob implements ShouldQueue public function __construct(string $id, int $times = 0) { - $this->id = $id; + $this->id = $id; $this->times = $times; } @@ -643,7 +643,7 @@ public static function recordFailure(string $message) public static function reset() { - self::$results = []; + self::$results = []; self::$failures = []; } } From a4610f45ae609ddd5ea207235e4ae6842eae16b3 Mon Sep 17 00:00:00 2001 From: Brett Bailey Date: Mon, 11 Dec 2023 09:46:37 +1000 Subject: [PATCH 3/3] styleci --- tests/Integration/Queue/JobChainingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Queue/JobChainingTest.php b/tests/Integration/Queue/JobChainingTest.php index 1ff8b7dee61e..3fa00618baa7 100644 --- a/tests/Integration/Queue/JobChainingTest.php +++ b/tests/Integration/Queue/JobChainingTest.php @@ -607,7 +607,7 @@ public function __construct(string $id, int $times = 0) public function handle() { for ($i = 0; $i < $this->times; $i++) { - $this->batch()->add(new JobChainingTestBatchedJob($this->id . '-' . $i)); + $this->batch()->add(new JobChainingTestBatchedJob($this->id.'-'.$i)); } JobRunRecorder::record($this->id); }