mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-17 15:59:56 +03:00
Created ReopeningEntityManagerDelegatorTest
This commit is contained in:
parent
bdc93a45b5
commit
f99053d251
3 changed files with 32 additions and 6 deletions
|
@ -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()
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue