From 71d32064e581ed6adb88fdbf597ddf53757d8422 Mon Sep 17 00:00:00 2001 From: mrsombre Date: Tue, 6 Apr 2021 15:25:51 +0700 Subject: [PATCH 1/2] Fix Doctrine Connection service alias --- src/Codeception/Module/Symfony.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 338c4cd6..689ded03 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -253,8 +253,8 @@ public function _getEntityManager() if ($container->has('doctrine.orm.default_entity_manager')) { $this->persistPermanentService('doctrine.orm.default_entity_manager'); } - if ($container->has('doctrine.dbal.backend_connection')) { - $this->persistPermanentService('doctrine.dbal.backend_connection'); + if ($container->has('doctrine.dbal.default_connection')) { + $this->persistPermanentService('doctrine.dbal.default_connection'); } } return $this->permanentServices[$emService]; From 6b381958e9f3500ca3c12b6b46ee286fa62657f9 Mon Sep 17 00:00:00 2001 From: mrsombre Date: Thu, 8 Apr 2021 02:29:18 +0700 Subject: [PATCH 2/2] Do not close Doctrine connections until test ends --- src/Codeception/Lib/Connector/Symfony.php | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Codeception/Lib/Connector/Symfony.php b/src/Codeception/Lib/Connector/Symfony.php index 4dfca1fe..174b1607 100644 --- a/src/Codeception/Lib/Connector/Symfony.php +++ b/src/Codeception/Lib/Connector/Symfony.php @@ -5,6 +5,7 @@ namespace Codeception\Lib\Connector; use InvalidArgumentException; +use Symfony\Bundle\FrameworkBundle\Test\TestContainer; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -86,6 +87,7 @@ public function rebootKernel(): void } } + $this->persistDoctrineConnections(); $this->kernel->reboot(null); $this->container = $this->getContainer(); @@ -131,4 +133,28 @@ private function getService(string $serviceName): ?object } return null; } + + private function persistDoctrineConnections() + { + if (!$this->container->hasParameter('doctrine.connections')) { + return; + } + + if ($this->container instanceof TestContainer) { + $reflectedTestContainer = new \ReflectionMethod($this->container, 'getPublicContainer'); + $reflectedTestContainer->setAccessible(true); + $publicContainer = $reflectedTestContainer->invoke($this->container); + } else { + $publicContainer = $this->container; + } + + $reflectedContainer = new \ReflectionClass($publicContainer); + $reflectionTarget = $reflectedContainer->hasProperty('parameters') ? $publicContainer : $publicContainer->getParameterBag(); + + $reflectedParameters = new \ReflectionProperty($reflectionTarget, 'parameters'); + $reflectedParameters->setAccessible(true); + $parameters = $reflectedParameters->getValue($reflectionTarget); + unset($parameters['doctrine.connections']); + $reflectedParameters->setValue($reflectionTarget, $parameters); + } }