From b6661ff891dafd939ffda1f4d2ce2ddf4d48b0f9 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Sat, 18 Nov 2023 10:58:30 +0100 Subject: [PATCH 1/2] refactor: deprecate config database_resetter.orm in favor of orm.reset --- .../DependencyInjection/Configuration.php | 31 +++++++++++ .../ZenstruckFoundryExtension.php | 27 ++++++--- tests/Fixtures/Kernel.php | 2 +- .../ZenstruckFoundryExtensionTest.php | 55 ++++++++++++++++--- 4 files changed, 99 insertions(+), 16 deletions(-) diff --git a/src/Bundle/DependencyInjection/Configuration.php b/src/Bundle/DependencyInjection/Configuration.php index c1016fa3..0e63505a 100644 --- a/src/Bundle/DependencyInjection/Configuration.php +++ b/src/Bundle/DependencyInjection/Configuration.php @@ -94,7 +94,38 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ->end() + ->arrayNode('orm') + // could be activated in 2.0 + //->addDefaultsIfNotSet() + ->children() +// ->booleanNode('auto_persist') +// ->info('Automatically persist entities when created.') +// ->defaultTrue() +// ->end() + ->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('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..7fa38620 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,30 @@ 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)) { + 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 "database_resetter.orm".'); } - if (isset($config['odm']) && !self::isBundleLoaded($container, DoctrineMongoDBBundle::class)) { + if (isset($legacyConfig['odm']) && !self::isBundleLoaded($container, DoctrineMongoDBBundle::class)) { throw new \InvalidArgumentException('doctrine/mongodb-odm-bundle should be enabled to use config under "database_resetter.odm".'); } - $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'] ?? []); } 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..5db57dfe 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(['object_manager_odm'], $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,10 +225,11 @@ 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)); @@ -230,8 +239,21 @@ public function cannot_configure_database_resetter_if_doctrine_not_enabled(strin /** * @test + * @testWith ["orm"] */ - 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 "database_resetter.%s"', $doctrine)); + + $this->load([$doctrine => []]); + } + + /** + * @test + * @group legacy + */ + public function can_configure_legacy_database_resetter_if_doctrine_enabled(): void { $this->setParameter('kernel.bundles', [DoctrineBundle::class, DoctrineMongoDBBundle::class]); @@ -249,6 +271,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' => ['object_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[] */ From a9fadb3c799570e0028d9e69c9b9af9b01eb2d0b Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Sat, 18 Nov 2023 11:48:49 +0100 Subject: [PATCH 2/2] refactor: deprecate config database_resetter.odm in favor of mongo.reset --- .../DependencyInjection/Configuration.php | 20 +++++++++++++++---- .../ZenstruckFoundryExtension.php | 12 +++++++---- .../ZenstruckFoundryExtensionTest.php | 13 ++++++------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Bundle/DependencyInjection/Configuration.php b/src/Bundle/DependencyInjection/Configuration.php index 0e63505a..8e56f439 100644 --- a/src/Bundle/DependencyInjection/Configuration.php +++ b/src/Bundle/DependencyInjection/Configuration.php @@ -98,10 +98,6 @@ public function getConfigTreeBuilder(): TreeBuilder // could be activated in 2.0 //->addDefaultsIfNotSet() ->children() -// ->booleanNode('auto_persist') -// ->info('Automatically persist entities when created.') -// ->defaultTrue() -// ->end() ->arrayNode('reset') ->addDefaultsIfNotSet() ->children() @@ -124,6 +120,22 @@ public function getConfigTreeBuilder(): TreeBuilder ->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() diff --git a/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php b/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php index 7fa38620..536ccd2c 100644 --- a/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php +++ b/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php @@ -120,17 +120,21 @@ private function configureDatabaseResetter(array $config, ContainerBuilder $cont } 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 "database_resetter.orm".'); + throw new \InvalidArgumentException('doctrine/doctrine-bundle should be enabled to use config under "orm.reset".'); } - if (isset($legacyConfig['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'])) { + 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($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', $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'] ?? []); + $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/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php b/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php index 5db57dfe..32d57aa7 100644 --- a/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php +++ b/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php @@ -62,7 +62,7 @@ public function default_config(): void $this->assertSame(['default'], $configurationArguments['$ormConnectionsToReset']); $this->assertSame(['default'], $configurationArguments['$ormObjectManagersToReset']); $this->assertSame('schema', $configurationArguments['$ormResetMode']); -// $this->assertSame(['object_manager_odm'], $configurationArguments['$odmObjectManagersToReset']); + $this->assertSame(['default'], $configurationArguments['$odmObjectManagersToReset']); } /** @@ -232,7 +232,7 @@ public function it_registers_makers_if_maker_bundle_enabled(): 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 => []]]); } @@ -240,13 +240,14 @@ public function cannot_configure_legacy_database_resetter_if_doctrine_not_enable /** * @test * @testWith ["orm"] + * ["mongo"] */ 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 "database_resetter.%s"', $doctrine)); + $this->expectExceptionMessage(\sprintf('should be enabled to use config under "%s.reset"', $doctrine)); - $this->load([$doctrine => []]); + $this->load([$doctrine => ['reset' => []]]); } /** @@ -280,14 +281,14 @@ public function can_configure_database_resetter_if_doctrine_enabled(): void $this->load([ 'orm' => ['reset' => ['connections' => ['orm_connection'], 'entity_managers' => ['object_manager_orm'], 'mode' => 'migrate']], -// 'mongo' => ['object_managers' => ['object_manager_odm']], + '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']); + $this->assertSame(['object_manager_odm'], $configurationArguments['$odmObjectManagersToReset']); } /**