| Total Complexity | 83 |
| Total Lines | 829 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CommonSchemaTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CommonSchemaTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class CommonSchemaTest extends AbstractSchemaTest |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @throws Exception |
||
| 42 | * @throws InvalidConfigException |
||
| 43 | * @throws Throwable |
||
| 44 | */ |
||
| 45 | public function testColumnComment(): void |
||
| 46 | { |
||
| 47 | $db = $this->getConnection(); |
||
| 48 | |||
| 49 | $command = $db->createCommand(); |
||
| 50 | $schema = $db->getSchema(); |
||
| 51 | |||
| 52 | if ($schema->getTableSchema('testCommentTable') !== null) { |
||
| 53 | $command->dropTable('testCommentTable')->execute(); |
||
| 54 | } |
||
| 55 | |||
| 56 | $command->createTable('testCommentTable', ['bar' => Schema::TYPE_INTEGER,])->execute(); |
||
| 57 | $command->addCommentOnColumn('testCommentTable', 'bar', 'Test comment for column.')->execute(); |
||
| 58 | |||
| 59 | $this->assertSame( |
||
| 60 | 'Test comment for column.', |
||
| 61 | $schema->getTableSchema('testCommentTable')->getColumn('bar')->getComment(), |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::columns() |
||
| 67 | */ |
||
| 68 | public function testColumnSchema(array $columns): void |
||
| 69 | { |
||
| 70 | $this->columnSchema($columns); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function testCompositeFk(): void |
||
| 74 | { |
||
| 75 | $db = $this->getConnection(true); |
||
| 76 | |||
| 77 | $schema = $db->getSchema(); |
||
| 78 | $table = $schema->getTableSchema('composite_fk'); |
||
| 79 | |||
| 80 | $this->assertNotNull($table); |
||
| 81 | |||
| 82 | $fk = $table->getForeignKeys(); |
||
| 83 | |||
| 84 | $expectedKey = match ($db->getName()) { |
||
| 85 | 'mysql', 'sqlsrv' => $fk['FK_composite_fk_order_item'], |
||
| 86 | default => $fk['fk_composite_fk_order_item'], |
||
| 87 | }; |
||
| 88 | |||
| 89 | $this->assertCount(1, $fk); |
||
| 90 | $this->assertTrue(isset($expectedKey)); |
||
| 91 | $this->assertSame('order_item', $expectedKey[0]); |
||
| 92 | $this->assertSame('order_id', $expectedKey['order_id']); |
||
| 93 | $this->assertSame('item_id', $expectedKey['item_id']); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function testContraintTablesExistance(): void |
||
| 97 | { |
||
| 98 | $db = $this->getConnection(true); |
||
| 99 | |||
| 100 | $tableNames = ['T_constraints_1', 'T_constraints_2', 'T_constraints_3', 'T_constraints_4']; |
||
| 101 | $schema = $db->getSchema(); |
||
| 102 | |||
| 103 | foreach ($tableNames as $tableName) { |
||
| 104 | $tableSchema = $schema->getTableSchema($tableName); |
||
| 105 | $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema, $tableName); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @throws Exception |
||
| 111 | * @throws InvalidConfigException |
||
| 112 | * @throws Throwable |
||
| 113 | */ |
||
| 114 | public function testFindUniquesIndexes(): void |
||
| 115 | { |
||
| 116 | $db = $this->getConnection(); |
||
| 117 | |||
| 118 | $command = $db->createCommand(); |
||
| 119 | $schema = $db->getSchema(); |
||
| 120 | |||
| 121 | try { |
||
| 122 | $command->dropTable('uniqueIndex')->execute(); |
||
| 123 | } catch (Exception) { |
||
|
|
|||
| 124 | } |
||
| 125 | |||
| 126 | $command->createTable( |
||
| 127 | 'uniqueIndex', |
||
| 128 | ['somecol' => 'string', 'someCol2' => 'string', 'someCol3' => 'string'], |
||
| 129 | )->execute(); |
||
| 130 | $tableSchema = $schema->getTableSchema('uniqueIndex', true); |
||
| 131 | |||
| 132 | $this->assertNotNull($tableSchema); |
||
| 133 | |||
| 134 | $uniqueIndexes = $schema->findUniqueIndexes($tableSchema); |
||
| 135 | |||
| 136 | $this->assertSame([], $uniqueIndexes); |
||
| 137 | |||
| 138 | $command->createIndex('somecolUnique', 'uniqueIndex', 'somecol', QueryBuilder::INDEX_UNIQUE)->execute(); |
||
| 139 | $tableSchema = $schema->getTableSchema('uniqueIndex', true); |
||
| 140 | |||
| 141 | $this->assertNotNull($tableSchema); |
||
| 142 | |||
| 143 | $uniqueIndexes = $schema->findUniqueIndexes($tableSchema); |
||
| 144 | |||
| 145 | $this->assertSame(['somecolUnique' => ['somecol']], $uniqueIndexes); |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Create another column with upper case letter that fails postgres. |
||
| 149 | * |
||
| 150 | * @link https://github.com/yiisoft/yii2/issues/10613 |
||
| 151 | */ |
||
| 152 | $command->createIndex('someCol2Unique', 'uniqueIndex', 'someCol2', QueryBuilder::INDEX_UNIQUE)->execute(); |
||
| 153 | $tableSchema = $schema->getTableSchema('uniqueIndex', true); |
||
| 154 | |||
| 155 | $this->assertNotNull($tableSchema); |
||
| 156 | |||
| 157 | $uniqueIndexes = $schema->findUniqueIndexes($tableSchema); |
||
| 158 | |||
| 159 | $this->assertSame(['someCol2Unique' => ['someCol2'], 'somecolUnique' => ['somecol']], $uniqueIndexes); |
||
| 160 | |||
| 161 | /** @link https://github.com/yiisoft/yii2/issues/13814 */ |
||
| 162 | $command->createIndex('another unique index', 'uniqueIndex', 'someCol3', QueryBuilder::INDEX_UNIQUE)->execute(); |
||
| 163 | $tableSchema = $schema->getTableSchema('uniqueIndex', true); |
||
| 164 | |||
| 165 | $this->assertNotNull($tableSchema); |
||
| 166 | |||
| 167 | $uniqueIndexes = $schema->findUniqueIndexes($tableSchema); |
||
| 168 | |||
| 169 | $this->assertSame( |
||
| 170 | ['another unique index' => ['someCol3'], 'someCol2Unique' => ['someCol2'], 'somecolUnique' => ['somecol']], |
||
| 171 | $uniqueIndexes, |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function testGetColumnNoExist(): void |
||
| 184 | } |
||
| 185 | |||
| 186 | public function testGetDefaultSchema(): void |
||
| 187 | { |
||
| 188 | $db = $this->getConnection(); |
||
| 189 | |||
| 190 | $schema = $db->getSchema(); |
||
| 191 | |||
| 192 | $this->assertNull($schema->getDefaultSchema()); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function testGetNonExistingTableSchema(): void |
||
| 196 | { |
||
| 197 | $db = $this->getConnection(); |
||
| 198 | |||
| 199 | $schema = $db->getSchema(); |
||
| 200 | |||
| 201 | $this->assertNull($schema->getTableSchema('nonexisting_table')); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @throws Exception |
||
| 206 | * @throws InvalidCallException |
||
| 207 | * @throws InvalidConfigException |
||
| 208 | * @throws Throwable |
||
| 209 | */ |
||
| 210 | public function testGetPrimaryKey(): void |
||
| 211 | { |
||
| 212 | $db = $this->getConnection(true); |
||
| 213 | |||
| 214 | $command = $db->createCommand(); |
||
| 215 | |||
| 216 | $insertResult = $command->insertEx('animal', ['type' => 'cat']); |
||
| 217 | $selectResult = $command->setSql( |
||
| 218 | DbHelper::replaceQuotes( |
||
| 219 | <<<SQL |
||
| 220 | SELECT [[id]] FROM [[animal]] WHERE [[type]] = 'cat' |
||
| 221 | SQL, |
||
| 222 | $db->getName(), |
||
| 223 | ) |
||
| 224 | )->queryOne(); |
||
| 225 | |||
| 226 | $this->assertIsArray($insertResult); |
||
| 227 | $this->assertIsArray($selectResult); |
||
| 228 | $this->assertEquals($selectResult['id'], $insertResult['id']); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function testGetSchemaChecks(): void |
||
| 232 | { |
||
| 233 | $db = $this->getConnection(); |
||
| 234 | |||
| 235 | $schema = $db->getSchema(); |
||
| 236 | $tableChecks = $schema->getSchemaChecks(); |
||
| 237 | |||
| 238 | $this->assertIsArray($tableChecks); |
||
| 239 | |||
| 240 | foreach ($tableChecks as $checks) { |
||
| 241 | $this->assertIsArray($checks); |
||
| 242 | $this->assertContainsOnlyInstancesOf(CheckConstraint::class, $checks); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | public function testGetSchemaDefaultValues(): void |
||
| 247 | { |
||
| 248 | $db = $this->getConnection(); |
||
| 249 | |||
| 250 | $schema = $db->getSchema(); |
||
| 251 | $tableDefaultValues = $schema->getSchemaDefaultValues(); |
||
| 252 | |||
| 253 | $this->assertIsArray($tableDefaultValues); |
||
| 254 | |||
| 255 | foreach ($tableDefaultValues as $defaultValues) { |
||
| 256 | $this->assertIsArray($defaultValues); |
||
| 257 | $this->assertContainsOnlyInstancesOf(DefaultValueConstraint::class, $defaultValues); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | public function testGetSchemaForeignKeys(): void |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | public function testGetSchemaIndexes(): void |
||
| 277 | { |
||
| 278 | $db = $this->getConnection(); |
||
| 279 | |||
| 280 | $schema = $db->getSchema(); |
||
| 281 | $tableIndexes = $schema->getSchemaIndexes(); |
||
| 282 | |||
| 283 | $this->assertIsArray($tableIndexes); |
||
| 284 | |||
| 285 | foreach ($tableIndexes as $indexes) { |
||
| 286 | $this->assertIsArray($indexes); |
||
| 287 | $this->assertContainsOnlyInstancesOf(IndexConstraint::class, $indexes); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | public function testGetSchemaPrimaryKeys(): void |
||
| 292 | { |
||
| 293 | $db = $this->getConnection(); |
||
| 294 | |||
| 295 | $schema = $db->getSchema(); |
||
| 296 | $tablePks = $schema->getSchemaPrimaryKeys(); |
||
| 297 | |||
| 298 | $this->assertIsArray($tablePks); |
||
| 299 | $this->assertContainsOnlyInstancesOf(Constraint::class, $tablePks); |
||
| 300 | } |
||
| 301 | |||
| 302 | public function testGetSchemaUniques(): void |
||
| 303 | { |
||
| 304 | $db = $this->getConnection(); |
||
| 305 | |||
| 306 | $schema = $db->getSchema(); |
||
| 307 | $tableUniques = $schema->getSchemaUniques(); |
||
| 308 | |||
| 309 | $this->assertIsArray($tableUniques); |
||
| 310 | |||
| 311 | foreach ($tableUniques as $uniques) { |
||
| 312 | $this->assertIsArray($uniques); |
||
| 313 | $this->assertContainsOnlyInstancesOf(Constraint::class, $uniques); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::columnsTypeChar() |
||
| 319 | */ |
||
| 320 | public function testGetStringFieldsSize( |
||
| 321 | string $columnName, |
||
| 322 | string $columnType, |
||
| 323 | int|null $columnSize, |
||
| 324 | string $columnDbType |
||
| 325 | ): void { |
||
| 326 | $db = $this->getConnection(true); |
||
| 327 | |||
| 328 | $schema = $db->getSchema(); |
||
| 329 | $tableSchema = $schema->getTableSchema('type'); |
||
| 330 | |||
| 331 | $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema); |
||
| 332 | |||
| 333 | $columns = $tableSchema->getColumns(); |
||
| 334 | |||
| 335 | foreach ($columns as $name => $column) { |
||
| 336 | $type = $column->getType(); |
||
| 337 | $size = $column->getSize(); |
||
| 338 | $dbType = $column->getDbType(); |
||
| 339 | |||
| 340 | if ($name === $columnName) { |
||
| 341 | $this->assertSame($columnType, $type); |
||
| 342 | $this->assertSame($columnSize, $size); |
||
| 343 | $this->assertSame($columnDbType, $dbType); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | public function testGetTableChecks(): void |
||
| 349 | { |
||
| 350 | $db = $this->getConnection(true); |
||
| 351 | |||
| 352 | $schema = $db->getSchema(); |
||
| 353 | $tableChecks = $schema->getTableChecks('T_constraints_1'); |
||
| 354 | |||
| 355 | $this->assertIsArray($tableChecks); |
||
| 356 | |||
| 357 | $this->assertContainsOnlyInstancesOf(CheckConstraint::class, $tableChecks); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes() |
||
| 362 | * |
||
| 363 | * @throws NotSupportedException |
||
| 364 | */ |
||
| 365 | public function testGetTableNames(array $pdoAttributes): void |
||
| 366 | { |
||
| 367 | $db = $this->getConnection(true); |
||
| 368 | |||
| 369 | foreach ($pdoAttributes as $name => $value) { |
||
| 370 | if ($name === PDO::ATTR_EMULATE_PREPARES) { |
||
| 371 | continue; |
||
| 372 | } |
||
| 373 | |||
| 374 | $db->getPDO()?->setAttribute($name, $value); |
||
| 375 | } |
||
| 376 | |||
| 377 | $schema = $db->getSchema(); |
||
| 378 | $tablesNames = $schema->getTableNames(); |
||
| 379 | |||
| 380 | $this->assertContains('customer', $tablesNames); |
||
| 381 | $this->assertContains('category', $tablesNames); |
||
| 382 | $this->assertContains('item', $tablesNames); |
||
| 383 | $this->assertContains('order', $tablesNames); |
||
| 384 | $this->assertContains('order_item', $tablesNames); |
||
| 385 | $this->assertContains('type', $tablesNames); |
||
| 386 | $this->assertContains('animal', $tablesNames); |
||
| 387 | $this->assertContains('animal_view', $tablesNames); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::tableSchema() |
||
| 392 | */ |
||
| 393 | public function testGetTableSchema(string $name, string $expectedName): void |
||
| 394 | { |
||
| 395 | $db = $this->getConnection(true); |
||
| 396 | |||
| 397 | $tableSchema = $db->getSchema()->getTableSchema($name); |
||
| 398 | |||
| 399 | $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema); |
||
| 400 | $this->assertEquals($expectedName, $tableSchema->getName()); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes() |
||
| 405 | * |
||
| 406 | * @throws NotSupportedException |
||
| 407 | */ |
||
| 408 | public function testGetTableSchemas(array $pdoAttributes): void |
||
| 409 | { |
||
| 410 | $db = $this->getConnection(true); |
||
| 411 | |||
| 412 | foreach ($pdoAttributes as $name => $value) { |
||
| 413 | if ($name === PDO::ATTR_EMULATE_PREPARES) { |
||
| 414 | continue; |
||
| 415 | } |
||
| 416 | |||
| 417 | $db->getPDO()?->setAttribute($name, $value); |
||
| 418 | } |
||
| 419 | |||
| 420 | $schema = $db->getSchema(); |
||
| 421 | $tables = $schema->getTableSchemas(); |
||
| 422 | |||
| 423 | $this->assertCount(count($schema->getTableNames()), $tables); |
||
| 424 | |||
| 425 | foreach ($tables as $table) { |
||
| 426 | $this->assertInstanceOf(TableSchemaInterface::class, $table); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @throws InvalidConfigException |
||
| 432 | * @throws NotSupportedException |
||
| 433 | * @throws Exception |
||
| 434 | */ |
||
| 435 | public function testGetTableSchemaWithAttrCase(): void |
||
| 436 | { |
||
| 437 | $db = $this->getConnection(true); |
||
| 438 | |||
| 439 | $schema = $db->getSchema(); |
||
| 440 | $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
||
| 441 | |||
| 442 | $this->assertCount(count($schema->getTableNames()), $schema->getTableSchemas()); |
||
| 443 | |||
| 444 | $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); |
||
| 445 | |||
| 446 | $this->assertCount(count($schema->getTableNames()), $schema->getTableSchemas()); |
||
| 447 | } |
||
| 448 | |||
| 449 | public function testGetViewNames(): void |
||
| 457 | } |
||
| 458 | |||
| 459 | public function testNegativeDefaultValues(): void |
||
| 460 | { |
||
| 461 | $schema = $this->getConnection(true); |
||
| 462 | |||
| 463 | $schema = $schema->getSchema(); |
||
| 464 | $table = $schema->getTableSchema('negative_default_values'); |
||
| 465 | |||
| 466 | $this->assertNotNull($table); |
||
| 467 | $this->assertSame(-123, $table->getColumn('tinyint_col')?->getDefaultValue()); |
||
| 468 | $this->assertSame(-123, $table->getColumn('smallint_col')?->getDefaultValue()); |
||
| 469 | $this->assertSame(-123, $table->getColumn('int_col')?->getDefaultValue()); |
||
| 470 | $this->assertSame(-123, $table->getColumn('bigint_col')?->getDefaultValue()); |
||
| 471 | $this->assertSame(-12345.6789, $table->getColumn('float_col')?->getDefaultValue()); |
||
| 472 | $this->assertEquals(-33.22, $table->getColumn('numeric_col')?->getDefaultValue()); |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @throws Exception |
||
| 477 | * @throws InvalidConfigException |
||
| 478 | * @throws Throwable |
||
| 479 | */ |
||
| 480 | public function testQuoterEscapingValue(): void |
||
| 481 | { |
||
| 482 | $db = $this->getConnection(true); |
||
| 483 | |||
| 484 | $quoter = $db->getQuoter(); |
||
| 485 | $db->createCommand( |
||
| 486 | <<<SQL |
||
| 487 | DELETE FROM [[quoter]] |
||
| 488 | SQL |
||
| 489 | )->execute(); |
||
| 490 | $data = $this->generateQuoterEscapingValues(); |
||
| 491 | |||
| 492 | foreach ($data as $index => $value) { |
||
| 493 | $quotedName = $quoter->quoteValue('testValue_' . $index); |
||
| 494 | $quoteValue = $quoter->quoteValue($value); |
||
| 495 | $db->createCommand( |
||
| 496 | <<<SQL |
||
| 497 | INSERT INTO [[quoter]] ([[name]], [[description]]) VALUES ($quotedName, $quoteValue) |
||
| 498 | SQL, |
||
| 499 | )->execute(); |
||
| 500 | $result = $db->createCommand( |
||
| 501 | <<<SQL |
||
| 502 | SELECT * FROM [[quoter]] WHERE [[name]]=$quotedName |
||
| 503 | SQL, |
||
| 504 | )->queryOne(); |
||
| 505 | |||
| 506 | $this->assertSame($value, $result['description']); |
||
| 507 | } |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @depends testSchemaCache |
||
| 512 | */ |
||
| 513 | public function testRefreshTableSchema(): void |
||
| 514 | { |
||
| 515 | $db = $this->getConnection(true); |
||
| 516 | |||
| 517 | $schema = $db->getSchema(); |
||
| 518 | $schema->schemaCacheEnable(true); |
||
| 519 | $noCacheTable = $schema->getTableSchema('type', true); |
||
| 520 | $schema->refreshTableSchema('type'); |
||
| 521 | $refreshedTable = $schema->getTableSchema('type'); |
||
| 522 | |||
| 523 | $this->assertNotSame($noCacheTable, $refreshedTable); |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @throws Exception |
||
| 528 | * @throws InvalidConfigException |
||
| 529 | */ |
||
| 530 | public function testSchemaCache(): void |
||
| 531 | { |
||
| 532 | $db = $this->getConnection(true); |
||
| 533 | |||
| 534 | $schema = $db->getSchema(); |
||
| 535 | $schema->schemaCacheEnable(true); |
||
| 536 | $noCacheTable = $schema->getTableSchema('type', true); |
||
| 537 | $cachedTable = $schema->getTableSchema('type'); |
||
| 538 | |||
| 539 | $this->assertSame($noCacheTable, $cachedTable); |
||
| 540 | |||
| 541 | $db->createCommand()->renameTable('type', 'type_test'); |
||
| 542 | $noCacheTable = $schema->getTableSchema('type', true); |
||
| 543 | |||
| 544 | $this->assertNotSame($noCacheTable, $cachedTable); |
||
| 545 | |||
| 546 | $db->createCommand()->renameTable('type_test', 'type'); |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::tableSchemaCachePrefixes() |
||
| 551 | */ |
||
| 552 | public function testTableSchemaCacheWithTablePrefixes( |
||
| 553 | string $tablePrefix, |
||
| 554 | string $tableName, |
||
| 555 | string $testTablePrefix, |
||
| 556 | string $testTableName |
||
| 557 | ): void { |
||
| 558 | $db = $this->getConnection(true); |
||
| 559 | |||
| 560 | $schema = $db->getSchema(); |
||
| 561 | $schema->schemaCacheEnable(true); |
||
| 562 | $db->setTablePrefix($tablePrefix); |
||
| 563 | $noCacheTable = $schema->getTableSchema($tableName, true); |
||
| 564 | |||
| 565 | $this->assertInstanceOf(TableSchemaInterface::class, $noCacheTable); |
||
| 566 | |||
| 567 | /* Compare */ |
||
| 568 | $db->setTablePrefix($testTablePrefix); |
||
| 569 | $testNoCacheTable = $schema->getTableSchema($testTableName); |
||
| 570 | |||
| 571 | $this->assertSame($noCacheTable, $testNoCacheTable); |
||
| 572 | |||
| 573 | $db->setTablePrefix($tablePrefix); |
||
| 574 | $schema->refreshTableSchema($tableName); |
||
| 575 | $refreshedTable = $schema->getTableSchema($tableName); |
||
| 576 | |||
| 577 | $this->assertInstanceOf(TableSchemaInterface::class, $refreshedTable); |
||
| 578 | $this->assertNotSame($noCacheTable, $refreshedTable); |
||
| 579 | |||
| 580 | /* Compare */ |
||
| 581 | $db->setTablePrefix($testTablePrefix); |
||
| 582 | $schema->refreshTableSchema($testTablePrefix); |
||
| 583 | $testRefreshedTable = $schema->getTableSchema($testTableName); |
||
| 584 | |||
| 585 | $this->assertInstanceOf(TableSchemaInterface::class, $testRefreshedTable); |
||
| 586 | $this->assertEquals($refreshedTable, $testRefreshedTable); |
||
| 587 | $this->assertNotSame($testNoCacheTable, $testRefreshedTable); |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::constraints() |
||
| 592 | * |
||
| 593 | * @throws Exception |
||
| 594 | * @throws JsonException |
||
| 595 | */ |
||
| 596 | public function testTableSchemaConstraints(string $tableName, string $type, mixed $expected): void |
||
| 597 | { |
||
| 598 | if ($expected === false) { |
||
| 599 | $this->expectException(NotSupportedException::class); |
||
| 600 | } |
||
| 601 | |||
| 602 | $db = $this->getConnection(true); |
||
| 603 | $schema = $db->getSchema(); |
||
| 604 | $constraints = $schema->{'getTable' . ucfirst($type)}($tableName); |
||
| 605 | |||
| 606 | $this->assertMetadataEquals($expected, $constraints); |
||
| 607 | |||
| 608 | $db->close(); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::constraints() |
||
| 613 | * |
||
| 614 | * @throws Exception |
||
| 615 | * @throws JsonException |
||
| 616 | * @throws InvalidConfigException |
||
| 617 | */ |
||
| 618 | public function testTableSchemaConstraintsWithPdoLowercase(string $tableName, string $type, mixed $expected): void |
||
| 619 | { |
||
| 620 | if ($expected === false) { |
||
| 621 | $this->expectException(NotSupportedException::class); |
||
| 622 | } |
||
| 623 | |||
| 624 | $db = $this->getConnection(true); |
||
| 625 | |||
| 626 | $schema = $db->getSchema(); |
||
| 627 | $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
||
| 628 | $constraints = $schema->{'getTable' . ucfirst($type)}($tableName, true); |
||
| 629 | |||
| 630 | $this->assertMetadataEquals($expected, $constraints); |
||
| 631 | |||
| 632 | $db->close(); |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::constraints() |
||
| 637 | * |
||
| 638 | * @throws Exception |
||
| 639 | * @throws JsonException |
||
| 640 | * @throws InvalidConfigException |
||
| 641 | */ |
||
| 642 | public function testTableSchemaConstraintsWithPdoUppercase(string $tableName, string $type, mixed $expected): void |
||
| 643 | { |
||
| 644 | if ($expected === false) { |
||
| 645 | $this->expectException(NotSupportedException::class); |
||
| 646 | } |
||
| 647 | |||
| 648 | $db = $this->getConnection(true); |
||
| 649 | |||
| 650 | $schema = $db->getSchema(); |
||
| 651 | $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); |
||
| 652 | $constraints = $schema->{'getTable' . ucfirst($type)}($tableName, true); |
||
| 653 | |||
| 654 | $this->assertMetadataEquals($expected, $constraints); |
||
| 655 | |||
| 656 | $db->close(); |
||
| 657 | } |
||
| 658 | |||
| 659 | private function generateQuoterEscapingValues(): array |
||
| 660 | { |
||
| 661 | $result = []; |
||
| 662 | $stringLength = 16; |
||
| 663 | |||
| 664 | for ($i = 32; $i < 128 - $stringLength; $i += $stringLength) { |
||
| 665 | $str = ''; |
||
| 666 | |||
| 667 | for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) { |
||
| 668 | $str .= mb_chr($symbol, 'UTF-8'); |
||
| 669 | } |
||
| 670 | |||
| 671 | $result[] = $str; |
||
| 672 | $str = ''; |
||
| 673 | |||
| 674 | for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) { |
||
| 675 | $str .= mb_chr($symbol, 'UTF-8') . mb_chr($symbol, 'UTF-8'); |
||
| 676 | } |
||
| 677 | |||
| 678 | $result[] = $str; |
||
| 679 | } |
||
| 680 | |||
| 681 | return $result; |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @throws JsonException |
||
| 686 | */ |
||
| 687 | protected function assertMetadataEquals($expected, $actual): void |
||
| 688 | { |
||
| 689 | switch (strtolower(gettype($expected))) { |
||
| 690 | case 'object': |
||
| 691 | $this->assertIsObject($actual); |
||
| 692 | break; |
||
| 693 | case 'array': |
||
| 694 | $this->assertIsArray($actual); |
||
| 695 | break; |
||
| 696 | case 'null': |
||
| 697 | $this->assertNull($actual); |
||
| 698 | break; |
||
| 699 | } |
||
| 700 | |||
| 701 | if (is_array($expected)) { |
||
| 702 | $this->normalizeArrayKeys($expected, false); |
||
| 703 | $this->normalizeArrayKeys($actual, false); |
||
| 704 | } |
||
| 705 | |||
| 706 | $this->normalizeConstraints($expected, $actual); |
||
| 707 | |||
| 708 | if (is_array($expected)) { |
||
| 709 | $this->normalizeArrayKeys($expected, true); |
||
| 710 | $this->normalizeArrayKeys($actual, true); |
||
| 711 | } |
||
| 712 | |||
| 713 | $this->assertEquals($expected, $actual); |
||
| 714 | } |
||
| 715 | |||
| 716 | protected function columnSchema(array $columns, string $table = 'type'): void |
||
| 717 | { |
||
| 718 | $db = $this->getConnection(true); |
||
| 719 | |||
| 720 | $table = $db->getTableSchema($table, true); |
||
| 721 | |||
| 722 | $this->assertNotNull($table); |
||
| 723 | |||
| 724 | $expectedColNames = array_keys($columns); |
||
| 725 | sort($expectedColNames); |
||
| 726 | $colNames = $table->getColumnNames(); |
||
| 727 | sort($colNames); |
||
| 728 | |||
| 729 | $this->assertSame($expectedColNames, $colNames); |
||
| 730 | |||
| 731 | foreach ($table->getColumns() as $name => $column) { |
||
| 732 | $expected = $columns[$name]; |
||
| 733 | |||
| 734 | $this->assertSame( |
||
| 735 | $expected['dbType'], |
||
| 736 | $column->getDbType(), |
||
| 737 | "dbType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}." |
||
| 738 | ); |
||
| 739 | $this->assertSame( |
||
| 740 | $expected['phpType'], |
||
| 741 | $column->getPhpType(), |
||
| 742 | "phpType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}." |
||
| 743 | ); |
||
| 744 | $this->assertSame($expected['type'], $column->getType(), "type of column $name does not match."); |
||
| 745 | $this->assertSame( |
||
| 746 | $expected['allowNull'], |
||
| 747 | $column->isAllowNull(), |
||
| 748 | "allowNull of column $name does not match." |
||
| 749 | ); |
||
| 750 | $this->assertSame( |
||
| 751 | $expected['autoIncrement'], |
||
| 752 | $column->isAutoIncrement(), |
||
| 753 | "autoIncrement of column $name does not match." |
||
| 754 | ); |
||
| 755 | $this->assertSame( |
||
| 756 | $expected['enumValues'], |
||
| 757 | $column->getEnumValues(), |
||
| 758 | "enumValues of column $name does not match." |
||
| 759 | ); |
||
| 760 | $this->assertSame($expected['size'], $column->getSize(), "size of column $name does not match."); |
||
| 761 | $this->assertSame( |
||
| 762 | $expected['precision'], |
||
| 763 | $column->getPrecision(), |
||
| 764 | "precision of column $name does not match." |
||
| 765 | ); |
||
| 766 | |||
| 767 | $this->assertSame($expected['scale'], $column->getScale(), "scale of column $name does not match."); |
||
| 768 | |||
| 769 | if (is_object($expected['defaultValue'])) { |
||
| 770 | $this->assertIsObject( |
||
| 771 | $column->getDefaultValue(), |
||
| 772 | "defaultValue of column $name is expected to be an object but it is not." |
||
| 773 | ); |
||
| 774 | $this->assertSame( |
||
| 775 | (string) $expected['defaultValue'], |
||
| 776 | (string) $column->getDefaultValue(), |
||
| 777 | "defaultValue of column $name does not match." |
||
| 778 | ); |
||
| 779 | } else { |
||
| 780 | $this->assertSame( |
||
| 781 | $expected['defaultValue'], |
||
| 782 | $column->getDefaultValue(), |
||
| 783 | "defaultValue of column $name does not match." |
||
| 784 | ); |
||
| 785 | } |
||
| 786 | |||
| 787 | /* Pgsql only */ |
||
| 788 | if (isset($expected['dimension'])) { |
||
| 789 | /** @psalm-suppress UndefinedMethod */ |
||
| 790 | $this->assertSame( |
||
| 791 | $expected['dimension'], |
||
| 792 | $column->getDimension(), |
||
| 793 | "dimension of column $name does not match" |
||
| 794 | ); |
||
| 795 | } |
||
| 796 | } |
||
| 797 | |||
| 798 | $db->close(); |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * @throws JsonException |
||
| 803 | */ |
||
| 804 | private function normalizeArrayKeys(array &$array, bool $caseSensitive): void |
||
| 805 | { |
||
| 806 | $newArray = []; |
||
| 807 | |||
| 808 | foreach ($array as $value) { |
||
| 809 | if ($value instanceof Constraint) { |
||
| 810 | $key = (array) $value; |
||
| 811 | unset( |
||
| 812 | $key["\000Yiisoft\Db\Constraint\Constraint\000name"], |
||
| 813 | $key["\u0000Yiisoft\\Db\\Constraint\\ForeignKeyConstraint\u0000foreignSchemaName"] |
||
| 814 | ); |
||
| 815 | |||
| 816 | foreach ($key as $keyName => $keyValue) { |
||
| 817 | if ($keyValue instanceof AnyCaseValue) { |
||
| 818 | $key[$keyName] = $keyValue->value; |
||
| 819 | } elseif ($keyValue instanceof AnyValue) { |
||
| 820 | $key[$keyName] = '[AnyValue]'; |
||
| 821 | } |
||
| 822 | } |
||
| 823 | |||
| 824 | ksort($key, SORT_STRING); |
||
| 825 | $newArray[$caseSensitive |
||
| 826 | ? json_encode($key, JSON_THROW_ON_ERROR) |
||
| 827 | : strtolower(json_encode($key, JSON_THROW_ON_ERROR))] = $value; |
||
| 828 | } else { |
||
| 829 | $newArray[] = $value; |
||
| 830 | } |
||
| 831 | } |
||
| 832 | |||
| 833 | ksort($newArray, SORT_STRING); |
||
| 834 | $array = $newArray; |
||
| 835 | } |
||
| 836 | |||
| 837 | private function normalizeConstraints($expected, $actual): void |
||
| 849 | } |
||
| 850 | } |
||
| 851 | |||
| 852 | private function normalizeConstraintPair(Constraint $expectedConstraint, Constraint $actualConstraint): void |
||
| 853 | { |
||
| 854 | if ($expectedConstraint::class !== $actualConstraint::class) { |
||
| 855 | return; |
||
| 856 | } |
||
| 857 | |||
| 858 | foreach (array_keys((array) $expectedConstraint) as $name) { |
||
| 859 | if ($expectedConstraint->getName() instanceof AnyValue) { |
||
| 860 | $actualConstraint->name($expectedConstraint->getName()); |
||
| 861 | } elseif ($expectedConstraint->getName() instanceof AnyCaseValue) { |
||
| 867 | } |
||
| 868 | } |
||
| 869 | } |
||
| 870 | } |
||
| 871 |