* Reset also fob_symfony.driver_kernel between scenarios

* Make sure reset() for the Mink driver implementation creates new KernelBrowser instances, to achieve consistent reboots of the `fob_symfony.driver_kernel` when making more than one request within a single scenario
This commit is contained in:
Matthias Pigulla
2022-11-24 12:22:48 +00:00
parent 8a0c40c9bc
commit f1971fde57
5 changed files with 270 additions and 14 deletions

View File

@@ -6,35 +6,72 @@ namespace FriendsOfBehat\SymfonyExtension\Driver;
use Behat\Mink\Driver\BrowserKitDriver;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\HttpKernel\KernelInterface;
final class SymfonyDriver extends BrowserKitDriver
{
/** @var KernelInterface */
private $kernel;
/** @var string|null */
private $baseUrl;
public function __construct(KernelInterface $kernel, ?string $baseUrl)
{
if (!$kernel->getContainer()->has('test.client')) {
$this->kernel = $kernel;
$this->baseUrl = $baseUrl;
if (!$this->kernel->getContainer()->has('test.client')) {
throw new \RuntimeException(sprintf(
'Kernel "%s" used by Behat with "%s" environment and debug %s does not have "test.client" service. ' . "\n" .
'Please make sure the kernel is using "test" environment or have "framework.test" configuration option enabled.',
get_class($kernel),
$kernel->getEnvironment(),
$kernel->isDebug() ? 'enabled' : 'disabled',
get_class($this->kernel),
$this->kernel->getEnvironment(),
$this->kernel->isDebug() ? 'enabled' : 'disabled',
));
}
/** @var object $testClient */
$testClient = $kernel->getContainer()->get('test.client');
parent::__construct($this->createBrowser(), $this->baseUrl);
}
if (!$testClient instanceof Client && !$testClient instanceof AbstractBrowser) {
public function reset()
{
parent::reset();
/*
* When \Behat\Mink\Driver\DriverInterface::visit() is called on this driver here,
* we ultimately end up in \Symfony\Bundle\FrameworkBundle\KernelBrowser::doRequest().
* That method tracks state across multiple requests to detect whether it is necessary
* to reboot the targeted-at kernel before performing the next request.
*
* We do not want this state to leak between Behat scenarios, and so this method here
* seems to be a good place to reset driver state as well.
*
* Since there is no other way to reset the KernelBrowser, we create a new instance.
*
* This also makes sense for another reason: The $kernel instance is rebooted by the
* KernelOrchestrator between Behat scenarios. So, every time we reset the driver
* (which happens at least for the first request during a scenario) we want to make
* sure we are using a KernelBrowser instance created in the currently active
* kernel "state" ("epoch"? "generation"?)
*/
parent::__construct($this->createBrowser(), $this->baseUrl);
}
private function createBrowser(): AbstractBrowser
{
/** @var object $testClient */
$testClient = $this->kernel->getContainer()->get('test.client');
if (!$testClient instanceof AbstractBrowser) {
throw new \RuntimeException(sprintf(
'Service "test.client" should be an instance of "%s" or "%s", "%s" given.',
Client::class,
'Service "test.client" should be an instance of "%s", "%s" given.',
AbstractBrowser::class,
get_class($testClient),
));
}
parent::__construct($testClient, $baseUrl);
return $testClient;
}
}

View File

@@ -15,12 +15,16 @@ final class KernelOrchestrator implements EventSubscriberInterface
/** @var KernelInterface */
private $symfonyKernel;
/** @var KernelInterface */
private $driverKernel;
/** @var ContainerInterface */
private $behatContainer;
public function __construct(KernelInterface $symfonyKernel, ContainerInterface $behatContainer)
public function __construct(KernelInterface $symfonyKernel, KernelInterface $driverKernel, ContainerInterface $behatContainer)
{
$this->symfonyKernel = $symfonyKernel;
$this->driverKernel = $driverKernel;
$this->behatContainer = $behatContainer;
}
@@ -42,8 +46,25 @@ final class KernelOrchestrator implements EventSubscriberInterface
public function tearDown(): void
{
$this->driverKernel->shutdown();
/*
* Reset both Kernel instances after a scenario has been run: The Kernel (and thus Container)
* used in Behat to configure Contexts; and the Kernel used by the SymfonyDriver to which
* requests are dispatched (through Mink).
*
* Since the "symfony" container is needed in a few other places (where and why exactly?) and
* has to be in a booted/usable state most of the time, we do not shut it down here in tearDown()
* and boot it in setUp().
*
* Instead, the definitions in \FriendsOfBehat\SymfonyExtension\ServiceContainer\SymfonyExtension
* make sure both kernels are booted immediately after being created, and we also initiate the
* re-boot() here right away.
*/
$this->symfonyKernel->getContainer()->set('behat.service_container', null);
$this->symfonyKernel->shutdown();
$this->symfonyKernel->boot();
$this->driverKernel->boot();
}
}

View File

@@ -134,7 +134,7 @@ final class SymfonyExtension implements Extension
private function loadKernelRebooter(ContainerBuilder $container): void
{
$definition = new Definition(KernelOrchestrator::class, [new Reference(self::KERNEL_ID), $container]);
$definition = new Definition(KernelOrchestrator::class, [new Reference(self::KERNEL_ID), new Reference(self::DRIVER_KERNEL_ID), $container]);
$definition->addTag(EventDispatcherExtension::SUBSCRIBER_TAG);
$container->setDefinition('fob_symfony.kernel_orchestrator', $definition);