Skip to content
This repository has been archived by the owner on Jan 27, 2020. It is now read-only.

Commit

Permalink
Using EntityManagerInterface instead of EntityManager
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Feb 26, 2019
1 parent 845e51f commit 949b5fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/DoctrinePersistentEntityManagerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
use DI\Container;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct(Container $container)
{
$this->container = $container;

if (! $this->container->has(EntityManager::class)) {
if (! $this->container->has(EntityManagerInterface::class)) {
throw new RuntimeException('The DI container must contain the EntityManager');
}
}
Expand All @@ -61,10 +62,10 @@ public function __construct(Container $container)
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$entityManager = $this->container->get(EntityManager::class);
$entityManager = $this->container->get(EntityManagerInterface::class);

if (! $entityManager->isOpen()) {
$this->container->set(EntityManager::class, function () use ($entityManager) {
$this->container->set(EntityManagerInterface::class, function () use ($entityManager) {
return EntityManager::create(
$entityManager->getConnection(),
$entityManager->getConfiguration(),
Expand Down
15 changes: 8 additions & 7 deletions tests/DoctrinePersistentEntityManagerMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DI\Container;
use DI\ContainerBuilder;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Setup as DoctrineSetup;
use PHPUnit\Framework\TestCase;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -56,29 +57,29 @@ public function testConstructorWithoutEntityManagerInContainer() : void
*/
public function testPreservingEntityManager() : void
{
$expectedEntityManager = $this->container->get(EntityManager::class);
$expectedEntityManager = $this->container->get(EntityManagerInterface::class);

(new RequestHandler)
->add(new DoctrinePersistentEntityManagerMiddleware($this->container))
->handle((new ServerRequestFactory)->createServerRequest('GET', '/', []));

$this->assertTrue($expectedEntityManager->isOpen());
$this->assertSame($expectedEntityManager, $this->container->get(EntityManager::class));
$this->assertSame($expectedEntityManager, $this->container->get(EntityManagerInterface::class));
}

/**
* @return void
*/
public function testReopeningEntityManager() : void
{
$closedEntityManager = $this->container->get(EntityManager::class);
$closedEntityManager = $this->container->get(EntityManagerInterface::class);
$closedEntityManager->close();

(new RequestHandler)
->add(new DoctrinePersistentEntityManagerMiddleware($this->container))
->handle((new ServerRequestFactory)->createServerRequest('GET', '/', []));

$reopenedEntityManager = $this->container->get(EntityManager::class);
$reopenedEntityManager = $this->container->get(EntityManagerInterface::class);

$this->assertFalse($closedEntityManager->isOpen());
$this->assertTrue($reopenedEntityManager->isOpen());
Expand All @@ -96,7 +97,7 @@ protected function setUp()

$this->container = $builder->build();

$this->container->set(EntityManager::class, function () : EntityManager {
$this->container->set(EntityManagerInterface::class, function () : EntityManagerInterface {
$config = DoctrineSetup::createAnnotationMetadataConfiguration([__DIR__], true, null, null, false);

// See the file "phpunit.xml.dist" in the package root
Expand All @@ -109,8 +110,8 @@ protected function setUp()
*/
protected function tearDown()
{
if ($this->container->has(EntityManager::class)) {
$entityManager = $this->container->get(EntityManager::class);
if ($this->container->has(EntityManagerInterface::class)) {
$entityManager = $this->container->get(EntityManagerInterface::class);
$entityManager->getConnection()->close();
$entityManager->close();
}
Expand Down

0 comments on commit 949b5fb

Please sign in to comment.