Created EntityManagerDecorator to handle the automatic reopening, and removed this behavior from ClosDbConnectionMiddleware

This commit is contained in:
Alejandro Celaya
2019-08-02 19:28:10 +02:00
parent a771743756
commit bdc93a45b5
9 changed files with 128 additions and 63 deletions

View File

@@ -10,7 +10,6 @@ use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;
use Shlinkio\Shlink\Common\Middleware\CloseDbConnectionMiddleware;
use Throwable;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
@@ -35,7 +34,6 @@ class CloseDbConnectionMiddlewareTest extends TestCase
$this->em->getConnection()->willReturn($this->conn->reveal());
$this->em->clear()->will(function () {
});
$this->em->isOpen()->willReturn(true);
$this->middleware = new CloseDbConnectionMiddleware($this->em->reveal());
}
@@ -71,31 +69,4 @@ class CloseDbConnectionMiddlewareTest extends TestCase
$this->middleware->process($req, $this->handler->reveal());
}
/**
* @test
* @dataProvider provideClosed
*/
public function entityManagerIsReopenedAfterAnExceptionWhichClosedIt(bool $closed): void
{
$req = new ServerRequest();
$expectedError = new RuntimeException();
$this->handler->handle($req)->willThrow($expectedError)
->shouldBeCalledOnce();
$this->em->closed = $closed;
$this->em->isOpen()->willReturn(false);
try {
$this->middleware->process($req, $this->handler->reveal());
$this->fail('Expected exception to be thrown but it did not.');
} catch (Throwable $e) {
$this->assertSame($expectedError, $e);
$this->assertFalse($this->em->closed);
}
}
public function provideClosed(): iterable
{
return [[true, false]];
}
}