From bfd2ce782c9f181047f07530ee2c528868025b6e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 2 Aug 2019 19:53:19 +0200 Subject: [PATCH] Created ReopeningEntityManagerTest --- .../src/Doctrine/ReopeningEntityManager.php | 12 ++- .../ReopeningEntityManagerDelegator.php | 3 +- .../Doctrine/ReopeningEntityManagerTest.php | 80 +++++++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 module/Common/test/Doctrine/ReopeningEntityManagerTest.php diff --git a/module/Common/src/Doctrine/ReopeningEntityManager.php b/module/Common/src/Doctrine/ReopeningEntityManager.php index e040b978..d9a80e2e 100644 --- a/module/Common/src/Doctrine/ReopeningEntityManager.php +++ b/module/Common/src/Doctrine/ReopeningEntityManager.php @@ -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() diff --git a/module/Common/src/Doctrine/ReopeningEntityManagerDelegator.php b/module/Common/src/Doctrine/ReopeningEntityManagerDelegator.php index 3128b0cf..3ea6a2f1 100644 --- a/module/Common/src/Doctrine/ReopeningEntityManagerDelegator.php +++ b/module/Common/src/Doctrine/ReopeningEntityManagerDelegator.php @@ -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']); } } diff --git a/module/Common/test/Doctrine/ReopeningEntityManagerTest.php b/module/Common/test/Doctrine/ReopeningEntityManagerTest.php new file mode 100644 index 00000000..74e8d260 --- /dev/null +++ b/module/Common/test/Doctrine/ReopeningEntityManagerTest.php @@ -0,0 +1,80 @@ +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']; + } +}