Skip to content

Commit

Permalink
Support versions of ODM without transaction support
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jan 9, 2024
1 parent 553f933 commit 67463b6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion 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 @@ -249,9 +250,12 @@ protected function loadDocumentManager(array $documentManager, string|null $defa
'setPersistentCollectionDir' => '%doctrine_mongodb.odm.persistent_collection_dir%',
'setPersistentCollectionNamespace' => '%doctrine_mongodb.odm.persistent_collection_namespace%',
'setAutoGeneratePersistentCollectionClasses' => '%doctrine_mongodb.odm.auto_generate_persistent_collection_classes%',
'setUseTransactionalFlush' => $documentManager['use_transactional_flush'],
];

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
29 changes: 29 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 @@ -370,8 +373,30 @@ public function testControllerResolver(): void
$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')) {
$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', []);
Expand All @@ -392,6 +417,10 @@ public function testDefaultTransactionalFlush(): void

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', []);
Expand Down

0 comments on commit 67463b6

Please sign in to comment.