Skip to content

Commit

Permalink
Make EntityManager @final and its constructor public
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jul 27, 2022
1 parent c125a85 commit 45e1cd6
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 20 deletions.
18 changes: 10 additions & 8 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@
* is not a valid extension point for the EntityManager. Instead you
* should take a look at the {@see \Doctrine\ORM\Decorator\EntityManagerDecorator}
* and wrap your entity manager in a decorator.
*
* @final
*/
/* final */class EntityManager implements EntityManagerInterface
class EntityManager implements EntityManagerInterface
{
/**
* The used Configuration.
Expand Down Expand Up @@ -154,11 +156,15 @@
* Creates a new EntityManager that operates on the given database connection
* and uses the given Configuration and EventManager implementations.
*/
protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager)
public function __construct(Connection $conn, Configuration $config)
{
if (! $config->getMetadataDriverImpl()) {
throw MissingMappingDriverImplementation::create();
}

$this->conn = $conn;
$this->config = $config;
$this->eventManager = $eventManager;
$this->eventManager = $conn->getEventManager();

$metadataFactoryClassName = $config->getClassMetadataFactoryName();

Expand Down Expand Up @@ -958,13 +964,9 @@ public function initializeObject($obj)
*/
public static function create($connection, Configuration $config, ?EventManager $eventManager = null)
{
if (! $config->getMetadataDriverImpl()) {
throw MissingMappingDriverImplementation::create();
}

$connection = static::createConnection($connection, $config, $eventManager);

return new EntityManager($connection, $config, $connection->getEventManager());
return new EntityManager($connection, $config);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Performance/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ public function executeQuery(string $sql, array $params = [], $types = [], ?Quer
}
};

return EntityManager::create($connection, $config);
return EntityManager::create($connection, $config, $connection->getEventManager());
}
}
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Mocks/EntityManagerMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function create($conn, ?Configuration $config = null, ?EventManage
}

if ($eventManager === null) {
$eventManager = new EventManager();
$eventManager = $conn->getEventManager();
}

return new EntityManagerMock($conn, $config, $eventManager);
Expand Down
8 changes: 7 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Configuration;
Expand Down Expand Up @@ -233,7 +234,12 @@ private function configureFilters(EntityManagerInterface $em): void
*/
private function getMockConnection(): Connection
{
return $this->createMock(Connection::class);
$connection = $this->createMock(Connection::class);

$connection->method('getEventManager')
->willReturn(new EventManager());

return $connection;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
Expand Down Expand Up @@ -31,6 +32,8 @@ public function testDQLDeferredEagerLoad(): void
$connection = $this->createMock(Connection::class);
$connection->method('getDatabasePlatform')
->willReturn($platform);
$connection->method('getEventManager')
->willReturn(new EventManager());

$em = new class (EntityManagerMock::create($connection)) extends EntityManagerDecorator {
/** @var int */
Expand Down
3 changes: 3 additions & 0 deletions tests/Doctrine/Tests/ORM/PersistentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Tests\ORM;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Result;
Expand Down Expand Up @@ -48,6 +49,8 @@ protected function setUp(): void
$connection = $this->createMock(Connection::class);
$connection->method('getDatabasePlatform')
->willReturn($platform);
$connection->method('getEventManager')
->willReturn(new EventManager());
$connection->method('executeQuery')
->willReturn($this->createMock(Result::class));

Expand Down
3 changes: 3 additions & 0 deletions tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Tests\ORM\Proxy;

use Closure;
use Doctrine\Common\EventManager;
use Doctrine\Common\Proxy\AbstractProxyFactory;
use Doctrine\Common\Proxy\Proxy;
use Doctrine\DBAL\Connection;
Expand Down Expand Up @@ -52,6 +53,8 @@ protected function setUp(): void
$connection = $this->createMock(Connection::class);
$connection->method('getDatabasePlatform')
->willReturn($platform);
$connection->method('getEventManager')
->willReturn(new EventManager());

$this->emMock = EntityManagerMock::create($connection);
$this->uowMock = new UnitOfWorkMock($this->emMock);
Expand Down
6 changes: 3 additions & 3 deletions tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ protected function createEntityManager(MappingDriver $metadataDriver): EntityMan
$connection = $this->createMock(Connection::class);
$connection->method('getDatabasePlatform')
->willReturn($platform);
$connection->method('getEventManager')
->willReturn(new EventManager());

$config = new Configuration();
$config->setProxyDir(__DIR__ . '/../../Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$eventManager = new EventManager();

$config->setMetadataDriverImpl($metadataDriver);

return EntityManagerMock::create($connection, $config, $eventManager);
return EntityManagerMock::create($connection, $config);
}

public function testTest(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ protected function createEntityManager($metadataDriver): EntityManagerMock
$connection = $this->createMock(Connection::class);
$connection->method('getDatabasePlatform')
->willReturn($platform);
$connection->method('getEventManager')
->willReturn(new EventManager());

$config = new Configuration();
$config->setProxyDir(__DIR__ . '/../../Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$eventManager = new EventManager();
$config->setMetadataDriverImpl($metadataDriver);

return EntityManagerMock::create($connection, $config, $eventManager);
return EntityManagerMock::create($connection, $config);
}

protected function createMetadataDriver(string $type, string $path): MappingDriver
Expand Down
6 changes: 2 additions & 4 deletions tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,9 @@ protected function setUp(): void
$driver->method('connect')
->willReturn($driverConnection);

$connection = new Connection([], $driver);

$this->_connectionMock = $connection;
$this->eventManager = $this->getMockBuilder(EventManager::class)->getMock();
$this->_emMock = EntityManagerMock::create($connection, null, $this->eventManager);
$this->_connectionMock = new Connection([], $driver, null, $this->eventManager);
$this->_emMock = EntityManagerMock::create($this->_connectionMock);
// SUT
$this->_unitOfWork = new UnitOfWorkMock($this->_emMock);
$this->_emMock->setUnitOfWork($this->_unitOfWork);
Expand Down

0 comments on commit 45e1cd6

Please sign in to comment.