diff --git a/docs/config.rst b/docs/config.rst index 0a81c71a..df881e3d 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -555,6 +555,7 @@ Full Default Configuration host: ~ port: ~ instance_class: ~ + use_transactional_flush: false mappings: # Prototype diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 92da7cea..83f05aa9 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -196,6 +196,7 @@ private function addDocumentManagersSection(ArrayNodeDefinition $rootNode): void ->scalarNode('namespace')->end() ->end() ->end() + ->booleanNode('use_transactional_flush')->defaultFalse()->end() ->end() ->fixXmlConfig('mapping') ->children() diff --git a/src/DependencyInjection/DoctrineMongoDBExtension.php b/src/DependencyInjection/DoctrineMongoDBExtension.php index 18b3476b..b2909c7a 100644 --- a/src/DependencyInjection/DoctrineMongoDBExtension.php +++ b/src/DependencyInjection/DoctrineMongoDBExtension.php @@ -45,6 +45,7 @@ use function in_array; use function interface_exists; use function is_dir; +use function method_exists; use function sprintf; /** @@ -251,6 +252,10 @@ protected function loadDocumentManager(array $documentManager, string|null $defa 'setAutoGeneratePersistentCollectionClasses' => '%doctrine_mongodb.odm.auto_generate_persistent_collection_classes%', ]; + if (method_exists(ODMConfiguration::class, 'setUseTransactionalFlush')) { + $methods['setUseTransactionalFlush'] = $documentManager['use_transactional_flush']; + } + if ($documentManager['repository_factory']) { $methods['setRepositoryFactory'] = new Reference($documentManager['repository_factory']); } diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 5cbff3ad..d007ab36 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -169,6 +169,7 @@ public function testFullConfiguration(array $config): void 'enabled' => true, 'pretty' => false, ], + 'use_transactional_flush' => false, ], 'dm2' => [ 'connection' => 'dm2_connection', @@ -195,6 +196,7 @@ public function testFullConfiguration(array $config): void 'enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%', ], + 'use_transactional_flush' => false, ], ], 'resolve_target_documents' => ['Foo\BarInterface' => 'Bar\FooClass'], @@ -285,7 +287,7 @@ public static function provideMergeOptions(): array ['document_managers' => ['default' => ['mappings' => ['foomap' => ['type' => 'val1'], 'barmap' => ['dir' => 'val2']]]]], ['document_managers' => ['default' => ['mappings' => ['barmap' => ['prefix' => 'val3']]]]], ], - ['document_managers' => ['default' => ['metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null, 'filters' => [], 'mappings' => ['foomap' => ['type' => 'val1', 'mapping' => true], 'barmap' => ['prefix' => 'val3', 'mapping' => true]]]]], + ['document_managers' => ['default' => ['metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null, 'filters' => [], 'mappings' => ['foomap' => ['type' => 'val1', 'mapping' => true], 'barmap' => ['prefix' => 'val3', 'mapping' => true]], 'use_transactional_flush' => false]]], ]; // connections are merged non-recursively. @@ -336,8 +338,8 @@ public static function provideMergeOptions(): array ], [ 'document_managers' => [ - 'foodm' => ['database' => 'val1', 'metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null, 'filters' => [], 'mappings' => []], - 'bardm' => ['database' => 'val3', 'metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null, 'filters' => [], 'mappings' => []], + 'foodm' => ['database' => 'val1', 'metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null, 'filters' => [], 'mappings' => [], 'use_transactional_flush' => false], + 'bardm' => ['database' => 'val3', 'metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null, 'filters' => [], 'mappings' => [], 'use_transactional_flush' => false], ], ], ]; @@ -393,8 +395,8 @@ public static function provideNormalizeOptions(): array ], [ 'document_managers' => [ - 'foo' => ['connection' => 'conn1', 'metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null, 'filters' => [], 'mappings' => []], - 'bar' => ['connection' => 'conn2', 'metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null,'filters' => [], 'mappings' => []], + 'foo' => ['connection' => 'conn1', 'metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null, 'filters' => [], 'mappings' => [], 'use_transactional_flush' => false], + 'bar' => ['connection' => 'conn2', 'metadata_cache_driver' => ['type' => 'array'], 'logging' => '%kernel.debug%', 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'default_document_repository_class' => DocumentRepository::class, 'default_gridfs_repository_class' => DefaultGridFSRepository::class, 'repository_factory' => 'doctrine_mongodb.odm.container_repository_factory', 'persistent_collection_factory' => null,'filters' => [], 'mappings' => [], 'use_transactional_flush' => false], ], ], ]; @@ -427,6 +429,7 @@ public static function provideNormalizeOptions(): array 'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'], 'auto_mapping' => false, 'filters' => [], + 'use_transactional_flush' => false, ], ], ], diff --git a/tests/DependencyInjection/DoctrineMongoDBExtensionTest.php b/tests/DependencyInjection/DoctrineMongoDBExtensionTest.php index ee14748c..049345ee 100644 --- a/tests/DependencyInjection/DoctrineMongoDBExtensionTest.php +++ b/tests/DependencyInjection/DoctrineMongoDBExtensionTest.php @@ -4,6 +4,8 @@ namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection; +use Composer\InstalledVersions; +use Composer\Semver\VersionParser; use Doctrine\Bundle\MongoDBBundle\Attribute\MapDocument; use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension; use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle\EventListener\TestAttributeListener; @@ -369,4 +371,70 @@ public function testControllerResolver(): void $container->compile(); $this->assertEquals(new MapDocument(null, null, null, [], null, null, null, true), $container->get('controller_resolver_defaults')); } + + public function testTransactionalFlushConfigurationWhenNotSupported(): void + { + if (InstalledVersions::satisfies(new VersionParser(), 'doctrine/mongodb-odm', '>=2.7@dev')) { + $this->markTestSkipped('Installed version of doctrine/mongodb-odm supports transactional flushes'); + } + + $container = $this->buildMinimalContainer(); + $container->setParameter('kernel.debug', false); + $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); + $loader = new DoctrineMongoDBExtension(); + $loader->load(self::buildConfiguration(['document_managers' => ['default' => ['use_transactional_flush' => true]]]), $container); + + $configuration = $container->getDefinition('doctrine_mongodb.odm.default_configuration'); + + $this->assertFalse($configuration->hasMethodCall('setUseTransactionalFlush'), 'setUseTransactionalFlush is not called'); + } + + public function testDefaultTransactionalFlush(): void + { + if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/mongodb-odm', '>=2.7@dev')) { + $this->markTestSkipped('Installed version of doctrine/mongodb-odm does not support transactional flushes'); + } + + $container = $this->buildMinimalContainer(); + $container->setParameter('kernel.debug', false); + $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); + $loader = new DoctrineMongoDBExtension(); + $loader->load(self::buildConfiguration(), $container); + + $configuration = $container->getDefinition('doctrine_mongodb.odm.default_configuration'); + + $this->assertContains( + [ + 'setUseTransactionalFlush', + [false], + ], + $configuration->getMethodCalls(), + ); + } + + public function testUseTransactionalFlush(): void + { + if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/mongodb-odm', '>=2.7@dev')) { + $this->markTestSkipped('Installed version of doctrine/mongodb-odm does not support transactional flushes'); + } + + $container = $this->buildMinimalContainer(); + $container->setParameter('kernel.debug', false); + $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); + $loader = new DoctrineMongoDBExtension(); + $loader->load(self::buildConfiguration(['document_managers' => ['default' => ['use_transactional_flush' => true]]]), $container); + + $configuration = $container->getDefinition('doctrine_mongodb.odm.default_configuration'); + + $this->assertContains( + [ + 'setUseTransactionalFlush', + [true], + ], + $configuration->getMethodCalls(), + ); + } }