Skip to content
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

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

Merged
merged 5 commits into from
Sep 13, 2022
Merged

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

merged 5 commits into from
Sep 13, 2022

Conversation

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

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

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

@mateusjunges
Copy link
Contributor Author

@dwightwatson I liked it, just updated the PR

@jasonmccreary
Copy link
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
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
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
Contributor Author

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

@jasonmccreary
Copy link
Contributor

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

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