From 67dd3c18fbbcb8a1c774b3b38dddca925b2622cf Mon Sep 17 00:00:00 2001 From: Manuele Menozzi Date: Mon, 18 Nov 2019 09:28:54 +0100 Subject: [PATCH 1/2] Add tip about accessing removed services in the testing doc --- testing.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/testing.rst b/testing.rst index f6e212fc786..11c9702a0fa 100644 --- a/testing.rst +++ b/testing.rst @@ -560,6 +560,15 @@ allows fetching both public and all non-removed private services:: For a list of services available in your application, use the ``debug:container`` command. +.. tip:: + + Keep in mind that, if a private service is never used as a dependency of another service in + your application, it is then removed from the container. So, if you try to access a private + service in a test through the special test container and that service isn't used elsewhere + you'll get a ``ServiceNotFoundException``. The solution, depending on the context, is to + define the service as explicitly ``public`` or to inject it where you'll need it so Symfony + doesn't remove it. + .. tip:: The special container that gives access to private services exists only in From f0388417d3c20f624fd801628ee5df1a157822ab Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 26 Feb 2020 08:59:58 -0500 Subject: [PATCH 2/2] clarifying how to create a public alias for a test --- testing.rst | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/testing.rst b/testing.rst index 11c9702a0fa..cd48004fc77 100644 --- a/testing.rst +++ b/testing.rst @@ -562,12 +562,53 @@ command. .. tip:: - Keep in mind that, if a private service is never used as a dependency of another service in - your application, it is then removed from the container. So, if you try to access a private - service in a test through the special test container and that service isn't used elsewhere - you'll get a ``ServiceNotFoundException``. The solution, depending on the context, is to - define the service as explicitly ``public`` or to inject it where you'll need it so Symfony - doesn't remove it. + If a private service is *never* used in your application (outside of your test), it + is *removed* from the container and cannot be accessed as described above. In that + case, you can create a public alias in the ``test`` environment and access it + via that alias: + + .. configuration-block:: + + .. code-block:: yaml + + # config/services_test.yaml + services: + # access the service in your test via + # self::$container->get('test.App\Test\SomeTestHelper') + test.App\Test\SomeTestHelper: + # the id of the private service + alias: 'App\Test\SomeTestHelper' + public: true + + .. code-block:: xml + + + + + + + + + + + + + .. code-block:: php + + // config/services_test.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; + + use App\Service\MessageGenerator; + use App\Updates\SiteUpdateManager; + + return function(ContainerConfigurator $configurator) { + // ... + + $services->alias('test.App\Test\SomeTestHelper', 'App\Test\SomeTestHelper')->public(); + }; .. tip::