Skip to content

Commit 0ab8a45

Browse files
committed
[Testing] Create a section dedicated to sending multiple requests in one functional test
1 parent 6a123d8 commit 0ab8a45

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

testing.rst

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -614,15 +614,58 @@ This allows you to create all types of requests you can think of:
614614
:ref:`framework.test <reference-framework-test>` option is enabled).
615615
This means you can override the service entirely if you need to.
616616

617-
.. caution::
617+
Multiple requests in one test
618+
.............................
619+
620+
After you send one request, subsequent ones will make the client reboot
621+
the kernel, recreating the container from scratch.
622+
This ensures that requests are "isolated" using "new" service objects,
623+
but can cause some unexpected behaviors. For example, the security token will
624+
be cleared, Doctrine entities will be "detached"…
625+
626+
Calling the client's
627+
:method:`Symfony\\Bundle\\FrameworkBundle\\KernelBrowser::disableReboot`
628+
method is the first step to work around this, as this will reset the kernel
629+
instead of rebooting it. Now, resetting the kernel will call the reset method
630+
of every ``kernel.reset`` tagged service, which will **also** clear the
631+
security token, detach entities and so on.
632+
633+
As such, the next step is to create a
634+
:doc:`compiler pass </service_container/compiler_passes>` to remove the
635+
``kernel.reset`` tag from these services in your test environment::
636+
637+
// src/Kernel.php
638+
namespace App;
639+
640+
use App\DependencyInjection\Compiler\CustomPass;
641+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
642+
use Symfony\Component\DependencyInjection\ContainerBuilder;
643+
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
644+
645+
class Kernel extends BaseKernel
646+
{
647+
use MicroKernelTrait;
648+
649+
// …
618650

619-
Before each request, the client reboots the kernel, recreating
620-
the container from scratch.
621-
This ensures that every requests are "isolated" using "new" service objects.
622-
Also, it means that entities loaded by Doctrine repositories will
623-
be "detached", so they will need to be refreshed by the manager or
624-
queried again from a repository.
625-
You can disable this behavior by calling the :method:`disableReboot() <Symfony\\Bundle\\FrameworkBundle\\KernelBrowser::disableReboot>` method.
651+
protected function build(ContainerBuilder $container): void
652+
{
653+
if ('test' === $this->environment) {
654+
$container->addCompilerPass(new class() implements CompilerPassInterface {
655+
public function process(ContainerBuilder $container): void
656+
{
657+
// prevents the security token to be cleared
658+
$container->getDefinition('security.token_storage')->clearTag('kernel.reset');
659+
660+
// prevents entities to be detached
661+
$container->getDefinition('doctrine')->clearTag('kernel.reset');
662+
663+
// …
664+
}
665+
});
666+
}
667+
}
668+
}
626669

627670
Browsing the Site
628671
.................

0 commit comments

Comments
 (0)