Skip to content

[DependencyInjection] Add note about testing service locators #19139

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down