From bcf71dbb0b7ae8a0cb18aadf979d7c2f4576cabe Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Thu, 2 Nov 2023 19:07:58 +0100 Subject: [PATCH] chore: add test with 2 connections --- .../AppConfigSqliteTwoConnectionsKernel.php | 38 +++++ .../AppConfigSqliteTwoConnections/config.yml | 40 +++++ tests/Test/ConfigSqliteTwoConnectionsTest.php | 153 ++++++++++++++++++ 3 files changed, 231 insertions(+) create mode 100644 tests/AppConfigSqliteTwoConnections/AppConfigSqliteTwoConnectionsKernel.php create mode 100644 tests/AppConfigSqliteTwoConnections/config.yml create mode 100644 tests/Test/ConfigSqliteTwoConnectionsTest.php diff --git a/tests/AppConfigSqliteTwoConnections/AppConfigSqliteTwoConnectionsKernel.php b/tests/AppConfigSqliteTwoConnections/AppConfigSqliteTwoConnectionsKernel.php new file mode 100644 index 00000000..cee993d4 --- /dev/null +++ b/tests/AppConfigSqliteTwoConnections/AppConfigSqliteTwoConnectionsKernel.php @@ -0,0 +1,38 @@ + + * + * 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'); + } +} diff --git a/tests/AppConfigSqliteTwoConnections/config.yml b/tests/AppConfigSqliteTwoConnections/config.yml new file mode 100644 index 00000000..638dca74 --- /dev/null +++ b/tests/AppConfigSqliteTwoConnections/config.yml @@ -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 diff --git a/tests/Test/ConfigSqliteTwoConnectionsTest.php b/tests/Test/ConfigSqliteTwoConnectionsTest.php new file mode 100644 index 00000000..6825da5b --- /dev/null +++ b/tests/Test/ConfigSqliteTwoConnectionsTest.php @@ -0,0 +1,153 @@ + + * + * 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' => 'foo@bar.com', + ]) + ; + + $this->assertSame( + 'foo@bar.com', + $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); + } +}