Skip to content

tests: add test permutation with multiple migration configuration files #892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 112 additions & 106 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,104 +7,104 @@ on:
- cron: '0 0 1,16 * *'

jobs:
tests:
name: P:${{ matrix.php }}, S:${{ matrix.symfony }}, D:${{ matrix.database }}, PU:${{ matrix.phpunit }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.use-phpunit-extension == 1 && ' (phpunit extension)' || '' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [ 8.2, 8.3, 8.4 ]
symfony: [ 6.4.*, 7.1.*, 7.2.* ]
database: [ mysql|mongo ]
phpunit: [ 11, 12 ]

# default values:
# deps: [ highest ]
# use-phpunit-extension: [ 0 ]

exclude:
- {php: 8.2, phpunit: 12}
- {php: 8.3, phpunit: 11}
- {php: 8.4, phpunit: 11}

include:
# php 8.1
- {php: 8.1, symfony: 6.4.*, phpunit: 9, database: mysql}

# old PHPUnit versions
- {php: 8.3, symfony: '*', phpunit: 9, database: mysql}
- {php: 8.4, symfony: '*', phpunit: 10, database: mysql}
- {php: 8.4, symfony: '*', phpunit: 11, database: mysql}

# test with no database (PHPUnit 9 is used to prevent some problems with empty data providers)
- {php: 8.3, symfony: '*', phpunit: 9, database: none}
- {php: 8.3, symfony: '*', phpunit: 9, database: none, deps: lowest}

# One permutation per DBMS
- {php: 8.4, symfony: '*', phpunit: 12, database: mongo}
- {php: 8.4, symfony: '*', phpunit: 12, database: pgsql}
- {php: 8.4, symfony: '*', phpunit: 12, database: sqlite}
- {php: 8.4, symfony: '*', phpunit: 12, database: mysql}

# lowest deps (one per DBMS)
- {php: 8.3, symfony: '*', phpunit: 9, database: mysql|mongo, deps: lowest}
- {php: 8.3, symfony: '*', phpunit: 9, database: mongo, deps: lowest}
- {php: 8.3, symfony: '*', phpunit: 9, database: pgsql, deps: lowest}
- {php: 8.3, symfony: '*', phpunit: 9, database: sqlite, deps: lowest}
- {php: 8.3, symfony: '*', phpunit: 9, database: mysql, deps: lowest}

# using Foundry's PHPUnit extension
- {php: 8.4, symfony: '*', phpunit: 12, database: mysql|mongo, use-phpunit-extension: 1}
env:
DATABASE_URL: ${{ contains(matrix.database, 'mysql') && 'mysql://root:root@localhost:3306/foundry?serverVersion=5.7.42' || contains(matrix.database, 'pgsql') && 'postgresql://root:root@localhost:5432/foundry?serverVersion=15' || contains(matrix.database, 'sqlite') && 'sqlite:///%kernel.project_dir%/var/data.db' || '' }}
MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ contains(matrix.database, 'sql') && 1 || 0 }}
USE_FOUNDRY_PHPUNIT_EXTENSION: ${{ matrix.use-phpunit-extension || 0 }}
PHPUNIT_VERSION: ${{ matrix.phpunit }}
services:
postgres:
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: foundry
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
mongo:
image: ${{ contains(matrix.database, 'mongo') && 'mongo:4' || '' }}
ports:
- 27017:27017
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
tools: flex

- name: Install dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.deps }}
composer-options: --prefer-dist
env:
SYMFONY_REQUIRE: ${{ matrix.symfony }}

- name: Set up MySQL
if: contains(matrix.database, 'mysql')
run: sudo /etc/init.d/mysql start

