Skip to content

Commit d9c56a4

Browse files
committed
tests: add test permutation with multiple migration configuration files
1 parent fed6e4a commit d9c56a4

File tree

12 files changed

+123
-39
lines changed

12 files changed

+123
-39
lines changed

.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ USE_DAMA_DOCTRINE_TEST_BUNDLE="0"
1111
USE_FOUNDRY_PHPUNIT_EXTENSION="0"
1212
USE_PHP_84_LAZY_OBJECTS="0"
1313
PHPUNIT_VERSION="12" # allowed values: 9, 10, 11, 12
14+
15+
# Only relevant for "reset-database" testsuite
16+
DATABASE_RESET_MODE="schema" # allowed values: schema, migrate
17+
18+
# Only relevant for "reset-database" testsuite with DATABASE_RESET_MODE="migrate"
19+
MIGRATION_CONFIGURATION_FILES=[]
20+
#MIGRATION_CONFIGURATION_FILES=["tests/Fixture/ResetDatabase/migration-configs/migration-configuration.php"]
21+
#MIGRATION_CONFIGURATION_FILES=["tests/Fixture/ResetDatabase/migration-configs/migration-configuration-transactional.php", "tests/Fixture/ResetDatabase/migration-configs/migration-dummy.php"]

.github/workflows/phpunit.yml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,33 @@ jobs:
125125
shell: bash
126126

127127
test-reset-database:
128-
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)' || '' }}
128+
name: Reset DB - D:${{ matrix.database }} ${{ matrix.use-dama == 1 && ' (dama)' || '' }} ${{ matrix.reset-database-mode == 'migrate' && ' (migrate)' || '' }} ${{ contains(matrix.migration-configuration-file, ',') && '(multi configuration files)' || contains(matrix.migration-configuration-file, 'transactional') && '(configuration file transactional)' || contains(matrix.migration-configuration-file, 'configuration') && '(configuration file)' || '' }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}
129129
runs-on: ubuntu-latest
130130
strategy:
131131
fail-fast: false
132132
matrix:
133133
database: [ mysql, pgsql, sqlite, mysql|mongo ]
134134
use-dama: [ 0, 1 ]
135135
reset-database-mode: [ schema, migrate ]
136-
migration-configuration-file: ['no']
136+
migration-configuration-file: ['[]']
137137
deps: [ highest, lowest ]
138138
include:
139-
- { database: mongo, migration-configuration-file: 'no', use-dama: 0, reset-database-mode: schema }
140-
- { database: pgsql, migration-configuration-file: 'migration-configuration', use-dama: 0, reset-database-mode: migration }
141-
- { database: pgsql, migration-configuration-file: 'migration-configuration-transactional', use-dama: 0, reset-database-mode: migration }
139+
- { database: mongo, migration-configuration-file: '[]', use-dama: 0, reset-database-mode: schema }
140+
- { database: pgsql, migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration.php"]', use-dama: 0, reset-database-mode: migrate }
141+
- { database: pgsql, migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration-transactional.php"]', use-dama: 0, reset-database-mode: migrate }
142+
-
143+
database: pgsql
144+
use-dama: 0
145+
reset-database-mode: migrate
146+
migration-configuration-file: '["tests/Fixture/ResetDatabase/migration-configs/migration-configuration-transactional.php", "tests/Fixture/ResetDatabase/migration-configs/migration-dummy.php"]'
147+
deps: highest
142148
env:
143149
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' }}
144150
MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
145151
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ matrix.use-dama == 1 && 1 || 0 }}
146-
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode == 1 && 1 || 0 }}
147-
MIGRATION_CONFIGURATION_FILE: ${{ matrix.migration-configuration-file == 'no' && '' || format('tests/Fixture/MigrationTests/configs/{0}.php', matrix.migration-configuration-file) }}
148-
PHPUNIT_VERSION: 11
152+
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode }}
153+
MIGRATION_CONFIGURATION_FILES: ${{ matrix.migration-configuration-file }}
154+
PHPUNIT_VERSION: 12
149155
services:
150156
postgres:
151157
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
@@ -193,7 +199,7 @@ jobs:
193199
194200
# We should be able to run the tests twice in order to check if the second run also starts from a fresh db
195201
# some bugs could be detected this way
196-
./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
202+
DATABASE_GENERATE_MIGRATIONS=0 ./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
197203
shell: bash
198204

199205
test-no-framework:
@@ -307,7 +313,7 @@ jobs:
307313
- name: Setup PHP
308314
uses: shivammathur/setup-php@v2
309315
with:
310-
php-version: 8.2
316+
php-version: 8.4
311317
coverage: xdebug
312318
ini-values: xdebug.mode=coverage
313319

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ $ composer update
5252
$ ./phpunit
5353

