Skip to content

Add tip about accessing removed services in the testing doc #12647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,56 @@ allows fetching both public and all non-removed private services::
For a list of services available in your application, use the ``debug:container``
command.

.. tip::

If a private service is *never* used in your application (outside of your test), it
is *removed* from the container and cannot be accessed as described above. In that
case, you can create a public alias in the ``test`` environment and access it
via that alias:

.. configuration-block::

.. code-block:: yaml

# config/services_test.yaml
services:
# access the service in your test via
# self::$container->get('test.App\Test\SomeTestHelper')
test.App\Test\SomeTestHelper:
# the id of the private service
alias: 'App\Test\SomeTestHelper'
public: true

.. code-block:: xml

<!-- config/services_test.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- ... -->

<service id="test.App\Test\SomeTestHelper" alias="App\Test\SomeTestHelper" public="true"/>
</services>
</container>

.. code-block:: php

// config/services_test.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\Service\MessageGenerator;
use App\Updates\SiteUpdateManager;

return function(ContainerConfigurator $configurator) {
// ...

$services->alias('test.App\Test\SomeTestHelper', 'App\Test\SomeTestHelper')->public();
};

.. tip::

The special container that gives access to private services exists only in
Expand Down