From 430e2ff0b56b2948789d7050ef104038afb27396 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 9 Apr 2022 17:46:13 +0200 Subject: [PATCH 1/2] Ensured db and api tests can be run without the need of creating the database beforehand --- CONTRIBUTING.md | 6 ------ composer.json | 4 ++-- config/test/bootstrap_db_tests.php | 2 +- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a8b93ecd..2024adca 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -123,12 +123,6 @@ Depending on the kind of contribution, maybe not all kinds of tests are needed, * Run `./indocker composer ci` to run all previous commands together. This command is run during the project's continuous integration. * Run `./indocker composer ci:parallel` to do the same as in previous case, but parallelizing non-conflicting tasks as much as possible. -> Note: Due to some limitations in the tooling used by shlink, the testing databases need to exist beforehand, both for db and api tests (except sqlite). -> -> However, they just need to be created empty, with no tables. Also, once created, they are automatically reset before every new execution. -> -> The testing database is always called `shlink_test`. You can create it using the database client of your choice. [DBeaver](https://dbeaver.io/) is a good multi-platform desktop database client which supports all the engines supported by shlink. - ## Pull request process **Important!**: Before starting to work on a pull request, make sure you always [open an issue](https://github.com/shlinkio/shlink/issues/new/choose) first. diff --git a/composer.json b/composer.json index 4d240acb..e0ffd0de 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "laminas/laminas-config-aggregator": "^1.7", "laminas/laminas-diactoros": "^2.8", "laminas/laminas-inputfilter": "^2.13", - "laminas/laminas-servicemanager": "^3.10", + "laminas/laminas-servicemanager": "^3.11.2", "laminas/laminas-stdlib": "^3.6", "lcobucci/jwt": "^4.1", "league/uri": "^6.4", @@ -74,7 +74,7 @@ "phpunit/phpunit": "^9.5", "roave/security-advisories": "dev-master", "shlinkio/php-coding-standard": "~2.2.0", - "shlinkio/shlink-test-utils": "^3.0", + "shlinkio/shlink-test-utils": "^3.0.1", "symfony/var-dumper": "^6.0", "veewee/composer-run-parallel": "^1.1" }, diff --git a/config/test/bootstrap_db_tests.php b/config/test/bootstrap_db_tests.php index 9f14c38d..0237d741 100644 --- a/config/test/bootstrap_db_tests.php +++ b/config/test/bootstrap_db_tests.php @@ -8,5 +8,5 @@ use Psr\Container\ContainerInterface; /** @var ContainerInterface $container */ $container = require __DIR__ . '/../container.php'; -$container->get(Helper\TestHelper::class)->createTestDb(); +$container->get(Helper\TestHelper::class)->createTestDb(['bin/cli', 'db:create'], ['bin/cli', 'db:migrate']); DbTest\DatabaseTestCase::setEntityManager($container->get('em')); From 3a82691503b560a202cc45bc818b10f0e48f634b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 10 Apr 2022 19:48:32 +0200 Subject: [PATCH 2/2] Small improvements on CreateDatabaseCommand --- .../CLI/src/Command/Db/CreateDatabaseCommand.php | 4 ++-- .../Command/Db/CreateDatabaseCommandTest.php | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/module/CLI/src/Command/Db/CreateDatabaseCommand.php b/module/CLI/src/Command/Db/CreateDatabaseCommand.php index afc57d1e..415290a3 100644 --- a/module/CLI/src/Command/Db/CreateDatabaseCommand.php +++ b/module/CLI/src/Command/Db/CreateDatabaseCommand.php @@ -66,7 +66,7 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand private function checkDbExists(): void { - if ($this->regularConn->getDatabasePlatform() instanceof SqlitePlatform) { + if ($this->regularConn->getDriver()->getDatabasePlatform() instanceof SqlitePlatform) { return; } @@ -74,7 +74,7 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand // Otherwise, it will fail to connect and will not be able to create the new database $schemaManager = $this->noDbNameConn->createSchemaManager(); $databases = $schemaManager->listDatabases(); - $shlinkDatabase = $this->regularConn->getDatabase(); + $shlinkDatabase = $this->regularConn->getParams()['dbname'] ?? null; if ($shlinkDatabase !== null && ! contains($databases, $shlinkDatabase)) { $schemaManager->createDatabase($shlinkDatabase); diff --git a/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php b/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php index ec7c080a..93e07d4d 100644 --- a/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php +++ b/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\CLI\Command\Db; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Driver; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; @@ -30,6 +31,7 @@ class CreateDatabaseCommandTest extends TestCase private ObjectProphecy $processHelper; private ObjectProphecy $regularConn; private ObjectProphecy $schemaManager; + private ObjectProphecy $driver; public function setUp(): void { @@ -48,7 +50,9 @@ class CreateDatabaseCommandTest extends TestCase $this->regularConn = $this->prophesize(Connection::class); $this->regularConn->createSchemaManager()->willReturn($this->schemaManager->reveal()); - $this->regularConn->getDatabasePlatform()->willReturn($this->prophesize(AbstractPlatform::class)->reveal()); + $this->driver = $this->prophesize(Driver::class); + $this->regularConn->getDriver()->willReturn($this->driver->reveal()); + $this->driver->getDatabasePlatform()->willReturn($this->prophesize(AbstractPlatform::class)->reveal()); $noDbNameConn = $this->prophesize(Connection::class); $noDbNameConn->createSchemaManager()->willReturn($this->schemaManager->reveal()); @@ -67,7 +71,7 @@ class CreateDatabaseCommandTest extends TestCase public function successMessageIsPrintedIfDatabaseAlreadyExists(): void { $shlinkDatabase = 'shlink_database'; - $getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); + $getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]); $listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']); $createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void { }); @@ -87,7 +91,7 @@ class CreateDatabaseCommandTest extends TestCase public function databaseIsCreatedIfItDoesNotExist(): void { $shlinkDatabase = 'shlink_database'; - $getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); + $getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]); $listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']); $createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void { }); @@ -108,7 +112,7 @@ class CreateDatabaseCommandTest extends TestCase public function tablesAreCreatedIfDatabaseIsEmpty(array $tables): void { $shlinkDatabase = 'shlink_database'; - $getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); + $getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]); $listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']); $createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void { }); @@ -141,10 +145,10 @@ class CreateDatabaseCommandTest extends TestCase /** @test */ public function databaseCheckIsSkippedForSqlite(): void { - $this->regularConn->getDatabasePlatform()->willReturn($this->prophesize(SqlitePlatform::class)->reveal()); + $this->driver->getDatabasePlatform()->willReturn($this->prophesize(SqlitePlatform::class)->reveal()); $shlinkDatabase = 'shlink_database'; - $getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); + $getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]); $listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']); $createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void { });