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