@@ -178,6 +178,43 @@ In order to solve this issue, add the following configuration to your kernel:
178
178
# allows to use app/Resources/views/ templates in the ApiKernel
179
179
" %kernel.root_dir%/../app/Resources/views " : ~
180
180
181
+ Running Tests Using a Different Kernel
182
+ --------------------------------------
183
+
184
+ In Symfony applications, functional tests extend by default from the
185
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Test\\ WebTestCase ` class. Inside that
186
+ class, a method called ``getKernelClass() `` tries to find the class of the kernel
187
+ to use to run the application during tests. The logic of this method does not
188
+ support multiple kernel applications, so your tests won't use the right kernel.
189
+
190
+ The solution is to create a custom base class for functional tests extending
191
+ from ``WebTestCase `` class and overriding the ``getKernelClass() `` method to
192
+ return the fully qualified class name of the kernel to use::
193
+
194
+ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
195
+
196
+ // tests needing the ApiKernel to work, now must extend this
197
+ // ApiTestCase class instead of the default WebTestCase class
198
+ class ApiTestCase extends WebTestCase
199
+ {
200
+ protected static function getKernelClass()
201
+ {
202
+ return 'ApiKernel';
203
+ }
204
+
205
+ // this is needed because the KernelTestCase class keeps a reference to
206
+ // the previously created kernel in its static $kernel property. Thus,
207
+ // if your functional tests do not run in isolated processes, a later run
208
+ // test for a different kernel will reuse the previously created instance,
209
+ // which points to a different kernel
210
+ protected function tearDown()
211
+ {
212
+ parent::tearDown();
213
+
214
+ static::$class = null;
215
+ }
216
+ }
217
+
181
218
Adding more Kernels to the Application
182
219
--------------------------------------
183
220
0 commit comments