Skip to content

Commit

Permalink
Expose transaction setting (#814)
Browse files Browse the repository at this point in the history
* Expose use_transactional_flush setting

* Add transaction setting to config reference

* Support versions of ODM without transaction support

* Use InstalledVersions to check for transaction support

* Fix phpcs violations
  • Loading branch information
alcaeus authored Jan 19, 2024
1 parent 9ceabb9 commit ee1e238
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ Full Default Configuration
host: ~
port: ~
instance_class: ~
use_transactional_flush: false
mappings:
# Prototype
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use function in_array;
use function interface_exists;
use function is_dir;
use function method_exists;
use function sprintf;

/**
Expand Down Expand Up @@ -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']);
}
Expand Down
13 changes: 8 additions & 5 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function testFullConfiguration(array $config): void
'enabled' => true,
'pretty' => false,
],
'use_transactional_flush' => false,
],
'dm2' => [
'connection' => 'dm2_connection',
Expand All @@ -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'],
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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],
],
],
];
Expand Down Expand Up @@ -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],
],
],
];
Expand Down Expand Up @@ -427,6 +429,7 @@ public static function provideNormalizeOptions(): array
'profiler' => ['enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'],
'auto_mapping' => false,
'filters' => [],
'use_transactional_flush' => false,
],
],
],
Expand Down
68 changes: 68 additions & 0 deletions tests/DependencyInjection/DoctrineMongoDBExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
);
}
}

0 comments on commit ee1e238

Please sign in to comment.