Skip to content

Commit bb879fd

Browse files
committed
tests: add test permutation with multiple migration configuration files
1 parent 50350cb commit bb879fd

File tree

8 files changed

+197
-130
lines changed

8 files changed

+197
-130
lines changed

.github/workflows/ci.yml

Lines changed: 112 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -7,104 +7,104 @@ on:
77
- cron: '0 0 1,16 * *'
88

99
jobs:
10-
tests:
11-
name: P:${{ matrix.php }}, S:${{ matrix.symfony }}, D:${{ matrix.database }}, PU:${{ matrix.phpunit }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.use-phpunit-extension == 1 && ' (phpunit extension)' || '' }}
12-
runs-on: ubuntu-latest
13-
strategy:
14-
fail-fast: false
15-
matrix:
16-
php: [ 8.2, 8.3, 8.4 ]
17-
symfony: [ 6.4.*, 7.1.*, 7.2.* ]
18-
database: [ mysql|mongo ]
19-
phpunit: [ 11, 12 ]
20-
21-
# default values:
22-
# deps: [ highest ]
23-
# use-phpunit-extension: [ 0 ]
24-
25-
exclude:
26-
- {php: 8.2, phpunit: 12}
27-
- {php: 8.3, phpunit: 11}
28-
- {php: 8.4, phpunit: 11}
29-
30-
include:
31-
# php 8.1
32-
- {php: 8.1, symfony: 6.4.*, phpunit: 9, database: mysql}
33-
34-
# old PHPUnit versions
35-
- {php: 8.3, symfony: '*', phpunit: 9, database: mysql}
36-
- {php: 8.4, symfony: '*', phpunit: 10, database: mysql}
37-
- {php: 8.4, symfony: '*', phpunit: 11, database: mysql}
38-
39-
# test with no database (PHPUnit 9 is used to prevent some problems with empty data providers)
40-
- {php: 8.3, symfony: '*', phpunit: 9, database: none}
41-
- {php: 8.3, symfony: '*', phpunit: 9, database: none, deps: lowest}
42-
43-
# One permutation per DBMS
44-
- {php: 8.4, symfony: '*', phpunit: 12, database: mongo}
45-
- {php: 8.4, symfony: '*', phpunit: 12, database: pgsql}
46-
- {php: 8.4, symfony: '*', phpunit: 12, database: sqlite}
47-
- {php: 8.4, symfony: '*', phpunit: 12, database: mysql}
48-
49-
# lowest deps (one per DBMS)
50-
- {php: 8.3, symfony: '*', phpunit: 9, database: mysql|mongo, deps: lowest}
51-
- {php: 8.3, symfony: '*', phpunit: 9, database: mongo, deps: lowest}
52-
- {php: 8.3, symfony: '*', phpunit: 9, database: pgsql, deps: lowest}
53-
- {php: 8.3, symfony: '*', phpunit: 9, database: sqlite, deps: lowest}
54-
- {php: 8.3, symfony: '*', phpunit: 9, database: mysql, deps: lowest}
55-
56-
# using Foundry's PHPUnit extension
57-
- {php: 8.4, symfony: '*', phpunit: 12, database: mysql|mongo, use-phpunit-extension: 1}
58-
env:
59-
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' || '' }}
60-
MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
61-
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ contains(matrix.database, 'sql') && 1 || 0 }}
62-
USE_FOUNDRY_PHPUNIT_EXTENSION: ${{ matrix.use-phpunit-extension || 0 }}
63-
PHPUNIT_VERSION: ${{ matrix.phpunit }}
64-
services:
65-
postgres:
66-
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
67-
env:
68-
POSTGRES_USER: root
69-
POSTGRES_PASSWORD: root
70-
POSTGRES_DB: foundry
71-
ports:
72-
- 5432:5432
73-
options: >-
74-
--health-cmd pg_isready
75-
--health-interval 10s
76-
--health-timeout 5s
77-
--health-retries 5
78-
mongo:
79-
image: ${{ contains(matrix.database, 'mongo') && 'mongo:4' || '' }}
80-
ports:
81-
- 27017:27017
82-
steps:
83-
- name: Checkout code
84-
uses: actions/checkout@v3
85-
86-
- name: Setup PHP
87-
uses: shivammathur/setup-php@v2
88-
with:
89-
php-version: ${{ matrix.php }}
90-
coverage: none
91-
tools: flex
92-
93-
- name: Install dependencies
94-
uses: ramsey/composer-install@v2
95-
with:
96-
dependency-versions: ${{ matrix.deps }}
97-
composer-options: --prefer-dist
98-
env:
99-
SYMFONY_REQUIRE: ${{ matrix.symfony }}
100-
101-
- name: Set up MySQL
102-
if: contains(matrix.database, 'mysql')
103-
run: sudo /etc/init.d/mysql start
104-
105-
- name: Test
106-
run: ./phpunit
107-
shell: bash
10+
# tests:
11+
# name: P:${{ matrix.php }}, S:${{ matrix.symfony }}, D:${{ matrix.database }}, PU:${{ matrix.phpunit }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.use-phpunit-extension == 1 && ' (phpunit extension)' || '' }}
12+
# runs-on: ubuntu-latest
13+
# strategy:
14+
# fail-fast: false
15+
# matrix:
16+
# php: [ 8.2, 8.3, 8.4 ]
17+
# symfony: [ 6.4.*, 7.1.*, 7.2.* ]
18+
# database: [ mysql|mongo ]
19+
# phpunit: [ 11, 12 ]
20+
#
21+
# # default values:
22+
# # deps: [ highest ]
23+
# # use-phpunit-extension: [ 0 ]
24+
#
25+
# exclude:
26+
# - {php: 8.2, phpunit: 12}
27+
# - {php: 8.3, phpunit: 11}
28+
# - {php: 8.4, phpunit: 11}
29+
#
30+
# include:
31+
# # php 8.1
32+
# - {php: 8.1, symfony: 6.4.*, phpunit: 9, database: mysql}
33+
#
34+
# # old PHPUnit versions
35+
# - {php: 8.3, symfony: '*', phpunit: 9, database: mysql}
36+
# - {php: 8.4, symfony: '*', phpunit: 10, database: mysql}
37+
# - {php: 8.4, symfony: '*', phpunit: 11, database: mysql}
38+
#
39+
# # test with no database (PHPUnit 9 is used to prevent some problems with empty data providers)
40+
# - {php: 8.3, symfony: '*', phpunit: 9, database: none}
41+
# - {php: 8.3, symfony: '*', phpunit: 9, database: none, deps: lowest}
42+
#
43+
# # One permutation per DBMS
44+
# - {php: 8.4, symfony: '*', phpunit: 12, database: mongo}
45+
# - {php: 8.4, symfony: '*', phpunit: 12, database: pgsql}
46+
# - {php: 8.4, symfony: '*', phpunit: 12, database: sqlite}
47+
# - {php: 8.4, symfony: '*', phpunit: 12, database: mysql}
48+
#
49+
# # lowest deps (one per DBMS)
50+
# - {php: 8.3, symfony: '*', phpunit: 9, database: mysql|mongo, deps: lowest}
51+
# - {php: 8.3, symfony: '*', phpunit: 9, database: mongo, deps: lowest}
52+
# - {php: 8.3, symfony: '*', phpunit: 9, database: pgsql, deps: lowest}
53+
# - {php: 8.3, symfony: '*', phpunit: 9, database: sqlite, deps: lowest}
54+
# - {php: 8.3, symfony: '*', phpunit: 9, database: mysql, deps: lowest}
55+
#
56+
# # using Foundry's PHPUnit extension
57+
# - {php: 8.4, symfony: '*', phpunit: 12, database: mysql|mongo, use-phpunit-extension: 1}
58+
# env:
59+
# 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' || '' }}
60+
# MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
61+
# USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ contains(matrix.database, 'sql') && 1 || 0 }}
62+
# USE_FOUNDRY_PHPUNIT_EXTENSION: ${{ matrix.use-phpunit-extension || 0 }}
63+
# PHPUNIT_VERSION: ${{ matrix.phpunit }}
64+
# services:
65+
# postgres:
66+
# image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
67+
# env:
68+
# POSTGRES_USER: root
69+
# POSTGRES_PASSWORD: root
70+
# POSTGRES_DB: foundry
71+
# ports:
72+
# - 5432:5432
73+
# options: >-
74+
# --health-cmd pg_isready
75+
# --health-interval 10s
76+
# --health-timeout 5s
77+
# --health-retries 5
78+
# mongo:
79+
# image: ${{ contains(matrix.database, 'mongo') && 'mongo:4' || '' }}
80+
# ports:
81+
# - 27017:27017
82+
# steps:
83+
# - name: Checkout code
84+
# uses: actions/checkout@v3
85+
#
86+
# - name: Setup PHP
87+
# uses: shivammathur/setup-php@v2
88+
# with:
89+
# php-version: ${{ matrix.php }}
90+
# coverage: none
91+
# tools: flex
92+
#
93+
# - name: Install dependencies
94+
# uses: ramsey/composer-install@v2
95+
# with:
96+
# dependency-versions: ${{ matrix.deps }}
97+
# composer-options: --prefer-dist
98+
# env:
99+
# SYMFONY_REQUIRE: ${{ matrix.symfony }}
100+
#
101+
# - name: Set up MySQL
102+
# if: contains(matrix.database, 'mysql')
103+
# run: sudo /etc/init.d/mysql start
104+
#
105+
# - name: Test
106+
# run: ./phpunit
107+
# shell: bash
108108

