From c58da45b074a63eda58f90c5f4601cc00612e0d7 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 4 Aug 2022 23:49:46 +0200 Subject: [PATCH] Deprecate EntityManager::create() --- UPGRADE.md | 7 ++++++ lib/Doctrine/ORM/EntityManager.php | 21 ++++++++++++++++++ psalm.xml | 1 + .../Performance/EntityManagerFactory.php | 10 ++++----- .../Tests/Mocks/EntityManagerMock.php | 22 ++++++++++++------- .../Doctrine/Tests/ORM/EntityManagerTest.php | 17 +++++++++++++- .../Functional/Locking/LockAgentWorker.php | 2 +- .../Tests/ORM/Functional/MergeProxiesTest.php | 2 +- .../ORM/Functional/Ticket/DDC3634Test.php | 2 +- .../ORM/Functional/Ticket/GH7869Test.php | 2 +- .../ORM/Mapping/ClassMetadataFactoryTest.php | 2 +- .../Tests/ORM/PersistentCollectionTest.php | 2 +- .../Tests/ORM/Proxy/ProxyFactoryTest.php | 2 +- .../ORM/Tools/ConvertDoctrine1SchemaTest.php | 2 +- .../Export/ClassMetadataExporterTestCase.php | 2 +- tests/Doctrine/Tests/ORM/UnitOfWorkTest.php | 4 ++-- .../Doctrine/Tests/OrmFunctionalTestCase.php | 2 +- tests/Doctrine/Tests/OrmTestCase.php | 2 +- 18 files changed, 76 insertions(+), 28 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index a367589573f..68ed48ed82c 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,12 @@ # Upgrade to 2.13 +## Deprecated `EntityManager::create()` + +The constructor of `EntityManager` is now public and should be used instead of the `create()` method. +However, the constructor expects a `Connection` while `create()` accepted an array with connection parameters. +You can pass that array to DBAL's `Doctrine\DBAL\DriverManager::getConnection()` method to bootstrap the +connection. + ## Deprecated `QueryBuilder` methods and constants. 1. The `QueryBuilder::getState()` method has been deprecated as the builder state is an internal concern. diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 3cc13bfb50d..ae9cb0c6bc1 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -952,6 +952,8 @@ public function initializeObject($obj) /** * Factory method to create EntityManager instances. * + * @deprecated Use {@see DriverManager::getConnection()} to bootstrap the connection and call the constructor. + * * @param mixed[]|Connection $connection An array with the connection parameters or an existing Connection instance. * @param Configuration $config The Configuration instance to use. * @param EventManager|null $eventManager The EventManager instance to use. @@ -964,6 +966,15 @@ public function initializeObject($obj) */ public static function create($connection, Configuration $config, ?EventManager $eventManager = null) { + Deprecation::trigger( + 'doctrine/orm', + 'TODO', + '%s() is deprecated. To boostrap a DBAL connection, call %s::getConnection() instead. Use the constructor to create an instance of %s.', + __METHOD__, + DriverManager::class, + self::class + ); + $connection = static::createConnection($connection, $config, $eventManager); return new EntityManager($connection, $config); @@ -972,6 +983,8 @@ public static function create($connection, Configuration $config, ?EventManager /** * Factory method to create Connection instances. * + * @deprecated Use {@see DriverManager::getConnection()} to bootstrap the connection. + * * @param mixed[]|Connection $connection An array with the connection parameters or an existing Connection instance. * @param Configuration $config The Configuration instance to use. * @param EventManager|null $eventManager The EventManager instance to use. @@ -984,6 +997,14 @@ public static function create($connection, Configuration $config, ?EventManager */ protected static function createConnection($connection, Configuration $config, ?EventManager $eventManager = null) { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/orm', + 'TODO', + '%s() is deprecated, call %s::getConnection() instead.', + __METHOD__, + DriverManager::class + ); + if (is_array($connection)) { return DriverManager::getConnection($connection, $config, $eventManager ?: new EventManager()); } diff --git a/psalm.xml b/psalm.xml index dd985cf7a64..386658c95cd 100644 --- a/psalm.xml +++ b/psalm.xml @@ -66,6 +66,7 @@ + diff --git a/tests/Doctrine/Performance/EntityManagerFactory.php b/tests/Doctrine/Performance/EntityManagerFactory.php index be81291cacf..0330ba785ec 100644 --- a/tests/Doctrine/Performance/EntityManagerFactory.php +++ b/tests/Doctrine/Performance/EntityManagerFactory.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver\PDO\SQLite\Driver; +use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Result; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManager; @@ -34,11 +35,8 @@ public static function getEntityManager(array $schemaClassNames): EntityManagerI realpath(__DIR__ . '/Models/GeoNames'), ])); - $entityManager = EntityManager::create( - [ - 'driverClass' => Driver::class, - 'memory' => true, - ], + $entityManager = new EntityManager( + DriverManager::getConnection(['driverClass' => Driver::class, 'memory' => true], $config), $config ); @@ -71,6 +69,6 @@ public function executeQuery(string $sql, array $params = [], $types = [], ?Quer } }; - return EntityManager::create($connection, $config, $connection->getEventManager()); + return new EntityManager($connection, $config); } } diff --git a/tests/Doctrine/Tests/Mocks/EntityManagerMock.php b/tests/Doctrine/Tests/Mocks/EntityManagerMock.php index ec59144f2ac..5072f6fbb28 100644 --- a/tests/Doctrine/Tests/Mocks/EntityManagerMock.php +++ b/tests/Doctrine/Tests/Mocks/EntityManagerMock.php @@ -5,6 +5,7 @@ namespace Doctrine\Tests\Mocks; use Doctrine\Common\EventManager; +use Doctrine\DBAL\Connection; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMSetup; @@ -22,6 +23,18 @@ class EntityManagerMock extends EntityManager /** @var ProxyFactory|null */ private $_proxyFactoryMock; + public function __construct(Connection $conn, ?Configuration $config = null) + { + if ($config === null) { + $config = new Configuration(); + $config->setProxyDir(__DIR__ . '/../Proxies'); + $config->setProxyNamespace('Doctrine\Tests\Proxies'); + $config->setMetadataDriverImpl(ORMSetup::createDefaultAnnotationDriver()); + } + + parent::__construct($conn, $config); + } + /** * {@inheritdoc} */ @@ -55,15 +68,8 @@ public function getProxyFactory(): ProxyFactory * * {@inheritdoc} */ - public static function create($conn, ?Configuration $config = null, ?EventManager $eventManager = null) + public static function create($conn, ?Configuration $config = null, ?EventManager $eventManager = null): self { - if ($config === null) { - $config = new Configuration(); - $config->setProxyDir(__DIR__ . '/../Proxies'); - $config->setProxyNamespace('Doctrine\Tests\Proxies'); - $config->setMetadataDriverImpl(ORMSetup::createDefaultAnnotationDriver()); - } - return new EntityManagerMock($conn, $config); } } diff --git a/tests/Doctrine/Tests/ORM/EntityManagerTest.php b/tests/Doctrine/Tests/ORM/EntityManagerTest.php index 9e69504de99..b558c7f2dbe 100644 --- a/tests/Doctrine/Tests/ORM/EntityManagerTest.php +++ b/tests/Doctrine/Tests/ORM/EntityManagerTest.php @@ -31,6 +31,7 @@ use function get_class; use function random_int; +use function sys_get_temp_dir; use function uniqid; class EntityManagerTest extends OrmTestCase @@ -274,12 +275,26 @@ public function transactionalCallback($em): string public function testCreateInvalidConnection(): void { + $config = new Configuration(); + $config->setMetadataDriverImpl($this->createMock(MappingDriver::class)); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid $connection argument of type int given: "1".'); + EntityManager::create(1, $config); + } + public function testNamedConstructorDeprecation(): void + { $config = new Configuration(); $config->setMetadataDriverImpl($this->createMock(MappingDriver::class)); - EntityManager::create(1, $config); + $config->setProxyDir(sys_get_temp_dir()); + $config->setProxyNamespace(__NAMESPACE__ . '\\MyProxies'); + + $this->expectDeprecationWithIdentifier('TODO'); + + $em = EntityManager::create(['driver' => 'pdo_sqlite', 'memory' => true], $config); + + self::assertInstanceOf(Connection::class, $em->getConnection()); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Locking/LockAgentWorker.php b/tests/Doctrine/Tests/ORM/Functional/Locking/LockAgentWorker.php index b933bc647d0..1d5429970fd 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Locking/LockAgentWorker.php +++ b/tests/Doctrine/Tests/ORM/Functional/Locking/LockAgentWorker.php @@ -125,7 +125,7 @@ protected function createEntityManager(Connection $conn): EntityManagerInterface $config->setQueryCache(new ArrayAdapter()); - return EntityManager::create($conn, $config); + return new EntityManager($conn, $config); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/MergeProxiesTest.php b/tests/Doctrine/Tests/ORM/Functional/MergeProxiesTest.php index 0f3edecaedd..cb512e18d7b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/MergeProxiesTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/MergeProxiesTest.php @@ -257,7 +257,7 @@ private function createEntityManager(): EntityManagerInterface $config ); - $entityManager = EntityManager::create($connection, $config); + $entityManager = new EntityManager($connection, $config); (new SchemaTool($entityManager))->createSchema([$entityManager->getClassMetadata(DateTimeModel::class)]); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3634Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3634Test.php index 226292446ff..61f0370ab78 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3634Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3634Test.php @@ -49,7 +49,7 @@ public function testSavesVeryLargeIntegerAutoGeneratedValue(): void { $veryLargeId = PHP_INT_MAX . PHP_INT_MAX; - $entityManager = EntityManager::create( + $entityManager = new EntityManager( new DDC3634LastInsertIdMockingConnection($veryLargeId, $this->_em->getConnection()), $this->_em->getConfiguration() ); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php index faf3332b504..d644519d1ee 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php @@ -35,7 +35,7 @@ public function testDQLDeferredEagerLoad(): void $connection->method('getEventManager') ->willReturn(new EventManager()); - $em = new class (EntityManagerMock::create($connection)) extends EntityManagerDecorator { + $em = new class (new EntityManagerMock($connection)) extends EntityManagerDecorator { /** @var int */ public $getClassMetadataCalls = 0; diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index 63694d43e8d..cf1914979eb 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -324,7 +324,7 @@ protected function createEntityManager(MappingDriver $metadataDriver, $conn = nu $config->setMetadataDriverImpl($metadataDriver); - return EntityManagerMock::create($conn, $config); + return new EntityManagerMock($conn, $config); } protected function createTestFactory(): ClassMetadataFactoryTestSubject diff --git a/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php b/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php index 4b0663c8d8b..b23d9dcceb3 100644 --- a/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php @@ -54,7 +54,7 @@ protected function setUp(): void $connection->method('executeQuery') ->willReturn($this->createMock(Result::class)); - $this->_emMock = EntityManagerMock::create($connection); + $this->_emMock = new EntityManagerMock($connection); $this->setUpPersistentCollection(); } diff --git a/tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php b/tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php index 0cd5ab42572..170238db06e 100644 --- a/tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php @@ -56,7 +56,7 @@ protected function setUp(): void $connection->method('getEventManager') ->willReturn(new EventManager()); - $this->emMock = EntityManagerMock::create($connection); + $this->emMock = new EntityManagerMock($connection); $this->uowMock = new UnitOfWorkMock($this->emMock); $this->emMock->setUnitOfWork($this->uowMock); $this->proxyFactory = new ProxyFactory($this->emMock, sys_get_temp_dir(), 'Proxies', AbstractProxyFactory::AUTOGENERATE_ALWAYS); diff --git a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php index 0b92773e2cb..d89218f821a 100644 --- a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php @@ -46,7 +46,7 @@ protected function createEntityManager(MappingDriver $metadataDriver): EntityMan $config->setProxyNamespace('Doctrine\Tests\Proxies'); $config->setMetadataDriverImpl($metadataDriver); - return EntityManagerMock::create($connection, $config); + return new EntityManagerMock($connection, $config); } public function testTest(): void diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/ClassMetadataExporterTestCase.php b/tests/Doctrine/Tests/ORM/Tools/Export/ClassMetadataExporterTestCase.php index aee26d6e333..bc187de6cc2 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Export/ClassMetadataExporterTestCase.php +++ b/tests/Doctrine/Tests/ORM/Tools/Export/ClassMetadataExporterTestCase.php @@ -66,7 +66,7 @@ protected function createEntityManager($metadataDriver): EntityManagerMock $config->setProxyNamespace('Doctrine\Tests\Proxies'); $config->setMetadataDriverImpl($metadataDriver); - return EntityManagerMock::create($connection, $config); + return new EntityManagerMock($connection, $config); } protected function createMetadataDriver(string $type, string $path): MappingDriver diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index df5c4f72875..8ee680f4cd6 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -116,7 +116,7 @@ protected function setUp(): void $this->eventManager = $this->getMockBuilder(EventManager::class)->getMock(); $this->_connectionMock = new Connection([], $driver, null, $this->eventManager); - $this->_emMock = EntityManagerMock::create($this->_connectionMock); + $this->_emMock = new EntityManagerMock($this->_connectionMock); // SUT $this->_unitOfWork = new UnitOfWorkMock($this->_emMock); $this->_emMock->setUnitOfWork($this->_unitOfWork); @@ -851,7 +851,7 @@ public function testCommitThrowOptimisticLockExceptionWhenConnectionCommitReturn $this->_connectionMock = $this->getMockBuilderWithOnlyMethods(ConnectionMock::class, ['commit']) ->setConstructorArgs([[], $driver]) ->getMock(); - $this->_emMock = EntityManagerMock::create($this->_connectionMock); + $this->_emMock = new EntityManagerMock($this->_connectionMock); $this->_unitOfWork = new UnitOfWorkMock($this->_emMock); $this->_emMock->setUnitOfWork($this->_unitOfWork); diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 25d5114a23d..96d63f69aba 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -834,7 +834,7 @@ protected function getEntityManager( $evm->addEventListener(['onFlush'], new DebugUnitOfWorkListener()); } - return EntityManager::create($conn, $config); + return new EntityManager($conn, $config); } final protected function createSchemaManager(): AbstractSchemaManager diff --git a/tests/Doctrine/Tests/OrmTestCase.php b/tests/Doctrine/Tests/OrmTestCase.php index 2a99c60aa2d..8cc988c6931 100644 --- a/tests/Doctrine/Tests/OrmTestCase.php +++ b/tests/Doctrine/Tests/OrmTestCase.php @@ -107,7 +107,7 @@ protected function getTestEntityManager(?Connection $connection = null): EntityM ], $config); } - return EntityManagerMock::create($connection, $config); + return new EntityManagerMock($connection, $config); } protected function enableSecondLevelCache(bool $log = true): void