diff --git a/src/EntityManager.php b/src/EntityManager.php index bb9e2ad2ce..8045ac2f5e 100644 --- a/src/EntityManager.php +++ b/src/EntityManager.php @@ -63,27 +63,27 @@ class EntityManager implements EntityManagerInterface /** * The metadata factory, used to retrieve the ORM metadata of entity classes. */ - private readonly ClassMetadataFactory $metadataFactory; + private ClassMetadataFactory $metadataFactory; /** * The UnitOfWork used to coordinate object-level transactions. */ - private readonly UnitOfWork $unitOfWork; + private UnitOfWork $unitOfWork; /** * The event manager that is the central point of the event system. */ - private readonly EventManager $eventManager; + private EventManager $eventManager; /** * The proxy factory used to create dynamic proxies. */ - private readonly ProxyFactory $proxyFactory; + private ProxyFactory $proxyFactory; /** * The repository factory used to create dynamic repositories. */ - private readonly RepositoryFactory $repositoryFactory; + private RepositoryFactory $repositoryFactory; /** * The expression builder instance used to generate query expressions. @@ -112,8 +112,8 @@ class EntityManager implements EntityManagerInterface * @param Connection $conn The database connection used by the EntityManager. */ public function __construct( - private readonly Connection $conn, - private readonly Configuration $config, + private Connection $conn, + private Configuration $config, EventManager|null $eventManager = null, ) { if (! $config->getMetadataDriverImpl()) { diff --git a/tests/Tests/ORM/EntityManagerTest.php b/tests/Tests/ORM/EntityManagerTest.php index 067b1c6874..501f86550c 100644 --- a/tests/Tests/ORM/EntityManagerTest.php +++ b/tests/Tests/ORM/EntityManagerTest.php @@ -7,6 +7,7 @@ use Doctrine\Common\EventManager; use Doctrine\DBAL\Connection; use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Exception\EntityManagerClosed; use Doctrine\ORM\Mapping\ClassMetadataFactory; @@ -21,7 +22,9 @@ use Generator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; +use ReflectionProperty; use stdClass; +use Symfony\Component\VarExporter\LazyGhostTrait; use TypeError; class EntityManagerTest extends OrmTestCase @@ -172,4 +175,36 @@ public function testWrapInTransactionReThrowsThrowables(): void self::assertFalse($this->entityManager->isOpen()); } } + + /** Resetting the EntityManager relies on lazy objects until https://github.com/doctrine/orm/issues/5933 is resolved */ + public function testLazyGhostEntityManager(): void + { + $em = new class () extends EntityManager { + use LazyGhostTrait; + + public function __construct() + { + } + }; + + $em = $em::createLazyGhost(static function ($em): void { + $r = new ReflectionProperty(EntityManager::class, 'unitOfWork'); + $r->setValue($em, new class () extends UnitOfWork { + public function __construct() + { + } + + public function clear(): void + { + } + }); + }); + + $this->assertTrue($em->isOpen()); + $em->close(); + $this->assertFalse($em->isOpen()); + + $em->resetLazyObject(); + $this->assertTrue($em->isOpen()); + } }