@@ -534,74 +534,51 @@ You can also get the objects related to the latest request::
534534Accessing the Container
535535~~~~~~~~~~~~~~~~~~~~~~~
536536
537- It's highly recommended that a functional test only tests the response. But
538- under certain very rare circumstances, you might want to access some services
539- to write assertions. Given that services are private by default, test classes
540- define a property that stores a special container created by Symfony which
541- allows fetching both public and all non-removed private services::
542-
543- // gives access to the same services used in your test, unless you're using
544- // $client->insulate() or using real HTTP requests to test your application
545- // don't forget to call self::bootKernel() before, otherwise, the container
546- // will be empty
547- $container = self::$container;
548-
549- For a list of services available in your application, use the ``debug:container ``
550- command.
551-
552- If a private service is *never * used in your application (outside of your test),
553- it is *removed * from the container and cannot be accessed as described above. In
554- that case, you can create a public alias in the ``test `` environment and access
555- it via that alias:
537+ Functional tests should only test the response (e.g. its contents or its HTTP
538+ status code). However, in some rare circumstances you may need to access the
539+ container to use some service.
556540
557- .. configuration-block ::
558-
559- .. code-block :: yaml
560-
561- # config/services_test.yaml
562- services :
563- # access the service in your test via
564- # self::$container->get('test.App\Test\SomeTestHelper')
565- test.App\Test\SomeTestHelper :
566- # the id of the private service
567- alias : ' App\Test\SomeTestHelper'
568- public : true
541+ First, you can get the same container used in the application, which only
542+ includes the public services::
569543
570- .. code-block :: xml
571-
572- <!-- config/services_test.xml -->
573- <?xml version =" 1.0" encoding =" UTF-8" ?>
574- <container xmlns =" http://symfony.com/schema/dic/services"
575- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
576- xsi : schemaLocation =" http://symfony.com/schema/dic/services
577- https://symfony.com/schema/dic/services/services-1.0.xsd" >
544+ public function testSomething()
545+ {
546+ $client = self::createClient();
547+ $container = $client->getContainer();
548+ // $someService = $container->get('the-service-ID');
578549
579- < services >
580- <!-- ... -->
550+ // ...
551+ }
581552
582- < service id = " test.App\Test\SomeTestHelper " alias = " App\Test\SomeTestHelper " public = " true " />
583- </ services >
584- </ container >
553+ Symfony tests also have access to a special container that includes both the
554+ public services and the non-removed :ref: ` private services < container-public >`
555+ services::
585556
586- .. code-block :: php
557+ public function testSomething()
558+ {
559+ // this call is needed; otherwise the container will be empty
560+ self::bootKernel();
587561
588- // config/services_test.php
589- namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
562+ $container = self::$container;
563+ // $someService = $container->get('the-service-ID') ;
590564
591- use App\Service\MessageGenerator;
592- use App\Service\SiteUpdateManager;
565+ // ...
566+ }
593567
594- return function(ContainerConfigurator $configurator) {
595- // ...
568+ Finally, for the most rare edge-cases, Symfony includes a special container
569+ which provides access to all services, public and private. This special
570+ container is a service that can be get via the normal container::
596571
597- $services->alias('test.App\Test\SomeTestHelper', 'App\Test\SomeTestHelper')->public();
598- };
572+ public function testSomething()
573+ {
574+ $client = self::createClient();
575+ $normalContainer = $client->getContainer();
576+ $specialContainer = $normalContainer->get('test.service_container');
599577
600- .. tip ::
578+ // $somePrivateService = $specialContainer->get('the-service-ID');
601579
602- The special container that gives access to private services exists only in
603- the ``test `` environment and is itself a service that you can get from the
604- real container using the ``test.service_container `` id.
580+ // ...
581+ }
605582
606583.. tip ::
607584
@@ -909,7 +886,7 @@ their type::
909886 $form['photo']->upload('/path/to/lucas.jpg');
910887
911888 // In the case of a multiple file upload
912- $form['my_form[field][O ]']->upload('/path/to/lucas.jpg');
889+ $form['my_form[field][0 ]']->upload('/path/to/lucas.jpg');
913890 $form['my_form[field][1]']->upload('/path/to/lisa.jpg');
914891
915892.. tip ::
0 commit comments