-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected
6.0.5
Description
Overriding an interface implementation (with default autowiring enabled) for a specific environment will fail when the specific implementation has dependencies that should get injected.
We use this when@test
feature to wire a different service that does not connect to the database in test
environments.
References
- Configure Multiple Environments in a Single File is new since Symfony 5.3 https://symfony.com/blog/new-in-symfony-5-3-configure-multiple-environments-in-a-single-file
How to reproduce
The services.yaml allows you to "wire" the implementation you want to use to the respective interface:
# services.yaml
services:
_defaults:
autowire: true
# The current product repository implementation that gets wired to the domain interface by DI
App\Domain\Product\ProductRepository:
class: App\Infrastructure\Product\ProductDatabaseRepository
When having multiple implementations of an interface, you can for another environment wire another implementation
# services.yaml
# ... see above
when@test:
services:
App\Domain\Product\ProductRepository:
class: App\Infrastructure\Product\ProductFromFixtureRepository
But, when ProductFromFixtureRepository
would have autowired dependencies, where the default ProductDatabaseRepository
has none, the compilation will fail (read: arguments will not get wired on test
env).
Possible Solution
Enabling autowiring on the overridden service solves the error.
# services.yaml
when@test:
services:
App\Domain\Product\ProductRepository:
class: App\Infrastructure\Product\ProductFromFixtureRepository
+ autowire: true
The cause might be that the _defaults
might not get included for the when
-parsed section?
Additional Context
No response