Issues (177)

tests/Unit/ConnectionPoolTest.php (2 issues)

1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit;
4
5
use Janisbiz\LightOrm\Connection\ConnectionInterface;
6
use Janisbiz\LightOrm\ConnectionPool;
7
use Janisbiz\LightOrm\Dms\MySQL\Connection\ConnectionConfig;
8
use Janisbiz\LightOrm\Exception\InvalidArgumentException;
9
use PHPUnit\Framework\TestCase;
10
11
class ConnectionPoolTest extends TestCase
12
{
13
    use ReflectionTrait;
14
15
    const CONNECTION_CONFIG_HOST = 'host';
16
    const CONNECTION_CONFIG_USERNAME = 'username';
17
    const CONNECTION_CONFIG_PASSWORD = 'password';
18
    const CONNECTION_CONFIG_DBNAME_ONE = 'dbname_one';
19
    const CONNECTION_CONFIG_DBNAME_TWO = 'dbname_two';
20
    const CONNECTION_CONFIG_DBNAME_NON_EXISTENT = 'dbname_non_existent';
21
    const CONNECTION_CONFIG_ADAPTER = ConnectionConfig::ADAPTER;
22
23
    /**
24
     * @var ConnectionPool
25
     */
26
    private $connectionPool;
27
28
    /**
29
     * @var ConnectionPool
30
     */
31
    private $connectionPoolMock;
32
33
    public function setUp()
34
    {
35
        $connectionConfigOne = new ConnectionConfig(
36
            static::CONNECTION_CONFIG_HOST,
37
            static::CONNECTION_CONFIG_USERNAME,
38
            static::CONNECTION_CONFIG_PASSWORD,
39
            static::CONNECTION_CONFIG_DBNAME_ONE
40
        );
41
42
        $connectionConfigTwo = new ConnectionConfig(
43
            static::CONNECTION_CONFIG_HOST,
44
            static::CONNECTION_CONFIG_USERNAME,
45
            static::CONNECTION_CONFIG_PASSWORD,
46
            static::CONNECTION_CONFIG_DBNAME_TWO
47
        );
48
49
        $this->connectionPool = (new ConnectionPool())
50
            ->addConnectionConfig($connectionConfigOne)
51
            ->addConnectionConfig($connectionConfigTwo)
52
        ;
53
54
        $connectionMock = $this->createMock(ConnectionInterface::class);
55
56
        $connectionPoolMock = $this->createPartialMock(ConnectionPool::class, ['createConnection']);
57
        $connectionPoolMock->method('createConnection')->willReturn($connectionMock);
0 ignored issues
show
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

57
        $connectionPoolMock->/** @scrutinizer ignore-call */ 
58
                             method('createConnection')->willReturn($connectionMock);

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...
58
59
        $this->connectionPoolMock = $connectionPoolMock;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connectionPoolMock of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Janisbiz\LightOrm\ConnectionPool of property $connectionPoolMock.

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...
60
    }
61
62
    public function testAddConnectionConfig()
63
    {
64
        $connectionPoolReflection = new \ReflectionClass($this->connectionPool);
65
        $connectionConfigProperty = $connectionPoolReflection->getProperty('connectionConfig');
66
        $connectionConfigProperty->setAccessible(true);
67
        $connectionConfig = $connectionConfigProperty->getValue($this->connectionPool);
68
        $connectionConfigProperty->setAccessible(false);
69
70
        $this->assertCount(2, $connectionConfig);
71
    }
72
73
    public function testGetConnection()
74
    {
75
        $this->assertTrue(
76
            $this->connectionPoolMock->getConnection(static::CONNECTION_CONFIG_DBNAME_ONE)
77
            instanceof ConnectionInterface
78
        );
79
        $this->assertTrue(
80
            $this->connectionPoolMock->getConnection(static::CONNECTION_CONFIG_DBNAME_TWO)
81
            instanceof ConnectionInterface
82
        );
83
    }
84
85
    public function testGetConnectionExceptionWhenConnectionConfigIsEmpty()
86
    {
87
        $this
88
            ->createAccessibleProperty($this->connectionPoolMock, 'connectionConfig')
89
            ->setValue($this->connectionPoolMock, [])
90
        ;
91
92
        $this->expectException(InvalidArgumentException::class);
93
        $this->expectExceptionMessageRegExp(
94
            '/Could not find connection by name "(.*)"\!/'
95
        );
96
97
        $this->connectionPoolMock->getConnection(static::CONNECTION_CONFIG_DBNAME_NON_EXISTENT);
98
    }
99
100
    public function testGetConnectionException()
101
    {
102
        $this->expectException(InvalidArgumentException::class);
103
        $this->expectExceptionMessageRegExp(
104
            '/Could not find connection by name "(.*)"\! Available connections: "(.*)"\./'
105
        );
106
107
        $this->connectionPoolMock->getConnection(static::CONNECTION_CONFIG_DBNAME_NON_EXISTENT);
108
    }
109
110
    public function testGetConnectionStatic()
111
    {
112
        $this->assertTrue(
113
            ConnectionPool::getConnectionStatic(static::CONNECTION_CONFIG_DBNAME_ONE)
114
            instanceof ConnectionInterface
115
        );
116
        $this->assertTrue(
117
            ConnectionPool::getConnectionStatic(static::CONNECTION_CONFIG_DBNAME_TWO)
118
            instanceof ConnectionInterface
119
        );
120
    }
121
122
    public function testGetConnectionStaticException()
123
    {
124
        $this->expectException(InvalidArgumentException::class);
125
        $this->expectExceptionMessageRegExp(
126
            '/Could not find connection by name "(.*)"\! Available connections: "(.*)"\./'
127
        );
128
129
        ConnectionPool::getConnectionStatic(static::CONNECTION_CONFIG_DBNAME_NON_EXISTENT);
130
    }
131
}
132