Created ReopeningEntityManagerDelegatorTest

This commit is contained in:
Alejandro Celaya 2019-08-02 19:33:31 +02:00
parent bdc93a45b5
commit f99053d251
3 changed files with 32 additions and 6 deletions

View file

@ -5,13 +5,14 @@ namespace Shlinkio\Shlink\Common\Doctrine;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
class ReopeningEntityManager extends EntityManagerDecorator
{
protected function getWrappedEntityManager(): EntityManager
protected function getWrappedEntityManager(): EntityManagerInterface
{
if (! $this->wrapped->isOpen()) {
$this->wrapped= EntityManager::create(
$this->wrapped = EntityManager::create(
$this->wrapped->getConnection(),
$this->wrapped->getConfiguration(),
$this->wrapped->getEventManager()

View file

@ -3,15 +3,12 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Doctrine;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;
class ReopeningEntityManagerDelegator
{
public function __invoke(ContainerInterface $container, string $name, callable $callback): ReopeningEntityManager
{
/** @var EntityManagerInterface $em */
$em = $callback();
return new ReopeningEntityManager($em);
return new ReopeningEntityManager($callback());
}
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Doctrine;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use Shlinkio\Shlink\Common\Doctrine\ReopeningEntityManagerDelegator;
use Zend\ServiceManager\ServiceManager;
class ReopeningEntityManagerDelegatorTest extends TestCase
{
/** @test */
public function decoratesEntityManagerFromCallback(): void
{
$em = $this->prophesize(EntityManagerInterface::class)->reveal();
$result = (new ReopeningEntityManagerDelegator())(new ServiceManager(), '', function () use ($em) {
return $em;
});
$ref = new ReflectionObject($result);
$prop = $ref->getProperty('wrapped');
$prop->setAccessible(true);
$this->assertSame($em, $prop->getValue($result));
}
}