From 18aaffb996053a8d1582062483abf88e94d0129c Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 15 Nov 2023 16:58:44 +0100 Subject: [PATCH] [DI] Add note about testing service locators --- service_container/service_subscribers_locators.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/service_container/service_subscribers_locators.rst b/service_container/service_subscribers_locators.rst index 8b7eeb16f21..58c6650b730 100644 --- a/service_container/service_subscribers_locators.rst +++ b/service_container/service_subscribers_locators.rst @@ -926,6 +926,15 @@ To unit test a service subscriber, you can create a fake container:: $serviceSubscriber = new MyService($container); // ... +.. note:: + + When defining the service locator like this, beware that the + :method:`Symfony\\Contracts\\Service\\ServiceLocatorTrait::getProvidedServices` + of your container will use the return type of the closures as the values of the + returned array. If no return type is defined, the value will be ``?``. If you + want the values to reflect the classes of your services, the return type has + to be set on your closures. + Another alternative is to mock it using ``PHPUnit``:: use Psr\Container\ContainerInterface;