Failed Conditions
Pull Request — master (#3892)
by David
61:44
created

testGetNotExistentConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Registry;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Registry\ConnectionRegistry;
9
use Doctrine\DBAL\Registry\Psr11ConnectionRegistry;
10
use InvalidArgumentException;
11
use PHPUnit\Framework\MockObject\Stub;
12
use PHPUnit\Framework\TestCase;
13
use Psr\Container\ContainerInterface;
14
use function array_keys;
15
16
class Psr11ConnectionRegistryTest extends TestCase
17
{
18
    /** @var ConnectionRegistry */
19
    private $registry;
20
21
    /** @var array<string, Connection&Stub> */
22
    private $connections;
23
24
    protected function setUp() : void
25
    {
26
        /** @var Connection&Stub $fooConnection */
27
        $fooConnection = $this->createStub(Connection::class);
28
        /** @var Connection&Stub $barConnection */
29
        $barConnection = $this->createStub(Connection::class);
30
31
        $this->connections = [
32
            'foo' => $fooConnection,
33
            'bar' => $barConnection,
34
        ];
35
36
        /** @var ContainerInterface&Stub $container */
37
        $container = $this->createStub(ContainerInterface::class);
38
        $container->method('has')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Psr\Container\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        $container->/** @scrutinizer ignore-call */ 
39
                    method('has')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\Stub. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        $container->/** @scrutinizer ignore-call */ 
39
                    method('has')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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