Skip to content

Commit

Permalink
Add Conditionable to Batched and Chained jobs (#49310)
Browse files Browse the repository at this point in the history
* Add Conditionable to Batched and Chained jobs

* styleci

* styleci

---------

Co-authored-by: Brett Bailey <bretto36@gmail.com>
  • Loading branch information
bretto36 and Brett Bailey committed Dec 11, 2023
1 parent 0d5072b commit 4fe214d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Illuminate/Bus/PendingBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Foundation/Bus/PendingChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Integration/Queue/JobChainingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4fe214d

Please sign in to comment.