diff --git a/testing.rst b/testing.rst index f6e212fc786..cd48004fc77 100644 --- a/testing.rst +++ b/testing.rst @@ -560,6 +560,56 @@ 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:: + + 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:: The special container that gives access to private services exists only in