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 diff --git a/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php new file mode 100644 index 00000000..85a8a8a1 --- /dev/null +++ b/tests/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php @@ -0,0 +1,35 @@ + + * + * 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; +use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +class AppConfigSqliteDifferentConnectionNameKernel extends AppKernel +{ + public function getCacheDir(): string + { + return __DIR__.'/var/cache/'; + } + + protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void + { + // Load the default file. + parent::configureContainer($container, $loader); + + // Load the file with 2 entity managers + $loader->load(__DIR__.'/config.yml'); + } +} 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 new file mode 100644 index 00000000..17232b9f --- /dev/null +++ b/tests/AppConfigSqliteDifferentConnectionName/config.yml @@ -0,0 +1,33 @@ +# inherits configuration from ../App/config.yml + +doctrine: + dbal: + default_connection: default + connections: + default: + url: 'sqlite:///%kernel.cache_dir%/test.db' + additional: + url: 'sqlite:///%kernel.cache_dir%/test_additional.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 + 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 new file mode 100644 index 00000000..c524dce8 --- /dev/null +++ b/tests/Test/ConfigSqliteDifferentConnectionNameTest.php @@ -0,0 +1,137 @@ + + * + * 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 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\AbstractDatabaseTool; +use PHPUnit\Framework\Attributes\DataProvider; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; + +/** + * 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 MAIN_CONNECTION_NAME = 'default'; + + private const ADDITIONAL_ENTITY_MANAGER_NAME = 'additional'; + private const ADDITIONAL_CONNECTION_NAME = 'additional'; + + /** + * @var EntityRepository + */ + private EntityRepository $queueRepository; + + protected function setUp(): void + { + parent::setUp(); + + $testContainer = static::getContainer(); + + $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() + ); + } + + /** + * @return iterable + */ + 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): AbstractDatabaseTool + { + $testContainer = static::getContainer(); + + /** @var DatabaseToolCollection $databaseToolCollection */ + $databaseToolCollection = $testContainer->get(DatabaseToolCollection::class); + + return $databaseToolCollection->get($omName); + } +}