-
Notifications
You must be signed in to change notification settings - Fork 49
Support different name entity manager connection #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexislefebvre
merged 9 commits into
liip:3.x
from
fe-optimax:support-different-name-entity-manager-connection
Mar 26, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7f2c02b
Take connection from EntityManager if ObjectManager implements it. Fa…
fe-optimax e0dd36a
Added related tests
fe-optimax e261707
Added license comment
fe-optimax a0fb35f
Fix php-cs-fixer warning
fe-optimax 0855e34
Added second entity manager config with tests
fe-optimax 47248a3
Fix phpstan warnings
fe-optimax 973f616
Fix PHP-CS-Fixer warnings
fe-optimax aa5cd63
apply suggestion
fe-optimax 218e8f0
apply suggestion
fe-optimax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...s/AppConfigSqliteDifferentConnectionName/AppConfigSqliteDifferentConnectionNameKernel.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the Liip/TestFixturesBundle | ||
| * | ||
| * (c) Lukas Kahwe Smith <smith@pooteeweet.org> | ||
| * | ||
| * 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'); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
tests/AppConfigSqliteDifferentConnectionName/DataFixtures/ORM/LoadQueueData.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the Liip/TestFixturesBundle | ||
| * | ||
| * (c) Lukas Kahwe Smith <smith@pooteeweet.org> | ||
| * | ||
| * 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(); | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
tests/AppConfigSqliteDifferentConnectionName/Entity/Queue.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the Liip/TestFixturesBundle | ||
| * | ||
| * (c) Lukas Kahwe Smith <smith@pooteeweet.org> | ||
| * | ||
| * 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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' ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the Liip/TestFixturesBundle | ||
| * | ||
| * (c) Lukas Kahwe Smith <smith@pooteeweet.org> | ||
| * | ||
| * 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'; | ||
|
fe-optimax marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * @var EntityRepository<User> | ||
| */ | ||
| 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<array{0:string,1:string}> | ||
| */ | ||
| 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); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.