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

Expose transaction setting #814

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@
'setAutoGeneratePersistentCollectionClasses' => '%doctrine_mongodb.odm.auto_generate_persistent_collection_classes%',
];

if (method_exists(ODMConfiguration::class, 'setUseTransactionalFlush')) {
$methods['setUseTransactionalFlush'] = $documentManager['use_transactional_flush'];

Check warning on line 256 in src/DependencyInjection/DoctrineMongoDBExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/DoctrineMongoDBExtension.php#L256

Added line #L256 was not covered by tests
}

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
69 changes: 69 additions & 0 deletions tests/DependencyInjection/DoctrineMongoDBExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection;

use Doctrine\Bundle\MongoDBBundle\Attribute\MapDocument;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Configuration;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle\EventListener\TestAttributeListener;
use Doctrine\ODM\MongoDB\Configuration as ODMConfiguration;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\DependencyInjection\Alias;
Expand All @@ -21,6 +23,7 @@
use function class_exists;
use function interface_exists;
use function is_dir;
use function method_exists;
use function sys_get_temp_dir;

class DoctrineMongoDBExtensionTest extends TestCase
Expand Down Expand Up @@ -369,4 +372,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 (method_exists(ODMConfiguration::class, 'setUseTransactionalFlush')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a weak test. If the method is renamed, the test is skipped while it should fail.

I think it would be more robust to use Composer\InstalledVersions:

Composer\InstalledVersions::satisfies(new VersionParser, 'doctrine/mongodb-odm', '>=2.7@dev')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. I wasn't even aware this existed. This is simply lovely :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see 3 skipped tests, is the version contraint correct?

$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 (! method_exists(Configuration::class, 'setUseTransactionalFlush')) {
$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 (! method_exists(Configuration::class, 'setUseTransactionalFlush')) {
$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(),
);
}
}