- name: Test
run: ./phpunit
shell: bash
# tests:
# name: P:${{ matrix.php }}, S:${{ matrix.symfony }}, D:${{ matrix.database }}, PU:${{ matrix.phpunit }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.use-phpunit-extension == 1 && ' (phpunit extension)' || '' }}
# runs-on: ubuntu-latest
# strategy:
# fail-fast: false
# matrix:
# php: [ 8.2, 8.3, 8.4 ]
# symfony: [ 6.4.*, 7.1.*, 7.2.* ]
# database: [ mysql|mongo ]
# phpunit: [ 11, 12 ]
#
# # default values:
# # deps: [ highest ]
# # use-phpunit-extension: [ 0 ]
#
# exclude:
# - {php: 8.2, phpunit: 12}
# - {php: 8.3, phpunit: 11}
# - {php: 8.4, phpunit: 11}
#
# include:
# # php 8.1
# - {php: 8.1, symfony: 6.4.*, phpunit: 9, database: mysql}
#
# # old PHPUnit versions
# - {php: 8.3, symfony: '*', phpunit: 9, database: mysql}
# - {php: 8.4, symfony: '*', phpunit: 10, database: mysql}
# - {php: 8.4, symfony: '*', phpunit: 11, database: mysql}
#
# # test with no database (PHPUnit 9 is used to prevent some problems with empty data providers)
# - {php: 8.3, symfony: '*', phpunit: 9, database: none}
# - {php: 8.3, symfony: '*', phpunit: 9, database: none, deps: lowest}
#
# # One permutation per DBMS
# - {php: 8.4, symfony: '*', phpunit: 12, database: mongo}
# - {php: 8.4, symfony: '*', phpunit: 12, database: pgsql}
# - {php: 8.4, symfony: '*', phpunit: 12, database: sqlite}
# - {php: 8.4, symfony: '*', phpunit: 12, database: mysql}
#
# # lowest deps (one per DBMS)
# - {php: 8.3, symfony: '*', phpunit: 9, database: mysql|mongo, deps: lowest}
# - {php: 8.3, symfony: '*', phpunit: 9, database: mongo, deps: lowest}
# - {php: 8.3, symfony: '*', phpunit: 9, database: pgsql, deps: lowest}
# - {php: 8.3, symfony: '*', phpunit: 9, database: sqlite, deps: lowest}
# - {php: 8.3, symfony: '*', phpunit: 9, database: mysql, deps: lowest}
#
# # using Foundry's PHPUnit extension
# - {php: 8.4, symfony: '*', phpunit: 12, database: mysql|mongo, use-phpunit-extension: 1}
# env:
# DATABASE_URL: ${{ contains(matrix.database, 'mysql') && 'mysql://root:root@localhost:3306/foundry?serverVersion=5.7.42' || contains(matrix.database, 'pgsql') && 'postgresql://root:root@localhost:5432/foundry?serverVersion=15' || contains(matrix.database, 'sqlite') && 'sqlite:///%kernel.project_dir%/var/data.db' || '' }}
# MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
# USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ contains(matrix.database, 'sql') && 1 || 0 }}
# USE_FOUNDRY_PHPUNIT_EXTENSION: ${{ matrix.use-phpunit-extension || 0 }}
# PHPUNIT_VERSION: ${{ matrix.phpunit }}
# services:
# postgres:
# image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
# env:
# POSTGRES_USER: root
# POSTGRES_PASSWORD: root
# POSTGRES_DB: foundry
# ports:
# - 5432:5432
# options: >-
# --health-cmd pg_isready
# --health-interval 10s
# --health-timeout 5s
# --health-retries 5
# mongo:
# image: ${{ contains(matrix.database, 'mongo') && 'mongo:4' || '' }}
# ports:
# - 27017:27017
# steps:
# - name: Checkout code
# uses: actions/checkout@v3
#
# - name: Setup PHP
# uses: shivammathur/setup-php@v2
# with:
# php-version: ${{ matrix.php }}
# coverage: none
# tools: flex
#
# - name: Install dependencies
# uses: ramsey/composer-install@v2
# with:
# dependency-versions: ${{ matrix.deps }}
# composer-options: --prefer-dist
# env:
# SYMFONY_REQUIRE: ${{ matrix.symfony }}
#
# - name: Set up MySQL
# if: contains(matrix.database, 'mysql')
# run: sudo /etc/init.d/mysql start
#
# - name: Test
# run: ./phpunit
# shell: bash

