Skip to content

Commit 4fe214d

Browse files
bretto36Brett Bailey
andauthored
Add Conditionable to Batched and Chained jobs (#49310)
* Add Conditionable to Batched and Chained jobs * styleci * styleci --------- Co-authored-by: Brett Bailey <[email protected]>
1 parent 0d5072b commit 4fe214d

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Illuminate/Bus/PendingBatch.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
99
use Illuminate\Support\Arr;
1010
use Illuminate\Support\Collection;
11+
use Illuminate\Support\Traits\Conditionable;
1112
use Laravel\SerializableClosure\SerializableClosure;
1213
use Throwable;
1314

1415
class PendingBatch
1516
{
17+
use Conditionable;
18+
1619
/**
1720
* The IoC container instance.
1821
*

src/Illuminate/Foundation/Bus/PendingChain.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
use Closure;
66
use Illuminate\Contracts\Bus\Dispatcher;
77
use Illuminate\Queue\CallQueuedClosure;
8+
use Illuminate\Support\Traits\Conditionable;
89
use Laravel\SerializableClosure\SerializableClosure;
910

1011
class PendingChain
1112
{
13+
use Conditionable;
14+
1215
/**
1316
* The class name of the job being dispatched.
1417
*

tests/Integration/Queue/JobChainingTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Illuminate\Tests\Integration\Queue;
44

55
use Illuminate\Bus\Batchable;
6+
use Illuminate\Bus\PendingBatch;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Contracts\Queue\ShouldQueue;
89
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Foundation\Bus\PendingChain;
911
use Illuminate\Foundation\Testing\DatabaseMigrations;
1012
use Illuminate\Queue\InteractsWithQueue;
1113
use Illuminate\Support\Carbon;
@@ -387,6 +389,43 @@ public function testChainBatchFailureNotAllowed()
387389
$this->assertEquals(['c1', 'c2', 'b1', 'b3'], JobRunRecorder::$results);
388390
$this->assertEquals(['batch failed', 'chain failed'], JobRunRecorder::$failures);
389391
}
392+
393+
public function testChainConditionable()
394+
{
395+
$chain = Bus::chain([])
396+
->onConnection('sync1')
397+
->when(true, function (PendingChain $chain) {
398+
$chain->onConnection('sync2');
399+
});
400+
401+
$this->assertEquals('sync2', $chain->connection);
402+
403+
$chain = Bus::chain([])
404+
->onConnection('sync1')
405+
->when(false, function (PendingChain $chain) {
406+
$chain->onConnection('sync2');
407+
});
408+
409+
$this->assertEquals('sync1', $chain->connection);
410+
}
411+
412+
public function testBatchConditionable()
413+
{
414+
$batch = Bus::batch([])
415+
->onConnection('sync1')
416+
->when(true, function (PendingBatch $batch) {
417+
$batch->onConnection('sync2');
418+
});
419+
420+
$this->assertEquals('sync2', $batch->connection());
421+
$batch = Bus::batch([])
422+
->onConnection('sync1')
423+
->when(false, function (PendingBatch $batch) {
424+
$batch->onConnection('sync2');
425+
});
426+
427+
$this->assertEquals('sync1', $batch->connection());
428+
}
390429
}
391430

392431
class JobChainingTestFirstJob implements ShouldQueue

0 commit comments

Comments
 (0)