From 284de28f768d329a7d0f81c6e7fcd965c445cc56 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 20 Jan 2019 22:08:32 +0100 Subject: [PATCH] Removed duplicated code to define testing database connection params --- config/cli-config.php | 16 ++-------------- config/test-container.php | 14 ++++++++++++++ db_tests_bootstrap.php | 15 ++++----------- 3 files changed, 20 insertions(+), 25 deletions(-) create mode 100644 config/test-container.php diff --git a/config/cli-config.php b/config/cli-config.php index 59373bbd..774b3412 100644 --- a/config/cli-config.php +++ b/config/cli-config.php @@ -6,6 +6,7 @@ use Doctrine\ORM\Tools\Console\ConsoleRunner; use Interop\Container\ContainerInterface; use Zend\ServiceManager\ServiceManager; +// If the "--test" flag was provided, we are on a test environment $isTest = false; foreach ($_SERVER['argv'] as $i => $arg) { if ($arg === '--test') { @@ -16,20 +17,7 @@ foreach ($_SERVER['argv'] as $i => $arg) { } /** @var ContainerInterface|ServiceManager $container */ -$container = include __DIR__ . '/container.php'; - -// If in testing env, override DB connection to use an in-memory sqlite database -if ($isTest) { - $container->setAllowOverride(true); - $config = $container->get('config'); - $config['entity_manager']['connection'] = [ - 'driver' => 'pdo_sqlite', - 'path' => realpath(sys_get_temp_dir()) . '/shlink-tests.db', - ]; - $container->setService('config', $config); -} - -/** @var EntityManager $em */ +$container = $isTest ? include __DIR__ . '/test-container.php' : include __DIR__ . '/container.php'; $em = $container->get(EntityManager::class); return ConsoleRunner::createHelperSet($em); diff --git a/config/test-container.php b/config/test-container.php new file mode 100644 index 00000000..8a3f4606 --- /dev/null +++ b/config/test-container.php @@ -0,0 +1,14 @@ +setAllowOverride(true); +$config = $container->get('config'); +$config['entity_manager']['connection'] = [ + 'driver' => 'pdo_sqlite', + 'path' => realpath(sys_get_temp_dir()) . '/shlink-tests.db', +]; +$container->setService('config', $config); + +return $container; diff --git a/db_tests_bootstrap.php b/db_tests_bootstrap.php index 5f44c7c5..4bf09ba9 100644 --- a/db_tests_bootstrap.php +++ b/db_tests_bootstrap.php @@ -1,9 +1,9 @@ setAllowOverride(true); -$config = $sm->get('config'); -$config['entity_manager']['connection'] = [ - 'driver' => 'pdo_sqlite', - 'path' => $shlinkDbPath, -]; -$sm->setService('config', $config); +/** @var ContainerInterface $container */ +$container = require __DIR__ . '/config/test-container.php'; // Create database $process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q', '--test'], __DIR__); $process->inheritEnvironmentVariables() ->mustRun(); -DatabaseTestCase::$em = $sm->get('em'); +DatabaseTestCase::$em = $container->get('em');