8000 minor #12647 Add tip about accessing removed services in the testing … · symfony/symfony-docs@df4b2cb · GitHub
[go: up one dir, main page]

Skip to content

Commit df4b2cb

Browse files
committed
minor #12647 Add tip about accessing removed services in the testing doc (mmenozzi, weaverryan)
This PR was submitted for the master branch but it was merged into the 4.4 branch instead (closes #12647). Discussion ---------- Add tip about accessing removed services in the testing doc As requested [here](symfony/symfony#30104 (comment)) this commit will add a tip about accessing removed services through the special test container in the testing docs. Commits ------- f038841 clarifying how to create a public alias for a test 67dd3c1 Add tip about accessing removed services in the testing doc
2 parents 1a3f107 + f038841 commit df4b2cb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

testing.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,56 @@ allows fetching both public and all non-removed private services::
560560
For a list of services available in your application, use the ``debug:container``
561561
command.
562562

563+
.. tip::
564+
565+
If a private service is *never* used in your application (outside of your test), it
566+
is *removed* from the container and cannot be accessed as described above. In that
567+
case, you can create a public alias in the ``test`` environment and access it
568+
via that alias:
569+
570+
.. configuration-block::
571+
572+
.. code-block:: yaml
573+
574+
# config/services_test.yaml
575+
services:
576+
# access the service in your test via
577+
# self::$container->get('test.App\Test\SomeTestHelper')
578+
test.App\Test\SomeTestHelper:
579+
# the id of the private service
580+
alias: 'App\Test\SomeTestHelper'
581+
public: true
582+
583+
.. code-block:: xml
584+
585+
<!-- config/services_test.xml -->
586+
<?xml version="1.0" encoding="UTF-8" ?>
587+
<container xmlns="http://symfony.com/schema/dic/services"
588+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
589+
xsi:schemaLocation="http://symfony.com/schema/dic/services
590+
https://symfony.com/schema/dic/services/services-1.0.xsd">
591+
592+
<services>
593+
<!-- ... -->
594+
595+
<service id="test.App\Test\SomeTestHelper" alias="App\Test\SomeTestHelper" public="true"/>
596+
</services>
597+
</container>
598+
599+
.. code-block:: php
600+
601+
// config/services_test.php
602+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
603+
604+
use App\Service\MessageGenerator;
605+
use App\Updates\SiteUpdateManager;
606+
607+
return function(ContainerConfigurator $configurator) {
608+
// ...
609+
610+
$services->alias('test.App\Test\SomeTestHelper', 'App\Test\SomeTestHelper')->public();
611+
};
612+
563613
.. tip::
564614

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

0 commit comments

Comments
 (0)
0