@@ -76,11 +76,71 @@ The service is found by its type, or an id if it's given in the `@inject` tag.
7676The ` createServiceContainer ` method would be usually provided by a base test case or a trait.
7777In case of Symfony, such a trait is provided by this package (see the next section).
7878
79- ### Symfony
79+ ### Symfony Test Container (Symfony >= 4.1)
8080
81- The simplest way to inject services from a Symfony service container is to include
82- the ` Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyContainer ` trait to get the default
83- ` Zalas\Injector\PHPUnit\TestListener\ServiceContainerTestCase ` implementation:
81+ The ` Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer ` trait provides
82+ access to the test container ([ introduced in Symfony 4.1] ( https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing ) ).
83+ Including the trait in a test case implementing the ` ServiceContainerTestCase ` will make that services are injected
84+ into annotated properties:
85+
86+ ``` php
87+ use PHPUnit\Framework\TestCase;
88+ use Psr\Log\LoggerInterface;
89+ use Symfony\Component\Serializer\SerializerInterface;
90+ use Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer;
91+ use Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase;
92+
93+ class ServiceInjectorTest extends TestCase implements ServiceContainerTestCase
94+ {
95+ use SymfonyTestContainer;
96+
97+ /**
98+ * @var SerializerInterface
99+ * @inject
100+ */
101+ private $serializer;
102+
103+ /**
104+ * @var LoggerInterface
105+ * @inject logger
106+ */
107+ private $logger;
108+
109+ public function testThatServicesAreInjected()
110+ {
111+ $this->assertInstanceOf(SerializerInterface::class, $this->serializer, 'The service is injectd by its type');
112+ $this->assertInstanceOf(LoggerInterface::class, $this->logger, 'The service is injected by its id');
113+ }
114+ }
115+ ```
116+
117+ Note that ` test ` needs to be set to ` true ` in your test environment configuration for the framework bundle:
118+
119+ ``` yaml
120+ framework :
121+ test : true
122+ ` ` `
123+
124+ Even though services are automatically made public by Symfony, the test container makes them available in your tests.
125+ Note that this only happens for private services that are actually used in your app (so are injected into
126+ a public service, i.e. a controller). If a service is not injected anywhere, it's removed by the container compiler.
127+
128+ The kernel used to bootstrap the container is created in a similar way to the ` KernelTestCase` known from the FrameworkBundle.
129+ Similar environment variables are supported :
130+
131+ * `KERNEL_CLASS` *required* - kernel class to instantiate to create the service container
132+ * `APP_ENV` default: test - kernel environment
133+ * `APP_DEBUG` default: false - kernel debug flag
134+
135+ These could for example be configured in `phpunit.xml`, or via [global variables](https://github.com/jakzal/phpunit-globals).
136+
137+ # ## Symfony Container (Symfony 3.4 & 4.0)
138+
139+ The `Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyContainer` trait gives access to the full Symfony Container
140+ and can be used with any Symfony version.
141+ Opposed to the Test Container approach for Symfony 4.1, this version provides access to each service even if it's
142+ not used by your application anywhere and would normally be removed by the compiler.
143+ This should be treated as a limitation rather than a feature.
84144
85145` ` ` php
86146use PHPUnit\F ramework\T estCase;
@@ -113,8 +173,8 @@ class ServiceInjectorTest extends TestCase implements ServiceContainerTestCase
113173}
114174` ` `
115175
116- To make this work the ` Zalas\Injector\PHPUnit\Symfony\Compiler\ExposeServicesForTestsPass ` needs to be
117- registered in test environment :
176+ Since the test container is not available until Symfony 4.1,
177+ you'll also have to register the `Zalas\Injector\PHPUnit\Symfony\Compiler\ExposeServicesForTestsPass` compiler pass :
118178
119179` ` ` php
120180use Zalas\I njector\P HPUnit\S ymfony\C ompiler\E xposeServicesForTestsPass;
@@ -134,13 +194,6 @@ class Kernel extends BaseKernel
134194
135195The compiler pass makes sure that even private services are available to be used in tests.
136196
137- The kernel is created in a similar way to the ` KernelTestCase ` known from the FrameworkBundle.
138- The same environment variables are supported:
139-
140- * ` KERNEL_CLASS ` * required* - kernel class to instantiate to create the service container
141- * ` APP_ENV ` default: test - kernel environment
142- * ` APP_DEBUG ` default: false - kernel debug flag
143-
144197# # Contributing
145198
146199Please read the [Contributing guide](CONTRIBUTING.md) to learn about contributing to this project.
0 commit comments