mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Defined config to run database tests against mysql and postgres
This commit is contained in:
parent
f563e777cc
commit
6fa255386b
@ -115,6 +115,8 @@
|
|||||||
"test:unit": "phpdbg -qrr vendor/bin/phpunit --order-by=random --coverage-php build/coverage-unit.cov --testdox",
|
"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: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": "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:api": "bin/test/run-api-tests.sh",
|
||||||
|
|
||||||
"test:pretty": [
|
"test:pretty": [
|
||||||
@ -141,7 +143,9 @@
|
|||||||
"test:ci": "<fg=blue;options=bold>Runs all test suites, generating all needed reports and logs for CI envs</>",
|
"test:ci": "<fg=blue;options=bold>Runs all test suites, generating all needed reports and logs for CI envs</>",
|
||||||
"test:unit": "<fg=blue;options=bold>Runs unit test suites</>",
|
"test:unit": "<fg=blue;options=bold>Runs unit test suites</>",
|
||||||
"test:unit:ci": "<fg=blue;options=bold>Runs unit test suites, generating all needed reports and logs for CI envs</>",
|
"test:unit:ci": "<fg=blue;options=bold>Runs unit test suites, generating all needed reports and logs for CI envs</>",
|
||||||
"test:db": "<fg=blue;options=bold>Runs database test suites (covering entity repositories)</>",
|
"test:db": "<fg=blue;options=bold>Runs database test suites on a SQLite database</>",
|
||||||
|
"test:db:mysql": "<fg=blue;options=bold>Runs database test suites on a MySQL database</>",
|
||||||
|
"test:db:postgres": "<fg=blue;options=bold>Runs database test suites on a PostgreSQL database</>",
|
||||||
"test:api": "<fg=blue;options=bold>Runs API test suites</>",
|
"test:api": "<fg=blue;options=bold>Runs API test suites</>",
|
||||||
"test:pretty": "<fg=blue;options=bold>Runs all test suites and generates an HTML code coverage report</>",
|
"test:pretty": "<fg=blue;options=bold>Runs all test suites and generates an HTML code coverage report</>",
|
||||||
"test:unit:pretty": "<fg=blue;options=bold>Runs unit test suites and generates an HTML code coverage report</>",
|
"test:unit:pretty": "<fg=blue;options=bold>Runs unit test suites and generates an HTML code coverage report</>",
|
||||||
|
@ -20,7 +20,7 @@ $testHelper = $container->get(TestHelper::class);
|
|||||||
$config = $container->get('config');
|
$config = $container->get('config');
|
||||||
$em = $container->get(EntityManager::class);
|
$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::setApiClient($container->get('shlink_test_api_client'));
|
||||||
ApiTest\ApiTestCase::setSeedFixturesCallback(function () use ($testHelper, $em, $config) {
|
ApiTest\ApiTestCase::setSeedFixturesCallback(function () use ($testHelper, $em, $config) {
|
||||||
$testHelper->seedFixtures($em, $config['data_fixtures'] ?? []);
|
$testHelper->seedFixtures($em, $config['data_fixtures'] ?? []);
|
||||||
|
@ -15,7 +15,5 @@ if (! file_exists('.env')) {
|
|||||||
|
|
||||||
/** @var ContainerInterface $container */
|
/** @var ContainerInterface $container */
|
||||||
$container = require __DIR__ . '/../container.php';
|
$container = require __DIR__ . '/../container.php';
|
||||||
$config = $container->get('config');
|
$container->get(TestHelper::class)->createTestDb();
|
||||||
|
|
||||||
$container->get(TestHelper::class)->createTestDb($config['entity_manager']['connection']['path']);
|
|
||||||
DbTest\DatabaseTestCase::setEntityManager($container->get('em'));
|
DbTest\DatabaseTestCase::setEntityManager($container->get('em'));
|
||||||
|
@ -4,15 +4,52 @@ declare(strict_types=1);
|
|||||||
namespace ShlinkioTest\Shlink;
|
namespace ShlinkioTest\Shlink;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use PDO;
|
||||||
use Zend\ConfigAggregator\ConfigAggregator;
|
use Zend\ConfigAggregator\ConfigAggregator;
|
||||||
use Zend\ServiceManager\Factory\InvokableFactory;
|
use Zend\ServiceManager\Factory\InvokableFactory;
|
||||||
|
|
||||||
|
use function Shlinkio\Shlink\Common\env;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
use function sys_get_temp_dir;
|
use function sys_get_temp_dir;
|
||||||
|
|
||||||
$swooleTestingHost = '127.0.0.1';
|
$swooleTestingHost = '127.0.0.1';
|
||||||
$swooleTestingPort = 9999;
|
$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 [
|
return [
|
||||||
|
|
||||||
'debug' => true,
|
'debug' => true,
|
||||||
@ -49,11 +86,7 @@ return [
|
|||||||
],
|
],
|
||||||
|
|
||||||
'entity_manager' => [
|
'entity_manager' => [
|
||||||
'connection' => [
|
'connection' => $buildDbConnection(),
|
||||||
'driver' => 'pdo_sqlite',
|
|
||||||
'path' => sys_get_temp_dir() . '/shlink-tests.db',
|
|
||||||
// 'path' => __DIR__ . '/../../data/shlink-tests.db',
|
|
||||||
],
|
|
||||||
],
|
],
|
||||||
|
|
||||||
'data_fixtures' => [
|
'data_fixtures' => [
|
||||||
|
@ -9,16 +9,13 @@ use Doctrine\Common\DataFixtures\Purger\ORMPurger;
|
|||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\Process\Process;
|
use Symfony\Component\Process\Process;
|
||||||
|
|
||||||
use function file_exists;
|
|
||||||
use function unlink;
|
|
||||||
|
|
||||||
class TestHelper
|
class TestHelper
|
||||||
{
|
{
|
||||||
public function createTestDb(string $shlinkDbPath): void
|
public function createTestDb(): void
|
||||||
{
|
{
|
||||||
if (file_exists($shlinkDbPath)) {
|
$process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:drop', '--force', '--no-interaction', '-q']);
|
||||||
unlink($shlinkDbPath);
|
$process->inheritEnvironmentVariables()
|
||||||
}
|
->mustRun();
|
||||||
|
|
||||||
$process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q']);
|
$process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q']);
|
||||||
$process->inheritEnvironmentVariables()
|
$process->inheritEnvironmentVariables()
|
||||||
|
@ -58,6 +58,6 @@ $builder->createOneToMany('visits', Entity\Visit::class)
|
|||||||
|
|
||||||
$builder->createManyToMany('tags', Entity\Tag::class)
|
$builder->createManyToMany('tags', Entity\Tag::class)
|
||||||
->setJoinTable('short_urls_in_tags')
|
->setJoinTable('short_urls_in_tags')
|
||||||
->addInverseJoinColumn('tag_id', 'id')
|
->addInverseJoinColumn('tag_id', 'id', true, false, 'CASCADE')
|
||||||
->addJoinColumn('short_url_id', 'id')
|
->addJoinColumn('short_url_id', 'id', true, false, 'CASCADE')
|
||||||
->build();
|
->build();
|
||||||
|
@ -43,10 +43,10 @@ $builder->createField('userAgent', Type::STRING)
|
|||||||
->build();
|
->build();
|
||||||
|
|
||||||
$builder->createManyToOne('shortUrl', Entity\ShortUrl::class)
|
$builder->createManyToOne('shortUrl', Entity\ShortUrl::class)
|
||||||
->addJoinColumn('short_url_id', 'id', false)
|
->addJoinColumn('short_url_id', 'id', false, false, 'CASCADE')
|
||||||
->build();
|
->build();
|
||||||
|
|
||||||
$builder->createManyToOne('visitLocation', Entity\VisitLocation::class)
|
$builder->createManyToOne('visitLocation', Entity\VisitLocation::class)
|
||||||
->addJoinColumn('visit_location_id', 'id')
|
->addJoinColumn('visit_location_id', 'id', true, false, 'Set NULL')
|
||||||
->cascadePersist()
|
->cascadePersist()
|
||||||
->build();
|
->build();
|
||||||
|
@ -18,9 +18,9 @@ use function count;
|
|||||||
class ShortUrlRepositoryTest extends DatabaseTestCase
|
class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||||
{
|
{
|
||||||
protected const ENTITIES_TO_EMPTY = [
|
protected const ENTITIES_TO_EMPTY = [
|
||||||
ShortUrl::class,
|
|
||||||
Visit::class,
|
|
||||||
Tag::class,
|
Tag::class,
|
||||||
|
Visit::class,
|
||||||
|
ShortUrl::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var ShortUrlRepository */
|
/** @var ShortUrlRepository */
|
||||||
|
Loading…
Reference in New Issue
Block a user