Skip to content
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

fix: simplify reset database extension #779

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
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
33 changes: 21 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,33 @@ jobs:
run: ./phpunit
shell: bash

test-reset-database-with-migration:
name: Test migration - D:${{ matrix.database }} ${{ matrix.use-dama == 1 && ' (dama)' || '' }} ${{ contains(matrix.with-migration-configuration-file, 'transactional') && '(configuration file transactional)' || contains(matrix.with-migration-configuration-file, 'configuration') && '(configuration file)' || '' }}
test-reset-database:
name: Test reset database - 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)' || '' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
database: [ mysql, pgsql, sqlite ]
database: [ mysql, pgsql, sqlite, mysql|mongo ]
use-dama: [ 0, 1 ]
with-migration-configuration-file:
- ''
- 'tests/Fixture/MigrationTests/configs/migration-configuration.php'
- 'tests/Fixture/MigrationTests/configs/migration-configuration-transactional.php'
reset-database-mode: [ schema, migrate ]
migration-configuration-file: ['no', 'migration-configuration', 'migration-configuration-transactional']
include:
- { database: mongo, migration-configuration-file: 'no', use-dama: 0, reset-database-mode: schema }
exclude:
# there is currently a bug with MySQL and transactional migrations
- database: mysql
with-migration-configuration-file: 'tests/Fixture/MigrationTests/configs/migration-configuration-transactional.php'
migration-configuration-file: 'migration-configuration-transactional'
- reset-database-mode: schema
migration-configuration-file: 'migration-configuration'
- reset-database-mode: schema
migration-configuration-file: 'migration-configuration-transactional'
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: ''
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 }}
WITH_MIGRATION_CONFIGURATION_FILE: ${{ matrix.with-migration-configuration-file }}
PHPUNIT_VERSION: 9
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) }}
PHPUNIT_VERSION: 11
services:
postgres:
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
Expand All @@ -125,6 +130,10 @@ jobs:
--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
Expand All @@ -149,7 +158,7 @@ jobs:
run: sudo /etc/init.d/mysql start

- name: Test
run: ./phpunit --testsuite migrate --bootstrap tests/bootstrap-migrate.php
run: ./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
shell: bash

