Skip to content

Commit f781f60

Browse files
committed
Add tests on event dispatcher injection
1 parent affbab3 commit f781f60

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/Doctrine/ODM/MongoDB/DocumentManager.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ protected function __construct(?Client $client = null, ?Configuration $config =
193193
$hydratorNs = $this->config->getHydratorNamespace();
194194
$this->hydratorFactory = new HydratorFactory(
195195
$this,
196-
$this->eventManager,
196+
$this->eventDispatcher,
197197
$hydratorDir,
198198
$hydratorNs,
199199
$this->config->getAutoGenerateHydratorClasses(),
@@ -217,9 +217,9 @@ public function getProxyFactory(): ProxyFactory
217217
* Creates a new Document that operates on the given Mongo connection
218218
* and uses the given Configuration.
219219
*/
220-
public static function create(?Client $client = null, ?Configuration $config = null, EventManager|EventDispatcherInterface|null $eventManager = null): DocumentManager
220+
public static function create(?Client $client = null, ?Configuration $config = null, EventManager|EventDispatcherInterface|null $eventDispatcher = null): DocumentManager
221221
{
222-
return new static($client, $config, $eventManager);
222+
return new static($client, $config, $eventDispatcher);
223223
}
224224

225225
/**

tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\Common\EventManager;
99
use Doctrine\ODM\MongoDB\Aggregation\Builder as AggregationBuilder;
1010
use Doctrine\ODM\MongoDB\Configuration;
11+
use Doctrine\ODM\MongoDB\DocumentManager;
1112
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
1213
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1314
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
@@ -36,6 +37,7 @@
3637
use PHPUnit\Framework\Attributes\DataProvider;
3738
use RuntimeException;
3839
use stdClass;
40+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
3941

4042
class DocumentManagerTest extends BaseTestCase
4143
{
@@ -261,6 +263,31 @@ public function testGetClassNameForAssociationReturnsTargetDocumentWithNullData(
261263
$mapping = ClassMetadataTestUtil::getFieldMapping(['targetDocument' => User::class]);
262264
self::assertEquals(User::class, $this->dm->getClassNameForAssociation($mapping, null));
263265
}
266+
267+
public function testCreateWithEventManager(): void
268+
{
269+
$config = static::getConfiguration();
270+
$client = new Client(self::getUri());
271+
272+
$eventManager = new EventManager();
273+
$dm = DocumentManager::create($client, $config, $eventManager);
274+
self::assertSame($eventManager, $dm->getEventManager());
275+
self::assertInstanceOf(EventDispatcherInterface::class, $dm->getEventDispatcher());
276+
}
277+
278+
public function testCreateWithEventDispatcher(): void
279+
{
280+
$config = static::getConfiguration();
281+
$client = new Client(self::getUri());
282+
283+
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
284+
$dm = DocumentManager::create($client, $config, $eventDispatcher);
285+
self::assertSame($eventDispatcher, $dm->getEventDispatcher());
286+
287+
self::expectException(\LogicException::class);
288+
self::expectExceptionMessage('Use getEventDispatcher() instead of getEventManager()');
289+
$dm->getEventManager();
290+
}
264291
}
265292

266293
#[ODM\Document]

0 commit comments

Comments
 (0)