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 | |||
20 | namespace Doctrine\Tests\DBAL\Sharding; |
||
21 | |||
22 | use Doctrine\DBAL\DriverManager; |
||
23 | use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser; |
||
24 | |||
25 | class PoolingShardConnectionTest extends \PHPUnit\Framework\TestCase |
||
26 | { |
||
27 | public function testConnect() |
||
28 | { |
||
29 | $conn = DriverManager::getConnection(array( |
||
30 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
31 | 'driver' => 'pdo_sqlite', |
||
32 | 'global' => array('memory' => true), |
||
33 | 'shards' => array( |
||
34 | array('id' => 1, 'memory' => true), |
||
35 | array('id' => 2, 'memory' => true), |
||
36 | ), |
||
37 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
38 | )); |
||
39 | |||
40 | self::assertFalse($conn->isConnected(0)); |
||
41 | $conn->connect(0); |
||
42 | self::assertEquals(1, $conn->fetchColumn('SELECT 1')); |
||
43 | self::assertTrue($conn->isConnected(0)); |
||
44 | |||
45 | self::assertFalse($conn->isConnected(1)); |
||
46 | $conn->connect(1); |
||
47 | self::assertEquals(1, $conn->fetchColumn('SELECT 1')); |
||
48 | self::assertTrue($conn->isConnected(1)); |
||
49 | |||
50 | self::assertFalse($conn->isConnected(2)); |
||
51 | $conn->connect(2); |
||
52 | self::assertEquals(1, $conn->fetchColumn('SELECT 1')); |
||
53 | self::assertTrue($conn->isConnected(2)); |
||
54 | |||
55 | $conn->close(); |
||
56 | self::assertFalse($conn->isConnected(0)); |
||
57 | self::assertFalse($conn->isConnected(1)); |
||
58 | self::assertFalse($conn->isConnected(2)); |
||
59 | } |
||
60 | |||
61 | public function testNoGlobalServerException() |
||
62 | { |
||
63 | $this->expectException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations."); |
||
64 | |||
65 | DriverManager::getConnection(array( |
||
66 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
67 | 'driver' => 'pdo_sqlite', |
||
68 | 'shards' => array( |
||
69 | array('id' => 1, 'memory' => true), |
||
70 | array('id' => 2, 'memory' => true), |
||
71 | ), |
||
72 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
73 | )); |
||
74 | } |
||
75 | |||
76 | public function testNoShardsServersException() |
||
77 | { |
||
78 | $this->expectException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations."); |
||
79 | |||
80 | DriverManager::getConnection(array( |
||
81 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
82 | 'driver' => 'pdo_sqlite', |
||
83 | 'global' => array('memory' => true), |
||
84 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
85 | )); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function testNoShardsChoserException() |
|
0 ignored issues
–
show
|
|||
89 | { |
||
90 | $this->expectException('InvalidArgumentException', "Missing Shard Choser configuration 'shardChoser'"); |
||
91 | |||
92 | DriverManager::getConnection(array( |
||
93 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
94 | 'driver' => 'pdo_sqlite', |
||
95 | 'global' => array('memory' => true), |
||
96 | 'shards' => array( |
||
97 | array('id' => 1, 'memory' => true), |
||
98 | array('id' => 2, 'memory' => true), |
||
99 | ), |
||
100 | )); |
||
101 | } |
||
102 | |||
103 | View Code Duplication | public function testShardChoserWrongInstance() |
|
104 | { |
||
105 | $this->expectException('InvalidArgumentException', "The 'shardChoser' configuration is not a valid instance of Doctrine\DBAL\Sharding\ShardChoser\ShardChoser"); |
||
106 | |||
107 | DriverManager::getConnection(array( |
||
108 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
109 | 'driver' => 'pdo_sqlite', |
||
110 | 'global' => array('memory' => true), |
||
111 | 'shards' => array( |
||
112 | array('id' => 1, 'memory' => true), |
||
113 | array('id' => 2, 'memory' => true), |
||
114 | ), |
||
115 | 'shardChoser' => new \stdClass, |
||
116 | )); |
||
117 | } |
||
118 | |||
119 | View Code Duplication | public function testShardNonNumericId() |
|
120 | { |
||
121 | $this->expectException('InvalidArgumentException', "Shard Id has to be a non-negative number."); |
||
122 | |||
123 | DriverManager::getConnection(array( |
||
124 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
125 | 'driver' => 'pdo_sqlite', |
||
126 | 'global' => array('memory' => true), |
||
127 | 'shards' => array( |
||
128 | array('id' => 'foo', 'memory' => true), |
||
129 | ), |
||
130 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
131 | )); |
||
132 | } |
||
133 | |||
134 | View Code Duplication | public function testShardMissingId() |
|
135 | { |
||
136 | $this->expectException('InvalidArgumentException', "Missing 'id' for one configured shard. Please specify a unique shard-id."); |
||
137 | |||
138 | DriverManager::getConnection(array( |
||
139 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
140 | 'driver' => 'pdo_sqlite', |
||
141 | 'global' => array('memory' => true), |
||
142 | 'shards' => array( |
||
143 | array('memory' => true), |
||
144 | ), |
||
145 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
146 | )); |
||
147 | } |
||
148 | |||
149 | View Code Duplication | public function testDuplicateShardId() |
|
150 | { |
||
151 | $this->expectException('InvalidArgumentException', "Shard 1 is duplicated in the configuration."); |
||
152 | |||
153 | DriverManager::getConnection(array( |
||
154 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
155 | 'driver' => 'pdo_sqlite', |
||
156 | 'global' => array('memory' => true), |
||
157 | 'shards' => array( |
||
158 | array('id' => 1, 'memory' => true), |
||
159 | array('id' => 1, 'memory' => true), |
||
160 | ), |
||
161 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
162 | )); |
||
163 | } |
||
164 | |||
165 | public function testSwitchShardWithOpenTransactionException() |
||
166 | { |
||
167 | $conn = DriverManager::getConnection(array( |
||
168 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
169 | 'driver' => 'pdo_sqlite', |
||
170 | 'global' => array('memory' => true), |
||
171 | 'shards' => array( |
||
172 | array('id' => 1, 'memory' => true), |
||
173 | ), |
||
174 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
175 | )); |
||
176 | |||
177 | $conn->beginTransaction(); |
||
178 | |||
179 | $this->expectException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard when transaction is active.'); |
||
180 | $conn->connect(1); |
||
181 | } |
||
182 | |||
183 | public function testGetActiveShardId() |
||
184 | { |
||
185 | $conn = DriverManager::getConnection(array( |
||
186 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
187 | 'driver' => 'pdo_sqlite', |
||
188 | 'global' => array('memory' => true), |
||
189 | 'shards' => array( |
||
190 | array('id' => 1, 'memory' => true), |
||
191 | ), |
||
192 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
193 | )); |
||
194 | |||
195 | self::assertNull($conn->getActiveShardId()); |
||
196 | |||
197 | $conn->connect(0); |
||
198 | self::assertEquals(0, $conn->getActiveShardId()); |
||
199 | |||
200 | $conn->connect(1); |
||
201 | self::assertEquals(1, $conn->getActiveShardId()); |
||
202 | |||
203 | $conn->close(); |
||
204 | self::assertNull($conn->getActiveShardId()); |
||
205 | } |
||
206 | |||
207 | public function testGetParamsOverride() |
||
208 | { |
||
209 | $conn = DriverManager::getConnection(array( |
||
210 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
211 | 'driver' => 'pdo_sqlite', |
||
212 | 'global' => array('memory' => true, 'host' => 'localhost'), |
||
213 | 'shards' => array( |
||
214 | array('id' => 1, 'memory' => true, 'host' => 'foo'), |
||
215 | ), |
||
216 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
217 | )); |
||
218 | |||
219 | self::assertEquals(array( |
||
220 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
221 | 'driver' => 'pdo_sqlite', |
||
222 | 'global' => array('memory' => true, 'host' => 'localhost'), |
||
223 | 'shards' => array( |
||
224 | array('id' => 1, 'memory' => true, 'host' => 'foo'), |
||
225 | ), |
||
226 | 'shardChoser' => new MultiTenantShardChoser(), |
||
227 | 'memory' => true, |
||
228 | 'host' => 'localhost', |
||
229 | ), $conn->getParams()); |
||
230 | |||
231 | $conn->connect(1); |
||
232 | self::assertEquals(array( |
||
233 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
234 | 'driver' => 'pdo_sqlite', |
||
235 | 'global' => array('memory' => true, 'host' => 'localhost'), |
||
236 | 'shards' => array( |
||
237 | array('id' => 1, 'memory' => true, 'host' => 'foo'), |
||
238 | ), |
||
239 | 'shardChoser' => new MultiTenantShardChoser(), |
||
240 | 'id' => 1, |
||
241 | 'memory' => true, |
||
242 | 'host' => 'foo', |
||
243 | ), $conn->getParams()); |
||
244 | } |
||
245 | |||
246 | View Code Duplication | public function testGetHostOverride() |
|
247 | { |
||
248 | $conn = DriverManager::getConnection(array( |
||
249 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
250 | 'driver' => 'pdo_sqlite', |
||
251 | 'host' => 'localhost', |
||
252 | 'global' => array('memory' => true), |
||
253 | 'shards' => array( |
||
254 | array('id' => 1, 'memory' => true, 'host' => 'foo'), |
||
255 | ), |
||
256 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
257 | )); |
||
258 | |||
259 | self::assertEquals('localhost', $conn->getHost()); |
||
260 | |||
261 | $conn->connect(1); |
||
262 | self::assertEquals('foo', $conn->getHost()); |
||
263 | } |
||
264 | |||
265 | View Code Duplication | public function testGetPortOverride() |
|
266 | { |
||
267 | $conn = DriverManager::getConnection(array( |
||
268 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
269 | 'driver' => 'pdo_sqlite', |
||
270 | 'port' => 3306, |
||
271 | 'global' => array('memory' => true), |
||
272 | 'shards' => array( |
||
273 | array('id' => 1, 'memory' => true, 'port' => 3307), |
||
274 | ), |
||
275 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
276 | )); |
||
277 | |||
278 | self::assertEquals(3306, $conn->getPort()); |
||
279 | |||
280 | $conn->connect(1); |
||
281 | self::assertEquals(3307, $conn->getPort()); |
||
282 | } |
||
283 | |||
284 | View Code Duplication | public function testGetUsernameOverride() |
|
285 | { |
||
286 | $conn = DriverManager::getConnection(array( |
||
287 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
288 | 'driver' => 'pdo_sqlite', |
||
289 | 'user' => 'foo', |
||
290 | 'global' => array('memory' => true), |
||
291 | 'shards' => array( |
||
292 | array('id' => 1, 'memory' => true, 'user' => 'bar'), |
||
293 | ), |
||
294 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
295 | )); |
||
296 | |||
297 | self::assertEquals('foo', $conn->getUsername()); |
||
298 | |||
299 | $conn->connect(1); |
||
300 | self::assertEquals('bar', $conn->getUsername()); |
||
301 | } |
||
302 | |||
303 | View Code Duplication | public function testGetPasswordOverride() |
|
304 | { |
||
305 | $conn = DriverManager::getConnection(array( |
||
306 | 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection', |
||
307 | 'driver' => 'pdo_sqlite', |
||
308 | 'password' => 'foo', |
||
309 | 'global' => array('memory' => true), |
||
310 | 'shards' => array( |
||
311 | array('id' => 1, 'memory' => true, 'password' => 'bar'), |
||
312 | ), |
||
313 | 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser', |
||
314 | )); |
||
315 | |||
316 | self::assertEquals('foo', $conn->getPassword()); |
||
317 | |||
318 | $conn->connect(1); |
||
319 | self::assertEquals('bar', $conn->getPassword()); |
||
320 | } |
||
321 | } |
||
322 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.