Skip to content

Commit 2b9dfeb

Browse files
committed
[Testing] Updated the contents about getting the container in tests
1 parent 4d52670 commit 2b9dfeb

File tree

1 file changed

+34
-57
lines changed

1 file changed

+34
-57
lines changed

testing.rst

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -543,74 +543,51 @@ You can also get the objects related to the latest request::
543543
Accessing the Container
544544
~~~~~~~~~~~~~~~~~~~~~~~
545545

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.
565549

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::
578552

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');
587558

588-
<services>
589-
<!-- ... -->
559+
// ...
560+
}
590561

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::
594565

595-
.. code-block:: php
566+
public function testSomething()
567+
{
568+
// this call is needed; otherwise the container will be empty
569+
self::bootKernel();
596570

597-
// config/services_test.php
598-
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
571+
$container = self::$container;
572+
// $someService = $container->get('the-service-ID');
599573

600-
use App\Service\MessageGenerator;
601-
use App\Updates\SiteUpdateManager;
574+
// ...
575+
}
602576

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::
605580

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');
608586

609-
.. tip::
587+
// $somePrivateService = $specialContainer->get('the-service-ID');
610588

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+
}
614591

615592
.. tip::
616593

0 commit comments

Comments
 (0)