|
10 | 10 | use Doctrine\Bundle\MongoDBBundle\Attribute\MapDocument; |
11 | 11 | use Doctrine\Bundle\MongoDBBundle\Command\Encryption\DiagnosticCommand; |
12 | 12 | use Doctrine\Bundle\MongoDBBundle\Command\Encryption\DumpFieldsMapCommand; |
| 13 | +use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass; |
13 | 14 | use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass; |
14 | 15 | use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension; |
| 16 | +use Doctrine\Bundle\MongoDBBundle\ManagerRegistry; |
| 17 | +use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\AttributesBundle\Document\TestDocument; |
15 | 18 | use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle\EventListener\TestAttributeListener; |
16 | 19 | use Doctrine\ODM\MongoDB\Configuration; |
| 20 | +use Doctrine\ODM\MongoDB\DocumentManager; |
17 | 21 | use Doctrine\ODM\MongoDB\Mapping\Annotations; |
18 | 22 | use InvalidArgumentException; |
19 | 23 | use MongoDB\Client; |
20 | 24 | use PHPUnit\Framework\Attributes\DataProvider; |
21 | 25 | use PHPUnit\Framework\TestCase; |
| 26 | +use ProxyManager\Proxy\GhostObjectInterface; |
22 | 27 | use stdClass; |
23 | 28 | use Symfony\Component\DependencyInjection\Alias; |
24 | 29 | use Symfony\Component\DependencyInjection\ChildDefinition; |
| 30 | +use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass; |
25 | 31 | use Symfony\Component\DependencyInjection\Container; |
26 | 32 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
27 | 33 | use Symfony\Component\DependencyInjection\Definition; |
28 | 34 | use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
29 | 35 | use Symfony\Component\DependencyInjection\Reference; |
| 36 | +use Symfony\Component\VarExporter\LazyGhostTrait; |
30 | 37 |
|
31 | 38 | use function array_diff_key; |
32 | 39 | use function array_merge; |
| 40 | +use function interface_exists; |
33 | 41 | use function is_dir; |
34 | 42 | use function method_exists; |
35 | 43 | use function sprintf; |
36 | 44 | use function sys_get_temp_dir; |
| 45 | +use function trait_exists; |
37 | 46 |
|
| 47 | +use const PHP_VERSION_ID; |
| 48 | + |
| 49 | +/** |
| 50 | + * @phpstan-type ConfigurationArray array{ |
| 51 | + * enable_lazy_ghost_objects?: bool, |
| 52 | + * enable_native_lazy_objects?: bool, |
| 53 | + * } |
| 54 | + */ |
38 | 55 | class DoctrineMongoDBExtensionTest extends TestCase |
39 | 56 | { |
40 | 57 | public static function buildConfiguration(array $settings = []): array |
@@ -716,6 +733,63 @@ public function testAutoEncryptionMinimumODMVersion(): void |
716 | 733 | $loader->load([$config], $container); |
717 | 734 | } |
718 | 735 |
|
| 736 | + /** @phpstan-param ConfigurationArray $config */ |
| 737 | + #[DataProvider('provideLazyObjectConfigurations')] |
| 738 | + public function testRegistryGetManagerForClass(array $config): void |
| 739 | + { |
| 740 | + $container = $this->getContainer('AttributesBundle'); |
| 741 | + $loader = new DoctrineMongoDBExtension(); |
| 742 | + $loader->load(self::buildConfiguration($config + [ |
| 743 | + 'connections' => [ |
| 744 | + 'default' => [], |
| 745 | + ], |
| 746 | + 'document_managers' => [ |
| 747 | + 'default' => [ |
| 748 | + 'mappings' => [ |
| 749 | + 'AttributesBundle' => ['type' => 'attribute'], |
| 750 | + ], |
| 751 | + ], |
| 752 | + ], |
| 753 | + ]), $container); |
| 754 | + (new ResolveParameterPlaceHoldersPass())->process($container); |
| 755 | + (new ServiceRepositoryCompilerPass())->process($container); |
| 756 | + (new CreateProxyDirectoryPass())->process($container); |
| 757 | + |
| 758 | + $container->compile(); |
| 759 | + $registry = $container->get('doctrine_mongodb'); |
| 760 | + $dm = $container->get('doctrine_mongodb.odm.document_manager'); |
| 761 | + |
| 762 | + self::assertInstanceOf(ManagerRegistry::class, $registry); |
| 763 | + self::assertInstanceOf(DocumentManager::class, $dm); |
| 764 | + |
| 765 | + // Create a lazy object |
| 766 | + $ref = $dm->getReference(TestDocument::class, 'some'); |
| 767 | + self::assertInstanceOf(TestDocument::class, $ref); |
| 768 | + self::assertSame($dm, $registry->getManagerForClass($ref::class), 'The manager is found for the proxy document class'); |
| 769 | + } |
| 770 | + |
| 771 | + /** @phpstan-return iterable<ConfigurationArray> */ |
| 772 | + public static function provideLazyObjectConfigurations(): iterable |
| 773 | + { |
| 774 | + if (interface_exists(GhostObjectInterface::class)) { |
| 775 | + yield 'Proxy Manager' => [ |
| 776 | + ['enable_lazy_ghost_objects' => false, 'enable_native_lazy_objects' => false], |
| 777 | + ]; |
| 778 | + } |
| 779 | + |
| 780 | + if (trait_exists(LazyGhostTrait::class) && method_exists(Configuration::class, 'setUseLazyGhostObject')) { |
| 781 | + yield 'Symfony Lazy Objects' => [ |
| 782 | + ['enable_lazy_ghost_objects' => true, 'enable_native_lazy_objects' => false], |
| 783 | + ]; |
| 784 | + } |
| 785 | + |
| 786 | + if (PHP_VERSION_ID >= 80400 && method_exists(Configuration::class, 'setUseNativeLazyObject')) { |
| 787 | + yield 'Native Lazy Objects' => [ |
| 788 | + ['enable_lazy_ghost_objects' => false, 'enable_native_lazy_objects' => true], |
| 789 | + ]; |
| 790 | + } |
| 791 | + } |
| 792 | + |
719 | 793 | private static function requireAutoEncryptionSupportInODM(): void |
720 | 794 | { |
721 | 795 | if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/mongodb-odm', '>=2.12@dev')) { |
|
0 commit comments