@@ -543,74 +543,51 @@ You can also get the objects related to the latest request::
543
543
Accessing the Container
544
544
~~~~~~~~~~~~~~~~~~~~~~~
545
545
546
- It's highly recommended that a functional test only tests the response. But
547
- under certain very rare circumstances, you might want to access some services
548
- to write assertions. Given that services are private by default, test classes
549
- define a property that stores a special container created by Symfony which
550
- allows fetching both public and all non-removed private services::
551
-
552
- // gives access to the same services used in your test, unless you're using
553
- // $client->insulate() or using real HTTP requests to test your application
554
- // don't forget to call self::bootKernel() before, otherwise, the container
555
- // will be empty
556
- $container = self::$container;
557
-
558
- For a list of services available in your application, use the ``debug:container ``
559
- command.
560
-
561
- If a private service is *never * used in your application (outside of your test),
562
- it is *removed * from the container and cannot be accessed as described above. In
563
- that case, you can create a public alias in the ``test `` environment and access
564
- it via that alias:
546
+ Functional tests should only test the response (e.g. its contents or its HTTP
547
+ status code). However, in some rare circumstances you may need to access the
548
+ container to use some service.
565
549
566
- .. configuration-block ::
567
-
568
- .. code-block :: yaml
569
-
570
- # config/services_test.yaml
571
- services :
572
- # access the service in your test via
573
- # self::$container->get('test.App\Test\SomeTestHelper')
574
- test.App\Test\SomeTestHelper :
575
- # the id of the private service
576
- alias : ' App\Test\SomeTestHelper'
577
- public : true
550
+ First, you can get the same container used in the application, which only
551
+ includes the public services::
578
552
579
- .. code-block :: xml
580
-
581
- <!-- config/services_test.xml -->
582
- <?xml version =" 1.0" encoding =" UTF-8" ?>
583
- <container xmlns =" http://symfony.com/schema/dic/services"
584
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
585
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
586
- https://symfony.com/schema/dic/services/services-1.0.xsd" >
553
+ public function testSomething()
554
+ {
555
+ $client = self::createClient();
556
+ $container = $client->getContainer();
557
+ // $someService = $container->get('the-service-ID');
587
558
588
- < services >
589
- <!-- ... -->
559
+ // ...
560
+ }
590
561
591
- < service id = " test.App\Test\SomeTestHelper " alias = " App\Test\SomeTestHelper " public = " true " />
592
- </ services >
593
- </ container >
562
+ Symfony tests also have access to a special container that includes both the
563
+ public services and the non-removed :ref: ` private services < container-public >`
564
+ services::
594
565
595
- .. code-block :: php
566
+ public function testSomething()
567
+ {
568
+ // this call is needed; otherwise the container will be empty
569
+ self::bootKernel();
596
570
597
- // config/services_test.php
598
- namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
571
+ $container = self::$container;
572
+ // $someService = $container->get('the-service-ID') ;
599
573
600
- use App\Service\MessageGenerator;
601
- use App\Updates\SiteUpdateManager;
574
+ // ...
575
+ }
602
576
603
- return function(ContainerConfigurator $configurator) {
604
- // ...
577
+ Finally, for the most rare edge-cases, Symfony includes a special container
578
+ which provides access to all services, public and private. This special
579
+ container is a service that can be get via the normal container::
605
580
606
- $services->alias('test.App\Test\SomeTestHelper', 'App\Test\SomeTestHelper')->public();
607
- };
581
+ public function testSomething()
582
+ {
583
+ $client = self::createClient();
584
+ $normalContainer = $client->getContainer();
585
+ $specialContainer = $normalContainer->get('test.service_container');
608
586
609
- .. tip ::
587
+ // $somePrivateService = $specialContainer->get('the-service-ID');
610
588
611
- The special container that gives access to private services exists only in
612
- the ``test `` environment and is itself a service that you can get from the
613
- real container using the ``test.service_container `` id.
589
+ // ...
590
+ }
614
591
615
592
.. tip ::
616
593
0 commit comments