Skip to content

[9.x] Fake Batches - #44104

Merged
taylorotwell merged 7 commits into
9.xfrom
fake-batches
Sep 16, 2022
Merged

taylorotwell merged 7 commits into
9.xfrom
fake-batches

Conversation

@taylorotwell

@taylorotwell taylorotwell commented Sep 12, 2022

Copy link
Copy Markdown
Member

It's currently hard to test things like if a batch was cancelled by a job or if a job added additional jobs to a batch. You have to create a FakeBatch manually and override the cancel / add methods, etc.

This solves that.

[$job, $batch] = (new TestJob)->withFakeBatch();

$job->handle();

$this->assertTrue($batch->cancelled());
$this->assertNotEmpty($batch->added);

@taylorotwell

taylorotwell commented Sep 12, 2022

Copy link
Copy Markdown
Member Author

Tempting to make withFakeBatch return [$job, $batch] so that you can do:

[$job, $batch] = (new ExampleJob)->withFakeBatch();

Edit: ✅

@Wulfheart

Copy link
Copy Markdown

Maybe adding an array shape would help with autocomplete in IDEs?

@bhaustein

Copy link
Copy Markdown

What about good old call by reference:

$batch = new TestBatch();
$job = (new TestJob)->withFakeBatch($batch);

$job->handle();

$this->assertTrue($batch->cancelled());
$this->assertNotEmpty($batch->added);

@jasonmccreary

jasonmccreary commented Sep 12, 2022

Copy link
Copy Markdown
Contributor

Not to be grammar police, but isn't the job within a batch? So maybe withinFakeBatch()

@bhaustein

Copy link
Copy Markdown

Not to be grammar police, but isn't the job within a batch? So maybe withinFakeBatch($batch)

Yes, you are right. That Sounds better.

@njames

njames commented Sep 12, 2022

Copy link
Copy Markdown

Not to be grammar police, but isn't the job within a batch? So maybe withinFakeBatch($batch)

Grammar police are the best kind of police 🚓 @jasonmccreary

@jasonmccreary

jasonmccreary commented Sep 13, 2022

Copy link
Copy Markdown
Contributor

I don't think I can edit this directly, but the BusFake and BatchRepositoryFake should be updated to use this new BatchFake.

It'd also be nice if the Bus had a similar dispatchFakeBatch() helper method to easily create fake batches for testing batches outside of specific jobs.

Comment thread src/Illuminate/Bus/Batchable.php Outdated
@gustavo-ticto

Copy link
Copy Markdown

🚀

Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
@RTippin

RTippin commented Sep 28, 2022

Copy link
Copy Markdown

Seems since this update, I have a few tests that fail now, due to calling $batch->add(...).

TypeError : array_merge(): Argument #2 must be of type array, Illuminate\Support\LazyCollection given

$this->added[] = array_merge($this->added, $jobs);

I have a batch created, and then use a lazy collection/chunk to add jobs to the batch.

private function addToBatch(Model $model, Batch $batch): void
{
    $model->relations()
        ->cursor()
        ->map(fn (Relation $relation): ThumbnailJob => $this->createThumbnailJob($relation))
        ->chunk(500)
        ->each(fn (LazyCollection $jobs) => $batch->add($jobs));
}

Wondering if I am going about this the wrong way, or should the BatchFake be updated?

@GrahamCampbell GrahamCampbell changed the title Fake Batches [9.x] Fake Batches Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants