Skip to content

Commit

Permalink
Add tests on event dispatcher injection
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 9, 2024
1 parent affbab3 commit f781f60
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,6 +37,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use RuntimeException;
use stdClass;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class DocumentManagerTest extends BaseTestCase
{
Expand Down Expand Up @@ -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);

Check failure on line 284 in tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 space after comma in argument list; 2 found
self::assertSame($eventDispatcher, $dm->getEventDispatcher());

self::expectException(\LogicException::class);

Check failure on line 287 in tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Class \LogicException should not be referenced via a fully qualified name, but via a use statement.
self::expectExceptionMessage('Use getEventDispatcher() instead of getEventManager()');
$dm->getEventManager();
}
}

#[ODM\Document]
Expand Down

0 comments on commit f781f60

Please sign in to comment.