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

refactor: deprecate config database_resetter #523

Merged
Merged
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
43 changes: 43 additions & 0 deletions src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,50 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('orm')
// could be activated in 2.0
//->addDefaultsIfNotSet()
->children()
->arrayNode('reset')
->addDefaultsIfNotSet()
->children()
->arrayNode('connections')
->info('DBAL connections to reset with ResetDatabase trait')
->defaultValue(['default'])
->scalarPrototype()->end()
->end()
->arrayNode('entity_managers')
->info('Entity Managers to reset with ResetDatabase trait')
->defaultValue(['default'])
->scalarPrototype()->end()
->end()
->enumNode('mode')
->info('Reset mode to use with ResetDatabase trait')
->defaultValue(ORMDatabaseResetter::RESET_MODE_SCHEMA)
->values([ORMDatabaseResetter::RESET_MODE_SCHEMA, ORMDatabaseResetter::RESET_MODE_MIGRATE])
->end()
->end()
->end()
->end()
->end()
->arrayNode('mongo')
// could be activated in 2.0
//->addDefaultsIfNotSet()
->children()
->arrayNode('reset')
->addDefaultsIfNotSet()
->children()
->arrayNode('document_managers')
->info('Document Managers to reset with ResetDatabase trait')
->defaultValue(['default'])
->scalarPrototype()->end()
->end()
->end()
->end()
->end()
->end()
->arrayNode('database_resetter')
->setDeprecated('zenstruck/foundry', '1.37.0', 'Configuration "zenstruck_foundry.database_resetter" is deprecated and will be removed in 2.0. Use "zenstruck_foundry.orm.reset" or "zenstruck_foundry.mongo.reset" instead.')
->canBeDisabled()
->addDefaultsIfNotSet()
->info('Configure database reset mechanism.')
Expand Down
35 changes: 25 additions & 10 deletions src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
use Zenstruck\Foundry\Bundle\Command\StubMakeStory;
use Zenstruck\Foundry\Instantiator;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy as ProxyBase;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\Story;
use Zenstruck\Foundry\Test\ORMDatabaseResetter;
use Zenstruck\Foundry\Test\ResetDatabase;