109109
test-reset-database:
110110
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)' || '' }}
@@ -115,18 +115,24 @@ jobs:
115115
database: [ mysql, pgsql, sqlite, mysql|mongo ]
116116
use-dama: [ 0, 1 ]
117117
reset-database-mode: [ schema, migrate ]
118-
migration-configuration-file: ['no']
118+
migration-configuration-file: ['[]']
119119
deps: [ highest, lowest ]
120120
include:
121-
- { database: mongo, migration-configuration-file: 'no', use-dama: 0, reset-database-mode: schema }
122-
- { database: pgsql, migration-configuration-file: 'migration-configuration', use-dama: 0, reset-database-mode: migration }
123-
- { database: pgsql, migration-configuration-file: 'migration-configuration-transactional', use-dama: 0, reset-database-mode: migration }
121+
- { database: mongo, migration-configuration-file: '[]', use-dama: 0, reset-database-mode: schema }
122+
- { database: pgsql, migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration.php"]', use-dama: 0, reset-database-mode: migrate }
123+
- { database: pgsql, migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration-transactional.php"]', use-dama: 0, reset-database-mode: migrate }
124+
-
125+
database: pgsql
126+
use-dama: 0
127+
reset-database-mode: migrate
128+
migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration-transactional.php", "tests/Fixture/ResetDatabase/migration-configs/migration-dummy.php"]'
129+
deps: highest
124130
env:
125131
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' }}
126132
MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
127133
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ matrix.use-dama == 1 && 1 || 0 }}
128-
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode == 1 && 1 || 0 }}
129-
MIGRATION_CONFIGURATION_FILE: ${{ matrix.migration-configuration-file == 'no' && '' || format('tests/Fixture/MigrationTests/configs/{0}.php', matrix.migration-configuration-file) }}
134+
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode }}
135+
MIGRATION_CONFIGURATION_FILES: ${{ matrix.migration-configuration-file }}
130136
PHPUNIT_VERSION: 11
131137
services:
132138
postgres:
@@ -163,7 +169,7 @@ jobs:
163169
dependency-versions: ${{ matrix.deps }}
164170
composer-options: --prefer-dist
165171
env:
166-
SYMFONY_REQUIRE: 7.1.*
172+
SYMFONY_REQUIRE: 7.2.*
167173

