Passed
Pull Request — master (#3892)
by David
65:26
created

testGetConnectionByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\ConnectionRegistry;
9
use Doctrine\DBAL\Psr11ConnectionRegistry;
10
use InvalidArgumentException;
11
use PHPUnit\Framework\TestCase;
12
use Psr\Container\ContainerInterface;
13
use function array_keys;
14
15
class Psr11ConnectionRegistryTest extends TestCase
16
{
17
    /** @var ConnectionRegistry */
18
    private $registry;
19
20
    /** @var array<string, Connection> */
21
    private $connections;
22
23
    protected function setUp() : void
24
    {
25
        $this->connections = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('foo' => $this->cr...BAL\Connection::class)) of type array<string,PHPUnit\Fra...\MockObject\MockObject> is incompatible with the declared type array<string,Doctrine\DBAL\Connection> of property $connections.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
            'foo' => $this->createMock(Connection::class),
27
            'bar' => $this->createMock(Connection::class),
28
        ];
29
30
        $container = $this->createMock(ContainerInterface::class);
31
        $container->expects($this->any())
32
            ->method('has')
33
            ->willReturnCallback(function (string $name) {
34
                return isset($this->connections[$name]);
35
            });
36
37
        $container->expects($this->any())
38
            ->method('get')
39
            ->willReturnCallback(function (string $name) {
40
                return $this->connections[$name];
41
            });
42
43
        $this->registry = new Psr11ConnectionRegistry($container, 'bar', array_keys($this->connections));
44
    }
45
46
    public function testGetDefaultConnection() : void
47
    {
48
        $this->assertSame($this->connections['bar'], $this->registry->getConnection());
49
    }
50
51
    public function testGetConnectionByName() : void
52
    {
53
        $this->assertSame($this->connections['foo'], $this->registry->getConnection('foo'));
54
        $this->assertSame($this->connections['bar'], $this->registry->getConnection('bar'));
55
    }
56
57
    public function testGetNotExistentConnection() : void
58
    {
59
        $this->expectException(InvalidArgumentException::class);
60
        $this->expectExceptionMessage('Connection with name "something" does not exist.');
61
        $this->registry->getConnection('something');
62
    }
63
64
    public function testGetDefaultConnectionName() : void
65
    {
66
        $this->assertSame('bar', $this->registry->getDefaultConnectionName());
67
    }
68
69
    public function getGetConnections() : void
70
    {
71
        $this->assertSame($this->connections, $this->registry->getConnections());
72
    }
73
74
    public function testGetConnectionNames() : void
75
    {
76
        $this->assertSame(array_keys($this->connections), $this->registry->getConnectionNames());
77
    }
78
}
79