Description
After updating my project from Symfony 4.4 to 5.4 tests began fail. I've looked deep into the issue and discovered that after this feature was introduced the subscribers (Doctrine listeneres) initialize after addEventListener
call, and if the subscriber depends on Doctrine services (such a repository) the connection creation called twice. Here is a stack trace:
-> StaticConnectionFactory.php:27, DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticConnectionFactory->createConnection()
App_KernelTestDebugContainer.php:1355, ContainerLXdkVH1\App_KernelTestDebugContainer->getDoctrine_Dbal_DefaultConnectionService()
App_KernelTestDebugContainer.php:1383, ContainerLXdkVH1\App_KernelTestDebugContainer->getDoctrine_Orm_DefaultEntityManagerService()
App_KernelTestDebugContainer.php:1368, ContainerLXdkVH1\App_KernelTestDebugContainer->ContainerLXdkVH1\{closure:/var/www/html/var/cache/test/ContainerLXdkVH1/App_KernelTestDebugContainer.php:1367-1373}()
EntityManager_9a5be93.php:36, Closure->__invoke()
EntityManager_9a5be93.php:36, ContainerLXdkVH1\EntityManager_9a5be93->getMetadataFactory()
AbstractManagerRegistry.php:182, Doctrine\Bundle\DoctrineBundle\Registry->getManagerForClass()
ServiceEntityRepository.php:37, App\Repository\UserRepository->__construct()
UserRepository.php:31, App\Repository\UserRepository->__construct()
App_KernelTestDebugContainer.php:2674, ContainerLXdkVH1\App_KernelTestDebugContainer->getUserRepositoryService()
getUserListenerService.php:23, ContainerLXdkVH1\getUserListenerService::do()
App_KernelTestDebugContainer.php:1005, ContainerLXdkVH1\App_KernelTestDebugContainer->load()
Container.php:422, ContainerLXdkVH1\App_KernelTestDebugContainer->getService()
ServiceLocator.php:42, Symfony\Component\DependencyInjection\Argument\ServiceLocator->get()
ContainerAwareEventManager.php:203, Symfony\Bridge\Doctrine\ContainerAwareEventManager->initializeSubscribers()
ContainerAwareEventManager.php:121, Symfony\Bridge\Doctrine\ContainerAwareEventManager->addEventListener()
-> StaticConnectionFactory.php:44, DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticConnectionFactory->createConnection()
App_KernelTestDebugContainer.php:1355, ContainerLXdkVH1\App_KernelTestDebugContainer->getDoctrine_Dbal_DefaultConnectionService()
App_KernelTestDebugContainer.php:1383, ContainerLXdkVH1\App_KernelTestDebugContainer->getDoctrine_Orm_DefaultEntityManagerService()
App_KernelTestDebugContainer.php:1368, ContainerLXdkVH1\App_KernelTestDebugContainer->ContainerLXdkVH1\{closure:/var/www/html/var/cache/test/ContainerLXdkVH1/App_KernelTestDebugContainer.php:1367-1373}()
EntityManager_9a5be93.php:239, Closure->__invoke()
EntityManager_9a5be93.php:239, ContainerLXdkVH1\EntityManager_9a5be93->getConfiguration()
ProxyCacheWarmer.php:54, Symfony\Bridge\Doctrine\CacheWarmer\ProxyCacheWarmer->warmUp()
CacheWarmerAggregate.php:99, Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp()
Kernel.php:587, App\Kernel->initializeContainer()
Kernel.php:789, App\Kernel->preBoot()
Kernel.php:128, App\Kernel->boot()
KernelTestCase.php:83, Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::bootKernel()
KernelTestCase.php:103, Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::getContainer()
WebTestCase.php:65, App\Tests\Controller\DefaultControllerTest->getEntityManager()
DefaultControllerTest.php:23, App\Tests\Controller\DefaultControllerTest->setUp()
TestCase.php:1145, App\Tests\Controller\DefaultControllerTest->runBare()
TestResult.php:726, PHPUnit\Framework\TestResult->run()
TestCase.php:904, App\Tests\Controller\DefaultControllerTest->run()
TestSuite.php:678, PHPUnit\Framework\DataProviderTestSuite->run()
TestSuite.php:678, PHPUnit\Framework\TestSuite->run()
TestSuite.php:678, PHPUnit\Framework\TestSuite->run()
TestRunner.php:670, PHPUnit\TextUI\TestRunner->run()
Command.php:143, Symfony\Bridge\PhpUnit\Legacy\CommandForV9->run()
Command.php:96, PHPUnit\TextUI\Command::main()
phpunit:22, include()
simple-phpunit.php:436, require()
phpunit:13, {main}()
I'm not sure this a right place to fix it but looks like event listener might be attached another way.