Skip to content

Commit 95dbccc

Browse files
committed
Fix proxy interface for ProxyManager or Symfony LazyGhost
1 parent de79766 commit 95dbccc

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/DependencyInjection/DoctrineMongoDBExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ public function load(array $configs, ContainerBuilder $container): void
487487
}
488488

489489
$container->getDefinition('doctrine_mongodb')
490-
->setArgument(5, $config['enable_lazy_ghost_objects'] ? LazyLoadingInterface::class : Proxy::class);
490+
->setArgument(5, $config['enable_lazy_ghost_objects'] ? Proxy::class : LazyLoadingInterface::class);
491491

492492
// load the connections
493493
$this->loadConnections($config['connections'], $container, $config);

tests/DependencyInjection/DoctrineMongoDBExtensionTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
use Doctrine\Bundle\MongoDBBundle\Attribute\MapDocument;
1111
use Doctrine\Bundle\MongoDBBundle\Command\Encryption\DiagnosticCommand;
1212
use Doctrine\Bundle\MongoDBBundle\Command\Encryption\DumpFieldsMapCommand;
13+
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
1314
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
1415
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
16+
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
17+
use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\AttributesBundle\Document\TestDocument;
1518
use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle\EventListener\TestAttributeListener;
1619
use Doctrine\ODM\MongoDB\Configuration;
20+
use Doctrine\ODM\MongoDB\DocumentManager;
1721
use Doctrine\ODM\MongoDB\Mapping\Annotations;
1822
use InvalidArgumentException;
1923
use MongoDB\Client;
@@ -22,6 +26,7 @@
2226
use stdClass;
2327
use Symfony\Component\DependencyInjection\Alias;
2428
use Symfony\Component\DependencyInjection\ChildDefinition;
29+
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
2530
use Symfony\Component\DependencyInjection\Container;
2631
use Symfony\Component\DependencyInjection\ContainerBuilder;
2732
use Symfony\Component\DependencyInjection\Definition;
@@ -716,6 +721,55 @@ public function testAutoEncryptionMinimumODMVersion(): void
716721
$loader->load([$config], $container);
717722
}
718723

724+
#[DataProvider('provideLazyObjectConfigurations')]
725+
public function testRegistryGetManagerForClass(array $config): void
726+
{
727+
$container = $this->getContainer('AttributesBundle');
728+
$loader = new DoctrineMongoDBExtension();
729+
$loader->load(self::buildConfiguration($config + [
730+
'connections' => [
731+
'default' => [],
732+
],
733+
'document_managers' => [
734+
'default' => [
735+
'mappings' => [
736+
'AttributesBundle' => ['type' => 'attribute'],
737+
],
738+
],
739+
],
740+
]), $container);
741+
(new ResolveParameterPlaceHoldersPass())->process($container);
742+
(new ServiceRepositoryCompilerPass())->process($container);
743+
(new CreateProxyDirectoryPass())->process($container);
744+
745+
$container->compile();
746+
$registry = $container->get('doctrine_mongodb');
747+
$dm = $container->get('doctrine_mongodb.odm.document_manager');
748+
749+
self::assertInstanceOf(ManagerRegistry::class, $registry);
750+
self::assertInstanceOf(DocumentManager::class, $dm);
751+
752+
// Create a lazy object
753+
$ref = $dm->getReference(TestDocument::class, 'some');
754+
self::assertInstanceOf(TestDocument::class, $ref);
755+
self::assertSame($dm, $registry->getManagerForClass($ref::class));
756+
}
757+
758+
public static function provideLazyObjectConfigurations(): iterable
759+
{
760+
yield 'Proxy Manager' => [
761+
['enable_lazy_ghost_objects' => false, 'enable_native_lazy_objects' => false],
762+
];
763+
764+
yield 'Symfony Lazy Objects' => [
765+
['enable_lazy_ghost_objects' => true, 'enable_native_lazy_objects' => false],
766+
];
767+
768+
yield 'Native Lazy Objects' => [
769+
['enable_lazy_ghost_objects' => false, 'enable_native_lazy_objects' => true],
770+
];
771+
}
772+
719773
private static function requireAutoEncryptionSupportInODM(): void
720774
{
721775
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/mongodb-odm', '>=2.12@dev')) {

tests/DependencyInjection/Fixtures/Bundles/AttributesBundle/Document/TestDocument.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@
99
#[MongoDB\Document]
1010
class TestDocument
1111
{
12+
#[MongoDB\Id]
13+
public ?string $id = null;
14+
15+
#[MongoDB\Field(type: 'string')]
16+
public ?string $name = null;
1217
}

0 commit comments

Comments
 (0)