From e10865e032d5e3f84884e6090327ed6bb87e8cee Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Fri, 24 Nov 2023 07:56:27 +0100 Subject: [PATCH] refactor: deprecate config database_resetter (#523) * refactor: deprecate config database_resetter.orm in favor of orm.reset * refactor: deprecate config database_resetter.odm in favor of mongo.reset --- .../DependencyInjection/Configuration.php | 43 ++++++++++++++ .../ZenstruckFoundryExtension.php | 35 +++++++---- tests/Fixtures/Kernel.php | 2 +- .../ZenstruckFoundryExtensionTest.php | 58 ++++++++++++++++--- 4 files changed, 119 insertions(+), 19 deletions(-) diff --git a/src/Bundle/DependencyInjection/Configuration.php b/src/Bundle/DependencyInjection/Configuration.php index c1016fa3..8e56f439 100644 --- a/src/Bundle/DependencyInjection/Configuration.php +++ b/src/Bundle/DependencyInjection/Configuration.php @@ -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.') diff --git a/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php b/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php index ede98733..536ccd2c 100644 --- a/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php +++ b/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php @@ -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 @@ -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']) { @@ -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 diff --git a/tests/Fixtures/Kernel.php b/tests/Fixtures/Kernel.php index 7b4a8749..ddaa8474 100644 --- a/tests/Fixtures/Kernel.php +++ b/tests/Fixtures/Kernel.php @@ -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')) { diff --git a/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php b/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php index 7d33d1a5..32d57aa7 100644 --- a/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php +++ b/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php @@ -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']); } /** @@ -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'); @@ -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]); @@ -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[] */