Failed Conditions
Pull Request — develop (#3348)
by Sergei
62:41
created

testNoShardsChoserException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Sharding;
4
5
use Doctrine\DBAL\DriverManager;
6
use Doctrine\DBAL\Sharding\PoolingShardConnection;
7
use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser;
8
use Doctrine\DBAL\Sharding\ShardingException;
9
use PHPUnit\Framework\TestCase;
10
use stdClass;
11
use function array_merge;
12
13
/**
14
 * @requires extension pdo_sqlite
15
 */
16
class PoolingShardConnectionTest extends TestCase
17
{
18
    public function testConnect() : void
19
    {
20
        $conn = $this->createConnection([
21
            'driver' => 'pdo_sqlite',
22
            'global' => ['memory' => true],
23
            'shards' => [
24
                ['id' => 1, 'memory' => true],
25
                ['id' => 2, 'memory' => true],
26
            ],
27
            'shardChoser' => MultiTenantShardChoser::class,
28
        ]);
29
30
        self::assertFalse($conn->isConnected(0));
31
        $conn->connect(0);
32
        self::assertEquals(1, $conn->fetchColumn('SELECT 1'));
33
        self::assertTrue($conn->isConnected(0));
34
35
        self::assertFalse($conn->isConnected(1));
36
        $conn->connect(1);
37
        self::assertEquals(1, $conn->fetchColumn('SELECT 1'));
38
        self::assertTrue($conn->isConnected(1));
39
40
        self::assertFalse($conn->isConnected(2));
41
        $conn->connect(2);
42
        self::assertEquals(1, $conn->fetchColumn('SELECT 1'));
43
        self::assertTrue($conn->isConnected(2));
44
45
        $conn->close();
46
        self::assertFalse($conn->isConnected(0));
47
        self::assertFalse($conn->isConnected(1));
48
        self::assertFalse($conn->isConnected(2));
49
    }
50
51
    public function testNoGlobalServerException() : void
52
    {
53
        $this->expectException('InvalidArgumentException');
54
        $this->expectExceptionMessage("Connection Parameters require 'global' and 'shards' configurations.");
55
56
        $this->createConnection([
57
            'driver' => 'pdo_sqlite',
58
            'shards' => [
59
                ['id' => 1, 'memory' => true],
60
                ['id' => 2, 'memory' => true],
61
            ],
62
            'shardChoser' => MultiTenantShardChoser::class,
63
        ]);
64
    }
65
66
    public function testNoShardsServersException() : void
67
    {
68
        $this->expectException('InvalidArgumentException');
69
        $this->expectExceptionMessage("Connection Parameters require 'global' and 'shards' configurations.");
70
71
        $this->createConnection([
72
            'driver' => 'pdo_sqlite',
73
            'global' => ['memory' => true],
74
            'shardChoser' => MultiTenantShardChoser::class,
75
        ]);
76
    }
77
78
    public function testNoShardsChoserException() : void
79
    {
80
        $this->expectException('InvalidArgumentException');
81
        $this->expectExceptionMessage("Missing Shard Choser configuration 'shardChoser'");
82
83
        $this->createConnection([
84
            'driver' => 'pdo_sqlite',
85
            'global' => ['memory' => true],
86
            'shards' => [
87
                ['id' => 1, 'memory' => true],
88
                ['id' => 2, 'memory' => true],
89
            ],
90
        ]);
91
    }
92
93
    public function testShardChoserWrongInstance() : void
94
    {
95
        $this->expectException('InvalidArgumentException');
96
        $this->expectExceptionMessage("The 'shardChoser' configuration is not a valid instance of Doctrine\DBAL\Sharding\ShardChoser\ShardChoser");
97
98
        $this->createConnection([
99
            'driver' => 'pdo_sqlite',
100
            'global' => ['memory' => true],
101
            'shards' => [
102
                ['id' => 1, 'memory' => true],
103
                ['id' => 2, 'memory' => true],
104
            ],
105
            'shardChoser' => new stdClass(),
106
        ]);
107
    }
108
109
    public function testShardNonNumericId() : void
110
    {
111
        $this->expectException('InvalidArgumentException');
112
        $this->expectExceptionMessage('Shard Id has to be a non-negative number.');
113
114
        $this->createConnection([
115
            'driver' => 'pdo_sqlite',
116
            'global' => ['memory' => true],
117
            'shards' => [
118
                ['id' => 'foo', 'memory' => true],
119
            ],
120
            'shardChoser' => MultiTenantShardChoser::class,
121
        ]);
122
    }
123
124
    public function testShardMissingId() : void
125
    {
126
        $this->expectException('InvalidArgumentException');
127
        $this->expectExceptionMessage("Missing 'id' for one configured shard. Please specify a unique shard-id.");
128
129
        $this->createConnection([
130
            'driver' => 'pdo_sqlite',
131
            'global' => ['memory' => true],
132
            'shards' => [
133
                ['memory' => true],
134
            ],
135
            'shardChoser' => MultiTenantShardChoser::class,
136
        ]);
137
    }
138
139
    public function testDuplicateShardId() : void
140
    {
141
        $this->expectException('InvalidArgumentException');
142
        $this->expectExceptionMessage('Shard 1 is duplicated in the configuration.');
143
144
        $this->createConnection([
145
            'driver' => 'pdo_sqlite',
146
            'global' => ['memory' => true],
147
            'shards' => [
148
                ['id' => 1, 'memory' => true],
149
                ['id' => 1, 'memory' => true],
150
            ],
151
            'shardChoser' => MultiTenantShardChoser::class,
152
        ]);
153
    }
154
155
    public function testSwitchShardWithOpenTransactionException() : void
156
    {
157
        $conn = $this->createConnection([
158
            'driver' => 'pdo_sqlite',
159
            'global' => ['memory' => true],
160
            'shards' => [
161
                ['id' => 1, 'memory' => true],
162
            ],
163
            'shardChoser' => MultiTenantShardChoser::class,
164
        ]);
165
166
        $conn->beginTransaction();
167
168
        $this->expectException(ShardingException::class);
169
        $this->expectExceptionMessage('Cannot switch shard when transaction is active.');
170
        $conn->connect(1);
171
    }
172
173
    public function testGetActiveShardId() : void
174
    {
175
        $conn = $this->createConnection([
176
            'driver' => 'pdo_sqlite',
177
            'global' => ['memory' => true],
178
            'shards' => [
179
                ['id' => 1, 'memory' => true],
180
            ],
181
            'shardChoser' => MultiTenantShardChoser::class,
182
        ]);
183
184
        self::assertNull($conn->getActiveShardId());
185
186
        $conn->connect(0);
187
        self::assertEquals(0, $conn->getActiveShardId());
188
189
        $conn->connect(1);
190
        self::assertEquals(1, $conn->getActiveShardId());
191
192
        $conn->close();
193
        self::assertNull($conn->getActiveShardId());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $conn->getActiveShardId() targeting Doctrine\DBAL\Sharding\P...ion::getActiveShardId() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
194
    }
195
196
    public function testGetParamsOverride() : void
197
    {
198
        $conn = $this->createConnection([
199
            'driver' => 'pdo_sqlite',
200
            'global' => ['memory' => true, 'host' => 'localhost'],
201
            'shards' => [
202
                ['id' => 1, 'memory' => true, 'host' => 'foo'],
203
            ],
204
            'shardChoser' => MultiTenantShardChoser::class,
205
        ]);
206
207
        self::assertEquals([
208
            'wrapperClass' => PoolingShardConnection::class,
209
            'driver' => 'pdo_sqlite',
210
            'global' => ['memory' => true, 'host' => 'localhost'],
211
            'shards' => [
212
                ['id' => 1, 'memory' => true, 'host' => 'foo'],
213
            ],
214
            'shardChoser' => new MultiTenantShardChoser(),
215
            'memory' => true,
216
            'host' => 'localhost',
217
        ], $conn->getParams());
218
219
        $conn->connect(1);
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
            'id' => 1,
229
            'memory' => true,
230
            'host' => 'foo',
231
        ], $conn->getParams());
232
    }
