diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index 98401fa80..3ad53185a 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -193,7 +193,7 @@ protected function __construct(?Client $client = null, ?Configuration $config = $hydratorNs = $this->config->getHydratorNamespace(); $this->hydratorFactory = new HydratorFactory( $this, - $this->eventManager, + $this->eventDispatcher, $hydratorDir, $hydratorNs, $this->config->getAutoGenerateHydratorClasses(), @@ -217,9 +217,9 @@ public function getProxyFactory(): ProxyFactory * Creates a new Document that operates on the given Mongo connection * and uses the given Configuration. */ - public static function create(?Client $client = null, ?Configuration $config = null, EventManager|EventDispatcherInterface|null $eventManager = null): DocumentManager + public static function create(?Client $client = null, ?Configuration $config = null, EventManager|EventDispatcherInterface|null $eventDispatcher = null): DocumentManager { - return new static($client, $config, $eventManager); + return new static($client, $config, $eventDispatcher); } /** diff --git a/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php index 832037e53..1e1fff465 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php @@ -8,6 +8,7 @@ use Doctrine\Common\EventManager; use Doctrine\ODM\MongoDB\Aggregation\Builder as AggregationBuilder; use Doctrine\ODM\MongoDB\Configuration; +use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; @@ -36,6 +37,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use RuntimeException; use stdClass; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class DocumentManagerTest extends BaseTestCase { @@ -261,6 +263,31 @@ public function testGetClassNameForAssociationReturnsTargetDocumentWithNullData( $mapping = ClassMetadataTestUtil::getFieldMapping(['targetDocument' => User::class]); self::assertEquals(User::class, $this->dm->getClassNameForAssociation($mapping, null)); } + + public function testCreateWithEventManager(): void + { + $config = static::getConfiguration(); + $client = new Client(self::getUri()); + + $eventManager = new EventManager(); + $dm = DocumentManager::create($client, $config, $eventManager); + self::assertSame($eventManager, $dm->getEventManager()); + self::assertInstanceOf(EventDispatcherInterface::class, $dm->getEventDispatcher()); + } + + public function testCreateWithEventDispatcher(): void + { + $config = static::getConfiguration(); + $client = new Client(self::getUri()); + + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $dm = DocumentManager::create($client, $config, $eventDispatcher); + self::assertSame($eventDispatcher, $dm->getEventDispatcher()); + + self::expectException(\LogicException::class); + self::expectExceptionMessage('Use getEventDispatcher() instead of getEventManager()'); + $dm->getEventManager(); + } } #[ODM\Document]