1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\TestSupport; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
10
|
|
|
use Yiisoft\Db\Driver\DriverInterface; |
11
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
12
|
|
|
use Yiisoft\Db\Exception\Exception; |
13
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
14
|
|
|
|
15
|
|
|
use Yiisoft\Db\Schema\TableNameInterface; |
16
|
|
|
use function PHPUnit\Framework\assertEquals; |
17
|
|
|
use function serialize; |
18
|
|
|
use function unserialize; |
19
|
|
|
|
20
|
|
|
trait TestConnectionTrait |
21
|
|
|
{ |
22
|
|
|
public function testCacheKey(): void |
23
|
|
|
{ |
24
|
|
|
$db = $this->getConnection(); |
|
|
|
|
25
|
|
|
$this->assertEquals([$this->dsn, $this->username], $db->getCacheKey()); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testGetName(): void |
29
|
|
|
{ |
30
|
|
|
$db = $this->getConnection(); |
31
|
|
|
$this->assertEquals($this->drivername, $db->getName()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testOpenClose(): void |
35
|
|
|
{ |
36
|
|
|
$db = $this->getConnection(); |
37
|
|
|
$this->assertFalse($db->isActive()); |
|
|
|
|
38
|
|
|
$this->assertNull($db->getPDO()); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$db->open(); |
41
|
|
|
$this->assertTrue($db->isActive()); |
|
|
|
|
42
|
|
|
$this->assertInstanceOf(PDO::class, $db->getPDO()); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$db->close(); |
45
|
|
|
$this->assertFalse($db->isActive()); |
46
|
|
|
$this->assertNull($db->getPDO()); |
47
|
|
|
|
48
|
|
|
$db = $this->getConnection(false, 'unknown::memory:'); |
49
|
|
|
$this->expectException(Exception::class); |
|
|
|
|
50
|
|
|
$this->expectExceptionMessage('could not find driver'); |
|
|
|
|
51
|
|
|
$db->open(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testSerialize(): void |
55
|
|
|
{ |
56
|
|
|
$db = $this->getConnection(); |
57
|
|
|
|
58
|
|
|
$db->open(); |
59
|
|
|
|
60
|
|
|
$serialized = serialize($db); |
61
|
|
|
|
62
|
|
|
$this->assertNotNull($db->getPDO()); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$unserialized = unserialize($serialized); |
65
|
|
|
|
66
|
|
|
$this->assertInstanceOf(ConnectionPDOInterface::class, $unserialized); |
67
|
|
|
$this->assertNull($unserialized->getPDO()); |
68
|
|
|
$this->assertEquals(123, $unserialized->createCommand('SELECT 123')->queryScalar()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testTransaction(): void |
72
|
|
|
{ |
73
|
|
|
$db = $this->getConnection(true); |
74
|
|
|
|
75
|
|
|
$this->assertNull($db->getTransaction()); |
76
|
|
|
|
77
|
|
|
$transaction = $db->beginTransaction(); |
78
|
|
|
|
79
|
|
|
$this->assertNotNull($db->getTransaction()); |
80
|
|
|
$this->assertTrue($transaction->isActive()); |
81
|
|
|
|
82
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute(); |
83
|
|
|
|
84
|
|
|
$transaction->rollBack(); |
85
|
|
|
|
86
|
|
|
$this->assertFalse($transaction->isActive()); |
87
|
|
|
$this->assertNull($db->getTransaction()); |
88
|
|
|
$this->assertEquals(0, $db->createCommand( |
89
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'" |
90
|
|
|
)->queryScalar()); |
91
|
|
|
|
92
|
|
|
$transaction = $db->beginTransaction(); |
93
|
|
|
|
94
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute(); |
95
|
|
|
|
96
|
|
|
$transaction->commit(); |
97
|
|
|
|
98
|
|
|
$this->assertFalse($transaction->isActive()); |
99
|
|
|
$this->assertNull($db->getTransaction()); |
100
|
|
|
$this->assertEquals(1, $db->createCommand( |
101
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'" |
102
|
|
|
)->queryScalar()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testRollbackTransactionsWithSavePoints(): void |
106
|
|
|
{ |
107
|
|
|
$db = $this->getConnection(true); |
108
|
|
|
$db->open(); |
109
|
|
|
|
110
|
|
|
$transaction = $db->beginTransaction(); |
111
|
|
|
assertEquals(1, $transaction->getLevel()); |
112
|
|
|
|
113
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute(); |
114
|
|
|
|
115
|
|
|
$transaction->begin(); |
116
|
|
|
assertEquals(2, $transaction->getLevel()); |
117
|
|
|
|
118
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute(); |
119
|
|
|
|
120
|
|
|
$transaction->rollBack(); |
121
|
|
|
assertEquals(1, $transaction->getLevel()); |
122
|
|
|
$this->assertTrue($transaction->isActive()); |
123
|
|
|
|
124
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute(); |
125
|
|
|
|
126
|
|
|
$transaction->rollBack(); |
127
|
|
|
assertEquals(0, $transaction->getLevel()); |
128
|
|
|
|
129
|
|
|
$this->assertFalse($transaction->isActive()); |
130
|
|
|
$this->assertNull($db->getTransaction()); |
131
|
|
|
$this->assertEquals(0, $db->createCommand( |
132
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'" |
133
|
|
|
)->queryScalar()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testPartialRollbackTransactionsWithSavePoints(): void |
137
|
|
|
{ |
138
|
|
|
$db = $this->getConnection(true); |
139
|
|
|
$db->open(); |
140
|
|
|
|
141
|
|
|
$transaction = $db->beginTransaction(); |
142
|
|
|
assertEquals(1, $transaction->getLevel()); |
143
|
|
|
|
144
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction1'])->execute(); |
145
|
|
|
|
146
|
|
|
$transaction->begin(); |
147
|
|
|
assertEquals(2, $transaction->getLevel()); |
148
|
|
|
|
149
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction2'])->execute(); |
150
|
|
|
|
151
|
|
|
$transaction->rollBack(); |
152
|
|
|
assertEquals(1, $transaction->getLevel()); |
153
|
|
|
$this->assertTrue($transaction->isActive()); |
154
|
|
|
|
155
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction3'])->execute(); |
156
|
|
|
|
157
|
|
|
$transaction->commit(); |
158
|
|
|
assertEquals(0, $transaction->getLevel()); |
159
|
|
|
|
160
|
|
|
$this->assertFalse($transaction->isActive()); |
161
|
|
|
$this->assertNull($db->getTransaction()); |
162
|
|
|
$this->assertEquals(1, $db->createCommand( |
163
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction1'" |
164
|
|
|
)->queryScalar()); |
165
|
|
|
$this->assertEquals(0, $db->createCommand( |
166
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction2'" |
167
|
|
|
)->queryScalar()); |
168
|
|
|
$this->assertEquals(1, $db->createCommand( |
169
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction3'" |
170
|
|
|
)->queryScalar()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testCommitTransactionsWithSavepoints(): void |
174
|
|
|
{ |
175
|
|
|
$db = $this->getConnection(true); |
176
|
|
|
|
177
|
|
|
$transaction = $db->beginTransaction(); |
178
|
|
|
assertEquals(1, $transaction->getLevel()); |
179
|
|
|
|
180
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction1'])->execute(); |
181
|
|
|
|
182
|
|
|
$transaction->begin(); |
183
|
|
|
assertEquals(2, $transaction->getLevel()); |
184
|
|
|
|
185
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction2'])->execute(); |
186
|
|
|
|
187
|
|
|
$transaction->commit(); |
188
|
|
|
assertEquals(1, $transaction->getLevel()); |
189
|
|
|
|
190
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction3'])->execute(); |
191
|
|
|
|
192
|
|
|
$transaction->commit(); |
193
|
|
|
assertEquals(0, $transaction->getLevel()); |
194
|
|
|
|
195
|
|
|
$this->assertFalse($transaction->isActive()); |
196
|
|
|
$this->assertNull($db->getTransaction()); |
197
|
|
|
$this->assertEquals(1, $db->createCommand( |
198
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction1'" |
199
|
|
|
)->queryScalar()); |
200
|
|
|
$this->assertEquals(1, $db->createCommand( |
201
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction2'" |
202
|
|
|
)->queryScalar()); |
203
|
|
|
$this->assertEquals(1, $db->createCommand( |
204
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction3'" |
205
|
|
|
)->queryScalar()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testTransactionShortcutException(): void |
209
|
|
|
{ |
210
|
|
|
$db = $this->getConnection(true); |
211
|
|
|
|
212
|
|
|
$this->expectException(Exception::class); |
213
|
|
|
|
214
|
|
|
$db->transaction(function () use ($db) { |
215
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute(); |
216
|
|
|
throw new Exception('Exception in transaction shortcut'); |
217
|
|
|
}); |
218
|
|
|
$profilesCount = $db->createCommand( |
219
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction shortcut'" |
220
|
|
|
)->queryScalar(); |
221
|
|
|
$this->assertEquals(0, $profilesCount, 'profile should not be inserted in transaction shortcut'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testTransactionShortcutCorrect(): void |
225
|
|
|
{ |
226
|
|
|
$db = $this->getConnection(true); |
227
|
|
|
|
228
|
|
|
$result = $db->transaction(static function () use ($db) { |
229
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute(); |
230
|
|
|
return true; |
231
|
|
|
}); |
232
|
|
|
|
233
|
|
|
$this->assertTrue($result, 'transaction shortcut valid value should be returned from callback'); |
234
|
|
|
|
235
|
|
|
$profilesCount = $db->createCommand( |
236
|
|
|
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction shortcut'" |
237
|
|
|
)->queryScalar(); |
238
|
|
|
|
239
|
|
|
$this->assertEquals(1, $profilesCount, 'profile should be inserted in transaction shortcut'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Tests nested transactions with partial rollback. |
244
|
|
|
* |
245
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/9851} |
246
|
|
|
*/ |
247
|
|
|
public function testNestedTransaction(): void |
248
|
|
|
{ |
249
|
|
|
$db = $this->getConnection(); |
250
|
|
|
|
251
|
|
|
$db->transaction(function (ConnectionInterface $db) { |
252
|
|
|
$this->assertNotNull($db->getTransaction()); |
253
|
|
|
|
254
|
|
|
$db->transaction(function (ConnectionInterface $db) { |
255
|
|
|
$transaction = $db->getTransaction(); |
256
|
|
|
$this->assertNotNull($transaction); |
257
|
|
|
$transaction->rollBack(); |
258
|
|
|
}); |
259
|
|
|
|
260
|
|
|
$this->assertNotNull($db->getTransaction()); |
261
|
|
|
}); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testNestedTransactionNotSupported(): void |
265
|
|
|
{ |
266
|
|
|
$db = $this->getConnection(); |
267
|
|
|
|
268
|
|
|
$db->setEnableSavepoint(false); |
269
|
|
|
|
270
|
|
|
$db->transaction(function (ConnectionInterface $db) { |
271
|
|
|
$this->assertNotNull($db->getTransaction()); |
272
|
|
|
$this->expectException(NotSupportedException::class); |
273
|
|
|
$db->beginTransaction(); |
274
|
|
|
}); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function testEnableQueryLog(): void |
278
|
|
|
{ |
279
|
|
|
$db = $this->getConnection(); |
280
|
|
|
|
281
|
|
|
foreach (['qlog1', 'qlog2', 'qlog3', 'qlog4'] as $table) { |
282
|
|
|
if ($db->getTableSchema($table, true) !== null) { |
283
|
|
|
$db->createCommand()->dropTable($table)->execute(); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/* profiling and logging */ |
288
|
|
|
$db->setLogger($this->logger); |
289
|
|
|
$db->setProfiler($this->profiler); |
290
|
|
|
|
291
|
|
|
$this->assertNotNull($this->logger); |
292
|
|
|
$this->assertNotNull($this->profiler); |
293
|
|
|
|
294
|
|
|
$this->logger->flush(); |
295
|
|
|
$this->profiler->flush(); |
296
|
|
|
|
297
|
|
|
$db->createCommand()->createTable('qlog1', ['id' => 'pk'])->execute(); |
298
|
|
|
|
299
|
|
|
$this->assertCount(1, $this->getInaccessibleProperty($this->logger, 'messages')); |
|
|
|
|
300
|
|
|
$this->assertCount(1, $this->getInaccessibleProperty($this->profiler, 'messages')); |
301
|
|
|
$this->assertNotNull($db->getTableSchema('qlog1', true)); |
302
|
|
|
|
303
|
|
|
$this->logger->flush(); |
304
|
|
|
$this->profiler->flush(); |
305
|
|
|
|
306
|
|
|
$db->createCommand('SELECT * FROM {{qlog1}}')->queryAll(); |
307
|
|
|
|
308
|
|
|
$this->assertCount(1, $this->getInaccessibleProperty($this->logger, 'messages')); |
309
|
|
|
$this->assertCount(1, $this->getInaccessibleProperty($this->profiler, 'messages')); |
310
|
|
|
|
311
|
|
|
/* profiling only */ |
312
|
|
|
$db->setLogger(new NullLogger()); |
313
|
|
|
$db->setProfiler($this->profiler); |
314
|
|
|
|
315
|
|
|
$this->logger->flush(); |
316
|
|
|
$this->profiler->flush(); |
317
|
|
|
|
318
|
|
|
$db->createCommand()->createTable('qlog2', ['id' => 'pk'])->execute(); |
319
|
|
|
|
320
|
|
|
$this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages')); |
321
|
|
|
$this->assertCount(1, $this->getInaccessibleProperty($this->profiler, 'messages')); |
322
|
|
|
$this->assertNotNull($db->getTableSchema('qlog2', true)); |
323
|
|
|
|
324
|
|
|
$this->logger->flush(); |
325
|
|
|
$this->profiler->flush(); |
326
|
|
|
|
327
|
|
|
$db->createCommand('SELECT * FROM {{qlog2}}')->queryAll(); |
328
|
|
|
|
329
|
|
|
$this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages')); |
330
|
|
|
$this->assertCount(1, $this->getInaccessibleProperty($this->profiler, 'messages')); |
331
|
|
|
|
332
|
|
|
/* logging only */ |
333
|
|
|
$db->setLogger($this->logger); |
334
|
|
|
$db->setProfiler(null); |
335
|
|
|
|
336
|
|
|
$this->logger->flush(); |
337
|
|
|
$this->profiler->flush(); |
338
|
|
|
|
339
|
|
|
$db->createCommand()->createTable('qlog3', ['id' => 'pk'])->execute(); |
340
|
|
|
|
341
|
|
|
$this->assertCount(1, $this->getInaccessibleProperty($this->logger, 'messages')); |
342
|
|
|
$this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages')); |
343
|
|
|
$this->assertNotNull($db->getTableSchema('qlog3', true)); |
344
|
|
|
|
345
|
|
|
$this->logger->flush(); |
346
|
|
|
$this->profiler->flush(); |
347
|
|
|
|
348
|
|
|
$db->createCommand('SELECT * FROM {{qlog3}}')->queryAll(); |
349
|
|
|
|
350
|
|
|
$this->assertCount(1, $this->getInaccessibleProperty($this->logger, 'messages')); |
351
|
|
|
$this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages')); |
352
|
|
|
|
353
|
|
|
/* disabled */ |
354
|
|
|
$db->setLogger(new NullLogger()); |
355
|
|
|
$db->setProfiler(null); |
356
|
|
|
|
357
|
|
|
$this->logger->flush(); |
358
|
|
|
$this->profiler->flush(); |
359
|
|
|
|
360
|
|
|
$db->createCommand()->createTable('qlog4', ['id' => 'pk'])->execute(); |
361
|
|
|
|
362
|
|
|
$this->assertNotNull($db->getTableSchema('qlog4', true)); |
363
|
|
|
$this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages')); |
364
|
|
|
$this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages')); |
365
|
|
|
|
366
|
|
|
$db->createCommand('SELECT * FROM {{qlog4}}')->queryAll(); |
367
|
|
|
|
368
|
|
|
$this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages')); |
369
|
|
|
$this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages')); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function testExceptionContainsRawQuery(): void |
373
|
|
|
{ |
374
|
|
|
$db = $this->getConnection(); |
375
|
|
|
|
376
|
|
|
if ($db->getTableSchema('qlog1', true) === null) { |
377
|
|
|
$db->createCommand()->createTable('qlog1', ['id' => 'pk'])->execute(); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$db->setEmulatePrepare(true); |
381
|
|
|
|
382
|
|
|
/* profiling and logging */ |
383
|
|
|
$db->setLogger($this->logger); |
384
|
|
|
$db->setProfiler($this->profiler); |
385
|
|
|
|
386
|
|
|
$this->runExceptionTest($db); |
387
|
|
|
|
388
|
|
|
/* profiling only */ |
389
|
|
|
$db->setLogger(new NullLogger()); |
390
|
|
|
$db->setProfiler($this->profiler); |
391
|
|
|
|
392
|
|
|
$this->runExceptionTest($db); |
393
|
|
|
|
394
|
|
|
/* logging only */ |
395
|
|
|
$db->setLogger($this->logger); |
396
|
|
|
$db->setProfiler(null); |
397
|
|
|
|
398
|
|
|
$this->runExceptionTest($db); |
399
|
|
|
|
400
|
|
|
/* disabled */ |
401
|
|
|
$db->setLogger(new NullLogger()); |
402
|
|
|
$db->setProfiler(null); |
403
|
|
|
|
404
|
|
|
$this->runExceptionTest($db); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @param ConnectionInterface $db |
409
|
|
|
*/ |
410
|
|
|
private function runExceptionTest(ConnectionInterface $db): void |
411
|
|
|
{ |
412
|
|
|
$thrown = false; |
413
|
|
|
|
414
|
|
|
try { |
415
|
|
|
$db->createCommand('INSERT INTO qlog1(a) VALUES(:a);', [':a' => 1])->execute(); |
416
|
|
|
} catch (Exception $e) { |
417
|
|
|
$this->assertStringContainsString( |
|
|
|
|
418
|
|
|
'INSERT INTO qlog1(a) VALUES(1);', |
419
|
|
|
$e->getMessage(), |
420
|
|
|
'Exceptions message should contain raw SQL query: ' . (string) $e |
421
|
|
|
); |
422
|
|
|
|
423
|
|
|
$thrown = true; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
$this->assertTrue($thrown, 'An exception should have been thrown by the command.'); |
427
|
|
|
|
428
|
|
|
$thrown = false; |
429
|
|
|
|
430
|
|
|
try { |
431
|
|
|
$db->createCommand( |
432
|
|
|
'SELECT * FROM qlog1 WHERE id=:a ORDER BY nonexistingcolumn;', |
433
|
|
|
[':a' => 1] |
434
|
|
|
)->queryAll(); |
435
|
|
|
} catch (Exception $e) { |
436
|
|
|
$this->assertStringContainsString( |
437
|
|
|
'SELECT * FROM qlog1 WHERE id=1 ORDER BY nonexistingcolumn;', |
438
|
|
|
$e->getMessage(), |
439
|
|
|
'Exceptions message should contain raw SQL query: ' . (string) $e |
440
|
|
|
); |
441
|
|
|
|
442
|
|
|
$thrown = true; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
$this->assertTrue($thrown, 'An exception should have been thrown by the command.'); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Ensure database connection is reset on when a connection is cloned. |
450
|
|
|
* |
451
|
|
|
* Make sure each connection element has its own PDO instance i.e. own connection to the DB. |
452
|
|
|
* Also transaction elements should not be shared between two connections. |
453
|
|
|
*/ |
454
|
|
|
public function testClone(): void |
455
|
|
|
{ |
456
|
|
|
$db = $this->getConnection(); |
457
|
|
|
|
458
|
|
|
$this->assertNull($db->getTransaction()); |
459
|
|
|
$this->assertNull($db->getPDO()); |
460
|
|
|
|
461
|
|
|
$db->open(); |
462
|
|
|
|
463
|
|
|
$this->assertNull($db->getTransaction()); |
464
|
|
|
$this->assertNotNull($db->getPDO()); |
465
|
|
|
|
466
|
|
|
$conn2 = clone $db; |
467
|
|
|
|
468
|
|
|
$this->assertNull($db->getTransaction()); |
469
|
|
|
$this->assertNotNull($db->getPDO()); |
470
|
|
|
|
471
|
|
|
$this->assertNull($conn2->getTransaction()); |
472
|
|
|
$this->assertNull($conn2->getPDO()); |
473
|
|
|
|
474
|
|
|
$db->beginTransaction(); |
475
|
|
|
|
476
|
|
|
$this->assertNotNull($db->getTransaction()); |
477
|
|
|
$this->assertNotNull($db->getPDO()); |
478
|
|
|
|
479
|
|
|
$this->assertNull($conn2->getTransaction()); |
480
|
|
|
$this->assertNull($conn2->getPDO()); |
481
|
|
|
|
482
|
|
|
$conn3 = clone $db; |
483
|
|
|
|
484
|
|
|
$this->assertNotNull($db->getTransaction()); |
485
|
|
|
$this->assertNotNull($db->getPDO()); |
486
|
|
|
$this->assertNull($conn3->getTransaction()); |
487
|
|
|
$this->assertNull($conn3->getPDO()); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function testGetDriver(): void |
491
|
|
|
{ |
492
|
|
|
$db = $this->getConnection(); |
493
|
|
|
$this->assertInstanceOf(DriverInterface::class, $db->getDriver()); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @dataProvider createTableNameProvider |
498
|
|
|
*/ |
499
|
|
|
public function testCreateTableName( |
500
|
|
|
string $expectedName, |
501
|
|
|
string $expectedFullName, |
502
|
|
|
string $name, |
503
|
|
|
string $schemaName = null, |
504
|
|
|
string $catalogName = null, |
505
|
|
|
string $serverName = null |
506
|
|
|
): void { |
507
|
|
|
$db = $this->getConnection(); |
508
|
|
|
|
509
|
|
|
$tableName = $db->createTableName($name, $schemaName, $catalogName, $serverName); |
510
|
|
|
|
511
|
|
|
$this->assertInstanceOf(TableNameInterface::class, $tableName); |
512
|
|
|
|
513
|
|
|
$this->assertEquals($expectedName, $tableName->getTableName()); |
514
|
|
|
$this->assertEquals($expectedFullName, (string) $tableName); |
515
|
|
|
|
516
|
|
|
$this->assertEquals($db->getTablePrefix(), $tableName->getPrefix()); |
517
|
|
|
|
518
|
|
|
$this->assertEquals($name, $tableName->getRawTableName()); |
519
|
|
|
$this->assertEquals($schemaName, $tableName->getSchemaName()); |
520
|
|
|
$this->assertEquals($catalogName, $tableName->getCatalogName()); |
521
|
|
|
$this->assertEquals($serverName, $tableName->getServerName()); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* @dataProvider createTableNameWithPrefixProvider |
526
|
|
|
*/ |
527
|
|
|
public function testCreateTableNameWithPrefix( |
528
|
|
|
string $expectedName, |
529
|
|
|
string $expectedFullName, |
530
|
|
|
string $name, |
531
|
|
|
string $schemaName = null, |
532
|
|
|
string $catalogName = null, |
533
|
|
|
string $serverName = null |
534
|
|
|
): void { |
535
|
|
|
$db = $this->getConnection(); |
536
|
|
|
$db->setTablePrefix('pre_'); |
537
|
|
|
|
538
|
|
|
$tableName = $db->createTableName($name, $schemaName, $catalogName, $serverName); |
539
|
|
|
|
540
|
|
|
$this->assertInstanceOf(TableNameInterface::class, $tableName); |
541
|
|
|
|
542
|
|
|
$this->assertEquals($expectedName, $tableName->getTableName()); |
543
|
|
|
$this->assertEquals($expectedFullName, (string) $tableName); |
544
|
|
|
|
545
|
|
|
$this->assertEquals($db->getTablePrefix(), $tableName->getPrefix()); |
546
|
|
|
|
547
|
|
|
$this->assertEquals($name, $tableName->getRawTableName()); |
548
|
|
|
$this->assertEquals($schemaName, $tableName->getSchemaName()); |
549
|
|
|
$this->assertEquals($catalogName, $tableName->getCatalogName()); |
550
|
|
|
$this->assertEquals($serverName, $tableName->getServerName()); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @return string[][] |
555
|
|
|
*/ |
556
|
|
|
public function createTableNameProvider(): array |
557
|
|
|
{ |
558
|
|
|
return [ |
559
|
|
|
'table without prefix1' => [ |
560
|
|
|
'table1', |
561
|
|
|
'table1', |
562
|
|
|
'table1', |
563
|
|
|
], |
564
|
|
|
'table without prefix2' => [ |
565
|
|
|
'table1', |
566
|
|
|
'table1', |
567
|
|
|
'{{table1}}', |
568
|
|
|
], |
569
|
|
|
'table with prefix' => [ |
570
|
|
|
'table1', |
571
|
|
|
'table1', |
572
|
|
|
'{{%table1}}', |
573
|
|
|
], |
574
|
|
|
[ |
575
|
|
|
'table1', |
576
|
|
|
'dbo.table1', |
577
|
|
|
'{{%table1}}', 'dbo', |
578
|
|
|
], |
579
|
|
|
[ |
580
|
|
|
'table1', |
581
|
|
|
'catalog1.dbo.table1', |
582
|
|
|
'{{%table1}}', 'dbo', 'catalog1', |
583
|
|
|
], |
584
|
|
|
[ |
585
|
|
|
'table1', |
586
|
|
|
'serverName1.catalog1.dbo.table1', |
587
|
|
|
'{{%table1}}', 'dbo', 'catalog1', 'serverName1', |
588
|
|
|
], |
589
|
|
|
]; |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* @return string[][] |
594
|
|
|
*/ |
595
|
|
|
public function createTableNameWithPrefixProvider(): array |
596
|
|
|
{ |
597
|
|
|
return [ |
598
|
|
|
'table without prefix1' => [ |
599
|
|
|
'table1', |
600
|
|
|
'table1', |
601
|
|
|
'table1', |
602
|
|
|
], |
603
|
|
|
'table without prefix2' => [ |
604
|
|
|
'table1', |
605
|
|
|
'table1', |
606
|
|
|
'{{table1}}', |
607
|
|
|
], |
608
|
|
|
'table with prefix' => [ |
609
|
|
|
'pre_table1', |
610
|
|
|
'pre_table1', |
611
|
|
|
'{{%table1}}', |
612
|
|
|
], |
613
|
|
|
[ |
614
|
|
|
'pre_table1', |
615
|
|
|
'dbo.pre_table1', |
616
|
|
|
'{{%table1}}', 'dbo', |
617
|
|
|
], |
618
|
|
|
[ |
619
|
|
|
'pre_table1', |
620
|
|
|
'catalog1.dbo.pre_table1', |
621
|
|
|
'{{%table1}}', 'dbo', 'catalog1', |
622
|
|
|
], |
623
|
|
|
[ |
624
|
|
|
'pre_table1', |
625
|
|
|
'serverName1.catalog1.dbo.pre_table1', |
626
|
|
|
'{{%table1}}', 'dbo', 'catalog1', 'serverName1', |
627
|
|
|
], |
628
|
|
|
]; |
629
|
|
|
} |
630
|
|
|
} |
631
|
|
|
|