Skip to content

[9.x] Allow to define which jobs should be actually dispatched when using Bus::fake#44106

Merged
taylorotwell merged 5 commits into
laravel:9.xfrom
mateusjunges:allow-to-fake-all-but-one-job
Sep 13, 2022
Merged

[9.x] Allow to define which jobs should be actually dispatched when using Bus::fake#44106
taylorotwell merged 5 commits into
laravel:9.xfrom
mateusjunges:allow-to-fake-all-but-one-job

Conversation

@mateusjunges
Copy link
Copy Markdown
Contributor

@mateusjunges mateusjunges commented Sep 12, 2022

The problem

Currently, we can use the Bus@fake method to determine which jobs should be faked when executing tests.

The problem with it is that if I have 100 jobs that will be dispatched during one request and I want only one of them to be actually executed, then I have to explicit define which 99 jobs should be faked, instead of just defining which job should be dispatched.

Solution

This PR adds the option to determine which jobs should be executed when using Bus::fake, like this example:

Bus::fake()->except(MyJob::class);

//Or:

Bus::fake()->except([
    MyJob::class,
    AnotherJob::class,
]);

Now, all jobs dispatched in the test case except the MyJob::class, in the first example, or MyJob::class and AnotherJob::class in the second one, will be faked.

If this PR gets merged, I can open another one adding this functionality to the Queue and Event classes. I can also add it to this PR if Taylor think that is that way to go.

@mateusjunges mateusjunges changed the title Allow to fake all but one job [9.x] Allow to define which jobs should be actually dispatched when using Bus::fake Sep 12, 2022
@dwightwatson
Copy link
Copy Markdown
Contributor

Perhaps it would be clearer to chain except onto the existing fake method?

Bus::fake()->except(MyJob::class)

@mateusjunges
Copy link
Copy Markdown
Contributor Author

@dwightwatson I liked it, just updated the PR

@jasonmccreary
Copy link
Copy Markdown
Contributor

I have it on my list to do this for the Queue::fake() as well. Being able to specify both exclusion and inclusion provides the most flexibility when testing.

@mateusjunges
Copy link
Copy Markdown
Contributor Author

mateusjunges commented Sep 13, 2022

@jasonmccreary I'm waiting for Taylor to review this PR before implementing it on other fake classes, so I don't waste time if it gets rejected. But yeah, I agree with you 🙂

@jasonmccreary
Copy link
Copy Markdown
Contributor

jasonmccreary commented Sep 13, 2022

@mateusjunges, I'll take it off my list then. 😉

@taylorotwell taylorotwell merged commit 8d1bfc1 into laravel:9.x Sep 13, 2022
@mateusjunges mateusjunges deleted the allow-to-fake-all-but-one-job branch September 13, 2022 15:08
@mateusjunges
Copy link
Copy Markdown
Contributor Author

I'm opening a follow up PR to implement this feature in other fake classes as well.

@jasonmccreary
Copy link
Copy Markdown
Contributor

@jasonmccreary, yay! Please at me for the Queue::fake() as I had some ideas there too.

@mateusjunges
Copy link
Copy Markdown
Contributor Author

Hey @jasonmccreary you can check it out here #44117

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.

4 participants