test-reset-database:
name: Reset DB - D:${{ matrix.database }} ${{ matrix.use-dama == 1 && ' (dama)' || '' }} ${{ matrix.reset-database-mode == 'migrate' && ' (migrate)' || '' }} ${{ contains(matrix.with-migration-configuration-file, 'transactional') && '(configuration file transactional)' || contains(matrix.with-migration-configuration-file, 'configuration') && '(configuration file)' || '' }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}
Expand All @@ -115,18 +115,24 @@ jobs:
database: [ mysql, pgsql, sqlite, mysql|mongo ]
use-dama: [ 0, 1 ]
reset-database-mode: [ schema, migrate ]
migration-configuration-file: ['no']
migration-configuration-file: ['[]']
deps: [ highest, lowest ]
include:
- { database: mongo, migration-configuration-file: 'no', use-dama: 0, reset-database-mode: schema }
- { database: pgsql, migration-configuration-file: 'migration-configuration', use-dama: 0, reset-database-mode: migration }
- { database: pgsql, migration-configuration-file: 'migration-configuration-transactional', use-dama: 0, reset-database-mode: migration }
- { database: mongo, migration-configuration-file: '[]', use-dama: 0, reset-database-mode: schema }
- { database: pgsql, migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration.php"]', use-dama: 0, reset-database-mode: migrate }
- { database: pgsql, migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration-transactional.php"]', use-dama: 0, reset-database-mode: migrate }
-
database: pgsql
use-dama: 0
reset-database-mode: migrate
migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration-transactional.php", "tests/Fixture/ResetDatabase/migration-configs/migration-dummy.php"]'
deps: highest
env:
DATABASE_URL: ${{ contains(matrix.database, 'mysql') && 'mysql://root:root@localhost:3306/foundry?serverVersion=5.7.42' || contains(matrix.database, 'pgsql') && 'postgresql://root:root@localhost:5432/foundry?serverVersion=15' || 'sqlite:///%kernel.project_dir%/var/data.db' }}
MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ matrix.use-dama == 1 && 1 || 0 }}
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode == 1 && 1 || 0 }}
MIGRATION_CONFIGURATION_FILE: ${{ matrix.migration-configuration-file == 'no' && '' || format('tests/Fixture/MigrationTests/configs/{0}.php', matrix.migration-configuration-file) }}
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode }}
MIGRATION_CONFIGURATION_FILES: ${{ matrix.migration-configuration-file }}
PHPUNIT_VERSION: 11
services:
postgres:
Expand Down Expand Up @@ -163,7 +169,7 @@ jobs:
dependency-versions: ${{ matrix.deps }}
composer-options: --prefer-dist
env:
SYMFONY_REQUIRE: 7.1.*
SYMFONY_REQUIRE: 7.2.*

- name: Set up MySQL
if: contains(matrix.database, 'mysql')
Expand All @@ -175,7 +181,7 @@ jobs:

# We should be able to run the tests twice in order to check if the second run also starts from a fresh db
# some bugs could be detected this way
./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
DATABASE_GENERATE_MIGRATIONS=0 ./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
shell: bash

test-with-paratest:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ PHPUNIT_VERSION="11" # possible values: 9, 10, 11, 11.4

# test reset database with migrations,
# only relevant for "reset-database" testsuite
MIGRATION_CONFIGURATION_FILE="tests/Fixture/MigrationTests/configs/migration-configuration.php"
MIGRATION_CONFIGURATION_FILES="[\"tests/Fixture/ResetDatabase/migration-configs/migration-configuration.php\"]"

