Failed Conditions
Push — develop ( 03730f...4e0a6e )
by Sergei
112:33 queued 47:35
created

PoolingShardConnectionTest::testGetUsernameOverride()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Sharding;
6
7
use Doctrine\DBAL\DriverManager;
8
use Doctrine\DBAL\Sharding\PoolingShardConnection;
9
use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser;
10
use Doctrine\DBAL\Sharding\ShardingException;
11
use InvalidArgumentException;
12
use PHPUnit\Framework\TestCase;
13
use stdClass;
14
15
/**
16
 * @requires extension pdo_sqlite
17
 */
18
class PoolingShardConnectionTest extends TestCase
19
{
20
    public function testConnect()
21
    {
22
        $conn = DriverManager::getConnection([
23
            'wrapperClass' => PoolingShardConnection::class,
24
            'driver' => 'pdo_sqlite',
25
            'global' => ['memory' => true],
26
            'shards' => [
27
                ['id' => 1, 'memory' => true],
28
                ['id' => 2, 'memory' => true],
29
            ],
30
            'shardChoser' => MultiTenantShardChoser::class,
31
        ]);
32
33
        self::assertFalse($conn->isConnected(0));
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Connection::isConnected() has too many arguments starting with 0. ( Ignorable by Annotation )

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

33
        self::assertFalse($conn->/** @scrutinizer ignore-call */ isConnected(0));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
34
        $conn->connect(0);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Connection::connect() has too many arguments starting with 0. ( Ignorable by Annotation )

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

34
        $conn->/** @scrutinizer ignore-call */ 
35
               connect(0);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
35
        self::assertEquals(1, $conn->fetchColumn('SELECT 1'));
36
        self::assertTrue($conn->isConnected(0));
37
38
        self::assertFalse($conn->isConnected(1));
39
        $conn->connect(1);
40
        self::assertEquals(1, $conn->fetchColumn('SELECT 1'));
41
        self::assertTrue($conn->isConnected(1));
42
43
        self::assertFalse($conn->isConnected(2));
44
        $conn->connect(2);
45
        self::assertEquals(1, $conn->fetchColumn('SELECT 1'));
46
        self::assertTrue($conn->isConnected(2));
47
48
        $conn->close();
49
        self::assertFalse($conn->isConnected(0));
50
        self::assertFalse($conn->isConnected(1));
51
        self::assertFalse($conn->isConnected(2));
52
    }
53
54
    public function testNoGlobalServerException()
55
    {
56
        $this->expectException(InvalidArgumentException::class);
57
        $this->expectExceptionMessage('Connection Parameters require "global" and "shards" configurations.');
58
59
        DriverManager::getConnection([
60
            'wrapperClass' => PoolingShardConnection::class,
61
            'driver' => 'pdo_sqlite',
62
            'shards' => [
63
                ['id' => 1, 'memory' => true],
64
                ['id' => 2, 'memory' => true],
65
            ],
66
            'shardChoser' => MultiTenantShardChoser::class,
67
        ]);
68
    }
69
70
    public function testNoShardsServersException()
71
    {
72
        $this->expectException(InvalidArgumentException::class);
73
        $this->expectExceptionMessage('Connection Parameters require "global" and "shards" configurations.');
74
75
        DriverManager::getConnection([
76
            'wrapperClass' => PoolingShardConnection::class,
77
            'driver' => 'pdo_sqlite',
78
            'global' => ['memory' => true],
79
            'shardChoser' => MultiTenantShardChoser::class,
80
        ]);
81
    }
82
83
    public function testNoShardsChoserException()
84
    {
85
        $this->expectException(InvalidArgumentException::class);
86
        $this->expectExceptionMessage('Missing Shard Choser configuration "shardChoser".');
87
88
        DriverManager::getConnection([
89
            'wrapperClass' => PoolingShardConnection::class,
90
            'driver' => 'pdo_sqlite',
91
            'global' => ['memory' => true],
92
            'shards' => [
93
                ['id' => 1, 'memory' => true],
94
                ['id' => 2, 'memory' => true],
95
            ],
96
        ]);
97
    }
98
99
    public function testShardChoserWrongInstance()
100
    {
101
        $this->expectException(InvalidArgumentException::class);
102
        $this->expectExceptionMessage('The "shardChoser" configuration is not a valid instance of Doctrine\DBAL\Sharding\ShardChoser\ShardChoser');
103
104
        DriverManager::getConnection([
105
            'wrapperClass' => PoolingShardConnection::class,
106
            'driver' => 'pdo_sqlite',
107
            'global' => ['memory' => true],
108
            'shards' => [
109
                ['id' => 1, 'memory' => true],
110
                ['id' => 2, 'memory' => true],
111
            ],
112
            'shardChoser' => new stdClass(),
113
        ]);
114
    }
115
116
    public function testShardNonNumericId()
117
    {
118
        $this->expectException('InvalidArgumentException');
119
        $this->expectExceptionMessage('Shard Id has to be a non-negative number.');
120
121
        DriverManager::getConnection([
122
            'wrapperClass' => PoolingShardConnection::class,
123
            'driver' => 'pdo_sqlite',
124
            'global' => ['memory' => true],
125
            'shards' => [
126
                ['id' => 'foo', 'memory' => true],
127
            ],
128
            'shardChoser' => MultiTenantShardChoser::class,
129
        ]);
130
    }
131
132
    public function testShardMissingId()
133
    {
134
        $this->expectException(InvalidArgumentException::class);
135
        $this->expectExceptionMessage('Missing "id" for one configured shard. Please specify a unique shard-id.');
136
137
        DriverManager::getConnection([
138
            'wrapperClass' => PoolingShardConnection::class,
139
            'driver' => 'pdo_sqlite',
140
            'global' => ['memory' => true],
141
            'shards' => [
142
                ['memory' => true],
143
            ],
144
            'shardChoser' => MultiTenantShardChoser::class,
145
        ]);
146
    }
147
148
    public function testDuplicateShardId()
149
    {
150
        $this->expectException(InvalidArgumentException::class);
151
        $this->expectExceptionMessage('Shard "1" is duplicated in the configuration.');
152
153
        DriverManager::getConnection([
154
            'wrapperClass' => PoolingShardConnection::class,
155
            'driver' => 'pdo_sqlite',
156
            'global' => ['memory' => true],
157
            'shards' => [
158
                ['id' => 1, 'memory' => true],
159
                ['id' => 1, 'memory' => true],
160
            ],
161
            'shardChoser' => MultiTenantShardChoser::class,
162
        ]);
163
    }
164
165
    public function testSwitchShardWithOpenTransactionException()
166
    {
167
        $conn = DriverManager::getConnection([
168
            'wrapperClass' => PoolingShardConnection::class,
169
            'driver' => 'pdo_sqlite',
170
            'global' => ['memory' => true],
171
            'shards' => [
172
                ['id' => 1, 'memory' => true],
173
            ],
174
            'shardChoser' => MultiTenantShardChoser::class,
175
        ]);
176
177
        $conn->beginTransaction();
178
179
        $this->expectException(ShardingException::class);
180
        $this->expectExceptionMessage('Cannot switch shard when transaction is active.');
181
        $conn->connect(1);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Connection::connect() has too many arguments starting with 1. ( Ignorable by Annotation )

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

181
        $conn->/** @scrutinizer ignore-call */ 
182
               connect(1);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
182
    }
183
184
    public function testGetActiveShardId()
185
    {
186
        $conn = DriverManager::getConnection([
187
            'wrapperClass' => PoolingShardConnection::class,
188
            'driver' => 'pdo_sqlite',
189
            'global' => ['memory' => true],
190
            'shards' => [
191
                ['id' => 1, 'memory' => true],
192
            ],
193
            'shardChoser' => MultiTenantShardChoser::class,
194
        ]);
195
196
        self::assertNull($conn->getActiveShardId());
0 ignored issues
show
Bug introduced by
The method getActiveShardId() does not exist on Doctrine\DBAL\Connection. It seems like you code against a sub-type of Doctrine\DBAL\Connection such as Doctrine\DBAL\Sharding\PoolingShardConnection. ( Ignorable by Annotation )

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

196
        self::assertNull($conn->/** @scrutinizer ignore-call */ getActiveShardId());
Loading history...
197
198
        $conn->connect(0);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Connection::connect() has too many arguments starting with 0. ( Ignorable by Annotation )

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

198
        $conn->/** @scrutinizer ignore-call */ 
199
               connect(0);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
199
        self::assertEquals(0, $conn->getActiveShardId());
200
201
        $conn->connect(1);
202
        self::assertEquals(1, $conn->getActiveShardId());
203
204
        $conn->close();
205
        self::assertNull($conn->getActiveShardId());
206
    }
207
208
    public function testGetParamsOverride()
209
    {
210
        $conn = DriverManager::getConnection([
211
            'wrapperClass' => PoolingShardConnection::class,
212
            'driver' => 'pdo_sqlite',
213
            'global' => ['memory' => true, 'host' => 'localhost'],
214
            'shards' => [
215
                ['id' => 1, 'memory' => true, 'host' => 'foo'],
216
            ],
217
            'shardChoser' => MultiTenantShardChoser::class,
218
        ]);
219
220
        self::assertEquals([
221
            'wrapperClass' => PoolingShardConnection::class,
222
            'driver' => 'pdo_sqlite',
223
            'global' => ['memory' => true, 'host' => 'localhost'],
224
            'shards' => [
225
                ['id' => 1, 'memory' => true, 'host' => 'foo'],
226
            ],
227
            'shardChoser' => new MultiTenantShardChoser(),
228
            'memory' => true,
229
            'host' => 'localhost',
230
        ], $conn->getParams());
231
232
        $conn->connect(1);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Connection::connect() has too many arguments starting with 1. ( Ignorable by Annotation )

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

232
        $conn->/** @scrutinizer ignore-call */ 
233
               connect(1);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
233
        self::assertEquals([
234
            'wrapperClass' => PoolingShardConnection::class,
235
            'driver' => 'pdo_sqlite',
236
            'global' => ['memory' => true, 'host' => 'localhost'],
237
            'shards' => [
238
                ['id' => 1, 'memory' => true, 'host' => 'foo'],
239
            ],
240
            'shardChoser' => new MultiTenantShardChoser(),
241
            'id' => 1,
242
            'memory' => true,
243
            'host' => 'foo',
244
        ], $conn->getParams());
245
    }
246
}
247