168174
- name: Set up MySQL
169175
if: contains(matrix.database, 'mysql')
@@ -175,7 +181,7 @@ jobs:
175181
176182
# We should be able to run the tests twice in order to check if the second run also starts from a fresh db
177183
# some bugs could be detected this way
178-
./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
184+
DATABASE_GENERATE_MIGRATIONS=0 ./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
179185
shell: bash
180186

181187
test-with-paratest:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ PHPUNIT_VERSION="11" # possible values: 9, 10, 11, 11.4
7777

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

8282
# run test suite with postgreSQL
8383
$ vendor/bin/phpunit

tests/Fixture/FoundryTestKernel.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ public static function hasMongo(): bool
6060
return (bool) \getenv('MONGO_URL');
6161
}
6262

63-
public static function usesMigrations(): bool
64-
{
65-
return 'migrate' === \getenv('DATABASE_RESET_MODE');
66-
}
67-
6863
public static function usesDamaDoctrineTestBundle(): bool
6964
{
7065
return (bool) \getenv('USE_DAMA_DOCTRINE_TEST_BUNDLE');
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Tests\Fixture\ResetDatabase;
4+
5+
use Doctrine\DBAL\Schema\Schema;
6+
use Doctrine\Migrations\AbstractMigration;
7+
8+
final class DummyMigration extends AbstractMigration
9+
{
10+
public function getDescription(): string
11+
{
12+
return 'Dummy migration to test migration reset with multiple configuration files.';
13+
}
14+
15+
public function up(Schema $schema): void
16+
{
17+
$this->addSql('INSERT INTO address (city) VALUES (\'city\')');
18+
}
19+
}

tests/Fixture/ResetDatabase/ResetDatabaseTestKernel.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function registerBundles(): iterable
2828
{
2929
yield from parent::registerBundles();
3030

31-
if (FoundryTestKernel::usesMigrations()) {
31+
if (self::usesMigrations()) {
3232
yield new DoctrineMigrationsBundle();
3333
}
3434
}
@@ -43,18 +43,18 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
4343
GlobalInvokableService::class,
4444
],
4545
'orm' => [
46-
'reset' => FoundryTestKernel::usesMigrations()
46+
'reset' => self::usesMigrations()
4747
? [
4848
'mode' => ResetDatabaseMode::MIGRATE,
4949
'migrations' => [
50-
'configurations' => ($configFile = \getenv('MIGRATION_CONFIGURATION_FILE')) ? [$configFile] : [],
50+
'configurations' => ($configFile = self::migrationFiles()) ? $configFile : [],
5151
],
5252
]
5353
: ['mode' => ResetDatabaseMode::SCHEMA],
5454
],
5555
]);
5656

