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