| Total Complexity | 70 |
| Total Lines | 950 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SchemaTest 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 SchemaTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | if (!version_compare($this->getConnection()->getPDO()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6', '>=')) { |
||
| 19 | $this->markTestSkipped('Default datetime columns are supported since MySQL 5.6.'); |
||
| 20 | } |
||
| 21 | $sql = <<<SQL |
||
| 22 | CREATE TABLE IF NOT EXISTS `datetime_test` ( |
||
| 23 | `id` int(11) NOT NULL AUTO_INCREMENT, |
||
| 24 | `dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||
| 25 | `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
||
| 26 | PRIMARY KEY (`id`) |
||
| 27 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
||
| 28 | SQL; |
||
| 29 | |||
| 30 | $this->getConnection()->createCommand($sql)->execute(); |
||
| 31 | |||
| 32 | $schema = $this->getConnection()->getTableSchema('datetime_test'); |
||
| 33 | |||
| 34 | $dt = $schema->getColumn('dt'); |
||
| 35 | |||
| 36 | $this->assertInstanceOf(Expression::class, $dt->getDefaultValue()); |
||
| 37 | $this->assertEquals('CURRENT_TIMESTAMP', (string) $dt->getDefaultValue()); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testDefaultDatetimeColumnWithMicrosecs(): void |
||
| 41 | { |
||
| 42 | if (!version_compare($this->getConnection()->getPDO()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6.4', '>=')) { |
||
| 43 | $this->markTestSkipped('CURRENT_TIMESTAMP with microseconds as default column value is supported since MySQL 5.6.4.'); |
||
| 44 | } |
||
| 45 | $sql = <<<SQL |
||
| 46 | CREATE TABLE IF NOT EXISTS `current_timestamp_test` ( |
||
| 47 | `dt` datetime(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2), |
||
| 48 | `ts` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) |
||
| 49 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
||
| 50 | SQL; |
||
| 51 | |||
| 52 | $this->getConnection()->createCommand($sql)->execute(); |
||
| 53 | |||
| 54 | $schema = $this->getConnection()->getTableSchema('current_timestamp_test'); |
||
| 55 | |||
| 56 | $dt = $schema->getColumn('dt'); |
||
| 57 | $this->assertInstanceOf(Expression::class, $dt->getDefaultValue()); |
||
| 58 | $this->assertEquals('CURRENT_TIMESTAMP(2)', (string) $dt->getDefaultValue()); |
||
| 59 | |||
| 60 | $ts = $schema->getColumn('ts'); |
||
| 61 | $this->assertInstanceOf(Expression::class, $ts->getDefaultValue()); |
||
| 62 | $this->assertEquals('CURRENT_TIMESTAMP(3)', (string) $ts->getDefaultValue()); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function testGetSchemaNames() |
||
| 66 | { |
||
| 67 | $this->markTestSkipped('Schemas are not supported in MySQL.'); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function constraintsProvider() |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * When displayed in the INFORMATION_SCHEMA.COLUMNS table, a default CURRENT TIMESTAMP is displayed as |
||
| 91 | * CURRENT_TIMESTAMP up until MariaDB 10.2.2, and as current_timestamp() from MariaDB 10.2.3. |
||
| 92 | * |
||
| 93 | * {@see https://mariadb.com/kb/en/library/now/#description} |
||
| 94 | * {@see https://github.com/yiisoft/yii2/issues/15167} |
||
| 95 | */ |
||
| 96 | public function testAlternativeDisplayOfDefaultCurrentTimestampInMariaDB(): void |
||
| 119 | } |
||
| 120 | } |
||
| 121 |