233
234
    public function testGetHostOverride() : void
235
    {
236
        $conn = $this->createConnection([
237
            'driver' => 'pdo_sqlite',
238
            'host' => 'localhost',
239
            'global' => ['memory' => true],
240
            'shards' => [
241
                ['id' => 1, 'memory' => true, 'host' => 'foo'],
242
            ],
243
            'shardChoser' => MultiTenantShardChoser::class,
244
        ]);
245
246
        self::assertEquals('localhost', $conn->getHost());
247
248
        $conn->connect(1);
249
        self::assertEquals('foo', $conn->getHost());
250
    }
251
252
    public function testGetPortOverride() : void
253
    {
254
        $conn = $this->createConnection([
255
            'driver' => 'pdo_sqlite',
256
            'port' => 3306,
257
            'global' => ['memory' => true],
258
            'shards' => [
259
                ['id' => 1, 'memory' => true, 'port' => 3307],
260
            ],
261
            'shardChoser' => MultiTenantShardChoser::class,
262
        ]);
263
264
        self::assertEquals(3306, $conn->getPort());
265
266
        $conn->connect(1);
267
        self::assertEquals(3307, $conn->getPort());
268
    }
269
270
    public function testGetUsernameOverride() : void
271
    {
272
        $conn = $this->createConnection([
273
            'driver' => 'pdo_sqlite',
274
            'user' => 'foo',
275
            'global' => ['memory' => true],
276
            'shards' => [
277
                ['id' => 1, 'memory' => true, 'user' => 'bar'],
278
            ],
279
            'shardChoser' => MultiTenantShardChoser::class,
280
        ]);
281
282
        self::assertEquals('foo', $conn->getUsername());
283
284
        $conn->connect(1);
285
        self::assertEquals('bar', $conn->getUsername());
286
    }
287
288
    public function testGetPasswordOverride() : void
289
    {
290
        $conn = $this->createConnection([
291
            'driver' => 'pdo_sqlite',
292
            'password' => 'foo',
293
            'global' => ['memory' => true],
294
            'shards' => [
295
                ['id' => 1, 'memory' => true, 'password' => 'bar'],
296
            ],
297
            'shardChoser' => MultiTenantShardChoser::class,
298
        ]);
299
300
        self::assertEquals('foo', $conn->getPassword());
301
302
        $conn->connect(1);
303
        self::assertEquals('bar', $conn->getPassword());
304
    }
305
306
    /**
307
     * @param mixed[] $parameters
308
     */
309
    private function createConnection(array $parameters) : PoolingShardConnection
310
    {
311
        return DriverManager::getConnection(array_merge(
0 ignored issues
show
Bug Best Practice introduced by
The expression return Doctrine\DBAL\Dri...::class), $parameters)) returns the type Doctrine\DBAL\Connection which includes types incompatible with the type-hinted return Doctrine\DBAL\Sharding\PoolingShardConnection.
Loading history...
312
            ['wrapperClass' => PoolingShardConnection::class],
313
            $parameters
314
        ));
315
    }
316
}
317