5454
# run "migrate" testsuite (with "migrate" reset database strategy)
55-
$ ./phpunit --testsuite reset-database
55+
$ ./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
5656
```
5757

5858
### Overriding the default configuration
@@ -73,7 +73,7 @@ PHPUNIT_VERSION="11" # possible values: 9, 10, 11, 11.4
7373

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

7878
# run test suite with postgreSQL
7979
$ vendor/bin/phpunit

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"doctrine/common": "^3.2.2",
3434
"doctrine/doctrine-bundle": "^2.10",
3535
"doctrine/doctrine-migrations-bundle": "^2.2|^3.0",
36+
"doctrine/migrations": "dev-feat/unfroze-dependencies-after-command as 3.9.5",
3637
"doctrine/mongodb-odm": "^2.4",
3738
"doctrine/mongodb-odm-bundle": "^4.6|^5.0",
3839
"doctrine/orm": "^2.16|^3.0",
@@ -64,6 +65,12 @@
6465
"src/symfony_console.php"
6566
]
6667
},
68+
"repositories": [
69+
{
70+
"type": "vcs",
71+
"url": "https://github.com/nikophil/migrations"
72+
}
73+
],
6774
"autoload-dev": {
6875
"psr-4": {
6976
"Zenstruck\\Foundry\\Tests\\": ["tests/"],

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: 36 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
}
@@ -38,23 +38,25 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
3838
parent::configureContainer($c, $loader);
3939

4040
$c->loadFromExtension('zenstruck_foundry', [
41+
'persistence' => ['flush_once' => true],
42+
'enable_auto_refresh_with_lazy_objects' => self::usePHP84LazyObjects(),
4143
'global_state' => [
4244
GlobalStory::class,
4345
GlobalInvokableService::class,
4446
],
4547
'orm' => [
46-
'reset' => FoundryTestKernel::usesMigrations()
48+
'reset' => self::usesMigrations()
4749
? [
4850
'mode' => ResetDatabaseMode::MIGRATE,
4951
'migrations' => [
50-
'configurations' => ($configFile = \getenv('MIGRATION_CONFIGURATION_FILE')) ? [$configFile] : [],
52+
'configurations' => ($configFile = self::migrationFiles()) ? $configFile : [],
5153
],
5254
]
5355
: ['mode' => ResetDatabaseMode::SCHEMA],
5456
],
5557
]);
5658

57-
if (FoundryTestKernel::usesMigrations() && !\getenv('MIGRATION_CONFIGURATION_FILE')) {
59+
if (self::usesMigrations() && self::migrationFiles() === []) {
5860
// if no configuration file was given in Foundry's config, let's use the main one as default.
5961
$c->loadFromExtension(
6062
'doctrine_migrations',
@@ -76,4 +78,34 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
7678
$c->register(MongoResetterDecorator::class)->setAutowired(true)->setAutoconfigured(true);
7779
}
7880
}
81+
82+
public static function usesMigrations(): bool
83+
{
84+
return 'migrate' === \getenv('DATABASE_RESET_MODE');
85+
}
86+
87+
public static function shouldGenerateMigrations(): bool
88+
{
89+
return '0' !== \getenv('DATABASE_GENERATE_MIGRATIONS');
90+
}
91+
92+
/**
93+
* @return list<string>
94+
*/
95+
public static function migrationFiles(): array
96+
{
97+
$files = json_decode(\getenv('MIGRATION_CONFIGURATION_FILES')?: '[]', flags: JSON_THROW_ON_ERROR);
98+
99+
if (!\is_array($files)) {
100+
throw new \InvalidArgumentException('MIGRATION_CONFIGURATION_FILES must be a JSON array.');
101+
}
102+
103+
foreach ($files as $file) {
104+
if (!\file_exists($file)) {
105+
throw new \InvalidArgumentException(\sprintf('Migration configuration file "%s" does not exist.', $file));
106+
}
107+
}
108+
109+
return $files; // @phpstan-ignore return.type
110+
}
79111
}

tests/Fixture/ResetDatabase/migration-configs/migration-configuration-transactional.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
return [
1313
'migrations_paths' => [
14-
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase\\Migrations' => \dirname(__DIR__, 4).'/var/cache/Migrations',
14+
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase\\Migrations' => \dirname(__DIR__, 4).'/var/Migrations',
1515
],
1616
'transactional' => true,
1717
];

tests/Fixture/ResetDatabase/migration-configs/migration-configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
return [
1313
'migrations_paths' => [
14-
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase\\Migrations' => \dirname(__DIR__, 4).'/var/cache/Migrations',
14+
'Zenstruck\\Foundry\\Tests\\Fixture\\ResetDatabase\\Migrations' => \dirname(__DIR__, 4).'/var/Migrations',
1515
],
1616
'transactional' => false,
1717
];
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 <kevinbond@gmail.com>
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)