| Total Complexity | 50 |
| Total Lines | 515 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TestSchemaTrait 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 TestSchemaTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | trait TestSchemaTrait |
||
| 29 | { |
||
| 30 | public function testGetTableSchemasWithAttrCase(): void |
||
| 31 | { |
||
| 32 | $db = $this->getConnection(false); |
||
|
|
|||
| 33 | |||
| 34 | $db->getSlavePdo()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
||
| 35 | |||
| 36 | $this->assertCount(count($db->getSchema()->getTableNames()), $db->getSchema()->getTableSchemas()); |
||
| 37 | |||
| 38 | $db->getSlavePdo()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); |
||
| 39 | |||
| 40 | $this->assertCount(count($db->getSchema()->getTableNames()), $db->getSchema()->getTableSchemas()); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testGetNonExistingTableSchema(): void |
||
| 44 | { |
||
| 45 | $this->assertNull($this->getConnection()->getSchema()->getTableSchema('nonexisting_table')); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function testSchemaCache(): void |
||
| 49 | { |
||
| 50 | $db = $this->getConnection(); |
||
| 51 | |||
| 52 | $schema = $db->getSchema(); |
||
| 53 | |||
| 54 | $this->schemaCache->setEnable(true); |
||
| 55 | |||
| 56 | $noCacheTable = $schema->getTableSchema('type', true); |
||
| 57 | $cachedTable = $schema->getTableSchema('type', false); |
||
| 58 | |||
| 59 | $this->assertEquals($noCacheTable, $cachedTable); |
||
| 60 | |||
| 61 | $db->createCommand()->renameTable('type', 'type_test'); |
||
| 62 | |||
| 63 | $noCacheTable = $schema->getTableSchema('type', true); |
||
| 64 | |||
| 65 | $this->assertNotSame($noCacheTable, $cachedTable); |
||
| 66 | |||
| 67 | $db->createCommand()->renameTable('type_test', 'type'); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @depends testSchemaCache |
||
| 72 | */ |
||
| 73 | public function testRefreshTableSchema(): void |
||
| 74 | { |
||
| 75 | $schema = $this->getConnection()->getSchema(); |
||
| 76 | |||
| 77 | $this->schemaCache->setEnable(true); |
||
| 78 | |||
| 79 | $noCacheTable = $schema->getTableSchema('type', true); |
||
| 80 | |||
| 81 | $schema->refreshTableSchema('type'); |
||
| 82 | |||
| 83 | $refreshedTable = $schema->getTableSchema('type', false); |
||
| 84 | |||
| 85 | $this->assertNotSame($noCacheTable, $refreshedTable); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function testCompositeFk(): void |
||
| 89 | { |
||
| 90 | $schema = $this->getConnection()->getSchema(); |
||
| 91 | |||
| 92 | $table = $schema->getTableSchema('composite_fk'); |
||
| 93 | |||
| 94 | $fk = $table->getForeignKeys(); |
||
| 95 | |||
| 96 | $this->assertCount(1, $fk); |
||
| 97 | $this->assertTrue(isset($fk['FK_composite_fk_order_item'])); |
||
| 98 | $this->assertEquals('order_item', $fk['FK_composite_fk_order_item'][0]); |
||
| 99 | $this->assertEquals('order_id', $fk['FK_composite_fk_order_item']['order_id']); |
||
| 100 | $this->assertEquals('item_id', $fk['FK_composite_fk_order_item']['item_id']); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function testGetPDOType(): void |
||
| 104 | { |
||
| 105 | $values = [ |
||
| 106 | [null, PDO::PARAM_NULL], |
||
| 107 | ['', PDO::PARAM_STR], |
||
| 108 | ['hello', PDO::PARAM_STR], |
||
| 109 | [0, PDO::PARAM_INT], |
||
| 110 | [1, PDO::PARAM_INT], |
||
| 111 | [1337, PDO::PARAM_INT], |
||
| 112 | [true, PDO::PARAM_BOOL], |
||
| 113 | [false, PDO::PARAM_BOOL], |
||
| 114 | [$fp = fopen(__FILE__, 'rb'), PDO::PARAM_LOB], |
||
| 115 | ]; |
||
| 116 | |||
| 117 | $schema = $this->getConnection()->getSchema(); |
||
| 118 | |||
| 119 | foreach ($values as $value) { |
||
| 120 | $this->assertEquals($value[1], $schema->getPdoType($value[0]), 'type for value ' . print_r($value[0], true) . ' does not match.'); |
||
| 121 | } |
||
| 122 | |||
| 123 | fclose($fp); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function testColumnSchema(): void |
||
| 127 | { |
||
| 128 | $columns = $this->getExpectedColumns(); |
||
| 129 | |||
| 130 | $table = $this->getConnection(false)->getSchema()->getTableSchema('type', true); |
||
| 131 | |||
| 132 | $expectedColNames = array_keys($columns); |
||
| 133 | |||
| 134 | sort($expectedColNames); |
||
| 135 | |||
| 136 | $colNames = $table->getColumnNames(); |
||
| 137 | |||
| 138 | sort($colNames); |
||
| 139 | |||
| 140 | $this->assertEquals($expectedColNames, $colNames); |
||
| 141 | |||
| 142 | foreach ($table->getColumns() as $name => $column) { |
||
| 143 | $expected = $columns[$name]; |
||
| 144 | $this->assertSame( |
||
| 145 | $expected['dbType'], |
||
| 146 | $column->getDbType(), |
||
| 147 | "dbType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}." |
||
| 148 | ); |
||
| 149 | $this->assertSame( |
||
| 150 | $expected['phpType'], |
||
| 151 | $column->getPhpType(), |
||
| 152 | "phpType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}." |
||
| 153 | ); |
||
| 154 | $this->assertSame($expected['type'], $column->getType(), "type of column $name does not match."); |
||
| 155 | $this->assertSame( |
||
| 156 | $expected['allowNull'], |
||
| 157 | $column->isAllowNull(), |
||
| 158 | "allowNull of column $name does not match." |
||
| 159 | ); |
||
| 160 | $this->assertSame( |
||
| 161 | $expected['autoIncrement'], |
||
| 162 | $column->isAutoIncrement(), |
||
| 163 | "autoIncrement of column $name does not match." |
||
| 164 | ); |
||
| 165 | $this->assertSame( |
||
| 166 | $expected['enumValues'], |
||
| 167 | $column->getEnumValues(), |
||
| 168 | "enumValues of column $name does not match." |
||
| 169 | ); |
||
| 170 | $this->assertSame($expected['size'], $column->getSize(), "size of column $name does not match."); |
||
| 171 | $this->assertSame( |
||
| 172 | $expected['precision'], |
||
| 173 | $column->getPrecision(), |
||
| 174 | "precision of column $name does not match." |
||
| 175 | ); |
||
| 176 | $this->assertSame($expected['scale'], $column->getScale(), "scale of column $name does not match."); |
||
| 177 | if (\is_object($expected['defaultValue'])) { |
||
| 178 | $this->assertIsObject( |
||
| 179 | $column->getDefaultValue(), |
||
| 180 | "defaultValue of column $name is expected to be an object but it is not." |
||
| 181 | ); |
||
| 182 | $this->assertEquals( |
||
| 183 | (string) $expected['defaultValue'], |
||
| 184 | (string) $column->getDefaultValue(), |
||
| 185 | "defaultValue of column $name does not match." |
||
| 186 | ); |
||
| 187 | } else { |
||
| 188 | $this->assertEquals( |
||
| 189 | $expected['defaultValue'], |
||
| 190 | $column->getDefaultValue(), |
||
| 191 | "defaultValue of column $name does not match." |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | /* Pgsql only */ |
||
| 195 | if (isset($expected['dimension'])) { |
||
| 196 | $this->assertSame( |
||
| 197 | $expected['dimension'], |
||
| 198 | $column->getDimension(), |
||
| 199 | "dimension of column $name does not match" |
||
| 200 | ); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | public function testColumnSchemaDbTypecastWithEmptyCharType(): void |
||
| 206 | { |
||
| 207 | $columnSchema = new ColumnSchema(); |
||
| 208 | |||
| 209 | $columnSchema->setType(Schema::TYPE_CHAR); |
||
| 210 | |||
| 211 | $this->assertSame('', $columnSchema->dbTypecast('')); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function testNegativeDefaultValues(): void |
||
| 215 | { |
||
| 216 | $schema = $this->getConnection()->getSchema(); |
||
| 217 | |||
| 218 | $table = $schema->getTableSchema('negative_default_values'); |
||
| 219 | |||
| 220 | $this->assertEquals(-123, $table->getColumn('tinyint_col')->getDefaultValue()); |
||
| 221 | $this->assertEquals(-123, $table->getColumn('smallint_col')->getDefaultValue()); |
||
| 222 | $this->assertEquals(-123, $table->getColumn('int_col')->getDefaultValue()); |
||
| 223 | $this->assertEquals(-123, $table->getColumn('bigint_col')->getDefaultValue()); |
||
| 224 | $this->assertEquals(-12345.6789, $table->getColumn('float_col')->getDefaultValue()); |
||
| 225 | $this->assertEquals(-33.22, $table->getColumn('numeric_col')->getDefaultValue()); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function testContraintTablesExistance(): void |
||
| 229 | { |
||
| 230 | $tableNames = [ |
||
| 231 | 'T_constraints_1', |
||
| 232 | 'T_constraints_2', |
||
| 233 | 'T_constraints_3', |
||
| 234 | 'T_constraints_4', |
||
| 235 | ]; |
||
| 236 | |||
| 237 | $schema = $this->getConnection()->getSchema(); |
||
| 238 | |||
| 239 | foreach ($tableNames as $tableName) { |
||
| 240 | $tableSchema = $schema->getTableSchema($tableName); |
||
| 241 | $this->assertInstanceOf(TableSchema::class, $tableSchema, $tableName); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | public function testGetColumnNoExist(): void |
||
| 246 | { |
||
| 247 | $schema = $this->getConnection()->getSchema(); |
||
| 248 | $table = $schema->getTableSchema('negative_default_values'); |
||
| 249 | |||
| 250 | $this->assertNull($table->getColumn('no_exist')); |
||
| 251 | } |
||
| 252 | |||
| 253 | private function assertMetadataEquals($expected, $actual): void |
||
| 254 | { |
||
| 255 | switch (strtolower(gettype($expected))) { |
||
| 256 | case 'object': |
||
| 257 | $this->assertIsObject($actual); |
||
| 258 | break; |
||
| 259 | case 'array': |
||
| 260 | $this->assertIsArray($actual); |
||
| 261 | break; |
||
| 262 | case 'null': |
||
| 263 | $this->assertNull($actual); |
||
| 264 | break; |
||
| 265 | } |
||
| 266 | |||
| 267 | if (is_array($expected)) { |
||
| 268 | $this->normalizeArrayKeys($expected, false); |
||
| 269 | $this->normalizeArrayKeys($actual, false); |
||
| 270 | } |
||
| 271 | |||
| 272 | $this->normalizeConstraints($expected, $actual); |
||
| 273 | |||
| 274 | if (is_array($expected)) { |
||
| 275 | $this->normalizeArrayKeys($expected, true); |
||
| 276 | $this->normalizeArrayKeys($actual, true); |
||
| 277 | } |
||
| 278 | |||
| 279 | $this->assertEquals($expected, $actual); |
||
| 280 | } |
||
| 281 | |||
| 282 | private function normalizeArrayKeys(array &$array, bool $caseSensitive): void |
||
| 283 | { |
||
| 284 | $newArray = []; |
||
| 285 | |||
| 286 | foreach ($array as $value) { |
||
| 287 | if ($value instanceof Constraint) { |
||
| 288 | $key = (array) $value; |
||
| 289 | unset( |
||
| 290 | $key["\000Yiisoft\Db\Constraint\Constraint\000name"], |
||
| 291 | $key["\u0000Yiisoft\\Db\\Constraint\\ForeignKeyConstraint\u0000foreignSchemaName"] |
||
| 292 | ); |
||
| 293 | |||
| 294 | foreach ($key as $keyName => $keyValue) { |
||
| 295 | if ($keyValue instanceof AnyCaseValue) { |
||
| 296 | $key[$keyName] = $keyValue->value; |
||
| 297 | } elseif ($keyValue instanceof AnyValue) { |
||
| 298 | $key[$keyName] = '[AnyValue]'; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | ksort($key, SORT_STRING); |
||
| 303 | |||
| 304 | $newArray[$caseSensitive |
||
| 305 | ? json_encode($key, JSON_THROW_ON_ERROR) |
||
| 306 | : strtolower(json_encode($key, JSON_THROW_ON_ERROR))] = $value; |
||
| 307 | } else { |
||
| 308 | $newArray[] = $value; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | ksort($newArray, SORT_STRING); |
||
| 313 | |||
| 314 | $array = $newArray; |
||
| 315 | } |
||
| 316 | |||
| 317 | private function normalizeConstraints(&$expected, &$actual): void |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | private function normalizeConstraintPair(Constraint $expectedConstraint, Constraint $actualConstraint): void |
||
| 333 | { |
||
| 334 | if (get_class($expectedConstraint) !== get_class($actualConstraint)) { |
||
| 335 | return; |
||
| 336 | } |
||
| 337 | |||
| 338 | foreach (array_keys((array) $expectedConstraint) as $name) { |
||
| 339 | if ($expectedConstraint->getName() instanceof AnyValue) { |
||
| 340 | $actualConstraint->name($expectedConstraint->getName()); |
||
| 341 | } elseif ($expectedConstraint->getName() instanceof AnyCaseValue) { |
||
| 342 | $actualConstraint->name(new AnyCaseValue($actualConstraint->getName())); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | public function constraintsProviderTrait(): array |
||
| 348 | { |
||
| 349 | return [ |
||
| 350 | '1: primary key' => [ |
||
| 351 | 'T_constraints_1', |
||
| 352 | 'primaryKey', |
||
| 353 | (new Constraint()) |
||
| 354 | ->name(AnyValue::getInstance()) |
||
| 355 | ->columnNames(['C_id']), |
||
| 356 | ], |
||
| 357 | '1: check' => [ |
||
| 358 | 'T_constraints_1', |
||
| 359 | 'checks', |
||
| 360 | [ |
||
| 361 | (new CheckConstraint()) |
||
| 362 | ->name(AnyValue::getInstance()) |
||
| 363 | ->columnNames(['C_check']) |
||
| 364 | ->expression("C_check <> ''"), |
||
| 365 | ], |
||
| 366 | ], |
||
| 367 | '1: unique' => [ |
||
| 368 | 'T_constraints_1', |
||
| 369 | 'uniques', |
||
| 370 | [ |
||
| 371 | (new Constraint()) |
||
| 372 | ->name('CN_unique') |
||
| 373 | ->columnNames(['C_unique']), |
||
| 374 | ], |
||
| 375 | ], |
||
| 376 | '1: index' => [ |
||
| 377 | 'T_constraints_1', |
||
| 378 | 'indexes', |
||
| 379 | [ |
||
| 380 | (new IndexConstraint()) |
||
| 381 | ->name(AnyValue::getInstance()) |
||
| 382 | ->columnNames(['C_id']) |
||
| 383 | ->unique(true) |
||
| 384 | ->primary(true), |
||
| 385 | (new IndexConstraint()) |
||
| 386 | ->name('CN_unique') |
||
| 387 | ->columnNames(['C_unique']) |
||
| 388 | ->primary(false) |
||
| 389 | ->unique(true), |
||
| 390 | ], |
||
| 391 | ], |
||
| 392 | '1: default' => ['T_constraints_1', 'defaultValues', false], |
||
| 393 | |||
| 394 | '2: primary key' => [ |
||
| 395 | 'T_constraints_2', |
||
| 396 | 'primaryKey', |
||
| 397 | (new Constraint()) |
||
| 398 | ->name('CN_pk') |
||
| 399 | ->columnNames(['C_id_1', 'C_id_2']), |
||
| 400 | ], |
||
| 401 | '2: unique' => [ |
||
| 402 | 'T_constraints_2', |
||
| 403 | 'uniques', |
||
| 404 | [ |
||
| 405 | (new Constraint()) |
||
| 406 | ->name('CN_constraints_2_multi') |
||
| 407 | ->columnNames(['C_index_2_1', 'C_index_2_2']), |
||
| 408 | ], |
||
| 409 | ], |
||
| 410 | '2: index' => [ |
||
| 411 | 'T_constraints_2', |
||
| 412 | 'indexes', |
||
| 413 | [ |
||
| 414 | (new IndexConstraint()) |
||
| 415 | ->name(AnyValue::getInstance()) |
||
| 416 | ->columnNames(['C_id_1', 'C_id_2']) |
||
| 417 | ->unique(true) |
||
| 418 | ->primary(true), |
||
| 419 | (new IndexConstraint()) |
||
| 420 | ->name('CN_constraints_2_single') |
||
| 421 | ->columnNames(['C_index_1']) |
||
| 422 | ->primary(false) |
||
| 423 | ->unique(false), |
||
| 424 | (new IndexConstraint()) |
||
| 425 | ->name('CN_constraints_2_multi') |
||
| 426 | ->columnNames(['C_index_2_1', 'C_index_2_2']) |
||
| 427 | ->primary(false) |
||
| 428 | ->unique(true), |
||
| 429 | ], |
||
| 430 | ], |
||
| 431 | '2: check' => ['T_constraints_2', 'checks', []], |
||
| 432 | '2: default' => ['T_constraints_2', 'defaultValues', false], |
||
| 433 | |||
| 434 | '3: primary key' => ['T_constraints_3', 'primaryKey', null], |
||
| 435 | '3: foreign key' => [ |
||
| 436 | 'T_constraints_3', |
||
| 437 | 'foreignKeys', |
||
| 438 | [ |
||
| 439 | (new ForeignKeyConstraint()) |
||
| 440 | ->name('CN_constraints_3') |
||
| 441 | ->columnNames(['C_fk_id_1', 'C_fk_id_2']) |
||
| 442 | ->foreignTableName('T_constraints_2') |
||
| 443 | ->foreignColumnNames(['C_id_1', 'C_id_2']) |
||
| 444 | ->onDelete('CASCADE') |
||
| 445 | ->onUpdate('CASCADE'), |
||
| 446 | ], |
||
| 447 | ], |
||
| 448 | '3: unique' => ['T_constraints_3', 'uniques', []], |
||
| 449 | '3: index' => [ |
||
| 450 | 'T_constraints_3', |
||
| 451 | 'indexes', |
||
| 452 | [ |
||
| 453 | (new IndexConstraint()) |
||
| 454 | ->name('CN_constraints_3') |
||
| 455 | ->columnNames(['C_fk_id_1', 'C_fk_id_2']) |
||
| 456 | ->unique(false) |
||
| 457 | ->primary(false), |
||
| 458 | ], |
||
| 459 | ], |
||
| 460 | '3: check' => ['T_constraints_3', 'checks', []], |
||
| 461 | '3: default' => ['T_constraints_3', 'defaultValues', false], |
||
| 462 | |||
| 463 | '4: primary key' => [ |
||
| 464 | 'T_constraints_4', |
||
| 465 | 'primaryKey', |
||
| 466 | (new Constraint()) |
||
| 467 | ->name(AnyValue::getInstance()) |
||
| 468 | ->columnNames(['C_id']), |
||
| 469 | ], |
||
| 470 | '4: unique' => [ |
||
| 471 | 'T_constraints_4', |
||
| 472 | 'uniques', |
||
| 473 | [ |
||
| 474 | (new Constraint()) |
||
| 475 | ->name('CN_constraints_4') |
||
| 476 | ->columnNames(['C_col_1', 'C_col_2']), |
||
| 477 | ], |
||
| 478 | ], |
||
| 479 | '4: check' => ['T_constraints_4', 'checks', []], |
||
| 480 | '4: default' => ['T_constraints_4', 'defaultValues', false], |
||
| 481 | ]; |
||
| 482 | } |
||
| 483 | |||
| 484 | public function pdoAttributesProviderTrait(): array |
||
| 485 | { |
||
| 486 | return [ |
||
| 487 | [[PDO::ATTR_EMULATE_PREPARES => true]], |
||
| 488 | [[PDO::ATTR_EMULATE_PREPARES => false]], |
||
| 489 | ]; |
||
| 490 | } |
||
| 491 | |||
| 492 | public function tableSchemaCachePrefixesProviderTrait(): array |
||
| 493 | { |
||
| 494 | $configs = [ |
||
| 495 | [ |
||
| 496 | 'prefix' => '', |
||
| 497 | 'name' => 'type', |
||
| 498 | ], |
||
| 499 | [ |
||
| 500 | 'prefix' => '', |
||
| 501 | 'name' => '{{%type}}', |
||
| 502 | ], |
||
| 503 | [ |
||
| 504 | 'prefix' => 'ty', |
||
| 505 | 'name' => '{{%pe}}', |
||
| 506 | ], |
||
| 507 | ]; |
||
| 508 | |||
| 509 | $data = []; |
||
| 510 | foreach ($configs as $config) { |
||
| 511 | foreach ($configs as $testConfig) { |
||
| 512 | if ($config === $testConfig) { |
||
| 513 | continue; |
||
| 514 | } |
||
| 515 | |||
| 516 | $description = sprintf( |
||
| 517 | "%s (with '%s' prefix) against %s (with '%s' prefix)", |
||
| 518 | $config['name'], |
||
| 519 | $config['prefix'], |
||
| 520 | $testConfig['name'], |
||
| 521 | $testConfig['prefix'] |
||
| 522 | ); |
||
| 523 | $data[$description] = [ |
||
| 524 | $config['prefix'], |
||
| 525 | $config['name'], |
||
| 526 | $testConfig['prefix'], |
||
| 527 | $testConfig['name'], |
||
| 528 | ]; |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | return $data; |
||
| 533 | } |
||
| 534 | |||
| 535 | public function lowercaseConstraintsProviderTrait(): array |
||
| 536 | { |
||
| 537 | return $this->constraintsProvider(); |
||
| 538 | } |
||
| 539 | |||
| 540 | public function uppercaseConstraintsProviderTrait(): array |
||
| 543 | } |
||
| 544 | } |
||
| 545 |