test-with-paratest:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
/docker/.makefile
/.env.local
/docker-compose.override.yaml
/tests/Fixture/MigrationTests/Migrations/
/tests/Fixture/Maker/tmp/
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ $ ./phpunit
# run "migrate" testsuite (with "migrate" reset database strategy)
$ composer test-migrate
# or
$ ./phpunit --testsuite migrate --bootstrap tests/bootstrap-migrate.php
$ ./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
```

### Overriding the default configuration
Expand All @@ -74,9 +74,9 @@ MONGO_URL="" # disables Mongo
USE_DAMA_DOCTRINE_TEST_BUNDLE="1" # enables dama/doctrine-test-bundle
PHPUNIT_VERSION="11" # possible values: 9, 10, 11, 11.4

# test reset database with configuration migration,
# only relevant for "migrate" testsuite
WITH_MIGRATION_CONFIGURATION_FILE="tests/Fixture/MigrationTests/configs/migration-configuration.php"
# test reset database with migrations,
# only relevant for "reset-database" testsuite
MIGRATION_CONFIGURATION_FILE="tests/Fixture/MigrationTests/configs/migration-configuration.php"

# run test suite with postgreSQL
$ vendor/bin/phpunit
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@
},
"scripts": {
"test": [
"@test-schema",
"@test-migrate"
"@test-main",
"@test-reset-database"
],
"test-schema": "./phpunit",
"test-migrate": "./phpunit --testsuite migrate --bootstrap tests/bootstrap-migrate.php"
"test-main": "./phpunit --testsuite main",
"test-reset-database": "./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php"
},
"scripts-descriptions": {
"test-schema": "Test with schema reset",
"test-migrate": "Test with migrations reset"
"test-main": "Main test suite",
"test-reset-database": "Test reset database test suite"
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
4 changes: 3 additions & 1 deletion config/mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Zenstruck\Foundry\Mongo\MongoPersistenceStrategy;
use Zenstruck\Foundry\Mongo\MongoResetter;
use Zenstruck\Foundry\Mongo\MongoSchemaResetter;

return static function (ContainerConfigurator $container): void {
Expand All @@ -13,7 +14,8 @@
abstract_arg('config'),
])
->tag('.foundry.persistence_strategy')
->set('.zenstruck_foundry.persistence.schema_resetter.mongo', MongoSchemaResetter::class)

->set(MongoResetter::class, MongoSchemaResetter::class)
->args([
abstract_arg('managers'),
])
Expand Down
20 changes: 4 additions & 16 deletions config/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use Zenstruck\Foundry\ORM\OrmV3PersistenceStrategy;
use Zenstruck\Foundry\ORM\ResetDatabase\BaseOrmResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\DamaDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\SchemaDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\MigrateDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\OrmResetter;

return static function (ContainerConfigurator $container): void {
$container->services()
Expand All @@ -26,27 +25,16 @@
->arg('$connections', service('connections'))
->abstract()

->set('.zenstruck_foundry.persistence.database_resetter.orm.schema', SchemaDatabaseResetter::class)
->parent('.zenstruck_foundry.persistence.database_resetter.orm.abstract')
->tag('.foundry.persistence.database_resetter')
->tag('.foundry.persistence.schema_resetter')

->set('.zenstruck_foundry.persistence.database_resetter.orm.migrate', MigrateDatabaseResetter::class)
->arg('$configurations', abstract_arg('configurations'))
->set(OrmResetter::class, /* class to be defined thanks to the configuration */)
->parent('.zenstruck_foundry.persistence.database_resetter.orm.abstract')
->tag('.foundry.persistence.database_resetter')
->tag('.foundry.persistence.schema_resetter')
;

if (\class_exists(StaticDriver::class)) {
$container->services()
->set('.zenstruck_foundry.persistence.database_resetter.orm.schema.dama', DamaDatabaseResetter::class)
->decorate('.zenstruck_foundry.persistence.database_resetter.orm.schema')
->args([
service('.inner'),
])
->set('.zenstruck_foundry.persistence.database_resetter.orm.migrate.dama', DamaDatabaseResetter::class)
->decorate('.zenstruck_foundry.persistence.database_resetter.orm.migrate')
->set('.zenstruck_foundry.persistence.database_resetter.orm.dama', DamaDatabaseResetter::class)
->decorate(OrmResetter::class, priority: 10)
->args([
service('.inner'),
])
Expand Down
2 changes: 1 addition & 1 deletion phpunit
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ check_phpunit_version() {
}

### >> load env vars from .env files if not in CI and not from a composer script
if [ -z "${CI:-}" ] && [ -z "${COMPOSER_BINARY:-}" ] ; then
if [ -z "${CI:-}" ] ; then
source .env

if [ -f .env.local ]; then
Expand Down
6 changes: 3 additions & 3 deletions phpunit-10.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Integration/ResetDatabase</exclude>
</testsuite>
<testsuite name="migrate">
<file>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</file>
<testsuite name="reset-database">
<directory>tests/Integration/ResetDatabase</directory>
</testsuite>
</testsuites>
<source ignoreSuppressionOfDeprecations="true">
Expand Down
2 changes: 1 addition & 1 deletion phpunit-paratest.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Integration/ResetDatabase</exclude>
</testsuite>
</testsuites>
<source ignoreSuppressionOfDeprecations="true">
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Integration/ResetDatabase</exclude>
</testsuite>
<testsuite name="migrate">
<file>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</file>
<testsuite name="reset-database">
<directory>tests/Integration/ResetDatabase</directory>
</testsuite>
</testsuites>

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/ResetDatabase/SchemaDatabaseResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function resetBeforeFirstTest(KernelInterface $kernel): void
$this->createSchema($application);
}

public function doResetBeforeEachTest(KernelInterface $kernel): void
protected function doResetBeforeEachTest(KernelInterface $kernel): void
{
$application = application($kernel);

Expand Down
29 changes: 16 additions & 13 deletions src/ZenstruckFoundryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
use Zenstruck\Foundry\Mongo\MongoResetter;
use Zenstruck\Foundry\Object\Instantiator;
use Zenstruck\Foundry\ORM\ResetDatabase\MigrateDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\OrmResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\ResetDatabaseMode;
use Zenstruck\Foundry\ORM\ResetDatabase\SchemaDatabaseResetter;

/**
* @author Kevin Bond <[email protected]>
Expand Down Expand Up @@ -249,18 +251,21 @@ public function loadExtension(array $config, ContainerConfigurator $configurator
->replaceArgument('$connections', $config['orm']['reset']['connections'])
;

$container->getDefinition('.zenstruck_foundry.persistence.database_resetter.orm.migrate')
->replaceArgument('$configurations', $config['orm']['reset']['migrations']['configurations'])
;

/** @var ResetDatabaseMode $resetMode */
$resetMode = $config['orm']['reset']['mode'];
$toRemove = ResetDatabaseMode::SCHEMA === $resetMode ? ResetDatabaseMode::MIGRATE->value : ResetDatabaseMode::SCHEMA->value;

$container->removeDefinition(".zenstruck_foundry.persistence.database_resetter.orm.{$toRemove}.dama");
$container->removeDefinition(".zenstruck_foundry.persistence.database_resetter.orm.{$toRemove}");

$container->setAlias(OrmResetter::class, ".zenstruck_foundry.persistence.database_resetter.orm.{$resetMode->value}");
$container->getDefinition(OrmResetter::class)
->setClass(
match ($resetMode) {
ResetDatabaseMode::SCHEMA => SchemaDatabaseResetter::class,
ResetDatabaseMode::MIGRATE => MigrateDatabaseResetter::class,
}
);

if ($resetMode === ResetDatabaseMode::MIGRATE) {
$container->getDefinition(OrmResetter::class)
->replaceArgument('$configurations', $config['orm']['reset']['migrations']['configurations'])
;
}
}

if (isset($bundles['DoctrineMongoDBBundle'])) {
Expand All @@ -270,11 +275,9 @@ public function loadExtension(array $config, ContainerConfigurator $configurator
->replaceArgument(1, $config['mongo'])
;

$container->getDefinition('.zenstruck_foundry.persistence.schema_resetter.mongo')
$container->getDefinition(MongoResetter::class)
->replaceArgument(0, $config['mongo']['reset']['document_managers'])
;

$container->setAlias(MongoResetter::class, '.zenstruck_foundry.persistence.schema_resetter.mongo');
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/EntityInAnotherSchema/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/**
* Create custom "cms" schema ({@see Article}) to ensure "migrate" mode is still working with multiple schemas.
* Note: this entity is added to mapping only for PostgreSQ, as it is the only supported DBMS which handles multiple schemas.
* Note: this entity is added to mapping only for PostgreSQL, as it is the only supported DBMS which handles multiple schemas.
*
* @see https://github.com/zenstruck/foundry/issues/618
*/
Expand Down
Loading
Loading