1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Sharding; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
6
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardManager; |
7
|
|
|
use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class PoolingShardManagerTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
private function createConnectionMock() |
13
|
|
|
{ |
14
|
|
|
return $this->getMockBuilder(PoolingShardConnection::class) |
15
|
|
|
->setMethods(['connect', 'getParams', 'fetchAll']) |
16
|
|
|
->disableOriginalConstructor() |
17
|
|
|
->getMock(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function createPassthroughShardChoser() |
21
|
|
|
{ |
22
|
|
|
$mock = $this->createMock(ShardChoser::class); |
23
|
|
|
$mock->expects($this->any()) |
24
|
|
|
->method('pickShard') |
25
|
|
|
->will($this->returnCallback(static function ($value) { |
26
|
|
|
return $value; |
27
|
|
|
})); |
28
|
|
|
return $mock; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function createStaticShardChooser() |
32
|
|
|
{ |
33
|
|
|
$mock = $this->createMock(ShardChoser::class); |
34
|
|
|
$mock->expects($this->any()) |
35
|
|
|
->method('pickShard') |
36
|
|
|
->willReturn(1); |
37
|
|
|
return $mock; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testSelectGlobal() |
41
|
|
|
{ |
42
|
|
|
$conn = $this->createConnectionMock(); |
43
|
|
|
$conn->expects($this->once())->method('connect')->with($this->equalTo(0)); |
44
|
|
|
|
45
|
|
|
$shardManager = new PoolingShardManager($conn); |
46
|
|
|
$shardManager->selectGlobal(); |
47
|
|
|
|
48
|
|
|
self::assertNull($shardManager->getCurrentDistributionValue()); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testSelectShard() |
52
|
|
|
{ |
53
|
|
|
$shardId = 10; |
54
|
|
|
$conn = $this->createConnectionMock(); |
55
|
|
|
$conn->expects($this->at(0))->method('getParams')->will($this->returnValue(['shardChoser' => $this->createPassthroughShardChoser()])); |
56
|
|
|
$conn->expects($this->at(1))->method('connect')->with($this->equalTo($shardId)); |
57
|
|
|
|
58
|
|
|
$shardManager = new PoolingShardManager($conn); |
59
|
|
|
$shardManager->selectShard($shardId); |
60
|
|
|
|
61
|
|
|
self::assertEquals($shardId, $shardManager->getCurrentDistributionValue()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGetShards() |
65
|
|
|
{ |
66
|
|
|
$conn = $this->createConnectionMock(); |
67
|
|
|
$conn->expects($this->any())->method('getParams')->will( |
68
|
|
|
$this->returnValue( |
69
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createPassthroughShardChoser()] |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$shardManager = new PoolingShardManager($conn); |
74
|
|
|
$shards = $shardManager->getShards(); |
75
|
|
|
|
76
|
|
|
self::assertEquals([['id' => 1], ['id' => 2]], $shards); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testQueryAll() |
80
|
|
|
{ |
81
|
|
|
$sql = 'SELECT * FROM table'; |
82
|
|
|
$params = [1]; |
83
|
|
|
$types = [1]; |
84
|
|
|
|
85
|
|
|
$conn = $this->createConnectionMock(); |
86
|
|
|
$conn->expects($this->at(0))->method('getParams')->will($this->returnValue( |
87
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createPassthroughShardChoser()] |
88
|
|
|
)); |
89
|
|
|
$conn->expects($this->at(1))->method('getParams')->will($this->returnValue( |
90
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createPassthroughShardChoser()] |
91
|
|
|
)); |
92
|
|
|
$conn->expects($this->at(2))->method('connect')->with($this->equalTo(1)); |
93
|
|
|
$conn->expects($this->at(3)) |
94
|
|
|
->method('fetchAll') |
95
|
|
|
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) |
96
|
|
|
->will($this->returnValue([ ['id' => 1] ])); |
97
|
|
|
$conn->expects($this->at(4))->method('connect')->with($this->equalTo(2)); |
98
|
|
|
$conn->expects($this->at(5)) |
99
|
|
|
->method('fetchAll') |
100
|
|
|
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) |
101
|
|
|
->will($this->returnValue([ ['id' => 2] ])); |
102
|
|
|
|
103
|
|
|
$shardManager = new PoolingShardManager($conn); |
104
|
|
|
$result = $shardManager->queryAll($sql, $params, $types); |
105
|
|
|
|
106
|
|
|
self::assertEquals([['id' => 1], ['id' => 2]], $result); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testQueryAllWithStaticShardChoser() |
110
|
|
|
{ |
111
|
|
|
$sql = 'SELECT * FROM table'; |
112
|
|
|
$params = [1]; |
113
|
|
|
$types = [1]; |
114
|
|
|
|
115
|
|
|
$conn = $this->createConnectionMock(); |
116
|
|
|
$conn->expects($this->at(0))->method('getParams')->will($this->returnValue( |
117
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createStaticShardChooser()] |
118
|
|
|
)); |
119
|
|
|
$conn->expects($this->at(1))->method('getParams')->will($this->returnValue( |
120
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createStaticShardChooser()] |
121
|
|
|
)); |
122
|
|
|
$conn->expects($this->at(2))->method('connect')->with($this->equalTo(1)); |
123
|
|
|
$conn->expects($this->at(3)) |
124
|
|
|
->method('fetchAll') |
125
|
|
|
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) |
126
|
|
|
->will($this->returnValue([ ['id' => 1] ])); |
127
|
|
|
$conn->expects($this->at(4))->method('connect')->with($this->equalTo(2)); |
128
|
|
|
$conn->expects($this->at(5)) |
129
|
|
|
->method('fetchAll') |
130
|
|
|
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) |
131
|
|
|
->will($this->returnValue([ ['id' => 2] ])); |
132
|
|
|
|
133
|
|
|
$shardManager = new PoolingShardManager($conn); |
134
|
|
|
$result = $shardManager->queryAll($sql, $params, $types); |
135
|
|
|
|
136
|
|
|
self::assertEquals([['id' => 1], ['id' => 2]], $result); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
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.