2018-11-24 14:49:25 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Common\Middleware;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Connection;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Shlinkio\Shlink\Common\Middleware\CloseDbConnectionMiddleware;
|
|
|
|
use Zend\Diactoros\Response;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2018-11-24 14:49:25 +03:00
|
|
|
|
|
|
|
class CloseDbConnectionMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var CloseDbConnectionMiddleware */
|
|
|
|
private $middleware;
|
|
|
|
/** @var ObjectProphecy */
|
|
|
|
private $handler;
|
|
|
|
/** @var ObjectProphecy */
|
|
|
|
private $em;
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-11-24 14:49:25 +03:00
|
|
|
{
|
|
|
|
$this->handler = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
|
|
|
$this->middleware = new CloseDbConnectionMiddleware($this->em->reveal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function connectionIsClosedWhenMiddlewareIsProcessed()
|
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$req = new ServerRequest();
|
2018-11-24 14:49:25 +03:00
|
|
|
$resp = new Response();
|
|
|
|
|
|
|
|
$conn = $this->prophesize(Connection::class);
|
|
|
|
$closeConn = $conn->close()->will(function () {
|
|
|
|
});
|
|
|
|
$getConn = $this->em->getConnection()->willReturn($conn->reveal());
|
2018-11-24 15:24:43 +03:00
|
|
|
$clear = $this->em->clear()->will(function () {
|
|
|
|
});
|
2018-11-24 14:49:25 +03:00
|
|
|
$handle = $this->handler->handle($req)->willReturn($resp);
|
|
|
|
|
|
|
|
$result = $this->middleware->process($req, $this->handler->reveal());
|
|
|
|
|
|
|
|
$this->assertSame($result, $resp);
|
|
|
|
$getConn->shouldHaveBeenCalledOnce();
|
|
|
|
$closeConn->shouldHaveBeenCalledOnce();
|
2018-11-24 15:24:43 +03:00
|
|
|
$clear->shouldHaveBeenCalledOnce();
|
2018-11-24 14:49:25 +03:00
|
|
|
$handle->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
}
|