Created ReopeningEntityManagerTest

This commit is contained in:
Alejandro Celaya
2019-08-02 19:53:19 +02:00
parent f99053d251
commit bfd2ce782c
3 changed files with 92 additions and 3 deletions

View File

@@ -4,15 +4,23 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Doctrine;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
class ReopeningEntityManager extends EntityManagerDecorator
{
/** @var callable */
private $emFactory;
public function __construct(EntityManagerInterface $wrapped, callable $emFactory)
{
parent::__construct($wrapped);
$this->emFactory = $emFactory;
}
protected function getWrappedEntityManager(): EntityManagerInterface
{
if (! $this->wrapped->isOpen()) {
$this->wrapped = EntityManager::create(
$this->wrapped = ($this->emFactory)(
$this->wrapped->getConnection(),
$this->wrapped->getConfiguration(),
$this->wrapped->getEventManager()

View File

@@ -3,12 +3,13 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Doctrine;
use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerInterface;
class ReopeningEntityManagerDelegator
{
public function __invoke(ContainerInterface $container, string $name, callable $callback): ReopeningEntityManager
{
return new ReopeningEntityManager($callback());
return new ReopeningEntityManager($callback(), [EntityManager::class, 'create']);
}
}