From 394074ff161caf95bf47a823bcf2f411d70b9ee8 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Thu, 16 Mar 2023 12:36:26 +0600 Subject: [PATCH] Add workflows for other DBMS (#47) * Add mysql workflow * Do not use template * Install composer dependencies * Separate sqlite / mysql * Fix creation of db manager * Apply fixes from StyleCI * Fix testsuite arg * Fix undefined driver * Add pgsql * Fix indentation * Fix dsn for pg * Add Mssql * Fix testsuite arg for mssql * Remove cache key --------- Co-authored-by: StyleCI Bot --- .github/workflows/mssql.yml | 98 +++++++++++++++++++ .github/workflows/mysql.yml | 88 +++++++++++++++++ .github/workflows/pgsql.yml | 93 ++++++++++++++++++ .github/workflows/{build.yml => sqlite.yml} | 2 +- phpunit.xml.dist | 14 ++- tests/{ => Base}/AssignmentsStorageTest.php | 4 +- .../{ => Base}/Command/RbacCycleInitTest.php | 6 +- tests/{ => Base}/ItemsStorageTest.php | 4 +- tests/{ => Base}/TestCase.php | 38 +------ tests/Mssql/AssignmentsStorageTest.php | 10 ++ tests/Mssql/Command/RbacCycleInitTest.php | 12 +++ tests/Mssql/ItemsStorageTest.php | 10 ++ tests/Mssql/MssqlTrait.php | 34 +++++++ tests/Mysql/AssignmentsStorageTest.php | 10 ++ tests/Mysql/Command/RbacCycleInitTest.php | 12 +++ tests/Mysql/ItemsStorageTest.php | 10 ++ tests/Mysql/MysqlTrait.php | 34 +++++++ tests/Pgsql/AssignmentsStorageTest.php | 10 ++ tests/Pgsql/Command/RbacCycleInitTest.php | 12 +++ tests/Pgsql/ItemsStorageTest.php | 10 ++ tests/Pgsql/PgsqlTrait.php | 34 +++++++ tests/Sqlite/AssignmentsStorageTest.php | 10 ++ tests/Sqlite/Command/RbacCycleInitTest.php | 12 +++ tests/Sqlite/ItemsStorageTest.php | 10 ++ tests/Sqlite/SqliteTrait.php | 31 ++++++ tests/{ => Sqlite}/runtime/.gitignore | 0 26 files changed, 564 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/mssql.yml create mode 100644 .github/workflows/mysql.yml create mode 100644 .github/workflows/pgsql.yml rename .github/workflows/{build.yml => sqlite.yml} (97%) rename tests/{ => Base}/AssignmentsStorageTest.php (98%) rename tests/{ => Base}/Command/RbacCycleInitTest.php (98%) rename tests/{ => Base}/ItemsStorageTest.php (98%) rename tests/{ => Base}/TestCase.php (56%) create mode 100644 tests/Mssql/AssignmentsStorageTest.php create mode 100644 tests/Mssql/Command/RbacCycleInitTest.php create mode 100644 tests/Mssql/ItemsStorageTest.php create mode 100644 tests/Mssql/MssqlTrait.php create mode 100644 tests/Mysql/AssignmentsStorageTest.php create mode 100644 tests/Mysql/Command/RbacCycleInitTest.php create mode 100644 tests/Mysql/ItemsStorageTest.php create mode 100644 tests/Mysql/MysqlTrait.php create mode 100644 tests/Pgsql/AssignmentsStorageTest.php create mode 100644 tests/Pgsql/Command/RbacCycleInitTest.php create mode 100644 tests/Pgsql/ItemsStorageTest.php create mode 100644 tests/Pgsql/PgsqlTrait.php create mode 100644 tests/Sqlite/AssignmentsStorageTest.php create mode 100644 tests/Sqlite/Command/RbacCycleInitTest.php create mode 100644 tests/Sqlite/ItemsStorageTest.php create mode 100644 tests/Sqlite/SqliteTrait.php rename tests/{ => Sqlite}/runtime/.gitignore (100%) diff --git a/.github/workflows/mssql.yml b/.github/workflows/mssql.yml new file mode 100644 index 0000000..cdd8e36 --- /dev/null +++ b/.github/workflows/mssql.yml @@ -0,0 +1,98 @@ +on: + pull_request: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + + push: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + +name: mssql + +jobs: + tests: + name: PHP ${{ matrix.php }}-mssql-${{ matrix.mssql }} + + env: + extensions: pdo, pdo_sqlsrv-5.10.1 + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - 8.0 + - 8.1 + + mssql: + - server:2017-latest + - server:2019-latest + + exclude: + - php: 8.0 + mssql: server:2017-latest + - php: 8.1 + mssql: server:2017-latest + + services: + mssql: + image: mcr.microsoft.com/mssql/${{ matrix.mssql }} + env: + SA_PASSWORD: YourStrong!Passw0rd + ACCEPT_EULA: Y + MSSQL_PID: Developer + ports: + - 1433:1433 + options: --name=mssql --health-cmd="/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'SELECT 1'" --health-interval=10s --health-timeout=5s --health-retries=3 + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Create MS SQL Database + run: docker exec -i mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE yiitest' + + - name: Install PHP with extensions + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: ${{ env.extensions }} + ini-values: date.timezone='UTC' + coverage: pcov + tools: composer:v2, pecl + + - name: Determine composer cache directory + if: matrix.os == 'ubuntu-latest' + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + + - name: Update composer + run: composer self-update + + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run tests with phpunit + run: vendor/bin/phpunit --testsuite Mssql --colors=always diff --git a/.github/workflows/mysql.yml b/.github/workflows/mysql.yml new file mode 100644 index 0000000..2f24f43 --- /dev/null +++ b/.github/workflows/mysql.yml @@ -0,0 +1,88 @@ +on: + pull_request: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + + push: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + +name: mysql + +jobs: + tests: + name: PHP ${{ matrix.php }}-mysql-${{ matrix.mysql }} + + env: + extensions: pdo, pdo_mysql + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - 8.0 + - 8.1 + + mysql: + - 5.7 + - latest + + services: + mysql: + image: mysql:${{ matrix.mysql }} + env: + MYSQL_ALLOW_EMPTY_PASSWORD: true + MYSQL_PASSWORD: '' + MYSQL_DATABASE: yiitest + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP with extensions + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: ${{ env.extensions }} + ini-values: date.timezone='UTC' + coverage: pcov + + - name: Determine composer cache directory + if: matrix.os == 'ubuntu-latest' + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + + - name: Update composer + run: composer self-update + + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run tests with phpunit + run: vendor/bin/phpunit --testsuite Mysql --colors=always diff --git a/.github/workflows/pgsql.yml b/.github/workflows/pgsql.yml new file mode 100644 index 0000000..8f360c9 --- /dev/null +++ b/.github/workflows/pgsql.yml @@ -0,0 +1,93 @@ +on: + pull_request: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + + push: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + +name: pgsql + +jobs: + tests: + name: PHP ${{ matrix.php }}-pgsql-${{ matrix.pgsql }} + + env: + extensions: pdo, pdo_pgsql + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - 8.0 + - 8.1 + + pgsql: + - 9 + - 10 + - 11 + - 12 + - 13 + - 14 + + services: + postgres: + image: postgres:${{ matrix.pgsql }} + env: + POSTGRES_USER: root + POSTGRES_PASSWORD: root + POSTGRES_DB: yiitest + ports: + - 5432:5432 + options: --name=postgres --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3 + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP with extensions + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: ${{ env.extensions }} + ini-values: date.timezone='UTC' + coverage: pcov + tools: composer:v2 + + - name: Determine composer cache directory + if: matrix.os == 'ubuntu-latest' + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + + - name: Update composer + run: composer self-update + + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run tests with phpunit + run: vendor/bin/phpunit --testsuite Pgsql --colors=always diff --git a/.github/workflows/build.yml b/.github/workflows/sqlite.yml similarity index 97% rename from .github/workflows/build.yml rename to .github/workflows/sqlite.yml index f6c41aa..23d1d93 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/sqlite.yml @@ -19,7 +19,7 @@ on: - 'infection.json.dist' - 'psalm.xml' -name: build +name: sqlite jobs: phpunit: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fbc56bf..3188542 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,6 +6,7 @@ bootstrap="vendor/autoload.php" failOnRisky="true" failOnWarning="true" + defaultTestSuite="Sqlite" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> @@ -20,8 +21,17 @@ - - ./tests + + ./tests/Sqlite + + + ./tests/Mysql + + + ./tests/Pgsql + + + ./tests/Mssql diff --git a/tests/AssignmentsStorageTest.php b/tests/Base/AssignmentsStorageTest.php similarity index 98% rename from tests/AssignmentsStorageTest.php rename to tests/Base/AssignmentsStorageTest.php index dc88cc9..d1965f3 100644 --- a/tests/AssignmentsStorageTest.php +++ b/tests/Base/AssignmentsStorageTest.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Yiisoft\Rbac\Cycle\Tests; +namespace Yiisoft\Rbac\Cycle\Tests\Base; use Yiisoft\Rbac\Assignment; use Yiisoft\Rbac\Cycle\AssignmentsStorage; use Yiisoft\Rbac\Item; -class AssignmentsStorageTest extends TestCase +abstract class AssignmentsStorageTest extends TestCase { public function testHasItem(): void { diff --git a/tests/Command/RbacCycleInitTest.php b/tests/Base/Command/RbacCycleInitTest.php similarity index 98% rename from tests/Command/RbacCycleInitTest.php rename to tests/Base/Command/RbacCycleInitTest.php index bdb90dc..4b39322 100644 --- a/tests/Command/RbacCycleInitTest.php +++ b/tests/Base/Command/RbacCycleInitTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Rbac\Cycle\Tests\Command; +namespace Yiisoft\Rbac\Cycle\Tests\Base\Command; use Cycle\Database\ForeignKeyInterface; use Cycle\Database\Schema\AbstractForeignKey; @@ -12,10 +12,10 @@ use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Console\Output\NullOutput; use Yiisoft\Rbac\Cycle\Command\RbacCycleInit; -use Yiisoft\Rbac\Cycle\Tests\TestCase; +use Yiisoft\Rbac\Cycle\Tests\Base\TestCase; use Yiisoft\Rbac\Item; -class RbacCycleInitTest extends TestCase +abstract class RbacCycleInitTest extends TestCase { protected function setUp(): void { diff --git a/tests/ItemsStorageTest.php b/tests/Base/ItemsStorageTest.php similarity index 98% rename from tests/ItemsStorageTest.php rename to tests/Base/ItemsStorageTest.php index 3fd7168..0a2c9c9 100644 --- a/tests/ItemsStorageTest.php +++ b/tests/Base/ItemsStorageTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Yiisoft\Rbac\Cycle\Tests; +namespace Yiisoft\Rbac\Cycle\Tests\Base; use Yiisoft\Rbac\Cycle\ItemsStorage; use Yiisoft\Rbac\Item; use Yiisoft\Rbac\Permission; use Yiisoft\Rbac\Role; -class ItemsStorageTest extends TestCase +abstract class ItemsStorageTest extends TestCase { public function testUpdate(): void { diff --git a/tests/TestCase.php b/tests/Base/TestCase.php similarity index 56% rename from tests/TestCase.php rename to tests/Base/TestCase.php index b440a25..d90c265 100644 --- a/tests/TestCase.php +++ b/tests/Base/TestCase.php @@ -2,15 +2,8 @@ declare(strict_types=1); -namespace Yiisoft\Rbac\Cycle\Tests; +namespace Yiisoft\Rbac\Cycle\Tests\Base; -use Cycle\Database\Config\DatabaseConfig; -use Cycle\Database\Config\MySQL\DsnConnectionConfig as MyQSLDsnConnectionConfig; -use Cycle\Database\Config\MySQLDriverConfig; -use Cycle\Database\Config\Postgres\DsnConnectionConfig as PostgresDsnConnectionConfig; -use Cycle\Database\Config\PostgresDriverConfig; -use Cycle\Database\Config\SQLite\FileConnectionConfig; -use Cycle\Database\Config\SQLiteDriverConfig; use Cycle\Database\DatabaseManager; use Cycle\Database\Schema\AbstractTable; use Symfony\Component\Console\Application; @@ -30,8 +23,9 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase protected function getDbal(): DatabaseManager { if ($this->databaseManager === null) { - $this->createConnection(); + $this->databaseManager = $this->createDbManager(); } + return $this->databaseManager; } @@ -72,31 +66,7 @@ protected function createApplication(string|null $itemsChildrenTable = self::ITE return $app; } - private function createConnection(): void - { - $dbConfig = new DatabaseConfig( - [ - 'default' => 'default', - 'databases' => [ - 'default' => ['connection' => 'sqlite'], - ], - 'connections' => [ - 'sqlite' => new SQLiteDriverConfig(new FileConnectionConfig(__DIR__ . '/runtime/test.db')), - // 'mysql' => new MySQLDriverConfig(new MyQSLDsnConnectionConfig( - // 'mysql:host=127.0.0.1;port=3306;dbname=test', - // 'root', - // '123456', - // )), - // 'postgres' => new PostgresDriverConfig(new PostgresDsnConnectionConfig( - // 'pgsql:host=127.0.0.1;port=5432;dbname=test', - // 'postgres', - // '123456', - // )), - ], - ] - ); - $this->databaseManager = new DatabaseManager($dbConfig); - } + abstract protected function createDbManager(): DatabaseManager; abstract protected function populateDb(): void; } diff --git a/tests/Mssql/AssignmentsStorageTest.php b/tests/Mssql/AssignmentsStorageTest.php new file mode 100644 index 0000000..15f8b97 --- /dev/null +++ b/tests/Mssql/AssignmentsStorageTest.php @@ -0,0 +1,10 @@ + 'default', + 'databases' => [ + 'default' => ['connection' => 'mssql'], + ], + 'connections' => [ + 'mssql' => new SQLServerDriverConfig(new DsnConnectionConfig( + 'sqlsrv:Server=127.0.0.1,1433;Database=yiitest', + 'SA', + 'YourStrong!Passw0rd', + )), + ], + ] + ); + + return new DatabaseManager($dbConfig); + } +} diff --git a/tests/Mysql/AssignmentsStorageTest.php b/tests/Mysql/AssignmentsStorageTest.php new file mode 100644 index 0000000..4847ed6 --- /dev/null +++ b/tests/Mysql/AssignmentsStorageTest.php @@ -0,0 +1,10 @@ + 'default', + 'databases' => [ + 'default' => ['connection' => 'mysql'], + ], + 'connections' => [ + 'mysql' => new MySQLDriverConfig(new DsnConnectionConfig( + 'mysql:host=127.0.0.1;port=3306;dbname=yiitest', + 'root', + '', + )), + ], + ] + ); + + return new DatabaseManager($dbConfig); + } +} diff --git a/tests/Pgsql/AssignmentsStorageTest.php b/tests/Pgsql/AssignmentsStorageTest.php new file mode 100644 index 0000000..a293588 --- /dev/null +++ b/tests/Pgsql/AssignmentsStorageTest.php @@ -0,0 +1,10 @@ + 'default', + 'databases' => [ + 'default' => ['connection' => 'pgsql'], + ], + 'connections' => [ + 'pgsql' => new PostgresDriverConfig(new DsnConnectionConfig( + 'pgsql:host=127.0.0.1;dbname=yiitest;port=5432', + 'root', + 'root', + )), + ], + ] + ); + + return new DatabaseManager($dbConfig); + } +} diff --git a/tests/Sqlite/AssignmentsStorageTest.php b/tests/Sqlite/AssignmentsStorageTest.php new file mode 100644 index 0000000..73ca35f --- /dev/null +++ b/tests/Sqlite/AssignmentsStorageTest.php @@ -0,0 +1,10 @@ + 'default', + 'databases' => [ + 'default' => ['connection' => 'sqlite'], + ], + 'connections' => [ + 'sqlite' => new SQLiteDriverConfig(new FileConnectionConfig($dbPath)), + ], + ] + ); + + return new DatabaseManager($dbConfig); + } +} diff --git a/tests/runtime/.gitignore b/tests/Sqlite/runtime/.gitignore similarity index 100% rename from tests/runtime/.gitignore rename to tests/Sqlite/runtime/.gitignore