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