Akhmed
Akhmed

Reputation: 1179

How to prevent Symfony 4 service override?

I have base /config/services.yaml which stores many services in my Symfony 4.3 project. For example:

My\Namespace\Service:
  - '@My\Namespace\Dependency'

For my test purposes, I have config/test/test_services.yaml where I store services with 'test.' prefix to test private services, making them public in test env.

One of the services, declared in test_services.yaml has no prefix. It is identical by its name (FQCN) to another one defined in services.yaml. They have different constructor arguments of the same type.

Test one (/config/test_services.yaml) has mocked dependencies returning fixtures data:

My\Namespace\Service:
  - '@My\Namespace\MockedDependency'

Is there a way to prevent service override to not replace mocked service with real one during test execution in test env?

Upvotes: 0

Views: 395

Answers (1)

Akhmed
Akhmed

Reputation: 1179

Solution for this situation is create services_test.yaml file and place it under /config folder along with services.yaml.

In that case, symfony will not override service and will use one defined in services_test.yaml during tests execution

Upvotes: 1

Related Questions