Skip to content

Commit ed6989d

Browse files
Fix SQLite CommandTest skipped tests and minor fixes. (#20648)
1 parent d1ecf2e commit ed6989d

File tree

2 files changed

+333
-94
lines changed

2 files changed

+333
-94
lines changed

tests/framework/db/CommandTest.php

Lines changed: 175 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,6 @@ public function testCreateTable(): void
741741

742742
public function testAlterTable(): void
743743
{
744-
if ($this->driverName === 'sqlite') {
745-
$this->markTestSkipped('Sqlite does not support alterTable');
746-
}
747-
748744
$db = $this->getConnection();
749745

750746
if ($db->getSchema()->getTableSchema('testAlterTable') !== null) {
@@ -1081,64 +1077,151 @@ public function testRenameColumn()
10811077
}
10821078
*/
10831079

1084-
public function testAddDropPrimaryKey(): void
1080+
public static function addPrimaryKeyProvider(): array
1081+
{
1082+
return [
1083+
[
1084+
'{{test_pk_constraint_1}}',
1085+
'{{test_pk}}',
1086+
'int1',
1087+
],
1088+
[
1089+
'{{test_pk_constraint_2}}',
1090+
'{{test_pk}}',
1091+
['int1'],
1092+
],
1093+
[
1094+
'{{test_pk_constraint_3}}',
1095+
'{{test_pk}}',
1096+
[
1097+
'int1',
1098+
'int2',
1099+
],
1100+
],
1101+
];
1102+
}
1103+
1104+
/**
1105+
* @dataProvider addPrimaryKeyProvider
1106+
*
1107+
* @param string $name
1108+
* @param string $tableName
1109+
* @param array|string $pk
1110+
*
1111+
* @phpstan-param list<string> $pk
1112+
*/
1113+
public function testAddDropPrimaryKey(string $name, string $tableName, $pk): void
10851114
{
10861115
$db = $this->getConnection(false);
1087-
$tableName = 'test_pk';
1088-
$name = 'test_pk_constraint';
1089-
/** @var \yii\db\pgsql\Schema $schema */
1116+
10901117
$schema = $db->getSchema();
10911118

10921119
if ($schema->getTableSchema($tableName) !== null) {
10931120
$db->createCommand()->dropTable($tableName)->execute();
10941121
}
1095-
$db->createCommand()->createTable($tableName, [
1096-
'int1' => 'integer not null',
1097-
'int2' => 'integer not null',
1098-
])->execute();
1122+
1123+
$db->createCommand()->createTable(
1124+
$tableName,
1125+
[
1126+
'int1' => 'integer not null',
1127+
'int2' => 'integer not null',
1128+
],
1129+
)->execute();
10991130

11001131
$this->assertNull($schema->getTablePrimaryKey($tableName, true));
1101-
$db->createCommand()->addPrimaryKey($name, $tableName, ['int1'])->execute();
1102-
$this->assertEquals(['int1'], $schema->getTablePrimaryKey($tableName, true)->columnNames);
1132+
1133+
$db->createCommand()->addPrimaryKey($name, $tableName, $pk)->execute();
1134+
1135+
$this->assertSame((array) $pk, $schema->getTablePrimaryKey($tableName, true)->columnNames);
11031136

11041137
$db->createCommand()->dropPrimaryKey($name, $tableName)->execute();
1138+
11051139
$this->assertNull($schema->getTablePrimaryKey($tableName, true));
11061140

1107-
$db->createCommand()->addPrimaryKey($name, $tableName, ['int1', 'int2'])->execute();
1108-
$this->assertEquals(['int1', 'int2'], $schema->getTablePrimaryKey($tableName, true)->columnNames);
1141+
$db->createCommand()->dropTable($tableName)->execute();
1142+
}
1143+
1144+
public static function addForeignKeyProvider(): array
1145+
{
1146+
return [
1147+
[
1148+
'{{test_fk_constraint_1}}',
1149+
'{{test_fk}}',
1150+
'int1',
1151+
'int3',
1152+
],
1153+
[
1154+
'{{test_fk_constraint_2}}',
1155+
'{{test_fk}}',
1156+
['int1'],
1157+
['int3'],
1158+
],
1159+
[
1160+
'{{test_fk_constraint_3}}',
1161+
'{{test_fk}}',
1162+
[
1163+
'int1',
1164+
'int2',
1165+
],
1166+
[
1167+
'int3',
1168+
'int4',
1169+
],
1170+
],
1171+
];
11091172
}
11101173

1111-
public function testAddDropForeignKey(): void
1174+
/**
1175+
* @dataProvider addForeignKeyProvider
1176+
*
1177+
* @param string $name
1178+
* @param string $tableName
1179+
* @param array|string $fkColumns
1180+
* @param array|string $refColumns
1181+
*
1182+
* @phpstan-param list<string> $fkColumns
1183+
* @phpstan-param list<string> $refColumns
1184+
*/
1185+
public function testAddDropForeignKey(string $name, string $tableName, $fkColumns, $refColumns): void
11121186
{
11131187
$db = $this->getConnection(false);
1114-
$tableName = 'test_fk';
1115-
$name = 'test_fk_constraint';
1116-
/** @var \yii\db\pgsql\Schema $schema */
1188+
11171189
$schema = $db->getSchema();
11181190

11191191
if ($schema->getTableSchema($tableName) !== null) {
11201192
$db->createCommand()->dropTable($tableName)->execute();
11211193
}
1122-
$db->createCommand()->createTable($tableName, [
1123-
'int1' => 'integer not null unique',
1124-
'int2' => 'integer not null unique',
1125-
'int3' => 'integer not null unique',
1126-
'int4' => 'integer not null unique',
1127-
'unique ([[int1]], [[int2]])',
1128-
'unique ([[int3]], [[int4]])',
1129-
])->execute();
1194+
1195+
$db->createCommand()->createTable(
1196+
$tableName,
1197+
[
1198+
'int1' => 'integer not null unique',
1199+
'int2' => 'integer not null unique',
1200+
'int3' => 'integer not null unique',
1201+
'int4' => 'integer not null unique',
1202+
'unique ([[int1]], [[int2]])',
1203+
'unique ([[int3]], [[int4]])',
1204+
],
1205+
)->execute();
11301206

11311207
$this->assertEmpty($schema->getTableForeignKeys($tableName, true));
1132-
$db->createCommand()->addForeignKey($name, $tableName, ['int1'], $tableName, ['int3'])->execute();
1133-
$this->assertEquals(['int1'], $schema->getTableForeignKeys($tableName, true)[0]->columnNames);
1134-
$this->assertEquals(['int3'], $schema->getTableForeignKeys($tableName, true)[0]->foreignColumnNames);
1208+
1209+
$db->createCommand()->addForeignKey(
1210+
$name,
1211+
$tableName,
1212+
(array) $fkColumns,
1213+
$tableName,
1214+
(array) $refColumns,
1215+
)->execute();
1216+
1217+
$this->assertSame((array) $fkColumns, $schema->getTableForeignKeys($tableName, true)[0]->columnNames);
1218+
$this->assertSame((array) $refColumns, $schema->getTableForeignKeys($tableName, true)[0]->foreignColumnNames);
11351219

11361220
$db->createCommand()->dropForeignKey($name, $tableName)->execute();
1221+
11371222
$this->assertEmpty($schema->getTableForeignKeys($tableName, true));
11381223

1139-
$db->createCommand()->addForeignKey($name, $tableName, ['int1', 'int2'], $tableName, ['int3', 'int4'])->execute();
1140-
$this->assertEquals(['int1', 'int2'], $schema->getTableForeignKeys($tableName, true)[0]->columnNames);
1141-
$this->assertEquals(['int3', 'int4'], $schema->getTableForeignKeys($tableName, true)[0]->foreignColumnNames);
1224+
$db->createCommand()->dropTable($tableName)->execute();
11421225
}
11431226

11441227
public function testCreateDropIndex(): void
@@ -1185,54 +1268,96 @@ public function testCreateDropIndex(): void
11851268
$this->assertTrue($schema->getTableIndexes($tableName, true)[0]->isUnique);
11861269
}
11871270

1188-
public function testAddDropUnique(): void
1271+
public static function addUniqueProvider(): array
1272+
{
1273+
return [
1274+
[
1275+
'{{test_unique_constraint_1}}',
1276+
'{{test_unique}}',
1277+
'int1',
1278+
],
1279+
[
1280+
'{{test_unique_constraint_2}}',
1281+
'{{test_unique}}',
1282+
['int1'],
1283+
],
1284+
[
1285+
'{{test_unique_constraint_3}}',
1286+
'{{test_unique}}',
1287+
[
1288+
'int1',
1289+
'int2',
1290+
],
1291+
],
1292+
];
1293+
}
1294+
1295+
/**
1296+
* @dataProvider addUniqueProvider
1297+
*
1298+
* @param string $name
1299+
* @param string $tableName
1300+
* @param array|string $columns
1301+
*
1302+
* @phpstan-param list<string> $columns
1303+
*/
1304+
public function testAddDropUnique(string $name, string $tableName, $columns): void
11891305
{
11901306
$db = $this->getConnection(false);
1191-
$tableName = 'test_uq';
1192-
$name = 'test_uq_constraint';
1193-
/** @var \yii\db\pgsql\Schema $schema */
1307+
11941308
$schema = $db->getSchema();
11951309

11961310
if ($schema->getTableSchema($tableName) !== null) {
11971311
$db->createCommand()->dropTable($tableName)->execute();
11981312
}
1199-
$db->createCommand()->createTable($tableName, [
1200-
'int1' => 'integer not null',
1201-
'int2' => 'integer not null',
1202-
])->execute();
1313+
1314+
$db->createCommand()->createTable(
1315+
$tableName,
1316+
[
1317+
'int1' => 'integer not null',
1318+
'int2' => 'integer not null',
1319+
],
1320+
)->execute();
12031321

12041322
$this->assertEmpty($schema->getTableUniques($tableName, true));
1205-
$db->createCommand()->addUnique($name, $tableName, ['int1'])->execute();
1206-
$this->assertEquals(['int1'], $schema->getTableUniques($tableName, true)[0]->columnNames);
1323+
1324+
$db->createCommand()->addUnique($name, $tableName, $columns)->execute();
1325+
1326+
$this->assertSame((array) $columns, $schema->getTableUniques($tableName, true)[0]->columnNames);
12071327

12081328
$db->createCommand()->dropUnique($name, $tableName)->execute();
1329+
12091330
$this->assertEmpty($schema->getTableUniques($tableName, true));
12101331

1211-
$db->createCommand()->addUnique($name, $tableName, ['int1', 'int2'])->execute();
1212-
$this->assertEquals(['int1', 'int2'], $schema->getTableUniques($tableName, true)[0]->columnNames);
1332+
$db->createCommand()->dropTable($tableName)->execute();
12131333
}
12141334

12151335
public function testAddDropCheck(): void
12161336
{
12171337
$db = $this->getConnection(false);
12181338

1219-
if (version_compare($db->getServerVersion(), '8.0.16', '<')) {
1339+
if ($db->getDriverName() === 'mysql' && version_compare($db->getServerVersion(), '8.0.16', '<')) {
12201340
$this->markTestSkipped('MySQL < 8.0.16 does not support CHECK constraints.');
12211341
}
12221342

12231343
$tableName = 'test_ck';
12241344
$name = 'test_ck_constraint';
1345+
12251346
$schema = $db->getSchema();
12261347

12271348
if ($schema->getTableSchema($tableName) !== null) {
12281349
$db->createCommand()->dropTable($tableName)->execute();
12291350
}
1230-
$db->createCommand()->createTable($tableName, [
1231-
'int1' => 'integer',
1232-
])->execute();
1351+
1352+
$db->createCommand()->createTable(
1353+
$tableName,
1354+
['int1' => 'integer'],
1355+
)->execute();
12331356

12341357
$this->assertEmpty($schema->getTableChecks($tableName, true));
1358+
12351359
$db->createCommand()->addCheck($name, $tableName, '[[int1]] > 1')->execute();
1360+
12361361
$this->assertMatchesRegularExpression(
12371362
'/^.*int1.*>.*1.*$/',
12381363
$schema->getTableChecks($tableName, true)[0]->expression

0 commit comments

Comments
 (0)