Skip to content

Conversation

@gdebrauwer
Copy link
Contributor

Currently, when you want to assert the jobs of a batch in a test, you have to manually check them all by accessing the jobs-property on the PendingBatchFake class.

Bus::assertBatched(function (PendingBatchFake $batch) {
    return $batch->jobs->count() === 3
        && $batch->jobs[0] instanceof FooJob & $batch->jobs[0]->someProperty === 'someValue'
        && $batch->jobs[1] instanceof BarJob & $batch->jobs[1]->someProperty === 'someOtherValue'
        && $batch->jobs[2] instanceof BazJob & $batch->jobs[2]->someProperty === 'againAnotherValue';
});

This PR fixes that by adding a assertJobs method to the PendingBatchFake class.

Bus::assertBatched(function (PendingBatchFake $batch) {
    return $batch->assertJobs([
        new FooJob(someProperty: 'someValue'),
        new BarJob(someProperty: 'someOtherValue'),
        new BazJob(someProperty: 'againAnotherValue'),
    ]);
});

@dxnter
Copy link
Contributor

dxnter commented Feb 3, 2026

Looks great! It's always felt so tedious writing it out manually

assertJobs is a little misleading since it's returning a boolean rather than calling PHPUnit assertions. It seems like the convention is has* for boolean checks (hasDispatched, hasPushed, etc.) and assert* for methods that throw on failure. hasJobs could be an option?

Also, the existing assertDispatchedWithChainOfObjects in BusFake supports closures for custom job inspection that might be worth adding here too:

$batch->hasJobs([
    fn (NotifyUserJob $job) => $job->user->id === 1 && $job->channel === 'email',
    fn (NotifyUserJob $job) => $job->user->id === 2 && $job->channel === 'sms',                                        
    fn (GenerateReportJob $job) => $job->format === 'pdf' && $job->createdAt->isToday(),
]);

There could also be a case to mirror Bus::assertChained([...]) with a top level Bus::assertBatchedWithJobs([...]) so users don't need the nested callback for the common case. Though that's a different approach that could be addressed separately:

Bus::assertBatchedWithJobs([
    new FooJob('value'),
    BarJob::class,
    fn (BazJob $job) => $job->user->id === 1,
]);

@taylorotwell
Copy link
Member

Agree with above comment it should probably just return a boolean and be hasJobs or something. 👍

@taylorotwell taylorotwell marked this pull request as draft February 5, 2026 19:15
@gdebrauwer gdebrauwer marked this pull request as ready for review February 6, 2026 09:44
@taylorotwell taylorotwell merged commit 7810109 into laravel:12.x Feb 6, 2026
70 checks passed
@taylorotwell
Copy link
Member

Thanks 👍

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.

3 participants