|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
use Shlinkio\Shlink\Common\Middleware\CloseDbConnectionMiddleware; |
|
13
|
|
|
use Zend\Diactoros\Response; |
|
14
|
|
|
use Zend\Diactoros\ServerRequest; |
|
15
|
|
|
|
|
16
|
|
|
class CloseDbConnectionMiddlewareTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var CloseDbConnectionMiddleware */ |
|
19
|
|
|
private $middleware; |
|
20
|
|
|
/** @var ObjectProphecy */ |
|
21
|
|
|
private $handler; |
|
22
|
|
|
/** @var ObjectProphecy */ |
|
23
|
|
|
private $em; |
|
24
|
|
|
/** @var ObjectProphecy */ |
|
25
|
|
|
private $conn; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->handler = $this->prophesize(RequestHandlerInterface::class); |
|
30
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class); |
|
31
|
|
|
$this->conn = $this->prophesize(Connection::class); |
|
32
|
|
|
$this->conn->close()->will(function () { |
|
33
|
|
|
}); |
|
34
|
|
|
$this->em->getConnection()->willReturn($this->conn->reveal()); |
|
35
|
|
|
$this->em->clear()->will(function () { |
|
36
|
|
|
}); |
|
37
|
|
|
|
|
38
|
|
|
$this->middleware = new CloseDbConnectionMiddleware($this->em->reveal()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** @test */ |
|
42
|
|
|
public function connectionIsClosedWhenMiddlewareIsProcessed(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$req = new ServerRequest(); |
|
45
|
|
|
$resp = new Response(); |
|
46
|
|
|
$handle = $this->handler->handle($req)->willReturn($resp); |
|
47
|
|
|
|
|
48
|
|
|
$result = $this->middleware->process($req, $this->handler->reveal()); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertSame($result, $resp); |
|
51
|
|
|
$this->em->getConnection()->shouldHaveBeenCalledOnce(); |
|
52
|
|
|
$this->conn->close()->shouldHaveBeenCalledOnce(); |
|
53
|
|
|
$this->em->clear()->shouldHaveBeenCalledOnce(); |
|
54
|
|
|
$handle->shouldHaveBeenCalledOnce(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** @test */ |
|
58
|
|
|
public function connectionIsClosedEvenIfExceptionIsThrownOnInnerMiddlewares(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$req = new ServerRequest(); |
|
61
|
|
|
$expectedError = new RuntimeException(); |
|
62
|
|
|
$this->handler->handle($req)->willThrow($expectedError) |
|
63
|
|
|
->shouldBeCalledOnce(); |
|
64
|
|
|
|
|
65
|
|
|
$this->em->getConnection()->shouldBeCalledOnce(); |
|
66
|
|
|
$this->conn->close()->shouldBeCalledOnce(); |
|
67
|
|
|
$this->em->clear()->shouldBeCalledOnce(); |
|
68
|
|
|
$this->expectExceptionObject($expectedError); |
|
69
|
|
|
|
|
70
|
|
|
$this->middleware->process($req, $this->handler->reveal()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|