/**
* @author Kevin Bond <kevinbond@gmail.com>
Expand All @@ -48,7 +51,7 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container

$this->configureFaker($mergedConfig['faker'], $container);
$this->configureDefaultInstantiator($mergedConfig['instantiator'], $container);
$this->configureDatabaseResetter($mergedConfig['database_resetter'], $container);
$this->configureDatabaseResetter($mergedConfig, $container);
$this->configureMakeFactory($mergedConfig['make_factory'], $container, $loader);

if (true === $mergedConfig['auto_refresh_proxies']) {
Expand Down Expand Up @@ -104,22 +107,34 @@ private function configureDatabaseResetter(array $config, ContainerBuilder $cont
{
$configurationDefinition = $container->getDefinition('.zenstruck_foundry.configuration');

if (false === $config['enabled']) {
$legacyConfig = $config['database_resetter'];

if (false === $legacyConfig['enabled']) {
trigger_deprecation('zenstruck\foundry', '1.37.0', sprintf('Disabling database reset via bundle configuration is deprecated and will be removed in 2.0. Instead you should not use "%s" trait in your test.', ResetDatabase::class));

$configurationDefinition->addMethodCall('disableDatabaseReset');
}

if (isset($config['orm']) && !self::isBundleLoaded($container, DoctrineBundle::class)) {
throw new \InvalidArgumentException('doctrine/doctrine-bundle should be enabled to use config under "database_resetter.orm".');
if (isset($legacyConfig['orm']) && isset($config['orm']['reset'])) {
throw new \InvalidArgumentException('Configurations "zenstruck_foundry.orm.reset" and "zenstruck_foundry.database_resetter.orm" are incompatible. You should only use "zenstruck_foundry.orm.reset".');
}

if ((isset($legacyConfig['orm']) || isset($config['orm']['reset'])) && !self::isBundleLoaded($container, DoctrineBundle::class)) {
throw new \InvalidArgumentException('doctrine/doctrine-bundle should be enabled to use config under "orm.reset".');
}

if (isset($legacyConfig['odm']) && isset($config['mongo']['reset'])) {
throw new \InvalidArgumentException('Configurations "zenstruck_foundry.mongo.reset" and "zenstruck_foundry.database_resetter.odm" are incompatible. You should only use "zenstruck_foundry.mongo.reset".');
}

if (isset($config['odm']) && !self::isBundleLoaded($container, DoctrineMongoDBBundle::class)) {
throw new \InvalidArgumentException('doctrine/mongodb-odm-bundle should be enabled to use config under "database_resetter.odm".');
if ((isset($legacyConfig['odm']) || isset($config['mongo']['reset'])) && !self::isBundleLoaded($container, DoctrineMongoDBBundle::class)) {
throw new \InvalidArgumentException('doctrine/mongodb-odm-bundle should be enabled to use config under "mongo.reset".');
}

$configurationDefinition->setArgument('$ormConnectionsToReset', $config['orm']['connections'] ?? []);
$configurationDefinition->setArgument('$ormObjectManagersToReset', $config['orm']['object_managers'] ?? []);
$configurationDefinition->setArgument('$ormResetMode', $config['orm']['reset_mode'] ?? ORMDatabaseResetter::RESET_MODE_SCHEMA);
$configurationDefinition->setArgument('$odmObjectManagersToReset', $config['odm']['object_managers'] ?? []);
$configurationDefinition->setArgument('$ormConnectionsToReset', $legacyConfig['orm']['connections'] ?? $config['orm']['reset']['connections'] ?? ['default']);
$configurationDefinition->setArgument('$ormObjectManagersToReset', $legacyConfig['orm']['object_managers'] ?? $config['orm']['reset']['entity_managers'] ?? ['default']);
$configurationDefinition->setArgument('$ormResetMode', $legacyConfig['orm']['reset_mode'] ?? $config['orm']['reset']['mode'] ?? ORMDatabaseResetter::RESET_MODE_SCHEMA);
$configurationDefinition->setArgument('$odmObjectManagersToReset', $legacyConfig['odm']['object_managers'] ?? $config['mongo']['reset']['document_managers'] ?? ['default']);
}

private function configureMakeFactory(array $makerConfig, ContainerBuilder $container, FileLoader $loader): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
$globalState[] = TagStory::class;
$globalState[] = TagStoryAsInvokableService::class;

$foundryConfig['database_resetter'] = ['orm' => ['reset_mode' => $this->ormResetMode]];
$foundryConfig['orm'] = ['reset' => ['mode' => $this->ormResetMode]];
}

if ($this->enableDoctrine && \getenv('MONGO_URL') && !\getenv('USE_DAMA_DOCTRINE_TEST_BUNDLE')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public function default_config(): void
$this->assertContainerBuilderNotHasService('.zenstruck_foundry.maker.story');
$this->assertContainerBuilderHasServiceDefinitionWithTag('.zenstruck_foundry.maker.factory_stub', 'console.command');
$this->assertContainerBuilderHasServiceDefinitionWithTag('.zenstruck_foundry.maker.story_stub', 'console.command');

$configurationArguments = $this->container->getDefinition('.zenstruck_foundry.configuration')->getArguments();
$this->assertSame(['default'], $configurationArguments['$ormConnectionsToReset']);
$this->assertSame(['default'], $configurationArguments['$ormObjectManagersToReset']);
$this->assertSame('schema', $configurationArguments['$ormResetMode']);
$this->assertSame(['default'], $configurationArguments['$odmObjectManagersToReset']);
}

/**
Expand Down Expand Up @@ -118,11 +124,13 @@ public function cannot_set_faker_seed_and_service(): void
*/
public function custom_instantiator_config(): void
{
$this->load(['instantiator' => [
'without_constructor' => true,
'allow_extra_attributes' => true,
'always_force_properties' => true,
]]);
$this->load([
'instantiator' => [
'without_constructor' => true,
'allow_extra_attributes' => true,
'always_force_properties' => true,
],
]);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('.zenstruck_foundry.default_instantiator', 'allowExtraAttributes');
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('.zenstruck_foundry.default_instantiator', 'alwaysForceProperties');
Expand Down Expand Up @@ -217,21 +225,36 @@ public function it_registers_makers_if_maker_bundle_enabled(): void

/**
* @test
* @group legacy
* @testWith ["orm"]
* ["odm"]
*/
public function cannot_configure_database_resetter_if_doctrine_not_enabled(string $doctrine): void
public function cannot_configure_legacy_database_resetter_if_doctrine_not_enabled(string $doctrine): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf('should be enabled to use config under "database_resetter.%s"', $doctrine));
$this->expectExceptionMessage(\sprintf('should be enabled to use config under "%s.reset"', $doctrine === 'orm' ? 'orm' : 'mongo'));

$this->load(['database_resetter' => [$doctrine => []]]);
}

/**
* @test
* @testWith ["orm"]
* ["mongo"]
*/
public function can_configure_database_resetter_if_doctrine_enabled(): void
public function cannot_configure_database_resetter_if_doctrine_not_enabled(string $doctrine): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf('should be enabled to use config under "%s.reset"', $doctrine));

$this->load([$doctrine => ['reset' => []]]);
}

/**
* @test
* @group legacy
*/
public function can_configure_legacy_database_resetter_if_doctrine_enabled(): void
{
$this->setParameter('kernel.bundles', [DoctrineBundle::class, DoctrineMongoDBBundle::class]);

Expand All @@ -249,6 +272,25 @@ public function can_configure_database_resetter_if_doctrine_enabled(): void
$this->assertSame(['object_manager_odm'], $configurationArguments['$odmObjectManagersToReset']);
}

/**
* @test
*/
public function can_configure_database_resetter_if_doctrine_enabled(): void
{
$this->setParameter('kernel.bundles', [DoctrineBundle::class, DoctrineMongoDBBundle::class]);

$this->load([
'orm' => ['reset' => ['connections' => ['orm_connection'], 'entity_managers' => ['object_manager_orm'], 'mode' => 'migrate']],
'mongo' => ['reset' => ['document_managers' => ['object_manager_odm']]],
]);

$configurationArguments = $this->container->getDefinition('.zenstruck_foundry.configuration')->getArguments();
$this->assertSame(['orm_connection'], $configurationArguments['$ormConnectionsToReset']);
$this->assertSame(['object_manager_orm'], $configurationArguments['$ormObjectManagersToReset']);
$this->assertSame('migrate', $configurationArguments['$ormResetMode']);
$this->assertSame(['object_manager_odm'], $configurationArguments['$odmObjectManagersToReset']);
}

/**
* @return ZenstruckFoundryExtension[]
*/
Expand Down