1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Yiisoft\Cache\Dependency\TagDependency; |
9
|
|
|
use Yiisoft\Db\Command\CommandInterface; |
10
|
|
|
use Yiisoft\Db\Command\Param; |
11
|
|
|
use Yiisoft\Db\Command\ParamInterface; |
12
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
13
|
|
|
use Yiisoft\Db\Exception\Exception; |
14
|
|
|
use Yiisoft\Db\Exception\InvalidCallException; |
15
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
16
|
|
|
use Yiisoft\Db\Exception\InvalidParamException; |
17
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
18
|
|
|
use Yiisoft\Db\Expression\Expression; |
19
|
|
|
use Yiisoft\Db\Query\Data\DataReader; |
20
|
|
|
use Yiisoft\Db\Query\Query; |
21
|
|
|
use Yiisoft\Db\Schema\Schema; |
22
|
|
|
use Yiisoft\Db\Schema\SchemaBuilderTrait; |
23
|
|
|
use Yiisoft\Db\Tests\Support\Assert; |
24
|
|
|
use Yiisoft\Db\Tests\Support\DbHelper; |
25
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
26
|
|
|
|
27
|
|
|
use function call_user_func_array; |
28
|
|
|
|
29
|
|
|
abstract class AbstractCommandTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
use SchemaBuilderTrait; |
32
|
|
|
use TestTrait; |
33
|
|
|
|
34
|
|
|
protected ConnectionPDOInterface $db; |
35
|
|
|
protected string $upsertTestCharCast = ''; |
36
|
|
|
|
37
|
|
|
public function testAddCheck(): void |
38
|
|
|
{ |
39
|
|
|
$db = $this->getConnection(); |
40
|
|
|
|
41
|
|
|
$command = $db->createCommand(); |
42
|
|
|
$sql = $command->addCheck('name', 'table', 'id > 0')->getSql(); |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->assertSame( |
46
|
|
|
DbHelper::replaceQuotes( |
47
|
|
|
<<<SQL |
48
|
|
|
ALTER TABLE [[table]] ADD CONSTRAINT [[name]] CHECK (id > 0) |
49
|
|
|
SQL, |
50
|
|
|
$db->getName(), |
51
|
|
|
), |
52
|
|
|
$sql, |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testAddColumn(): void |
57
|
|
|
{ |
58
|
|
|
$db = $this->getConnection(); |
59
|
|
|
|
60
|
|
|
$command = $db->createCommand(); |
61
|
|
|
$sql = $command->addColumn('table', 'column', Schema::TYPE_INTEGER)->getSql(); |
62
|
|
|
|
63
|
|
|
$this->assertSame( |
64
|
|
|
DbHelper::replaceQuotes( |
65
|
|
|
<<<SQL |
66
|
|
|
ALTER TABLE [[table]] ADD [[column]] integer |
67
|
|
|
SQL, |
68
|
|
|
$db->getName(), |
69
|
|
|
), |
70
|
|
|
$sql, |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testAddCommentOnColumn(): void |
75
|
|
|
{ |
76
|
|
|
$db = $this->getConnection(); |
77
|
|
|
|
78
|
|
|
$command = $db->createCommand(); |
79
|
|
|
$sql = $command->addCommentOnColumn('customer', 'id', 'Primary key.')->getSql(); |
80
|
|
|
|
81
|
|
|
$this->assertStringContainsString( |
82
|
|
|
DbHelper::replaceQuotes( |
83
|
|
|
<<<SQL |
84
|
|
|
COMMENT ON COLUMN [[customer]].[[id]] IS 'Primary key.' |
85
|
|
|
SQL, |
86
|
|
|
$db->getName(), |
87
|
|
|
), |
88
|
|
|
$sql, |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testAddCommentOnTable(): void |
93
|
|
|
{ |
94
|
|
|
$db = $this->getConnection(); |
95
|
|
|
|
96
|
|
|
$command = $db->createCommand(); |
97
|
|
|
$sql = $command->addCommentOnTable('table', 'comment')->getSql(); |
98
|
|
|
|
99
|
|
|
$this->assertSame( |
100
|
|
|
DbHelper::replaceQuotes( |
101
|
|
|
<<<SQL |
102
|
|
|
COMMENT ON TABLE [[table]] IS 'comment' |
103
|
|
|
SQL, |
104
|
|
|
$db->getName(), |
105
|
|
|
), |
106
|
|
|
$sql, |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testAddForeignKey(): void |
111
|
|
|
{ |
112
|
|
|
$db = $this->getConnection(); |
113
|
|
|
|
114
|
|
|
$command = $db->createCommand(); |
115
|
|
|
$sql = $command->addForeignKey('name', 'table', 'column', 'ref_table', 'ref_column')->getSql(); |
116
|
|
|
|
117
|
|
|
$this->assertSame( |
118
|
|
|
DbHelper::replaceQuotes( |
119
|
|
|
<<<SQL |
120
|
|
|
ALTER TABLE [[table]] ADD CONSTRAINT [[name]] FOREIGN KEY ([[column]]) REFERENCES [[ref_table]] ([[ref_column]]) |
121
|
|
|
SQL, |
122
|
|
|
$db->getName(), |
123
|
|
|
), |
124
|
|
|
$sql, |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$sql = $command->addForeignKey('name', 'table', 'column', 'ref_table', 'ref_column', 'CASCADE')->getSql(); |
128
|
|
|
|
129
|
|
|
$this->assertSame( |
130
|
|
|
DbHelper::replaceQuotes( |
131
|
|
|
<<<SQL |
132
|
|
|
ALTER TABLE [[table]] ADD CONSTRAINT [[name]] FOREIGN KEY ([[column]]) REFERENCES [[ref_table]] ([[ref_column]]) ON DELETE CASCADE |
133
|
|
|
SQL, |
134
|
|
|
$db->getName(), |
135
|
|
|
), |
136
|
|
|
$sql, |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$sql = $command |
140
|
|
|
->addForeignKey('name', 'table', 'column', 'ref_table', 'ref_column', 'CASCADE', 'CASCADE') |
141
|
|
|
->getSql(); |
142
|
|
|
|
143
|
|
|
$this->assertSame( |
144
|
|
|
DbHelper::replaceQuotes( |
145
|
|
|
<<<SQL |
146
|
|
|
ALTER TABLE [[table]] ADD CONSTRAINT [[name]] FOREIGN KEY ([[column]]) REFERENCES [[ref_table]] ([[ref_column]]) ON DELETE CASCADE ON UPDATE CASCADE |
147
|
|
|
SQL, |
148
|
|
|
$db->getName(), |
149
|
|
|
), |
150
|
|
|
$sql, |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testAddPrimaryKey(): void |
155
|
|
|
{ |
156
|
|
|
$db = $this->getConnection(); |
157
|
|
|
|
158
|
|
|
$command = $db->createCommand(); |
159
|
|
|
$sql = $command->addPrimaryKey('name', 'table', 'column')->getSql(); |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
$this->assertSame( |
163
|
|
|
DbHelper::replaceQuotes( |
164
|
|
|
<<<SQL |
165
|
|
|
ALTER TABLE [[table]] ADD CONSTRAINT [[name]] PRIMARY KEY ([[column]]) |
166
|
|
|
SQL, |
167
|
|
|
$db->getName(), |
168
|
|
|
), |
169
|
|
|
$sql, |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$sql = $command->addPrimaryKey('name', 'table', ['column1', 'column2'])->getSql(); |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
$this->assertSame( |
176
|
|
|
DbHelper::replaceQuotes( |
177
|
|
|
<<<SQL |
178
|
|
|
ALTER TABLE [[table]] ADD CONSTRAINT [[name]] PRIMARY KEY ([[column1]], [[column2]]) |
179
|
|
|
SQL, |
180
|
|
|
$db->getName(), |
181
|
|
|
), |
182
|
|
|
$sql, |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testAddUnique(): void |
187
|
|
|
{ |
188
|
|
|
$db = $this->getConnection(); |
189
|
|
|
|
190
|
|
|
$command = $db->createCommand(); |
191
|
|
|
$sql = $command->addUnique('name', 'table', 'column')->getSql(); |
192
|
|
|
|
193
|
|
|
$this->assertSame( |
194
|
|
|
DbHelper::replaceQuotes( |
195
|
|
|
<<<SQL |
196
|
|
|
ALTER TABLE [[table]] ADD CONSTRAINT [[name]] UNIQUE ([[column]]) |
197
|
|
|
SQL, |
198
|
|
|
$db->getName(), |
199
|
|
|
), |
200
|
|
|
$sql, |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
$sql = $command->addUnique('name', 'table', ['column1', 'column2'])->getSql(); |
204
|
|
|
|
205
|
|
|
$this->assertSame( |
206
|
|
|
DbHelper::replaceQuotes( |
207
|
|
|
<<<SQL |
208
|
|
|
ALTER TABLE [[table]] ADD CONSTRAINT [[name]] UNIQUE ([[column1]], [[column2]]) |
209
|
|
|
SQL, |
210
|
|
|
$db->getName(), |
211
|
|
|
), |
212
|
|
|
$sql, |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testAlterColumn(): void |
217
|
|
|
{ |
218
|
|
|
$db = $this->getConnection(); |
219
|
|
|
|
220
|
|
|
$command = $db->createCommand(); |
221
|
|
|
$sql = $command->alterColumn('table', 'column', Schema::TYPE_INTEGER)->getSql(); |
222
|
|
|
|
223
|
|
|
$this->assertSame( |
224
|
|
|
DbHelper::replaceQuotes( |
225
|
|
|
<<<SQL |
226
|
|
|
ALTER TABLE [[table]] CHANGE [[column]] [[column]] integer |
227
|
|
|
SQL, |
228
|
|
|
$db->getName(), |
229
|
|
|
), |
230
|
|
|
$sql, |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function testBindValues(): void |
235
|
|
|
{ |
236
|
|
|
$db = $this->getConnection(); |
237
|
|
|
|
238
|
|
|
$command = $db->createCommand(); |
239
|
|
|
|
240
|
|
|
$values = ['int' => 1, 'string' => 'str']; |
241
|
|
|
$command->bindValues($values); |
242
|
|
|
$bindedValues = $command->getParams(false); |
243
|
|
|
|
244
|
|
|
$this->assertIsArray($bindedValues); |
245
|
|
|
$this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues); |
246
|
|
|
$this->assertCount(2, $bindedValues); |
247
|
|
|
|
248
|
|
|
$param = new Param('str', 99); |
249
|
|
|
$command->bindValues(['param' => $param]); |
250
|
|
|
$bindedValues = $command->getParams(false); |
251
|
|
|
|
252
|
|
|
$this->assertIsArray($bindedValues); |
253
|
|
|
$this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues); |
254
|
|
|
$this->assertCount(3, $bindedValues); |
255
|
|
|
$this->assertSame($param, $bindedValues['param']); |
256
|
|
|
$this->assertNotEquals($param, $bindedValues['int']); |
257
|
|
|
|
258
|
|
|
/* Replace test */ |
259
|
|
|
$command->bindValues(['int' => $param]); |
260
|
|
|
$bindedValues = $command->getParams(false); |
261
|
|
|
|
262
|
|
|
$this->assertIsArray($bindedValues); |
263
|
|
|
$this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues); |
264
|
|
|
$this->assertCount(3, $bindedValues); |
265
|
|
|
$this->assertSame($param, $bindedValues['int']); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testCache(): void |
269
|
|
|
{ |
270
|
|
|
$db = $this->getConnection(); |
271
|
|
|
|
272
|
|
|
$tagDependency = new TagDependency('tag'); |
273
|
|
|
$command = $db->createCommand(); |
274
|
|
|
$command->cache(100, $tagDependency); |
275
|
|
|
|
276
|
|
|
$this->assertInstanceOf(CommandInterface::class, $command); |
277
|
|
|
$this->assertSame(100, Assert::getInaccessibleProperty($command, 'queryCacheDuration')); |
278
|
|
|
$this->assertSame($tagDependency, Assert::getInaccessibleProperty($command, 'queryCacheDependency')); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function testConstruct(): void |
282
|
|
|
{ |
283
|
|
|
$db = $this->getConnection(); |
284
|
|
|
|
285
|
|
|
$command = $db->createCommand(); |
286
|
|
|
|
287
|
|
|
$this->assertEmpty($command->getSql()); |
288
|
|
|
|
289
|
|
|
$sql = <<<SQL |
290
|
|
|
SELECT * FROM customer WHERE name=:name |
291
|
|
|
SQL; |
292
|
|
|
$command = $db->createCommand($sql, [':name' => 'John']); |
293
|
|
|
|
294
|
|
|
$this->assertSame($sql, $command->getSql()); |
295
|
|
|
$this->assertSame([':name' => 'John'], $command->getParams()); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::createIndex() |
300
|
|
|
*/ |
301
|
|
|
public function testCreateIndex( |
302
|
|
|
string $name, |
303
|
|
|
string $table, |
304
|
|
|
array|string $column, |
305
|
|
|
string $indexType, |
306
|
|
|
string $indexMethod, |
307
|
|
|
string $expected, |
308
|
|
|
): void { |
309
|
|
|
$db = $this->getConnection(); |
310
|
|
|
|
311
|
|
|
$command = $db->createCommand(); |
312
|
|
|
|
313
|
|
|
$sql = $command->createIndex($name, $table, $column, $indexType, $indexMethod)->getSql(); |
314
|
|
|
|
315
|
|
|
$this->assertSame($expected, $sql); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function testCreateView(): void |
319
|
|
|
{ |
320
|
|
|
$db = $this->getConnection(); |
321
|
|
|
|
322
|
|
|
$command = $db->createCommand(); |
323
|
|
|
|
324
|
|
|
$sql = $command->createView( |
325
|
|
|
'view', |
326
|
|
|
<<<SQL |
327
|
|
|
SELECT * FROM table |
328
|
|
|
SQL, |
329
|
|
|
)->getSql(); |
330
|
|
|
|
331
|
|
|
$this->assertSame( |
332
|
|
|
DbHelper::replaceQuotes( |
333
|
|
|
<<<SQL |
334
|
|
|
CREATE VIEW [[view]] AS SELECT * FROM table |
335
|
|
|
SQL, |
336
|
|
|
$db->getName(), |
337
|
|
|
), |
338
|
|
|
$sql, |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
public function testDataReaderCreationException(): void |
343
|
|
|
{ |
344
|
|
|
$db = $this->getConnection(); |
345
|
|
|
|
346
|
|
|
$this->expectException(InvalidParamException::class); |
347
|
|
|
$this->expectExceptionMessage('The PDOStatement cannot be null.'); |
348
|
|
|
|
349
|
|
|
$sql = 'SELECT * FROM {{customer}}'; |
350
|
|
|
new DataReader($db->createCommand($sql)); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function testDelete(): void |
354
|
|
|
{ |
355
|
|
|
$db = $this->getConnection(); |
356
|
|
|
|
357
|
|
|
$command = $db->createCommand(); |
358
|
|
|
$sql = $command->delete('table', ['column' => 'value'])->getSql(); |
359
|
|
|
|
360
|
|
|
$this->assertSame( |
361
|
|
|
DbHelper::replaceQuotes( |
362
|
|
|
<<<SQL |
363
|
|
|
DELETE FROM [[table]] WHERE [[column]]=:qp0 |
364
|
|
|
SQL, |
365
|
|
|
$db->getName(), |
366
|
|
|
), |
367
|
|
|
$sql, |
368
|
|
|
); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function testDropCheck(): void |
372
|
|
|
{ |
373
|
|
|
$db = $this->getConnection(); |
374
|
|
|
|
375
|
|
|
$command = $db->createCommand(); |
376
|
|
|
$sql = $command->dropCheck('name', 'table')->getSql(); |
377
|
|
|
|
378
|
|
|
$this->assertSame( |
379
|
|
|
DbHelper::replaceQuotes( |
380
|
|
|
<<<SQL |
381
|
|
|
ALTER TABLE [[table]] DROP CONSTRAINT [[name]] |
382
|
|
|
SQL, |
383
|
|
|
$db->getName(), |
384
|
|
|
), |
385
|
|
|
$sql, |
386
|
|
|
); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
public function testDropColumn(): void |
390
|
|
|
{ |
391
|
|
|
$db = $this->getConnection(); |
392
|
|
|
|
393
|
|
|
$command = $db->createCommand(); |
394
|
|
|
$sql = $command->dropColumn('table', 'column')->getSql(); |
395
|
|
|
|
396
|
|
|
$this->assertSame( |
397
|
|
|
DbHelper::replaceQuotes( |
398
|
|
|
<<<SQL |
399
|
|
|
ALTER TABLE [[table]] DROP COLUMN [[column]] |
400
|
|
|
SQL, |
401
|
|
|
$db->getName(), |
402
|
|
|
), |
403
|
|
|
$sql, |
404
|
|
|
); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function testDropCommentFromColumn(): void |
408
|
|
|
{ |
409
|
|
|
$db = $this->getConnection(); |
410
|
|
|
|
411
|
|
|
$command = $db->createCommand(); |
412
|
|
|
$sql = $command->dropCommentFromColumn('table', 'column')->getSql(); |
413
|
|
|
|
414
|
|
|
$this->assertSame( |
415
|
|
|
DbHelper::replaceQuotes( |
416
|
|
|
<<<SQL |
417
|
|
|
COMMENT ON COLUMN [[table]].[[column]] IS NULL |
418
|
|
|
SQL, |
419
|
|
|
$db->getName(), |
420
|
|
|
), |
421
|
|
|
$sql, |
422
|
|
|
); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
public function testDropCommentFromTable(): void |
426
|
|
|
{ |
427
|
|
|
$db = $this->getConnection(); |
428
|
|
|
|
429
|
|
|
$command = $db->createCommand(); |
430
|
|
|
$sql = $command->dropCommentFromTable('table')->getSql(); |
431
|
|
|
|
432
|
|
|
$this->assertSame( |
433
|
|
|
DbHelper::replaceQuotes( |
434
|
|
|
<<<SQL |
435
|
|
|
COMMENT ON TABLE [[table]] IS NULL |
436
|
|
|
SQL, |
437
|
|
|
$db->getName(), |
438
|
|
|
), |
439
|
|
|
$sql, |
440
|
|
|
); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
public function testDropForeingKey(): void |
444
|
|
|
{ |
445
|
|
|
$db = $this->getConnection(); |
446
|
|
|
|
447
|
|
|
$command = $db->createCommand(); |
448
|
|
|
$sql = $command->dropForeignKey('name', 'table')->getSql(); |
449
|
|
|
|
450
|
|
|
$this->assertSame( |
451
|
|
|
DbHelper::replaceQuotes( |
452
|
|
|
<<<SQL |
453
|
|
|
ALTER TABLE [[table]] DROP CONSTRAINT [[name]] |
454
|
|
|
SQL, |
455
|
|
|
$db->getName(), |
456
|
|
|
), |
457
|
|
|
$sql, |
458
|
|
|
); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
public function testDropIndex(): void |
462
|
|
|
{ |
463
|
|
|
$db = $this->getConnection(); |
464
|
|
|
|
465
|
|
|
$command = $db->createCommand(); |
466
|
|
|
$sql = $command->dropIndex('name', 'table')->getSql(); |
467
|
|
|
|
468
|
|
|
$this->assertSame( |
469
|
|
|
DbHelper::replaceQuotes( |
470
|
|
|
<<<SQL |
471
|
|
|
DROP INDEX [[name]] ON [[table]] |
472
|
|
|
SQL, |
473
|
|
|
$db->getName(), |
474
|
|
|
), |
475
|
|
|
$sql, |
476
|
|
|
); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
public function testDropPrimaryKey(): void |
480
|
|
|
{ |
481
|
|
|
$db = $this->getConnection(); |
482
|
|
|
|
483
|
|
|
$command = $db->createCommand(); |
484
|
|
|
$sql = $command->dropPrimaryKey('name', 'table')->getSql(); |
485
|
|
|
|
486
|
|
|
$this->assertSame( |
487
|
|
|
DbHelper::replaceQuotes( |
488
|
|
|
<<<SQL |
489
|
|
|
ALTER TABLE [[table]] DROP CONSTRAINT [[name]] |
490
|
|
|
SQL, |
491
|
|
|
$db->getName(), |
492
|
|
|
), |
493
|
|
|
$sql, |
494
|
|
|
); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
public function testDropTable(): void |
498
|
|
|
{ |
499
|
|
|
$db = $this->getConnection(); |
500
|
|
|
|
501
|
|
|
$command = $db->createCommand(); |
502
|
|
|
$sql = $command->dropTable('table')->getSql(); |
503
|
|
|
|
504
|
|
|
$this->assertSame( |
505
|
|
|
DbHelper::replaceQuotes( |
506
|
|
|
<<<SQL |
507
|
|
|
DROP TABLE [[table]] |
508
|
|
|
SQL, |
509
|
|
|
$db->getName(), |
510
|
|
|
), |
511
|
|
|
$sql, |
512
|
|
|
); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
public function testDropView(): void |
516
|
|
|
{ |
517
|
|
|
$db = $this->getConnection(); |
518
|
|
|
|
519
|
|
|
$command = $db->createCommand(); |
520
|
|
|
$sql = $command->dropView('view')->getSql(); |
521
|
|
|
|
522
|
|
|
$this->assertSame( |
523
|
|
|
DbHelper::replaceQuotes( |
524
|
|
|
<<<SQL |
525
|
|
|
DROP VIEW [[view]] |
526
|
|
|
SQL, |
527
|
|
|
$db->getName(), |
528
|
|
|
), |
529
|
|
|
$sql, |
530
|
|
|
); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
public function testDropUnique(): void |
534
|
|
|
{ |
535
|
|
|
$db = $this->getConnection(); |
536
|
|
|
|
537
|
|
|
$command = $db->createCommand(); |
538
|
|
|
$sql = $command->dropUnique('name', 'table')->getSql(); |
539
|
|
|
|
540
|
|
|
$this->assertSame( |
541
|
|
|
DbHelper::replaceQuotes( |
542
|
|
|
<<<SQL |
543
|
|
|
ALTER TABLE [[table]] DROP CONSTRAINT [[name]] |
544
|
|
|
SQL, |
545
|
|
|
$db->getName(), |
546
|
|
|
), |
547
|
|
|
$sql, |
548
|
|
|
); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
public function testExecuteWithSqlEmtpy(): void |
552
|
|
|
{ |
553
|
|
|
$db = $this->getConnection(); |
554
|
|
|
|
555
|
|
|
$command = $db->createCommand(); |
556
|
|
|
|
557
|
|
|
$this->assertSame(0, $command->execute()); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
public function testGetParams(): void |
561
|
|
|
{ |
562
|
|
|
$db = $this->getConnection(); |
563
|
|
|
|
564
|
|
|
$command = $db->createCommand(); |
565
|
|
|
$values = [ |
566
|
|
|
'int' => 1, |
567
|
|
|
'string' => 'str', |
568
|
|
|
]; |
569
|
|
|
$command->bindValues($values); |
570
|
|
|
$bindedValues = $command->getParams(false); |
571
|
|
|
|
572
|
|
|
$this->assertIsArray($bindedValues); |
573
|
|
|
$this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues); |
574
|
|
|
$this->assertCount(2, $bindedValues); |
575
|
|
|
|
576
|
|
|
$param = new Param('str', 99); |
577
|
|
|
$command->bindValues(['param' => $param]); |
578
|
|
|
$bindedValues = $command->getParams(false); |
579
|
|
|
|
580
|
|
|
$this->assertIsArray($bindedValues); |
581
|
|
|
$this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues); |
582
|
|
|
$this->assertCount(3, $bindedValues); |
583
|
|
|
$this->assertEquals($param, $bindedValues['param']); |
584
|
|
|
$this->assertNotEquals($param, $bindedValues['int']); |
585
|
|
|
|
586
|
|
|
/* Replace test */ |
587
|
|
|
$command->bindValues(['int' => $param]); |
588
|
|
|
$bindedValues = $command->getParams(false); |
589
|
|
|
|
590
|
|
|
$this->assertIsArray($bindedValues); |
591
|
|
|
$this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues); |
592
|
|
|
$this->assertCount(3, $bindedValues); |
593
|
|
|
$this->assertEquals($param, $bindedValues['int']); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Test command getRawSql. |
598
|
|
|
* |
599
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::rawSql() |
600
|
|
|
* |
601
|
|
|
* @throws Exception |
602
|
|
|
* @throws InvalidConfigException |
603
|
|
|
* @throws NotSupportedException |
604
|
|
|
* |
605
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/8592} |
606
|
|
|
*/ |
607
|
|
|
public function testGetRawSql(string $sql, array $params, string $expectedRawSql): void |
608
|
|
|
{ |
609
|
|
|
$db = $this->getConnection(); |
610
|
|
|
|
611
|
|
|
$command = $db->createCommand($sql, $params); |
612
|
|
|
|
613
|
|
|
$this->assertSame($expectedRawSql, $command->getRawSql()); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
public function testGetSetSql(): void |
617
|
|
|
{ |
618
|
|
|
$db = $this->getConnection(); |
619
|
|
|
|
620
|
|
|
$sql = <<<SQL |
621
|
|
|
SELECT * FROM customer |
622
|
|
|
SQL; |
623
|
|
|
$command = $db->createCommand($sql); |
624
|
|
|
$this->assertSame($sql, $command->getSql()); |
625
|
|
|
|
626
|
|
|
$sql2 = <<<SQL |
627
|
|
|
SELECT * FROM order |
628
|
|
|
SQL; |
629
|
|
|
$command->setSql($sql2); |
630
|
|
|
$this->assertSame($sql2, $command->getSql()); |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
public function testLastInsertIdException(): void |
634
|
|
|
{ |
635
|
|
|
$db = $this->getConnection(); |
636
|
|
|
|
637
|
|
|
$db->close(); |
638
|
|
|
|
639
|
|
|
$this->expectException(InvalidCallException::class); |
640
|
|
|
|
641
|
|
|
$db->getLastInsertID(); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
public function testNoCache(): void |
645
|
|
|
{ |
646
|
|
|
$db = $this->getConnection(); |
647
|
|
|
|
648
|
|
|
$command = $db->createCommand()->noCache(); |
649
|
|
|
|
650
|
|
|
$this->assertSame(-1, Assert::getInaccessibleProperty($command, 'queryCacheDuration')); |
651
|
|
|
$this->assertInstanceOf(CommandInterface::class, $command); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
public function testPrepareCancel(): void |
655
|
|
|
{ |
656
|
|
|
$db = $this->getConnection('customer'); |
657
|
|
|
|
658
|
|
|
$command = $db->createCommand(); |
659
|
|
|
$command->setSql( |
660
|
|
|
<<<SQL |
661
|
|
|
SELECT * FROM {{customer}} |
662
|
|
|
SQL |
663
|
|
|
); |
664
|
|
|
|
665
|
|
|
$this->assertNull($command->getPdoStatement()); |
|
|
|
|
666
|
|
|
|
667
|
|
|
$command->prepare(); |
668
|
|
|
|
669
|
|
|
$this->assertNotNull($command->getPdoStatement()); |
670
|
|
|
|
671
|
|
|
$command->cancel(); |
672
|
|
|
|
673
|
|
|
$this->assertNull($command->getPdoStatement()); |
|
|
|
|
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
public function testRenameColumn(): void |
677
|
|
|
{ |
678
|
|
|
$db = $this->getConnection(); |
679
|
|
|
|
680
|
|
|
$sql = $db->createCommand()->renameColumn('table', 'oldname', 'newname')->getSql(); |
681
|
|
|
|
682
|
|
|
$this->assertSame( |
683
|
|
|
DbHelper::replaceQuotes( |
684
|
|
|
<<<SQL |
685
|
|
|
ALTER TABLE [[table]] RENAME COLUMN [[oldname]] TO [[newname]] |
686
|
|
|
SQL, |
687
|
|
|
$db->getName(), |
688
|
|
|
), |
689
|
|
|
$sql, |
690
|
|
|
); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
public function testSetRawSql(): void |
694
|
|
|
{ |
695
|
|
|
$db = $this->getConnection(); |
696
|
|
|
|
697
|
|
|
$command = $db->createCommand(); |
698
|
|
|
$command->setRawSql( |
699
|
|
|
<<<SQL |
700
|
|
|
SELECT 123 |
701
|
|
|
SQL |
702
|
|
|
); |
703
|
|
|
|
704
|
|
|
$this->assertSame('SELECT 123', $command->getRawSql()); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
public function testSetRetryHandler(): void |
708
|
|
|
{ |
709
|
|
|
$db = $this->getConnection(); |
710
|
|
|
|
711
|
|
|
$command = $db->createCommand(); |
712
|
|
|
|
713
|
|
|
$handler = static fn (): bool => true; |
714
|
|
|
|
715
|
|
|
Assert::invokeMethod($command, 'setRetryHandler', [$handler]); |
716
|
|
|
|
717
|
|
|
$this->assertSame($handler, Assert::getInaccessibleProperty($command, 'retryHandler')); |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
public function testTruncateTable(): void |
721
|
|
|
{ |
722
|
|
|
$db = $this->getConnection(); |
723
|
|
|
|
724
|
|
|
$command = $db->createCommand(); |
725
|
|
|
$sql = $command->truncateTable('table')->getSql(); |
726
|
|
|
|
727
|
|
|
$this->assertSame( |
728
|
|
|
DbHelper::replaceQuotes( |
729
|
|
|
<<<SQL |
730
|
|
|
TRUNCATE TABLE [[table]] |
731
|
|
|
SQL, |
732
|
|
|
$db->getName(), |
733
|
|
|
), |
734
|
|
|
$sql, |
735
|
|
|
); |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
protected function performAndCompareUpsertResult(ConnectionPDOInterface $db, array $data): void |
739
|
|
|
{ |
740
|
|
|
$params = $data['params']; |
741
|
|
|
$expected = $data['expected'] ?? $params[1]; |
742
|
|
|
|
743
|
|
|
$command = $db->createCommand(); |
744
|
|
|
|
745
|
|
|
call_user_func_array([$command, 'upsert'], $params); |
746
|
|
|
|
747
|
|
|
$command->execute(); |
748
|
|
|
|
749
|
|
|
$actual = (new Query($db)) |
750
|
|
|
->select(['email', 'address' => new Expression($this->upsertTestCharCast), 'status']) |
751
|
|
|
->from('T_upsert') |
752
|
|
|
->one(); |
753
|
|
|
$this->assertEquals($expected, $actual, $this->upsertTestCharCast); |
754
|
|
|
} |
755
|
|
|
} |
756
|
|
|
|
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.