B
B
BonBon Slick2018-05-06 14:55:19
symfony
BonBon Slick, 2018-05-06 14:55:19

Multiple interface implementations, how to determine which DInject implementation?

There is a UserRepo interface and there are 2 implementations, DoctrineUserRepo and InMemoryUserRepo for mocks. Symphony service container is throwing an error now

Cannot autowire service...: argument "$userRepository" of method "__construct()" references interface "UserReposito  
  ry" but no such service exists. You should maybe alias this interface to one of these existing services: "...DoctrineUserRepository", "....InMemoryUserRepository".

How to make it take InMemoryRepo in tests, and where it needs another, 2nd or 3rd, etc. implementation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
voronkovich, 2018-05-06
@BonBonSlick

The documentation mentions this: https://symfony.com/doc/current/service_container/...

# services.yml

services:
    App\Repository\UserRepoInterface: '@App\Repository\DoctrineUserRepo'

For a test environment, you can override:
# services_test.yml

services:
    App\Repository\UserRepoInterface: '@App\Repository\InMemoryUserRepo'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question