57-
if (FoundryTestKernel::usesMigrations() && !\getenv('MIGRATION_CONFIGURATION_FILE')) {
57+
if (self::usesMigrations() && self::migrationFiles() === []) {
5858
// if no configuration file was given in Foundry's config, let's use the main one as default.
5959
$c->loadFromExtension(
6060
'doctrine_migrations',
@@ -72,4 +72,34 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
7272
$c->register(MongoResetterDecorator::class)->setAutowired(true)->setAutoconfigured(true);
7373
}
7474
}
75+
76+
public static function usesMigrations(): bool
77+
{
78+
return 'migrate' === \getenv('DATABASE_RESET_MODE');
79+
}
80+
81+
public static function shouldGenerateMigrations(): bool
82+
{
83+
return '0' !== \getenv('DATABASE_GENERATE_MIGRATIONS');
84+
}
85+
86+
/**
87+
* @return list<string>
88+
*/
89+
public static function migrationFiles(): array
90+
{
91+
$files = json_decode(\getenv('MIGRATION_CONFIGURATION_FILES')?: '[]', flags: JSON_THROW_ON_ERROR);
92+
93+
if (!\is_array($files)) {
94+
throw new \InvalidArgumentException('MIGRATION_CONFIGURATION_FILES must be a JSON array.');
95+
}
96+
97+
foreach ($files as $file) {
98+
if (!\file_exists($file)) {
99+
throw new \InvalidArgumentException(\sprintf('Migration configuration file "%s" does not exist.', $file));
100+
}
101+
}
102+
103+
return $files; // @phpstan-ignore return.type
104+
}
75105
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the zenstruck/foundry package.
5+
*
6+
* (c) Kevin Bond <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
return [
13+
'migrations_paths' => [
14+
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase' => \dirname(__DIR__),
15+
],
16+
'transactional' => false,
17+
];

0 commit comments

Comments
 (0)