Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] fix adding jobs from iterable to the pending batch #41786

Merged
merged 3 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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