From 7f2c02b3eae57f895598355ca9e94320c9591e93 Mon Sep 17 00:00:00 2001 From: "fominov.evgeny" Date: Mon, 2 Feb 2026 16:24:02 +0400 Subject: [PATCH 1/9] Take connection from EntityManager if ObjectManager implements it. Fallback to the original logic otherwise --- src/Services/DatabaseTools/AbstractDbalDatabaseTool.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Services/DatabaseTools/AbstractDbalDatabaseTool.php b/src/Services/DatabaseTools/AbstractDbalDatabaseTool.php index 21744124..b0613d4e 100755 --- a/src/Services/DatabaseTools/AbstractDbalDatabaseTool.php +++ b/src/Services/DatabaseTools/AbstractDbalDatabaseTool.php @@ -18,6 +18,7 @@ use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\SQLitePlatform; +use Doctrine\ORM\EntityManagerInterface; abstract class AbstractDbalDatabaseTool extends AbstractDatabaseTool { @@ -26,7 +27,12 @@ abstract class AbstractDbalDatabaseTool extends AbstractDatabaseTool public function setObjectManagerName(?string $omName = null): void { parent::setObjectManagerName($omName); - $this->connection = $this->registry->getConnection($omName); + + if ($this->om instanceof EntityManagerInterface) { + $this->connection = $this->om->getConnection(); + } else { + $this->connection = $this->registry->getConnection($omName); + } } protected function getPlatformName(): string From e0dd36a508554c81dc61e0fd43d4c19ada3b555f Mon Sep 17 00:00:00 2001 From: "fominov.evgeny" Date: Mon, 2 Feb 2026 16:24:30 +0400 Subject: [PATCH 2/9] Added related tests --- ...figSqliteDifferentConnectionNameKernel.php | 29 ++++++++++++++++++ .../config.yml | 19 ++++++++++++ ...onfigSqliteDifferentConnectionNameTest.php | 30 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php create mode 100644 tests/AppConfigSqliteDifferentConnectionName/config.yml create mode 100644 tests/Test/ConfigSqliteDifferentConnectionNameTest.php diff --git a/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php new file mode 100644 index 00000000..b3fd07eb --- /dev/null +++ b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php @@ -0,0 +1,29 @@ +load(__DIR__.'/config.yml'); + } +} diff --git a/tests/AppConfigSqliteDifferentConnectionName/config.yml b/tests/AppConfigSqliteDifferentConnectionName/config.yml new file mode 100644 index 00000000..fd2f9901 --- /dev/null +++ b/tests/AppConfigSqliteDifferentConnectionName/config.yml @@ -0,0 +1,19 @@ +# inherits configuration from ../App/config.yml + +doctrine: + dbal: + default_connection: default + connections: + default: + url: 'sqlite:///%kernel.cache_dir%/test.db' + + orm: + default_entity_manager: different + entity_managers: + different: + connection: default + mappings: + LiipAcme: + dir: "%kernel.project_dir%/Entity" + prefix: 'Liip\Acme\Tests\App\Entity' + is_bundle: false diff --git a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php new file mode 100644 index 00000000..18d97907 --- /dev/null +++ b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php @@ -0,0 +1,30 @@ +databaseTool = $testContainer->get(DatabaseToolCollection::class)->get('different'); + } + + public static function getKernelClass(): string + { + return AppConfigSqliteDifferentConnectionNameKernel::class; + } +} From e261707693315e2ccf00ee1b6b4833dba1870c6a Mon Sep 17 00:00:00 2001 From: "fominov.evgeny" Date: Mon, 2 Feb 2026 17:15:37 +0400 Subject: [PATCH 3/9] Added license comment --- .../AppConfigSqliteDifferentConnectionNameKernel.php | 9 +++++++++ tests/Test/ConfigSqliteDifferentConnectionNameTest.php | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php index b3fd07eb..f73f70ed 100644 --- a/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php +++ b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php @@ -2,6 +2,15 @@ declare(strict_types=1); +/* + * This file is part of the Liip/TestFixturesBundle + * + * (c) Lukas Kahwe Smith + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName; use Liip\Acme\Tests\App\AppKernel; diff --git a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php index 18d97907..46e02f5a 100644 --- a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php +++ b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php @@ -2,6 +2,15 @@ declare(strict_types=1); +/* + * This file is part of the Liip/TestFixturesBundle + * + * (c) Lukas Kahwe Smith + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace Liip\Acme\Tests\Test; use Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\AppConfigSqliteDifferentConnectionNameKernel; From a0fb35ff6f70b41f6e480ce8f854e4b635b8c687 Mon Sep 17 00:00:00 2001 From: "fominov.evgeny" Date: Mon, 2 Feb 2026 18:37:30 +0400 Subject: [PATCH 4/9] Fix php-cs-fixer warning --- .../AppConfigSqliteDifferentConnectionNameKernel.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php index f73f70ed..10ccbea7 100644 --- a/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php +++ b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php @@ -19,9 +19,6 @@ class AppConfigSqliteDifferentConnectionNameKernel extends AppKernel { - /** - * {@inheritdoc} - */ public function getCacheDir(): string { return __DIR__.'/var/cache/'; From 0855e340023b5bab8f316bcc8ef4467a454fdd3b Mon Sep 17 00:00:00 2001 From: "fominov.evgeny" Date: Tue, 17 Mar 2026 11:18:12 +0400 Subject: [PATCH 5/9] Added second entity manager config with tests --- .../DataFixtures/ORM/LoadQueueData.php | 43 ++++++++ .../Entity/Queue.php | 73 ++++++++++++++ .../config.yml | 14 +++ ...onfigSqliteDifferentConnectionNameTest.php | 99 ++++++++++++++++++- 4 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 tests/AppConfigSqliteDifferentConnectionName/DataFixtures/ORM/LoadQueueData.php create mode 100644 tests/AppConfigSqliteDifferentConnectionName/Entity/Queue.php diff --git a/tests/AppConfigSqliteDifferentConnectionName/DataFixtures/ORM/LoadQueueData.php b/tests/AppConfigSqliteDifferentConnectionName/DataFixtures/ORM/LoadQueueData.php new file mode 100644 index 00000000..15b7174e --- /dev/null +++ b/tests/AppConfigSqliteDifferentConnectionName/DataFixtures/ORM/LoadQueueData.php @@ -0,0 +1,43 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Persistence\ObjectManager; +use Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\Entity\Queue; + +class LoadQueueData extends AbstractFixture +{ + public function load(ObjectManager $manager): void + { + $queue1 = new Queue(); + $queue1->setId(1); + $queue1->setPriority(100); + $queue1->setJobContext(json_encode([ + 'className' => 'someClass1', + ])); + + $manager->persist($queue1); + + $queue2 = new Queue(); + $queue2->setId(2); + $queue2->setPriority(200); + $queue2->setJobContext(json_encode([ + 'className' => 'someClass2', + ])); + + $manager->persist($queue2); + $manager->flush(); + } +} diff --git a/tests/AppConfigSqliteDifferentConnectionName/Entity/Queue.php b/tests/AppConfigSqliteDifferentConnectionName/Entity/Queue.php new file mode 100644 index 00000000..d0d952d2 --- /dev/null +++ b/tests/AppConfigSqliteDifferentConnectionName/Entity/Queue.php @@ -0,0 +1,73 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\Entity; + +use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Mapping as ORM; + +#[ORM\Entity] +#[ORM\Table(name: 'liip_queue')] +class Queue +{ + #[ORM\Id] + #[ORM\Column(type: Types::INTEGER)] + #[ORM\GeneratedValue(strategy: 'AUTO')] + private ?int $id = null; + + #[ORM\Column] + private int $priority; + + #[ORM\Column] + private string $jobContext; + + public function __construct() + { + } + + public function setId(int $id): self + { + $this->id = $id; + + return $this; + } + + public function getId(): int + { + return $this->id; + } + + public function setPriority(int $priority): self + { + $this->priority = $priority; + + return $this; + } + + public function getPriority(): int + { + return $this->priority; + } + + public function setJobContext(string $jobContext): self + { + $this->jobContext = $jobContext; + + return $this; + } + + public function getJobContext(): string + { + return $this->jobContext; + } +} diff --git a/tests/AppConfigSqliteDifferentConnectionName/config.yml b/tests/AppConfigSqliteDifferentConnectionName/config.yml index fd2f9901..17232b9f 100644 --- a/tests/AppConfigSqliteDifferentConnectionName/config.yml +++ b/tests/AppConfigSqliteDifferentConnectionName/config.yml @@ -6,6 +6,8 @@ doctrine: connections: default: url: 'sqlite:///%kernel.cache_dir%/test.db' + additional: + url: 'sqlite:///%kernel.cache_dir%/test_additional.db' orm: default_entity_manager: different @@ -17,3 +19,15 @@ doctrine: dir: "%kernel.project_dir%/Entity" prefix: 'Liip\Acme\Tests\App\Entity' is_bundle: false + additional: + connection: additional + mappings: + LiipAcme: + dir: "%kernel.project_dir%/../AppConfigSqliteDifferentConnectionName/Entity" + prefix: 'Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\Entity' + is_bundle: false + +services: + Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\DataFixtures\ORM\: + resource: 'DataFixtures/ORM/*' + tags: [ 'doctrine.fixture.orm' ] diff --git a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php index 46e02f5a..14f51b9e 100644 --- a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php +++ b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php @@ -13,27 +13,122 @@ namespace Liip\Acme\Tests\Test; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\EntityRepository; +use Doctrine\Persistence\ManagerRegistry; +use Liip\Acme\Tests\App\Entity\User; use Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\AppConfigSqliteDifferentConnectionNameKernel; +use Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\DataFixtures\ORM\LoadQueueData; +use Liip\Acme\Tests\AppConfigSqliteDifferentConnectionName\Entity\Queue; +use Liip\TestFixturesBundle\Event\PostFixtureSetupEvent; +use Liip\TestFixturesBundle\LiipTestFixturesEvents; use Liip\TestFixturesBundle\Services\DatabaseToolCollection; +use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool; +use PHPUnit\Framework\Attributes\DataProvider; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** - * Test SQLite database with entity manager having different name than his connection. + * Test SQLite database with entity manager having different name than its connection. * * @internal */ class ConfigSqliteDifferentConnectionNameTest extends ConfigSqliteTest { + private const MAIN_ENTITY_MANAGER_NAME = 'different'; + private const ADDITIONAL_ENTITY_MANAGER_NAME = 'additional'; + + private const MAIN_CONNECTION_NAME = 'default'; + private const ADDITIONAL_CONNECTION_NAME = 'additional'; + + /** + * @var EntityRepository + */ + private EntityRepository $queueRepository; + protected function setUp(): void { parent::setUp(); $testContainer = static::getContainer(); - $this->databaseTool = $testContainer->get(DatabaseToolCollection::class)->get('different'); + $this->queueRepository = $testContainer->get('doctrine') + ->getRepository(Queue::class); + + $this->databaseTool = $testContainer->get(DatabaseToolCollection::class) + ->get(self::MAIN_ENTITY_MANAGER_NAME); + } + + public function testLoadFixturesForSecondEntityManager(): void + { + $this->getDatabaseTool(self::ADDITIONAL_ENTITY_MANAGER_NAME) + ->loadFixtures([LoadQueueData::class]); + + $queueList = $this->queueRepository->findAll(); + + $this->assertCount(2, $queueList); + + /** @var Queue $queue */ + $queue = $this->queueRepository + ->findOneBy([ + 'id' => 1, + ]); + + $this->assertSame( + '{"className":"someClass1"}', + $queue->getJobContext() + ); + } + + public static function objectManagerNameViaEventDataProvider(): iterable + { + yield [self::MAIN_ENTITY_MANAGER_NAME, self::MAIN_CONNECTION_NAME]; + yield [self::ADDITIONAL_ENTITY_MANAGER_NAME, self::ADDITIONAL_CONNECTION_NAME]; + } + + #[DataProvider('objectManagerNameViaEventDataProvider')] + public function testObjectManagerAndConnectionViaEvent(string $onName, string $connectionName): void + { + /** @var EventDispatcherInterface $eventDispatcher */ + $eventDispatcher = $this->getContainer()->get(EventDispatcherInterface::class); + /** @var ManagerRegistry $registry */ + $registry = $this->getContainer()->get(ManagerRegistry::class); + + $eventDispatcher->addListener( + LiipTestFixturesEvents::POST_FIXTURE_SETUP, + function (PostFixtureSetupEvent $event) use ($registry, $onName, $connectionName) { + /** @var EntityManagerInterface $entityManager */ + $entityManager = $event->getManager(); + + $this->assertInstanceOf(EntityManagerInterface::class, $entityManager); + + $this->assertSame( + $registry->getManager($onName), + $entityManager, + ); + + $this->assertSame( + $registry->getConnection($connectionName), + $entityManager->getConnection(), + ); + } + ); + + $this->getDatabaseTool($onName) + ->loadFixtures(); } public static function getKernelClass(): string { return AppConfigSqliteDifferentConnectionNameKernel::class; } + + private function getDatabaseTool(string $omName): ORMSqliteDatabaseTool + { + $testContainer = static::getContainer(); + + /** @var DatabaseToolCollection $databaseToolCollection */ + $databaseToolCollection = $testContainer->get(DatabaseToolCollection::class); + + return $databaseToolCollection->get($omName); + } } From 47248a366a91abc255893b81fba761c90792e3bb Mon Sep 17 00:00:00 2001 From: "fominov.evgeny" Date: Tue, 17 Mar 2026 11:23:24 +0400 Subject: [PATCH 6/9] Fix phpstan warnings --- tests/Test/ConfigSqliteDifferentConnectionNameTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php index 14f51b9e..2b4370a4 100644 --- a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php +++ b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php @@ -23,6 +23,7 @@ use Liip\TestFixturesBundle\Event\PostFixtureSetupEvent; use Liip\TestFixturesBundle\LiipTestFixturesEvents; use Liip\TestFixturesBundle\Services\DatabaseToolCollection; +use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool; use PHPUnit\Framework\Attributes\DataProvider; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -79,6 +80,9 @@ public function testLoadFixturesForSecondEntityManager(): void ); } + /** + * @return iterable + */ public static function objectManagerNameViaEventDataProvider(): iterable { yield [self::MAIN_ENTITY_MANAGER_NAME, self::MAIN_CONNECTION_NAME]; @@ -122,7 +126,7 @@ public static function getKernelClass(): string return AppConfigSqliteDifferentConnectionNameKernel::class; } - private function getDatabaseTool(string $omName): ORMSqliteDatabaseTool + private function getDatabaseTool(string $omName): AbstractDatabaseTool { $testContainer = static::getContainer(); From 973f616861da63bcfc24cd52b89c27f612d36436 Mon Sep 17 00:00:00 2001 From: "fominov.evgeny" Date: Tue, 17 Mar 2026 11:25:33 +0400 Subject: [PATCH 7/9] Fix PHP-CS-Fixer warnings --- tests/Test/ConfigSqliteDifferentConnectionNameTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php index 2b4370a4..c6c87ed1 100644 --- a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php +++ b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php @@ -24,7 +24,6 @@ use Liip\TestFixturesBundle\LiipTestFixturesEvents; use Liip\TestFixturesBundle\Services\DatabaseToolCollection; use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; -use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool; use PHPUnit\Framework\Attributes\DataProvider; use Symfony\Component\EventDispatcher\EventDispatcherInterface; From aa5cd63226c5fbebd4a52cd3997df4db9a42e1e5 Mon Sep 17 00:00:00 2001 From: Fominov Evgeny <140058418+fe-optimax@users.noreply.github.com> Date: Tue, 24 Mar 2026 16:57:51 +0400 Subject: [PATCH 8/9] apply suggestion Co-authored-by: Alexis Lefebvre <2071331+alexislefebvre@users.noreply.github.com> --- .../AppConfigSqliteDifferentConnectionNameKernel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php index 10ccbea7..85a8a8a1 100644 --- a/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php +++ b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php @@ -29,7 +29,7 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa // Load the default file. parent::configureContainer($container, $loader); - // Load the file with MySQL configuration + // Load the file with 2 entity managers $loader->load(__DIR__.'/config.yml'); } } From 218e8f0549063224786d115f660ed40eb1def198 Mon Sep 17 00:00:00 2001 From: Fominov Evgeny <140058418+fe-optimax@users.noreply.github.com> Date: Tue, 24 Mar 2026 16:58:18 +0400 Subject: [PATCH 9/9] apply suggestion Co-authored-by: Alexis Lefebvre <2071331+alexislefebvre@users.noreply.github.com> --- tests/Test/ConfigSqliteDifferentConnectionNameTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php index c6c87ed1..c524dce8 100644 --- a/tests/Test/ConfigSqliteDifferentConnectionNameTest.php +++ b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php @@ -35,9 +35,9 @@ class ConfigSqliteDifferentConnectionNameTest extends ConfigSqliteTest { private const MAIN_ENTITY_MANAGER_NAME = 'different'; - private const ADDITIONAL_ENTITY_MANAGER_NAME = 'additional'; - private const MAIN_CONNECTION_NAME = 'default'; + + private const ADDITIONAL_ENTITY_MANAGER_NAME = 'additional'; private const ADDITIONAL_CONNECTION_NAME = 'additional'; /**