From 6fa255386b9adb27e9834843c638854630abcf86 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Mar 2019 20:36:35 +0100 Subject: [PATCH 1/3] 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 */ From 840e377245672828b0071ecae265a49cd7819037 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Mar 2019 20:50:32 +0100 Subject: [PATCH 2/3] Added execution of db tests with mysql and postgres to travis --- .travis.yml | 8 ++++++++ composer.json | 2 ++ config/test/test_config.global.php | 9 +++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index f54e3358..637e9e7d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,10 @@ php: - 7.2 - 7.3 +services: + - mysql + - postgresql + before_install: - echo 'extension = memcached.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - echo 'extension = apcu.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini @@ -19,6 +23,10 @@ install: - composer self-update - composer install --no-interaction +before_script: + - mysql -e 'CREATE DATABASE shlink_test;' + - psql -c 'create database shlink_test;' -U postgres + script: - mkdir build - composer ci diff --git a/composer.json b/composer.json index 6fe1c2e2..16def607 100644 --- a/composer.json +++ b/composer.json @@ -110,6 +110,8 @@ "test:ci": [ "@test:unit:ci", "@test:db", + "@test:db:mysql", + "@test:db:postgres", "@test:api" ], "test:unit": "phpdbg -qrr vendor/bin/phpunit --order-by=random --coverage-php build/coverage-unit.cov --testdox", diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index 7d0dd739..37a3d884 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -17,6 +17,7 @@ $swooleTestingPort = 9999; $buildDbConnection = function () { $driver = env('DB_DRIVER', 'sqlite'); + $isCi = env('TRAVIS') === 'true'; switch ($driver) { case 'sqlite': @@ -27,9 +28,9 @@ $buildDbConnection = function () { case 'mysql': return [ 'driver' => 'pdo_mysql', - 'host' => 'shlink_db', + 'host' => $isCi ? 'localhost' : 'shlink_db', 'user' => 'root', - 'password' => 'root', + 'password' => $isCi ? '' : 'root', 'dbname' => 'shlink_test', 'charset' => 'utf8', 'driverOptions' => [ @@ -39,9 +40,9 @@ $buildDbConnection = function () { case 'postgres': return [ 'driver' => 'pdo_pgsql', - 'host' => 'shlink_db_postgres', + 'host' => $isCi ? 'localhost' : 'shlink_db_postgres', 'user' => 'postgres', - 'password' => 'root', + 'password' => $isCi ? '' : 'root', 'dbname' => 'shlink_test', 'charset' => 'utf8', ]; From a22beeed08740ffbeb6354ee6bab27efea3b2ecb Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Mar 2019 20:58:48 +0100 Subject: [PATCH 3/3] Replaced localhost name by 127.0.0.1 for databases when in travis --- .travis.yml | 2 +- config/test/test_config.global.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 637e9e7d..47020d15 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,9 +26,9 @@ install: before_script: - mysql -e 'CREATE DATABASE shlink_test;' - psql -c 'create database shlink_test;' -U postgres + - mkdir build script: - - mkdir build - composer ci after_success: diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index 37a3d884..31f53e28 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -17,7 +17,7 @@ $swooleTestingPort = 9999; $buildDbConnection = function () { $driver = env('DB_DRIVER', 'sqlite'); - $isCi = env('TRAVIS') === 'true'; + $isCi = env('TRAVIS', false); switch ($driver) { case 'sqlite': @@ -28,7 +28,7 @@ $buildDbConnection = function () { case 'mysql': return [ 'driver' => 'pdo_mysql', - 'host' => $isCi ? 'localhost' : 'shlink_db', + 'host' => $isCi ? '127.0.0.1' : 'shlink_db', 'user' => 'root', 'password' => $isCi ? '' : 'root', 'dbname' => 'shlink_test', @@ -40,7 +40,7 @@ $buildDbConnection = function () { case 'postgres': return [ 'driver' => 'pdo_pgsql', - 'host' => $isCi ? 'localhost' : 'shlink_db_postgres', + 'host' => $isCi ? '127.0.0.1' : 'shlink_db_postgres', 'user' => 'postgres', 'password' => $isCi ? '' : 'root', 'dbname' => 'shlink_test',