1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Common\Doctrine; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Driver; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
11
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
use Shlinkio\Shlink\Common\Doctrine\NoDbNameConnectionFactory; |
14
|
|
|
|
15
|
|
|
class NoDbNameConnectionFactoryTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
use ProphecyTrait; |
18
|
|
|
|
19
|
|
|
private NoDbNameConnectionFactory $factory; |
20
|
|
|
private ObjectProphecy $container; |
21
|
|
|
private ObjectProphecy $originalConn; |
22
|
|
|
|
23
|
|
|
public function setUp(): void |
24
|
|
|
{ |
25
|
|
|
$this->container = $this->prophesize(ContainerInterface::class); |
26
|
|
|
$this->originalConn = $this->prophesize(Connection::class); |
27
|
|
|
$this->container->get(Connection::class)->willReturn($this->originalConn->reveal()); |
28
|
|
|
|
29
|
|
|
$this->factory = new NoDbNameConnectionFactory(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @test */ |
33
|
|
|
public function createsNewConnectionRemovingDbNameFromOriginalConnectionParams(): void |
34
|
|
|
{ |
35
|
|
|
$params = [ |
36
|
|
|
'username' => 'foo', |
37
|
|
|
'password' => 'bar', |
38
|
|
|
'dbname' => 'something', |
39
|
|
|
]; |
40
|
|
|
$getOriginalParams = $this->originalConn->getParams()->willReturn($params); |
41
|
|
|
$getOriginalDriver = $this->originalConn->getDriver()->willReturn($this->prophesize(Driver::class)->reveal()); |
42
|
|
|
$getOriginalConfig = $this->originalConn->getConfiguration()->willReturn(null); |
43
|
|
|
$getOriginalEvents = $this->originalConn->getEventManager()->willReturn(null); |
44
|
|
|
|
45
|
|
|
$conn = ($this->factory)($this->container->reveal()); |
46
|
|
|
|
47
|
|
|
$this->assertEquals([ |
48
|
|
|
'username' => 'foo', |
49
|
|
|
'password' => 'bar', |
50
|
|
|
], $conn->getParams()); |
51
|
|
|
$getOriginalParams->shouldHaveBeenCalledOnce(); |
52
|
|
|
$getOriginalDriver->shouldHaveBeenCalledOnce(); |
53
|
|
|
$getOriginalConfig->shouldHaveBeenCalledOnce(); |
54
|
|
|
$getOriginalEvents->shouldHaveBeenCalledOnce(); |
55
|
|
|
$this->container->get(Connection::class)->shouldHaveBeenCalledOnce(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|