Removed duplicated code to define testing database connection params

This commit is contained in:
Alejandro Celaya
2019-01-20 22:08:32 +01:00
parent 687d8d91a9
commit 284de28f76
3 changed files with 20 additions and 25 deletions

View File

@@ -6,6 +6,7 @@ use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Interop\Container\ContainerInterface; use Interop\Container\ContainerInterface;
use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManager;
// If the "--test" flag was provided, we are on a test environment
$isTest = false; $isTest = false;
foreach ($_SERVER['argv'] as $i => $arg) { foreach ($_SERVER['argv'] as $i => $arg) {
if ($arg === '--test') { if ($arg === '--test') {
@@ -16,20 +17,7 @@ foreach ($_SERVER['argv'] as $i => $arg) {
} }
/** @var ContainerInterface|ServiceManager $container */ /** @var ContainerInterface|ServiceManager $container */
$container = include __DIR__ . '/container.php'; $container = $isTest ? include __DIR__ . '/test-container.php' : 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 */
$em = $container->get(EntityManager::class); $em = $container->get(EntityManager::class);
return ConsoleRunner::createHelperSet($em); return ConsoleRunner::createHelperSet($em);

14
config/test-container.php Normal file
View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
$container = include __DIR__ . '/container.php';
$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);
return $container;

View File

@@ -1,9 +1,9 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
use Psr\Container\ContainerInterface;
use ShlinkioTest\Shlink\Common\DbUnit\DatabaseTestCase; use ShlinkioTest\Shlink\Common\DbUnit\DatabaseTestCase;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
use Zend\ServiceManager\ServiceManager;
// Create an empty .env file // Create an empty .env file
if (! file_exists('.env')) { if (! file_exists('.env')) {
@@ -15,19 +15,12 @@ if (file_exists($shlinkDbPath)) {
unlink($shlinkDbPath); unlink($shlinkDbPath);
} }
/** @var ServiceManager $sm */ /** @var ContainerInterface $container */
$sm = require __DIR__ . '/config/container.php'; $container = require __DIR__ . '/config/test-container.php';
$sm->setAllowOverride(true);
$config = $sm->get('config');
$config['entity_manager']['connection'] = [
'driver' => 'pdo_sqlite',
'path' => $shlinkDbPath,
];
$sm->setService('config', $config);
// Create database // Create database
$process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q', '--test'], __DIR__); $process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q', '--test'], __DIR__);
$process->inheritEnvironmentVariables() $process->inheritEnvironmentVariables()
->mustRun(); ->mustRun();
DatabaseTestCase::$em = $sm->get('em'); DatabaseTestCase::$em = $container->get('em');