1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
namespace Doctrine\Tests\DBAL\Sharding; |
20
|
|
|
|
21
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
22
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardManager; |
23
|
|
|
use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser; |
24
|
|
|
use PHPUnit\Framework\TestCase; |
25
|
|
|
|
26
|
|
|
class PoolingShardManagerTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
private function createConnectionMock() |
29
|
|
|
{ |
30
|
|
|
return $this->getMockBuilder(PoolingShardConnection::class) |
31
|
|
|
->setMethods(['connect', 'getParams', 'fetchAll']) |
32
|
|
|
->disableOriginalConstructor() |
33
|
|
|
->getMock(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function createPassthroughShardChoser() |
37
|
|
|
{ |
38
|
|
|
$mock = $this->createMock(ShardChoser::class); |
39
|
|
|
$mock->expects($this->any()) |
40
|
|
|
->method('pickShard') |
41
|
|
|
->will($this->returnCallback(static function ($value) { |
42
|
|
|
return $value; |
43
|
|
|
})); |
44
|
|
|
return $mock; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function createStaticShardChooser() |
48
|
|
|
{ |
49
|
|
|
$mock = $this->createMock(ShardChoser::class); |
50
|
|
|
$mock->expects($this->any()) |
51
|
|
|
->method('pickShard') |
52
|
|
|
->willReturn(1); |
53
|
|
|
return $mock; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testSelectGlobal() |
57
|
|
|
{ |
58
|
|
|
$conn = $this->createConnectionMock(); |
59
|
|
|
$conn->expects($this->once())->method('connect')->with($this->equalTo(0)); |
60
|
|
|
|
61
|
|
|
$shardManager = new PoolingShardManager($conn); |
62
|
|
|
$shardManager->selectGlobal(); |
63
|
|
|
|
64
|
|
|
self::assertNull($shardManager->getCurrentDistributionValue()); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testSelectShard() |
68
|
|
|
{ |
69
|
|
|
$shardId = 10; |
70
|
|
|
$conn = $this->createConnectionMock(); |
71
|
|
|
$conn->expects($this->at(0))->method('getParams')->will($this->returnValue(['shardChoser' => $this->createPassthroughShardChoser()])); |
72
|
|
|
$conn->expects($this->at(1))->method('connect')->with($this->equalTo($shardId)); |
73
|
|
|
|
74
|
|
|
$shardManager = new PoolingShardManager($conn); |
75
|
|
|
$shardManager->selectShard($shardId); |
76
|
|
|
|
77
|
|
|
self::assertEquals($shardId, $shardManager->getCurrentDistributionValue()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testGetShards() |
81
|
|
|
{ |
82
|
|
|
$conn = $this->createConnectionMock(); |
83
|
|
|
$conn->expects($this->any())->method('getParams')->will( |
84
|
|
|
$this->returnValue( |
85
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createPassthroughShardChoser()] |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$shardManager = new PoolingShardManager($conn); |
90
|
|
|
$shards = $shardManager->getShards(); |
91
|
|
|
|
92
|
|
|
self::assertEquals([['id' => 1], ['id' => 2]], $shards); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testQueryAll() |
96
|
|
|
{ |
97
|
|
|
$sql = 'SELECT * FROM table'; |
98
|
|
|
$params = [1]; |
99
|
|
|
$types = [1]; |
100
|
|
|
|
101
|
|
|
$conn = $this->createConnectionMock(); |
102
|
|
|
$conn->expects($this->at(0))->method('getParams')->will($this->returnValue( |
103
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createPassthroughShardChoser()] |
104
|
|
|
)); |
105
|
|
|
$conn->expects($this->at(1))->method('getParams')->will($this->returnValue( |
106
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createPassthroughShardChoser()] |
107
|
|
|
)); |
108
|
|
|
$conn->expects($this->at(2))->method('connect')->with($this->equalTo(1)); |
109
|
|
|
$conn->expects($this->at(3)) |
110
|
|
|
->method('fetchAll') |
111
|
|
|
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) |
112
|
|
|
->will($this->returnValue([ ['id' => 1] ])); |
113
|
|
|
$conn->expects($this->at(4))->method('connect')->with($this->equalTo(2)); |
114
|
|
|
$conn->expects($this->at(5)) |
115
|
|
|
->method('fetchAll') |
116
|
|
|
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) |
117
|
|
|
->will($this->returnValue([ ['id' => 2] ])); |
118
|
|
|
|
119
|
|
|
$shardManager = new PoolingShardManager($conn); |
120
|
|
|
$result = $shardManager->queryAll($sql, $params, $types); |
121
|
|
|
|
122
|
|
|
self::assertEquals([['id' => 1], ['id' => 2]], $result); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testQueryAllWithStaticShardChoser() |
126
|
|
|
{ |
127
|
|
|
$sql = 'SELECT * FROM table'; |
128
|
|
|
$params = [1]; |
129
|
|
|
$types = [1]; |
130
|
|
|
|
131
|
|
|
$conn = $this->createConnectionMock(); |
132
|
|
|
$conn->expects($this->at(0))->method('getParams')->will($this->returnValue( |
133
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createStaticShardChooser()] |
134
|
|
|
)); |
135
|
|
|
$conn->expects($this->at(1))->method('getParams')->will($this->returnValue( |
136
|
|
|
['shards' => [ ['id' => 1], ['id' => 2] ], 'shardChoser' => $this->createStaticShardChooser()] |
137
|
|
|
)); |
138
|
|
|
$conn->expects($this->at(2))->method('connect')->with($this->equalTo(1)); |
139
|
|
|
$conn->expects($this->at(3)) |
140
|
|
|
->method('fetchAll') |
141
|
|
|
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) |
142
|
|
|
->will($this->returnValue([ ['id' => 1] ])); |
143
|
|
|
$conn->expects($this->at(4))->method('connect')->with($this->equalTo(2)); |
144
|
|
|
$conn->expects($this->at(5)) |
145
|
|
|
->method('fetchAll') |
146
|
|
|
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) |
147
|
|
|
->will($this->returnValue([ ['id' => 2] ])); |
148
|
|
|
|
149
|
|
|
$shardManager = new PoolingShardManager($conn); |
150
|
|
|
$result = $shardManager->queryAll($sql, $params, $types); |
151
|
|
|
|
152
|
|
|
self::assertEquals([['id' => 1], ['id' => 2]], $result); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.