|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Bundle\MongoDBBundle\Tests; |
| 6 | + |
| 7 | +use Doctrine\Bundle\MongoDBBundle\ManagerRegistry; |
| 8 | +use Doctrine\ODM\MongoDB\DocumentManager; |
| 9 | +use Doctrine\Persistence\ObjectManager; |
| 10 | +use Symfony\Component\DependencyInjection\Container; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 12 | +use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
| 13 | + |
| 14 | +class ManagerRegistryTest extends TestCase |
| 15 | +{ |
| 16 | + public function testReset(): void |
| 17 | + { |
| 18 | + $container = new ContainerBuilder(); |
| 19 | + $container->register('manager.default', DocumentManagerStub::class) |
| 20 | + ->setPublic(true); |
| 21 | + $container->register('manager.lazy', DocumentManagerStub::class) |
| 22 | + ->setPublic(true) |
| 23 | + ->setLazy(true) |
| 24 | + ->addTag('proxy', ['interface' => ObjectManager::class]); |
| 25 | + $container->compile(); |
| 26 | + |
| 27 | + /** @var class-string<Container> $containerClass */ |
| 28 | + $containerClass = 'MongoDBManagerRepositoryTestResetContainer'; |
| 29 | + $dumper = new PhpDumper($container); |
| 30 | + eval('?' . '>' . $dumper->dump(['class' => $containerClass])); |
| 31 | + |
| 32 | + $container = new $containerClass(); |
| 33 | + $repository = new ManagerRegistry('MongoDB', [], [ |
| 34 | + 'default' => 'manager.default', |
| 35 | + 'lazy' => 'manager.lazy', |
| 36 | + ], '', '', '', $container); |
| 37 | + |
| 38 | + DocumentManagerStub::$clearCount = 0; |
| 39 | + |
| 40 | + $repository->reset(); |
| 41 | + |
| 42 | + // Service was not initialized, so reset should not be called |
| 43 | + $this->assertSame(0, DocumentManagerStub::$clearCount); |
| 44 | + |
| 45 | + // The lazy service is reinitialized instead of being cleared |
| 46 | + $container->get('manager.lazy')->flush(); |
| 47 | + $repository->reset(); |
| 48 | + $this->assertSame(0, DocumentManagerStub::$clearCount); |
| 49 | + |
| 50 | + // The default service is cleared when initialized |
| 51 | + $container->get('manager.default')->flush(); |
| 52 | + $repository->reset(); |
| 53 | + $this->assertSame(1, DocumentManagerStub::$clearCount); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class DocumentManagerStub extends DocumentManager |
| 58 | +{ |
| 59 | + public static int $clearCount; |
| 60 | + |
| 61 | + public function __construct() |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + /** {@inheritDoc} */ |
| 66 | + public function clear($objectName = null): void |
| 67 | + { |
| 68 | + self::$clearCount++; |
| 69 | + } |
| 70 | + |
| 71 | + public function flush(array $options = []): void |
| 72 | + { |
| 73 | + } |
| 74 | +} |
0 commit comments