mirror of
https://github.com/shlinkio/shlink.git
synced 2025-01-11 08:32:02 -06:00
Created ReopeningEntityManagerTest
This commit is contained in:
parent
f99053d251
commit
bfd2ce782c
@ -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()
|
||||
|
@ -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']);
|
||||
}
|
||||
}
|
||||
|
80
module/Common/test/Doctrine/ReopeningEntityManagerTest.php
Normal file
80
module/Common/test/Doctrine/ReopeningEntityManagerTest.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Common\Doctrine;
|
||||
|
||||
use Doctrine\Common\EventManager;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\ORM\Configuration;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Common\Doctrine\ReopeningEntityManager;
|
||||
use stdClass;
|
||||
|
||||
class ReopeningEntityManagerTest extends TestCase
|
||||
{
|
||||
/** @var ReopeningEntityManager */
|
||||
private $decoratorEm;
|
||||
/** @var ObjectProphecy */
|
||||
private $wrapped;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->wrapped = $this->prophesize(EntityManagerInterface::class);
|
||||
$this->wrapped->getConnection()->willReturn($this->prophesize(Connection::class));
|
||||
$this->wrapped->getConfiguration()->willReturn($this->prophesize(Configuration::class));
|
||||
$this->wrapped->getEventManager()->willReturn($this->prophesize(EventManager::class));
|
||||
|
||||
$wrappedMock = $this->wrapped->reveal();
|
||||
$this->decoratorEm = new ReopeningEntityManager($wrappedMock, function () use ($wrappedMock) {
|
||||
return $wrappedMock;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideMethodNames
|
||||
*/
|
||||
public function wrappedInstanceIsTransparentlyCalledWhenItIsNotClosed(string $methodName): void
|
||||
{
|
||||
$method = $this->wrapped->__call($methodName, [Argument::cetera()])->willReturnArgument();
|
||||
$isOpen = $this->wrapped->isOpen()->willReturn(true);
|
||||
|
||||
$this->decoratorEm->{$methodName}(new stdClass());
|
||||
|
||||
$method->shouldHaveBeenCalledOnce();
|
||||
$isOpen->shouldHaveBeenCalledOnce();
|
||||
$this->wrapped->getConnection()->shouldNotHaveBeenCalled();
|
||||
$this->wrapped->getConfiguration()->shouldNotHaveBeenCalled();
|
||||
$this->wrapped->getEventManager()->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideMethodNames
|
||||
*/
|
||||
public function wrappedInstanceIsRecreatedWhenItIsClosed(string $methodName): void
|
||||
{
|
||||
$method = $this->wrapped->__call($methodName, [Argument::cetera()])->willReturnArgument();
|
||||
$isOpen = $this->wrapped->isOpen()->willReturn(false);
|
||||
|
||||
$this->decoratorEm->{$methodName}(new stdClass());
|
||||
|
||||
$method->shouldHaveBeenCalledOnce();
|
||||
$isOpen->shouldHaveBeenCalledOnce();
|
||||
$this->wrapped->getConnection()->shouldHaveBeenCalledOnce();
|
||||
$this->wrapped->getConfiguration()->shouldHaveBeenCalledOnce();
|
||||
$this->wrapped->getEventManager()->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideMethodNames(): iterable
|
||||
{
|
||||
yield 'flush' => ['flush'];
|
||||
yield 'persist' => ['persist'];
|
||||
yield 'remove' => ['remove'];
|
||||
yield 'refresh' => ['refresh'];
|
||||
yield 'merge' => ['merge'];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user