-
-
Notifications
You must be signed in to change notification settings - Fork 358
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
[2.x] Added toUseTrait
arch expectation
#960
Conversation
Hey, @ash-jc-allen ! Thank you for your amazing work here. Thank you for your amazing work here. My only concern is whether we are not doing the same thing as The |
Hey @devajmeireles! I'll be completely honest, I didn't know you could use |
Don't worry, it's normal. PestPhp is too big. But yes, you can use |
Ahh, so maybe this new |
Feel that - if anything were to be done here - would be at toUse level. Meaning that, if you pass a trait into toUse, the code would actually check if the class uses the trait, etc. |
@nunomaduro Yeah that's a fair point! If you're open to it, I'd like to have a go at making a PR to add that functionality to the |
What:
Description:
Hey! This PR proposes a new
toUseTrait
arch expectation. There are times when I want to ensure that classes in a given directory are all using a specific trait.For example, in my
app/Actions
folder, I might be using thelorisleiva/laravel-actions
package which allows me to add anLorisleiva\Actions\Concerns\AsAction
trait to all my action classes. Example:In this situation, I'd like be to able to do something like:
I've also added support for checking multiple at once. For example:
Running the above would ensure that all the classes in the
app/Actions
folder will use theAsAction
trait AND theMyAwesomeTrait
.There's also the opposite expectation that you can use to make sure a given trait isn't being used:
This expectation will bubble up and check all the given class' traits too. So if a class has access to a trait, but via a parent class, this should detect that and pass the expectation.
Caveats
I think the main caveat to what I've done so far is that it's not detecting traits used by other traits. For example, say we have a trait called
BaseActionTrait
and this trait uses theAsAction
trait:Then let's say you have a class that uses the
BaseActionTrait
:If you were to run the following test, it would fail:
I don't know if this is an issue or not. But I didn't want to spend too much time working on it just in case it's something that might not be needed or wanted. I'm happy to have a crack at adding it though 🙂
Related:
N/A
If you think this new
toUseTrait
expectation would be useful, please give me a shout if there's anything you'd like me to update.I've tried my best to test this out, but I might have missed some edges. Apologies if so! 😄