Skip to content

[9.x] Fake Batches #44104

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

Merged
merged 7 commits into from
Sep 16, 2022
Merged

[9.x] Fake Batches #44104

merged 7 commits into from
Sep 16, 2022

Conversation

taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Sep 12, 2022

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
Copy link
Member Author

taylorotwell commented Sep 12, 2022

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

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

Edit: ✅

@Wulfheart
Copy link

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

@bhaustein
Copy link

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
Copy link
Contributor

jasonmccreary commented Sep 12, 2022

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

@bhaustein
Copy link

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
Copy link

njames commented Sep 12, 2022

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
Copy link
Contributor

jasonmccreary commented Sep 13, 2022

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.

@gustavo-ticto
Copy link

🚀

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

RTippin commented Sep 28, 2022

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.

None yet

9 participants