Skip to content

[9.x] Allow BusFake to use custom BusRepository#45202

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
mihaliak:9.x
Dec 7, 2022
Merged

[9.x] Allow BusFake to use custom BusRepository#45202
taylorotwell merged 2 commits into
laravel:9.xfrom
mihaliak:9.x

Conversation

@mihaliak

@mihaliak mihaliak commented Dec 6, 2022

Copy link
Copy Markdown
Contributor

As described in PR name, allow BusFake to use custom BusRepository. This is useful in tests where we use Bus::fake() and later in code we use resolve(BusRepository::class)->find(...).

Before this change that test would fail since Bus::fake() is creating always new BusRepository so using resolve(BusRepository::class)->find(...) will not use BatchRepositoryFake and throw error that it cant find batch in unit tests.

Usage:

$myBusRepository = new BatchRepositoryFake;

Bus::fake([JobsToFake::class], $myBusRepository);


// either bind $myBusRepository to \Illuminate\Bus\BatchRepository interface or send as constructor param to user defined class

app()->bind(\Illuminate\Bus\BatchRepository::class, $myBusRepository);


...
$myTestedService->method(); // this method calls  -> resolve(BatchRepository::class)->find(...)  -> correctly found fake batch

@mihaliak mihaliak changed the title Allow BusFake to use custom BusRepository [9.x] Allow BusFake to use custom BusRepository Dec 7, 2022
@taylorotwell taylorotwell merged commit 8ec69f8 into laravel:9.x Dec 7, 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.

2 participants