Skip to content

Commit

Permalink
Deprecate EntityManager::create()
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Aug 4, 2022
1 parent 33f4db8 commit c58da45
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 28 deletions.
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
21 changes: 21 additions & 0 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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());
}
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<referencedMethod name="Doctrine\ORM\Internal\Hydration\AbstractHydrator::hydrateRow"/>
<referencedMethod name="Doctrine\ORM\Configuration::ensureProductionSettings"/>
<referencedMethod name="Doctrine\ORM\Configuration::newDefaultAnnotationDriver"/>
<referencedMethod name="Doctrine\ORM\EntityManager::createConnection"/>
<referencedMethod name="Doctrine\ORM\Id\AbstractIdGenerator::generate"/>
<referencedMethod name="Doctrine\ORM\ORMInvalidArgumentException::invalidEntityName"/>
<referencedMethod name="Doctrine\ORM\Query\TreeWalkerAdapter::_getQueryComponents"/>
Expand Down
10 changes: 4 additions & 6 deletions tests/Doctrine/Performance/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
);

Expand Down Expand Up @@ -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);
}
}
22 changes: 14 additions & 8 deletions tests/Doctrine/Tests/Mocks/EntityManagerMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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);
}
}
17 changes: 16 additions & 1 deletion tests/Doctrine/Tests/ORM/EntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function createEntityManager(Connection $conn): EntityManagerInterface

$config->setQueryCache(new ArrayAdapter());

return EntityManager::create($conn, $config);
return new EntityManager($conn, $config);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/ORM/Functional/MergeProxiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3634Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/ORM/PersistentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/OrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c58da45

Please sign in to comment.