@@ -556,12 +556,53 @@ command.
556
556
557
557
.. tip ::
558
558
559
- Keep in mind that, if a private service is never used as a dependency of another service in
560
- your application, it is then removed from the container. So, if you try to access a private
561
- service in a test through the special test container and that service isn't used elsewhere
562
- you'll get a ``ServiceNotFoundException ``. The solution, depending on the context, is to
563
- define the service as explicitly ``public `` or to inject it where you'll need it so Symfony
564
- doesn't remove it.
559
+ If a private service is *never * used in your application (outside of your test), it
560
+ is *removed * from the container and cannot be accessed as described above. In that
561
+ case, you can create a public alias in the ``test `` environment and access it
562
+ via that alias:
563
+
564
+ .. configuration-block ::
565
+
566
+ .. code-block :: yaml
567
+
568
+ # config/services_test.yaml
569
+ services :
570
+ # access the service in your test via
571
+ # self::$container->get('test.App\Test\SomeTestHelper')
572
+ test.App\Test\SomeTestHelper :
573
+ # the id of the private service
574
+ alias : ' App\Test\SomeTestHelper'
575
+ public : true
576
+
577
+ .. code-block :: xml
578
+
579
+ <!-- config/services_test.xml -->
580
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
581
+ <container xmlns =" http://symfony.com/schema/dic/services"
582
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
583
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
584
+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
585
+
586
+ <services >
587
+ <!-- ... -->
588
+
589
+ <service id =" test.App\Test\SomeTestHelper" alias =" App\Test\SomeTestHelper" public =" true" />
590
+ </services >
591
+ </container >
592
+
593
+ .. code-block :: php
594
+
595
+ // config/services_test.php
596
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
597
+
598
+ use App\Service\MessageGenerator;
599
+ use App\Updates\SiteUpdateManager;
600
+
601
+ return function(ContainerConfigurator $configurator) {
602
+ // ...
603
+
604
+ $services->alias('test.App\Test\SomeTestHelper', 'App\Test\SomeTestHelper')->public();
605
+ };
565
606
566
607
.. tip ::
567
608
0 commit comments