From 6fa255386b9adb27e9834843c638854630abcf86 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Mar 2019 20:36:35 +0100 Subject: [PATCH] Defined config to run database tests against mysql and postgres --- composer.json | 6 ++- config/test/bootstrap_api_tests.php | 2 +- config/test/bootstrap_db_tests.php | 4 +- config/test/test_config.global.php | 43 ++++++++++++++++--- module/Common/test-db/TestHelper.php | 11 ++--- .../Shlinkio.Shlink.Core.Entity.ShortUrl.php | 4 +- .../Shlinkio.Shlink.Core.Entity.Visit.php | 4 +- .../Repository/ShortUrlRepositoryTest.php | 4 +- 8 files changed, 55 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index 194b8c80..6fe1c2e2 100644 --- a/composer.json +++ b/composer.json @@ -115,6 +115,8 @@ "test:unit": "phpdbg -qrr vendor/bin/phpunit --order-by=random --coverage-php build/coverage-unit.cov --testdox", "test:unit:ci": "phpdbg -qrr vendor/bin/phpunit --order-by=random --coverage-php build/coverage-unit.cov --coverage-clover=build/clover.xml --coverage-xml=build/coverage-xml --log-junit=build/phpunit.junit.xml --testdox", "test:db": "APP_ENV=test phpdbg -qrr vendor/bin/phpunit --order-by=random -c phpunit-db.xml --coverage-php build/coverage-db.cov --testdox", + "test:db:mysql": "DB_DRIVER=mysql composer test:db", + "test:db:postgres": "DB_DRIVER=postgres composer test:db", "test:api": "bin/test/run-api-tests.sh", "test:pretty": [ @@ -141,7 +143,9 @@ "test:ci": "Runs all test suites, generating all needed reports and logs for CI envs", "test:unit": "Runs unit test suites", "test:unit:ci": "Runs unit test suites, generating all needed reports and logs for CI envs", - "test:db": "Runs database test suites (covering entity repositories)", + "test:db": "Runs database test suites on a SQLite database", + "test:db:mysql": "Runs database test suites on a MySQL database", + "test:db:postgres": "Runs database test suites on a PostgreSQL database", "test:api": "Runs API test suites", "test:pretty": "Runs all test suites and generates an HTML code coverage report", "test:unit:pretty": "Runs unit test suites and generates an HTML code coverage report", diff --git a/config/test/bootstrap_api_tests.php b/config/test/bootstrap_api_tests.php index 0b2b4e16..5551094f 100644 --- a/config/test/bootstrap_api_tests.php +++ b/config/test/bootstrap_api_tests.php @@ -20,7 +20,7 @@ $testHelper = $container->get(TestHelper::class); $config = $container->get('config'); $em = $container->get(EntityManager::class); -$testHelper->createTestDb($config['entity_manager']['connection']['path']); +$testHelper->createTestDb(); ApiTest\ApiTestCase::setApiClient($container->get('shlink_test_api_client')); ApiTest\ApiTestCase::setSeedFixturesCallback(function () use ($testHelper, $em, $config) { $testHelper->seedFixtures($em, $config['data_fixtures'] ?? []); diff --git a/config/test/bootstrap_db_tests.php b/config/test/bootstrap_db_tests.php index 893e2b9a..f2788257 100644 --- a/config/test/bootstrap_db_tests.php +++ b/config/test/bootstrap_db_tests.php @@ -15,7 +15,5 @@ if (! file_exists('.env')) { /** @var ContainerInterface $container */ $container = require __DIR__ . '/../container.php'; -$config = $container->get('config'); - -$container->get(TestHelper::class)->createTestDb($config['entity_manager']['connection']['path']); +$container->get(TestHelper::class)->createTestDb(); DbTest\DatabaseTestCase::setEntityManager($container->get('em')); diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index d5f27a95..7d0dd739 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -4,15 +4,52 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink; use GuzzleHttp\Client; +use PDO; use Zend\ConfigAggregator\ConfigAggregator; use Zend\ServiceManager\Factory\InvokableFactory; +use function Shlinkio\Shlink\Common\env; use function sprintf; use function sys_get_temp_dir; $swooleTestingHost = '127.0.0.1'; $swooleTestingPort = 9999; +$buildDbConnection = function () { + $driver = env('DB_DRIVER', 'sqlite'); + + switch ($driver) { + case 'sqlite': + return [ + 'driver' => 'pdo_sqlite', + 'path' => sys_get_temp_dir() . '/shlink-tests.db', + ]; + case 'mysql': + return [ + 'driver' => 'pdo_mysql', + 'host' => 'shlink_db', + 'user' => 'root', + 'password' => 'root', + 'dbname' => 'shlink_test', + 'charset' => 'utf8', + 'driverOptions' => [ + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', + ], + ]; + case 'postgres': + return [ + 'driver' => 'pdo_pgsql', + 'host' => 'shlink_db_postgres', + 'user' => 'postgres', + 'password' => 'root', + 'dbname' => 'shlink_test', + 'charset' => 'utf8', + ]; + default: + return []; + } +}; + return [ 'debug' => true, @@ -49,11 +86,7 @@ return [ ], 'entity_manager' => [ - 'connection' => [ - 'driver' => 'pdo_sqlite', - 'path' => sys_get_temp_dir() . '/shlink-tests.db', -// 'path' => __DIR__ . '/../../data/shlink-tests.db', - ], + 'connection' => $buildDbConnection(), ], 'data_fixtures' => [ diff --git a/module/Common/test-db/TestHelper.php b/module/Common/test-db/TestHelper.php index 411d2773..e7cce810 100644 --- a/module/Common/test-db/TestHelper.php +++ b/module/Common/test-db/TestHelper.php @@ -9,16 +9,13 @@ use Doctrine\Common\DataFixtures\Purger\ORMPurger; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Process\Process; -use function file_exists; -use function unlink; - class TestHelper { - public function createTestDb(string $shlinkDbPath): void + public function createTestDb(): void { - if (file_exists($shlinkDbPath)) { - unlink($shlinkDbPath); - } + $process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:drop', '--force', '--no-interaction', '-q']); + $process->inheritEnvironmentVariables() + ->mustRun(); $process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q']); $process->inheritEnvironmentVariables() diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php index c6bdcb86..8012ea56 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php @@ -58,6 +58,6 @@ $builder->createOneToMany('visits', Entity\Visit::class) $builder->createManyToMany('tags', Entity\Tag::class) ->setJoinTable('short_urls_in_tags') - ->addInverseJoinColumn('tag_id', 'id') - ->addJoinColumn('short_url_id', 'id') + ->addInverseJoinColumn('tag_id', 'id', true, false, 'CASCADE') + ->addJoinColumn('short_url_id', 'id', true, false, 'CASCADE') ->build(); diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php index 5e0c4c31..bf0b921a 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php @@ -43,10 +43,10 @@ $builder->createField('userAgent', Type::STRING) ->build(); $builder->createManyToOne('shortUrl', Entity\ShortUrl::class) - ->addJoinColumn('short_url_id', 'id', false) + ->addJoinColumn('short_url_id', 'id', false, false, 'CASCADE') ->build(); $builder->createManyToOne('visitLocation', Entity\VisitLocation::class) - ->addJoinColumn('visit_location_id', 'id') + ->addJoinColumn('visit_location_id', 'id', true, false, 'Set NULL') ->cascadePersist() ->build(); diff --git a/module/Core/test-db/Repository/ShortUrlRepositoryTest.php b/module/Core/test-db/Repository/ShortUrlRepositoryTest.php index e4db2e9b..fd7e8789 100644 --- a/module/Core/test-db/Repository/ShortUrlRepositoryTest.php +++ b/module/Core/test-db/Repository/ShortUrlRepositoryTest.php @@ -18,9 +18,9 @@ use function count; class ShortUrlRepositoryTest extends DatabaseTestCase { protected const ENTITIES_TO_EMPTY = [ - ShortUrl::class, - Visit::class, Tag::class, + Visit::class, + ShortUrl::class, ]; /** @var ShortUrlRepository */