# run test suite with postgreSQL
$ vendor/bin/phpunit
Expand Down
5 changes: 0 additions & 5 deletions tests/Fixture/FoundryTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public static function hasMongo(): bool
return (bool) \getenv('MONGO_URL');
}

public static function usesMigrations(): bool
{
return 'migrate' === \getenv('DATABASE_RESET_MODE');
}

public static function usesDamaDoctrineTestBundle(): bool
{
return (bool) \getenv('USE_DAMA_DOCTRINE_TEST_BUNDLE');
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixture/ResetDatabase/DummyMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Zenstruck\Foundry\Tests\Fixture\ResetDatabase;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class DummyMigration extends AbstractMigration
{
public function getDescription(): string
{
return 'Dummy migration to test migration reset with multiple configuration files.';
}

public function up(Schema $schema): void
{
$this->addSql('INSERT INTO address (city) VALUES (\'city\')');
}
}
38 changes: 34 additions & 4 deletions tests/Fixture/ResetDatabase/ResetDatabaseTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function registerBundles(): iterable
{
yield from parent::registerBundles();

if (FoundryTestKernel::usesMigrations()) {
if (self::usesMigrations()) {
yield new DoctrineMigrationsBundle();
}
}
Expand All @@ -43,18 +43,18 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
GlobalInvokableService::class,
],
'orm' => [
'reset' => FoundryTestKernel::usesMigrations()
'reset' => self::usesMigrations()
? [
'mode' => ResetDatabaseMode::MIGRATE,
'migrations' => [
'configurations' => ($configFile = \getenv('MIGRATION_CONFIGURATION_FILE')) ? [$configFile] : [],
'configurations' => ($configFile = self::migrationFiles()) ? $configFile : [],
],
]
: ['mode' => ResetDatabaseMode::SCHEMA],
],
]);

if (FoundryTestKernel::usesMigrations() && !\getenv('MIGRATION_CONFIGURATION_FILE')) {
if (self::usesMigrations() && self::migrationFiles() === []) {
// if no configuration file was given in Foundry's config, let's use the main one as default.
$c->loadFromExtension(
'doctrine_migrations',
Expand All @@ -72,4 +72,34 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
$c->register(MongoResetterDecorator::class)->setAutowired(true)->setAutoconfigured(true);
}
}

public static function usesMigrations(): bool
{
return 'migrate' === \getenv('DATABASE_RESET_MODE');
}

public static function shouldGenerateMigrations(): bool
{
return '0' !== \getenv('DATABASE_GENERATE_MIGRATIONS');
}

/**
* @return list<string>
*/
public static function migrationFiles(): array
{
$files = json_decode(\getenv('MIGRATION_CONFIGURATION_FILES')?: '[]', flags: JSON_THROW_ON_ERROR);

if (!\is_array($files)) {
throw new \InvalidArgumentException('MIGRATION_CONFIGURATION_FILES must be a JSON array.');
}

foreach ($files as $file) {
if (!\file_exists($file)) {
throw new \InvalidArgumentException(\sprintf('Migration configuration file "%s" does not exist.', $file));
}
}

return $files; // @phpstan-ignore return.type
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

return [
'migrations_paths' => [
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase\\Migrations' => \dirname(__DIR__, 4).'/var/cache/Migrations',
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase\\Migrations' => \dirname(__DIR__, 4).'/var/Migrations',
],
'transactional' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

return [
'migrations_paths' => [
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase\\Migrations' => \dirname(__DIR__, 4).'/var/cache/Migrations',
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase\\Migrations' => \dirname(__DIR__, 4).'/var/Migrations',
],
'transactional' => false,
];
17 changes: 17 additions & 0 deletions tests/Fixture/ResetDatabase/migration-configs/migration-dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'migrations_paths' => [
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase' => \dirname(__DIR__),
],
'transactional' => false,
];
Loading
Loading