Skip to content

Commit

Permalink
[9.x] fix adding jobs from iterable to the pending batch (#41786)
Browse files Browse the repository at this point in the history
* fix adding jobs from iterable to the pending batch

* code style

* Update PendingBatch.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
romanpravda and taylorotwell committed Apr 2, 2022
1 parent 9879b90 commit 88e3617
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Bus/PendingBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ public function __construct(Container $container, Collection $jobs)
/**
* Add jobs to the batch.
*
* @param \Illuminate\Support\Enumerable|object|array $jobs
* @param iterable|object|array $jobs
* @return $this
*/
public function add($jobs)
{
foreach (Arr::wrap($jobs) as $job) {
$jobs = is_iterable($jobs) ? $jobs : Arr::wrap($jobs);

foreach ($jobs as $job) {
$this->jobs->push($job);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Bus/BusBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ public function test_jobs_can_be_added_to_pending_batch()
$this->assertCount(2, $batch->jobs);
}

public function test_jobs_can_be_added_to_the_pending_batch_from_iterable()
{
$batch = new PendingBatch(new Container, collect());
$this->assertCount(0, $batch->jobs);

$count = 3;
$generator = function (int $jobsCount) {
for ($i = 0; $i < $jobsCount; $i++) {
yield new class
{
use Batchable;
};
}
};

$batch->add($generator($count));
$this->assertCount($count, $batch->jobs);
}

public function test_processed_jobs_can_be_calculated()
{
$queue = m::mock(Factory::class);
Expand Down

0 comments on commit 88e3617

Please sign in to comment.