Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Services/DatabaseTools/AbstractDbalDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
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');
}
}
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 tests/AppConfigSqliteDifferentConnectionName/Entity/Queue.php
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;
}
}
33 changes: 33 additions & 0 deletions tests/AppConfigSqliteDifferentConnectionName/config.yml
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:
Comment thread
alexislefebvre marked this conversation as resolved.
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' ]
137 changes: 137 additions & 0 deletions tests/Test/ConfigSqliteDifferentConnectionNameTest.php
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';
Comment thread
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);
}
}
Loading