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

Ability to configure a container to inject dependencies in PHPat tests #298

Open
garak opened this issue Dec 10, 2024 · 5 comments
Open
Labels
enhancement New feature or request

Comments

@garak
Copy link

garak commented Dec 10, 2024

Question

I have a Symfony project with some tagged services, and I'd like to enforce that every class implementing a given interface has the expected tag applied in the container.

I was wondering if this could be currently feasible using phpat... I've read the documentation, checked the examples, and got an idea about how a custom rule is built, but in my specific case I'd need something where I could inject the container, which seems not possible to me.

Am I missing something? If not, do you think it could be implemented somehow in the future?
Or maybe you have some alternate suggestions about how could I enforce my costraint?

Thanks in advance

@carlosas
Copy link
Owner

carlosas commented Dec 10, 2024

PHPat is framework-agnostic. If your tags are declared via class attribute, you can enforce X classes to apply an attribute (I think not with specific parameters atm), but you cant enforce stuff that requieres the tool to "understand" Symfony.
PHPat is based on PHPStan, you might find easier to write a manual custom rule, probably using the phpstan-symfony extension

@garak
Copy link
Author

garak commented Dec 11, 2024

I fully understand (and support) the neutrality of this library.
I mentioned my specific case with Symfony just to be as clear as possible.

Anyway, I think that my case could be of more general interest, when someone could need adding a generic dependency (using a constructor injection of any other possible method) when defining a rule

@carlosas
Copy link
Owner

I'm not sure I understand what you mean by adding a generic dependency. Your test classes can have a constructor and you can add any logic you need as long as the rule methods start by test or have the #[TestRule] attribute, and return a Rule or an iterable of Rules.

@garak
Copy link
Author

garak commented Dec 27, 2024

That's exactly my point: the constructor is useless if you can't pass arguments

@carlosas carlosas changed the title Check if a class has given container tag Ability to configure a container to inject dependencies in PHPat tests Jan 5, 2025
@carlosas carlosas added the enhancement New feature or request label Jan 5, 2025
@carlosas
Copy link
Owner

carlosas commented Jan 5, 2025

Got it 👌 I added it on the TO-DO list #304

My idea is to configure the PHPStan container by default (with a PSR wrapper) and make the container configurable somehow via config key. I've been playing around but it's still not working #311
Contribution welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants