Skip to content

Commit

Permalink
chore: add test with 2 connections
Browse files Browse the repository at this point in the history
  • Loading branch information
alexislefebvre committed Nov 2, 2023
1 parent e19bfbb commit bcf71db
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\Acme\Tests\AppConfigSqliteTwoConnections;

use Liip\Acme\Tests\App\AppKernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AppConfigSqliteTwoConnectionsKernel extends AppKernel
{
/**
* {@inheritdoc}
*/
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 MySQL configuration
$loader->load(__DIR__.'/config.yml');
}
}
40 changes: 40 additions & 0 deletions tests/AppConfigSqliteTwoConnections/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# inherits configuration from ../App/config.yml

doctrine:
dbal:
default_connection: default
connections:
default:
url: 'sqlite:///%kernel.cache_dir%/default.db'
custom:
url: 'sqlite:///%kernel.cache_dir%/custom.db'

orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
LiipAcme:
type: php
dir: "%kernel.project_dir%/../App/Entity"
prefix: 'Liip\Acme\Tests\App\Entity'
is_bundle: false
LiipAcmeYml:
type: "yml"
dir: "%kernel.project_dir%/../App/Resources/config/doctrine"
prefix: 'Liip\Acme\Tests\App\Entity'
is_bundle: false
custom:
connection: custom
mappings:
LiipAcmeCustom:
type: php
dir: "%kernel.project_dir%/../App/Entity"
prefix: 'Liip\Acme\Tests\App\Entity'
is_bundle: false
LiipAcmeCustomYml:
type: "yml"
dir: "%kernel.project_dir%/../App/Resources/config/doctrine"
prefix: 'Liip\Acme\Tests\App\Entity'
is_bundle: false
153 changes: 153 additions & 0 deletions tests/Test/ConfigSqliteTwoConnectionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <[email protected]>
*
* 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\Common\Annotations\Annotation\IgnoreAnnotation;
use Liip\Acme\Tests\App\Entity\User;
use Liip\Acme\Tests\AppConfigSqliteTwoConnections\AppConfigSqliteTwoConnectionsKernel;
use Liip\Acme\Tests\Traits\ContainerProvider;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
use Liip\TestFixturesBundle\Services\DatabaseTools\ORMDatabaseTool;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

// BC, needed by "theofidry/alice-data-fixtures: <1.3" not compatible with "doctrine/persistence: ^2.0"
if (interface_exists('\Doctrine\Persistence\ObjectManager')
&& !interface_exists('\Doctrine\Common\Persistence\ObjectManager')) {
class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence\ObjectManager');
}

/**
* Test MySQL database with 2 entity managers and connections.
*
* The following tests require a connection to a MySQL database,
* they are disabled by default (see phpunit.xml.dist).
*
* In order to run them, you have to set the MySQL connection
* parameters in the Tests/ConfigSqliteTwoConnectionsTest/config.yml file and
* add “--exclude-group ""” when running PHPUnit.
*
* Use Tests/ConfigSqliteTwoConnectionsTest/ConfigSqliteTwoConnectionsTestKernel.php
* instead of Tests/App/AppKernel.php.
* So it must be loaded in a separate process.
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
* @IgnoreAnnotation("group")
*
* @internal
*/
class ConfigSqliteTwoConnectionsTest extends KernelTestCase
{
use ContainerProvider;

/** @var AbstractDatabaseTool */
protected $databaseToolDefault;
/** @var AbstractDatabaseTool */
protected $databaseToolCustom;

protected function setUp(): void
{
parent::setUp();

self::bootKernel();

$this->databaseToolDefault = $this->getTestContainer()->get(DatabaseToolCollection::class)->get('default');
$this->assertInstanceOf(ORMDatabaseTool::class, $this->databaseToolDefault);

$this->databaseToolCustom = $this->getTestContainer()->get(DatabaseToolCollection::class)->get('custom', 'custom');
$this->assertInstanceOf(ORMDatabaseTool::class, $this->databaseToolCustom);
}

/**
* Data fixtures.
*
* @group mysql
*/
public function testLoadEmptyFixtures(): void
{
$fixturesDefault = $this->databaseToolDefault->loadFixtures([]);
$this->assertInstanceOf(
'Doctrine\Common\DataFixtures\Executor\ORMExecutor',
$fixturesDefault
);

$fixturesCustom = $this->databaseToolCustom->loadFixtures([]);
$this->assertInstanceOf(
'Doctrine\Common\DataFixtures\Executor\ORMExecutor',
$fixturesCustom
);
}

/**
* @group mysql
*/
public function testLoadFixtures(int $firstUserId = 1): void
{
$this->databaseToolDefault->loadFixtures([
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
]);

$userRepositoryDefault = $this->getTestContainer()->get('doctrine')
->getRepository(User::class, 'default')
;

// Load data from database
/** @var User $user */
$user = $userRepositoryDefault
->findOneBy([
'email' => '[email protected]',
])
;

$this->assertSame(
'[email protected]',
$user->getEmail()
);

$this->databaseToolCustom->loadFixtures([
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
]);

// Load data from the other database
$userRepositoryCustom = $this->getTestContainer()->get('doctrine')
->getRepository(User::class, 'custom')
;

/** @var User $user */
$user = $userRepositoryCustom
->findOneBy([
'id' => 1,
])
;

$this->assertSame(
'hey',
$user->getName()
);
}

protected static function getKernelClass(): string
{
return AppConfigSqliteTwoConnectionsKernel::class;
}

protected function tearDown(): void
{
parent::tearDown();
unset($this->userRepositoryDefault);
unset($this->databaseToolDefault);
unset($this->databaseToolCustom);
}
}

0 comments on commit bcf71db

Please sign in to comment.