diff --git a/composer.json b/composer.json index 15eae276e..4133c2c51 100644 --- a/composer.json +++ b/composer.json @@ -52,7 +52,11 @@ } }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "infection/extension-installer": true, + "composer/package-versions-deprecated": true + } }, "scripts": { "test": "phpunit --testdox --no-interaction", diff --git a/src/Command.php b/src/Command.php index af4796a9f..26f90738e 100644 --- a/src/Command.php +++ b/src/Command.php @@ -45,11 +45,15 @@ public function execute(): int foreach ($statements as $statement) { /** @var array $statement */ [$statementSql, $statementParams] = $statement; - $this->setSql($statementSql)->bindValues($statementParams); + $this + ->setSql($statementSql) + ->bindValues($statementParams); $result = parent::execute(); } - $this->setSql($sql)->bindValues($params); + $this + ->setSql($sql) + ->bindValues($params); return $result; } @@ -87,15 +91,21 @@ protected function queryInternal(string $method, $fetchMode = null) foreach ($statements as $statement) { [$statementSql, $statementParams] = $statement; - $this->setSql($statementSql)->bindValues($statementParams); + $this + ->setSql($statementSql) + ->bindValues($statementParams); parent::execute(); } - $this->setSql($lastStatementSql)->bindValues($lastStatementParams); + $this + ->setSql($lastStatementSql) + ->bindValues($lastStatementParams); $result = parent::queryInternal($method, $fetchMode); - $this->setSql($sql)->bindValues($params); + $this + ->setSql($sql) + ->bindValues($params); return $result; } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 8099d1932..4379bcef6 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -89,11 +89,14 @@ protected function defaultExpressionBuilders(): array * For example, * * ```php - * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ + * $connection + * ->createCommand() + * ->batchInsert('user', ['name', 'age'], [ * ['Tom', 30], * ['Jane', 20], * ['Linda', 25], - * ])->execute(); + * ]) + * ->execute(); * ``` * * Note that the values in each row must match the corresponding column names. @@ -118,13 +121,19 @@ public function batchInsert(string $table, array $columns, $rows, array &$params * * {@see http://www.sqlite.org/releaselog/3_7_11.html} */ - $this->getDb()->open(); + $this + ->getDb() + ->open(); - if (version_compare($this->getDb()->getServerVersion(), '3.7.11', '>=')) { + if (version_compare($this + ->getDb() + ->getServerVersion(), '3.7.11', '>=')) { return parent::batchInsert($table, $columns, $rows, $params); } - $schema = $this->getDb()->getSchema(); + $schema = $this + ->getDb() + ->getSchema(); if (($tableSchema = $schema->getTableSchema($table)) !== null) { $columnSchemas = $tableSchema->getColumns(); @@ -166,7 +175,7 @@ public function batchInsert(string $table, array $columns, $rows, array &$params } return 'INSERT INTO ' . $schema->quoteTableName($table) - . ' (' . implode(', ', $columns) . ') SELECT ' . implode(' UNION SELECT ', $values); + . ' (' . implode(', ', $columns) . ') SELECT ' . implode(' UNION SELECT ', $values); } /** @@ -194,10 +203,16 @@ public function resetSequence(string $tableName, $value = null): string $tableName = $db->quoteTableName($tableName); if ($value === null) { $pk = $table->getPrimaryKey(); - $key = $this->getDb()->quoteColumnName(reset($pk)); - $value = $this->getDb()->useMaster(static function (Connection $db) use ($key, $tableName) { - return $db->createCommand("SELECT MAX($key) FROM $tableName")->queryScalar(); - }); + $key = $this + ->getDb() + ->quoteColumnName(reset($pk)); + $value = $this + ->getDb() + ->useMaster(static function (Connection $db) use ($key, $tableName) { + return $db + ->createCommand("SELECT MAX($key) FROM $tableName") + ->queryScalar(); + }); } else { $value = (int) $value - 1; } @@ -235,7 +250,9 @@ public function checkIntegrity(string $schema = '', string $table = '', bool $ch */ public function truncateTable(string $table): string { - return 'DELETE FROM ' . $this->getDb()->quoteTableName($table); + return 'DELETE FROM ' . $this + ->getDb() + ->quoteTableName($table); } /** @@ -248,7 +265,9 @@ public function truncateTable(string $table): string */ public function dropIndex(string $name, string $table): string { - return 'DROP INDEX ' . $this->getDb()->quoteTableName($name); + return 'DROP INDEX ' . $this + ->getDb() + ->quoteTableName($name); } /** @@ -306,9 +325,9 @@ public function renameColumn(string $table, string $oldName, string $newName): s public function addForeignKey( string $name, string $table, - $columns, + $columns, string $refTable, - $refColumns, + $refColumns, ?string $delete = null, ?string $update = null ): string { @@ -333,16 +352,22 @@ public function addForeignKey( $tmp_table_name = "temp_{$schema}_" . $this->unquoteTableName($table); $schema .= '.'; $unquoted_tablename = $schema . $this->unquoteTableName($table); - $quoted_tablename = $schema . $this->getDb()->quoteTableName($table); + $quoted_tablename = $schema . $this + ->getDb() + ->quoteTableName($table); } else { $unquoted_tablename = $this->unquoteTableName($table); - $quoted_tablename = $this->getDb()->quoteTableName($table); + $quoted_tablename = $this + ->getDb() + ->quoteTableName($table); $tmp_table_name = 'temp_' . $this->unquoteTableName($table); } $fields_definitions_tokens = $this->getFieldDefinitionsTokens($unquoted_tablename); $ddl_fields_defs = $fields_definitions_tokens->getSql(); - $ddl_fields_defs .= ",\nCONSTRAINT " . $this->getDb()->quoteColumnName($name) . ' FOREIGN KEY (' . + $ddl_fields_defs .= ",\nCONSTRAINT " . $this + ->getDb() + ->quoteColumnName($name) . ' FOREIGN KEY (' . implode(',', (array)$columns) . ") REFERENCES $refTable(" . implode(',', (array)$refColumns) . ')'; if ($update !== null) { @@ -357,12 +382,18 @@ public function addForeignKey( $return_queries = []; $return_queries[] = 'PRAGMA foreign_keys = off'; $return_queries[] = "SAVEPOINT add_foreign_key_to_$tmp_table_name"; - $return_queries[] = 'CREATE TEMP TABLE ' . $this->getDb()->quoteTableName($tmp_table_name) + $return_queries[] = 'CREATE TEMP TABLE ' . $this + ->getDb() + ->quoteTableName($tmp_table_name) . " AS SELECT * FROM $quoted_tablename"; $return_queries[] = "DROP TABLE $quoted_tablename"; $return_queries[] = "CREATE TABLE $quoted_tablename (" . trim($ddl_fields_defs, " \n\r\t,") . ')'; - $return_queries[] = "INSERT INTO $quoted_tablename SELECT * FROM " . $this->getDb()->quoteTableName($tmp_table_name); - $return_queries[] = 'DROP TABLE ' . $this->getDb()->quoteTableName($tmp_table_name); + $return_queries[] = "INSERT INTO $quoted_tablename SELECT * FROM " . $this + ->getDb() + ->quoteTableName($tmp_table_name); + $return_queries[] = 'DROP TABLE ' . $this + ->getDb() + ->quoteTableName($tmp_table_name); $return_queries = array_merge($return_queries, $this->getIndexSqls($unquoted_tablename)); $return_queries[] = "RELEASE add_foreign_key_to_$tmp_table_name"; @@ -389,9 +420,13 @@ public function dropForeignKey(string $name, string $table): string $sql_fields_to_insert = []; $skipping = false; $foreign_found = false; - $quoted_foreign_name = $this->getDb()->quoteColumnName($name); + $quoted_foreign_name = $this + ->getDb() + ->quoteColumnName($name); - $quoted_tablename = $this->getDb()->quoteTableName($table); + $quoted_tablename = $this + ->getDb() + ->quoteTableName($table); $unquoted_tablename = $this->unquoteTableName($table); $fields_definitions_tokens = $this->getFieldDefinitionsTokens($unquoted_tablename); @@ -420,11 +455,15 @@ public function dropForeignKey(string $name, string $table): string $other_offset = $offset; if ($keyword === 'CONSTRAINT') { - $constraint_name = $this->getDb()->quoteColumnName( - $fields_definitions_tokens[$other_offset]->getContent() - ); + $constraint_name = $this + ->getDb() + ->quoteColumnName( + $fields_definitions_tokens[$other_offset]->getContent() + ); } else { - $constraint_name = $this->getDb()->quoteColumnName((string) $constraint_pos); + $constraint_name = $this + ->getDb() + ->quoteColumnName((string) $constraint_pos); } /** @psalm-suppress TypeDoesNotContainType */ @@ -474,13 +513,19 @@ public function dropForeignKey(string $name, string $table): string $return_queries[] = 'PRAGMA foreign_keys = 0'; $return_queries[] = "SAVEPOINT drop_column_$unquoted_tablename"; - $return_queries[] = 'CREATE TABLE ' . $this->getDb()->quoteTableName("temp_$unquoted_tablename") + $return_queries[] = 'CREATE TABLE ' . $this + ->getDb() + ->quoteTableName("temp_$unquoted_tablename") . " AS SELECT * FROM $quoted_tablename"; $return_queries[] = "DROP TABLE $quoted_tablename"; $return_queries[] = "CREATE TABLE $quoted_tablename (" . trim($ddl_fields_def, " \n\r\t,") . ')'; $return_queries[] = "INSERT INTO $quoted_tablename SELECT " . implode(',', $sql_fields_to_insert) . ' FROM ' - . $this->getDb()->quoteTableName("temp_$unquoted_tablename"); - $return_queries[] = 'DROP TABLE ' . $this->getDb()->quoteTableName("temp_$unquoted_tablename"); + . $this + ->getDb() + ->quoteTableName("temp_$unquoted_tablename"); + $return_queries[] = 'DROP TABLE ' . $this + ->getDb() + ->quoteTableName("temp_$unquoted_tablename"); $return_queries = array_merge($return_queries, $this->getIndexSqls($unquoted_tablename)); $return_queries[] = "RELEASE drop_column_$unquoted_tablename"; $return_queries[] = "PRAGMA foreign_keys = $foreign_keys_state"; @@ -498,7 +543,11 @@ public function dropForeignKey(string $name, string $table): string */ public function renameTable(string $oldName, string $newName): string { - return 'ALTER TABLE ' . $this->getDb()->quoteTableName($oldName) . ' RENAME TO ' . $this->getDb()->quoteTableName($newName); + return 'ALTER TABLE ' . $this + ->getDb() + ->quoteTableName($oldName) . ' RENAME TO ' . $this + ->getDb() + ->quoteTableName($newName); } /** @@ -541,27 +590,39 @@ public function addPrimaryKey(string $name, string $table, $columns): string $schema = $this->unquoteTableName(substr($table, 0, $pos)); $table = substr($table, $pos + 1); $unquoted_tablename = $schema . '.' . $this->unquoteTableName($table); - $quoted_tablename = $schema . '.' . $this->getDb()->quoteTableName($table); + $quoted_tablename = $schema . '.' . $this + ->getDb() + ->quoteTableName($table); $tmp_table_name = "temp_{$schema}_" . $this->unquoteTableName($table); } else { $unquoted_tablename = $this->unquoteTableName($table); - $quoted_tablename = $this->getDb()->quoteTableName($table); + $quoted_tablename = $this + ->getDb() + ->quoteTableName($table); $tmp_table_name = 'temp_' . $this->unquoteTableName($table); } $fields_definitions_tokens = $this->getFieldDefinitionsTokens($unquoted_tablename); $ddl_fields_defs = $fields_definitions_tokens->getSql(); - $ddl_fields_defs .= ', CONSTRAINT ' . $this->getDb()->quoteColumnName($name) . ' PRIMARY KEY (' . + $ddl_fields_defs .= ', CONSTRAINT ' . $this + ->getDb() + ->quoteColumnName($name) . ' PRIMARY KEY (' . implode(',', (array)$columns) . ')'; $foreign_keys_state = $this->foreignKeysState(); $return_queries[] = 'PRAGMA foreign_keys = 0'; $return_queries[] = "SAVEPOINT add_primary_key_to_$tmp_table_name"; - $return_queries[] = 'CREATE TABLE ' . $this->getDb()->quoteTableName($tmp_table_name) . + $return_queries[] = 'CREATE TABLE ' . $this + ->getDb() + ->quoteTableName($tmp_table_name) . " AS SELECT * FROM $quoted_tablename"; $return_queries[] = "DROP TABLE $quoted_tablename"; $return_queries[] = "CREATE TABLE $quoted_tablename (" . trim($ddl_fields_defs, " \n\r\t,") . ')'; - $return_queries[] = "INSERT INTO $quoted_tablename SELECT * FROM " . $this->getDb()->quoteTableName($tmp_table_name); - $return_queries[] = 'DROP TABLE ' . $this->getDb()->quoteTableName($tmp_table_name); + $return_queries[] = "INSERT INTO $quoted_tablename SELECT * FROM " . $this + ->getDb() + ->quoteTableName($tmp_table_name); + $return_queries[] = 'DROP TABLE ' . $this + ->getDb() + ->quoteTableName($tmp_table_name); $return_queries = array_merge($return_queries, $this->getIndexSqls($unquoted_tablename)); @@ -869,8 +930,12 @@ public function createIndex(string $name, string $table, $columns, bool $unique } return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') - . $this->getDb()->quoteTableName(($schema ? $schema . '.' : '') . $name) . ' ON ' - . $this->getDb()->quoteTableName($table) + . $this + ->getDb() + ->quoteTableName(($schema ? $schema . '.' : '') . $name) . ' ON ' + . $this + ->getDb() + ->quoteTableName($table) . ' (' . $this->buildColumns($columns) . ')'; } @@ -952,7 +1017,9 @@ public function upsert(string $table, $insertColumns, $updateColumns, array &$pa [, $placeholders, $values, $params] = $this->prepareInsertValues($table, $insertColumns, $params); - $insertSql = 'INSERT OR IGNORE INTO ' . $this->getDb()->quoteTableName($table) + $insertSql = 'INSERT OR IGNORE INTO ' . $this + ->getDb() + ->quoteTableName($table) . (!empty($insertNames) ? ' (' . implode(', ', $insertNames) . ')' : '') . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values); @@ -961,12 +1028,16 @@ public function upsert(string $table, $insertColumns, $updateColumns, array &$pa } $updateCondition = ['or']; - $quotedTableName = $this->getDb()->quoteTableName($table); + $quotedTableName = $this + ->getDb() + ->quoteTableName($table); foreach ($constraints as $constraint) { $constraintCondition = ['and']; foreach ($constraint->getColumnNames() as $name) { - $quotedName = $this->getDb()->quoteColumnName($name); + $quotedName = $this + ->getDb() + ->quoteColumnName($name); $constraintCondition[] = "$quotedTableName.$quotedName=(SELECT $quotedName FROM `EXCLUDED`)"; } $updateCondition[] = $constraintCondition; @@ -975,7 +1046,9 @@ public function upsert(string $table, $insertColumns, $updateColumns, array &$pa if ($updateColumns === true) { $updateColumns = []; foreach ($updateNames as $name) { - $quotedName = $this->getDb()->quoteColumnName($name); + $quotedName = $this + ->getDb() + ->quoteColumnName($name); if (strrpos($quotedName, '.') === false) { $quotedName = "(SELECT $quotedName FROM `EXCLUDED`)"; @@ -986,14 +1059,19 @@ public function upsert(string $table, $insertColumns, $updateColumns, array &$pa $updateSql = 'WITH "EXCLUDED" (' . implode(', ', $insertNames) . ') AS (' . (!empty($placeholders) ? 'VALUES (' . implode(', ', $placeholders) . ')' - : ltrim($values, ' ')) . ') ' . $this->update($table, $updateColumns, $updateCondition, $params); + : ltrim($values, ' ')) . ') ' . $this->update($table, $updateColumns, $updateCondition, $params); return "$updateSql; $insertSql;"; } private function unquoteTableName(string $tableName): string { - return $this->getDb()->getSchema()->unquoteSimpleTableName($this->getDb()->quoteSql($tableName)); + return $this + ->getDb() + ->getSchema() + ->unquoteSimpleTableName($this + ->getDb() + ->quoteSql($tableName)); } private function getFieldDefinitionsTokens(string $tableName): ?SqlToken @@ -1020,9 +1098,12 @@ private function getCreateTable(string $tableName): string $schema = ''; } - $create_table = $this->getDb()->createCommand( - "select SQL from {$schema}SQLite_Master where tbl_name = '$tableName' and type='table'" - )->queryScalar(); + $create_table = $this + ->getDb() + ->createCommand( + "select SQL from {$schema}SQLite_Master where tbl_name = '$tableName' and type='table'" + ) + ->queryScalar(); if ($create_table === null) { throw new InvalidParamException("Table not found: $tableName"); @@ -1036,21 +1117,29 @@ private function getCreateTable(string $tableName): string */ private function foreignKeysState() { - return $this->getDb()->createCommand('PRAGMA foreign_keys')->queryScalar(); + return $this + ->getDb() + ->createCommand('PRAGMA foreign_keys') + ->queryScalar(); } private function getIndexSqls(string $tableName, $skipColumn = null, $newColumn = null): array { /** Get all indexes on this table */ - $indexes = $this->getDb()->createCommand( - "select SQL from SQLite_Master where tbl_name = '$tableName' and type='index'" - )->queryAll(); + $indexes = $this + ->getDb() + ->createCommand( + "select SQL from SQLite_Master where tbl_name = '$tableName' and type='index'" + ) + ->queryAll(); if ($skipColumn === null) { return array_column($indexes, 'sql'); } - $quoted_skip_column = $this->getDb()->quoteColumnName((string) $skipColumn); + $quoted_skip_column = $this + ->getDb() + ->quoteColumnName((string) $skipColumn); if ($newColumn === null) { /** Skip indexes which contain this column */ foreach ($indexes as $key => $index) { @@ -1106,7 +1195,9 @@ private function getIndexSqls(string $tableName, $skipColumn = null, $newColumn $tokenType = $token->getType(); if ($tokenType === SqlToken::TYPE_IDENTIFIER) { if ((string) $token === $skipColumn || (string) $token === $quoted_skip_column) { - $token = $this->getDb()->quoteColumnName((string) $newColumn); + $token = $this + ->getDb() + ->quoteColumnName((string) $newColumn); } } $new_index_def .= $token; diff --git a/src/Schema.php b/src/Schema.php index 0d406c98a..0ecd37378 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -98,7 +98,10 @@ protected function findTableNames(string $schema = ''): array { $sql = "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence' ORDER BY tbl_name"; - return $this->getDb()->createCommand($sql)->queryColumn(); + return $this + ->getDb() + ->createCommand($sql) + ->queryColumn(); } /** @@ -151,9 +154,12 @@ protected function loadTablePrimaryKey(string $tableName): ?Constraint */ protected function loadTableForeignKeys(string $tableName): array { - $foreignKeys = $this->getDb()->createCommand( - 'PRAGMA FOREIGN_KEY_LIST (' . $this->quoteValue($tableName) . ')' - )->queryAll(); + $foreignKeys = $this + ->getDb() + ->createCommand( + 'PRAGMA FOREIGN_KEY_LIST (' . $this->quoteValue($tableName) . ')' + ) + ->queryAll(); $foreignKeys = $this->normalizePdoRowKeyCase($foreignKeys, true); @@ -216,9 +222,12 @@ protected function loadTableUniques(string $tableName): array */ protected function loadTableChecks(string $tableName): array { - $sql = $this->getDb()->createCommand('SELECT `sql` FROM `sqlite_master` WHERE name = :tableName', [ - ':tableName' => $tableName, - ])->queryScalar(); + $sql = $this + ->getDb() + ->createCommand('SELECT `sql` FROM `sqlite_master` WHERE name = :tableName', [ + ':tableName' => $tableName, + ]) + ->queryScalar(); /** @var SqlToken[]|SqlToken[][]|SqlToken[][][] $code */ $code = (new SqlTokenizer($sql))->tokenize(); @@ -314,7 +323,10 @@ public function createColumnSchemaBuilder(string $type, $length = null): ColumnS protected function findColumns(TableSchema $table): bool { $sql = 'PRAGMA table_info(' . $this->quoteSimpleTableName($table->getName()) . ')'; - $columns = $this->getDb()->createCommand($sql)->queryAll(); + $columns = $this + ->getDb() + ->createCommand($sql) + ->queryAll(); if (empty($columns)) { return false; @@ -329,9 +341,13 @@ protected function findColumns(TableSchema $table): bool } $pk = $table->getPrimaryKey(); - if (count($pk) === 1 && !strncasecmp($table->getColumn($pk[0])->getDbType(), 'int', 3)) { + if (count($pk) === 1 && !strncasecmp($table + ->getColumn($pk[0]) + ->getDbType(), 'int', 3)) { $table->sequenceName(''); - $table->getColumn($pk[0])->autoIncrement(true); + $table + ->getColumn($pk[0]) + ->autoIncrement(true); } return true; @@ -347,7 +363,10 @@ protected function findColumns(TableSchema $table): bool protected function findConstraints(TableSchema $table): void { $sql = 'PRAGMA foreign_key_list(' . $this->quoteSimpleTableName($table->getName()) . ')'; - $keys = $this->getDb()->createCommand($sql)->queryAll(); + $keys = $this + ->getDb() + ->createCommand($sql) + ->queryAll(); foreach ($keys as $key) { $id = (int) $key['id']; @@ -382,14 +401,20 @@ protected function findConstraints(TableSchema $table): void public function findUniqueIndexes(TableSchema $table): array { $sql = 'PRAGMA index_list(' . $this->quoteSimpleTableName($table->getName()) . ')'; - $indexes = $this->getDb()->createCommand($sql)->queryAll(); + $indexes = $this + ->getDb() + ->createCommand($sql) + ->queryAll(); $uniqueIndexes = []; foreach ($indexes as $index) { $indexName = $index['name']; - $indexInfo = $this->getDb()->createCommand( - 'PRAGMA index_info(' . $this->quoteValue($index['name']) . ')' - )->queryAll(); + $indexInfo = $this + ->getDb() + ->createCommand( + 'PRAGMA index_info(' . $this->quoteValue($index['name']) . ')' + ) + ->queryAll(); if ($index['unique']) { $uniqueIndexes[$indexName] = []; @@ -476,10 +501,16 @@ public function setTransactionIsolationLevel(string $level): void { switch ($level) { case Transaction::SERIALIZABLE: - $this->getDb()->createCommand('PRAGMA read_uncommitted = False;')->execute(); + $this + ->getDb() + ->createCommand('PRAGMA read_uncommitted = False;') + ->execute(); break; case Transaction::READ_UNCOMMITTED: - $this->getDb()->createCommand('PRAGMA read_uncommitted = True;')->execute(); + $this + ->getDb() + ->createCommand('PRAGMA read_uncommitted = True;') + ->execute(); break; default: throw new NotSupportedException( @@ -499,9 +530,12 @@ public function setTransactionIsolationLevel(string $level): void */ private function loadTableColumnsInfo(string $tableName): array { - $tableColumns = $this->getDb()->createCommand( - 'PRAGMA TABLE_INFO (' . $this->quoteValue($tableName) . ')' - )->queryAll(); + $tableColumns = $this + ->getDb() + ->createCommand( + 'PRAGMA TABLE_INFO (' . $this->quoteValue($tableName) . ')' + ) + ->queryAll(); $tableColumns = $this->normalizePdoRowKeyCase($tableColumns, true); @@ -521,9 +555,12 @@ private function loadTableColumnsInfo(string $tableName): array private function loadTableConstraints(string $tableName, string $returnType) { $tableColumns = null; - $indexList = $this->getDb()->createCommand( - 'PRAGMA INDEX_LIST (' . $this->quoteValue($tableName) . ')' - )->queryAll(); + $indexList = $this + ->getDb() + ->createCommand( + 'PRAGMA INDEX_LIST (' . $this->quoteValue($tableName) . ')' + ) + ->queryAll(); $indexes = $this->normalizePdoRowKeyCase($indexList, true); if (!empty($indexes) && !isset($indexes[0]['origin'])) { @@ -621,7 +658,10 @@ private function createColumnSchema(): ColumnSchema */ private function getPragmaIndexInfo(string $name): array { - $column = $this->getDb()->createCommand('PRAGMA INDEX_INFO (' . $this->quoteValue($name) . ')')->queryAll(); + $column = $this + ->getDb() + ->createCommand('PRAGMA INDEX_INFO (' . $this->quoteValue($name) . ')') + ->queryAll(); $columns = $this->normalizePdoRowKeyCase($column, true); ArraySorter::multisort($columns, 'seqno', SORT_ASC, SORT_NUMERIC); diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 3201f00c3..87285771d 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -39,7 +39,9 @@ public function testForeingKeyException(): void { $db = $this->getConnection(); - $db->createCommand('PRAGMA foreign_keys = ON')->execute(); + $db + ->createCommand('PRAGMA foreign_keys = ON') + ->execute(); $tableMaster = 'departments'; $tableRelation = 'students'; @@ -48,40 +50,59 @@ public function testForeingKeyException(): void $schema = $db->getSchema(); if ($schema->getTableSchema($tableRelation) !== null) { - $db->createCommand()->dropTable($tableRelation)->execute(); + $db + ->createCommand() + ->dropTable($tableRelation) + ->execute(); } if ($schema->getTableSchema($tableMaster) !== null) { - $db->createCommand()->dropTable($tableMaster)->execute(); + $db + ->createCommand() + ->dropTable($tableMaster) + ->execute(); } - $db->createCommand()->createTable($tableMaster, [ - 'department_id' => 'integer not null primary key autoincrement', - 'department_name' => 'nvarchar(50) null', - ])->execute(); - - $db->createCommand()->createTable($tableRelation, [ - 'student_id' => 'integer primary key autoincrement not null', - 'student_name' => 'nvarchar(50) null', - 'department_id' => 'integer not null', - 'dateOfBirth' => 'date null', - ])->execute(); - - $db->createCommand()->addForeignKey( - $name, - $tableRelation, - ['Department_id'], - $tableMaster, - ['Department_id'] - )->execute(); - - $db->createCommand( - "INSERT INTO departments VALUES (1, 'IT')" - )->execute(); - - $db->createCommand( - 'INSERT INTO students(student_name, department_id) VALUES ("John", 1);' - )->execute(); + $db + ->createCommand() + ->createTable($tableMaster, [ + 'department_id' => 'integer not null primary key autoincrement', + 'department_name' => 'nvarchar(50) null', + ]) + ->execute(); + + $db + ->createCommand() + ->createTable($tableRelation, [ + 'student_id' => 'integer primary key autoincrement not null', + 'student_name' => 'nvarchar(50) null', + 'department_id' => 'integer not null', + 'dateOfBirth' => 'date null', + ]) + ->execute(); + + $db + ->createCommand() + ->addForeignKey( + $name, + $tableRelation, + ['Department_id'], + $tableMaster, + ['Department_id'] + ) + ->execute(); + + $db + ->createCommand( + "INSERT INTO departments VALUES (1, 'IT')" + ) + ->execute(); + + $db + ->createCommand( + 'INSERT INTO students(student_name, department_id) VALUES ("John", 1);' + ) + ->execute(); $expectedMessageError = str_replace( "\r\n", @@ -95,9 +116,11 @@ public function testForeingKeyException(): void $this->expectException(IntegrityException::class); $this->expectExceptionMessage($expectedMessageError); - $db->createCommand( - 'INSERT INTO students(student_name, department_id) VALUES ("Samdark", 5);' - )->execute(); + $db + ->createCommand( + 'INSERT INTO students(student_name, department_id) VALUES ("Samdark", 5);' + ) + ->execute(); } public function testMultiStatementSupport(): void @@ -114,10 +137,12 @@ public function testMultiStatementSupport(): void INSERT INTO {{T_multistatement}} VALUES(42, :val2); SQL; - $db->createCommand($sql, [ - 'val1' => 'foo', - 'val2' => 'bar', - ])->execute(); + $db + ->createCommand($sql, [ + 'val1' => 'foo', + 'val2' => 'bar', + ]) + ->execute(); /** @todo need fix for this behaviour PHP8.1 + pdo_mysql */ $this->assertEquals([ @@ -129,7 +154,9 @@ public function testMultiStatementSupport(): void 'intcol' => '42', 'textcol' => 'bar', ], - ], $db->createCommand('SELECT * FROM {{T_multistatement}}')->queryAll()); + ], $db + ->createCommand('SELECT * FROM {{T_multistatement}}') + ->queryAll()); $sql = <<<'SQL' UPDATE {{T_multistatement}} SET [[intcol]] = :newInt WHERE [[textcol]] = :val1; @@ -143,11 +170,13 @@ public function testMultiStatementSupport(): void 'intcol' => '410', 'textcol' => 'foo', ], - ], $db->createCommand($sql, [ - 'newInt' => 410, - 'val1' => 'foo', - 'val2' => 'bar', - ])->queryAll()); + ], $db + ->createCommand($sql, [ + 'newInt' => 410, + 'val1' => 'foo', + 'val2' => 'bar', + ]) + ->queryAll()); } public function batchInsertSqlProvider(): array @@ -212,14 +241,17 @@ public function testBindParamsNonWhere(string $sql): void { $db = $this->getConnection(); - $db->createCommand()->insert( - 'customer', - [ - 'name' => 'testParams', - 'email' => 'testParams@example.com', - 'address' => '1', - ] - )->execute(); + $db + ->createCommand() + ->insert( + 'customer', + [ + 'name' => 'testParams', + 'email' => 'testParams@example.com', + 'address' => '1', + ] + ) + ->execute(); $params = [ ':email' => 'testParams@example.com', @@ -272,17 +304,21 @@ public function testInsertSelectFailed($invalidSelectColumns): void $query = new Query($db); - $query->select($invalidSelectColumns)->from('{{customer}}'); + $query + ->select($invalidSelectColumns) + ->from('{{customer}}'); $command = $db->createCommand(); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Expected select query object with enumerated (named) parameters'); - $command->insert( - '{{customer}}', - $query - )->execute(); + $command + ->insert( + '{{customer}}', + $query + ) + ->execute(); } /** @@ -297,7 +333,9 @@ public function testInsertSelectFailed($invalidSelectColumns): void */ public function testUpsert(array $firstData, array $secondData) { - if (version_compare($this->getConnection()->getServerVersion(), '3.8.3', '<')) { + if (version_compare($this + ->getConnection() + ->getServerVersion(), '3.8.3', '<')) { $this->markTestSkipped('SQLite < 3.8.3 does not support "WITH" keyword.'); return; @@ -305,11 +343,15 @@ public function testUpsert(array $firstData, array $secondData) $db = $this->getConnection(true); - $this->assertEquals(0, $db->createCommand('SELECT COUNT(*) FROM {{T_upsert}}')->queryScalar()); + $this->assertEquals(0, $db + ->createCommand('SELECT COUNT(*) FROM {{T_upsert}}') + ->queryScalar()); $this->performAndCompareUpsertResult($db, $firstData); - $this->assertEquals(1, $db->createCommand('SELECT COUNT(*) FROM {{T_upsert}}')->queryScalar()); + $this->assertEquals(1, $db + ->createCommand('SELECT COUNT(*) FROM {{T_upsert}}') + ->queryScalar()); $this->performAndCompareUpsertResult($db, $secondData); } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 1681fbc94..07e43d9fa 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -37,7 +37,10 @@ public function testExceptionContainsRawQuery(): void $db = $this->getConnection(); if ($db->getTableSchema('qlog1', true) === null) { - $db->createCommand()->createTable('qlog1', ['id' => 'pk'])->execute(); + $db + ->createCommand() + ->createTable('qlog1', ['id' => 'pk']) + ->execute(); } $db->setEmulatePrepare(true); @@ -75,7 +78,9 @@ private function runExceptionTest(ConnectionInterface $db): void $thrown = false; try { - $db->createCommand('INSERT INTO qlog1(a) VALUES(:a);', [':a' => 1])->execute(); + $db + ->createCommand('INSERT INTO qlog1(a) VALUES(:a);', [':a' => 1]) + ->execute(); } catch (Exception $e) { $this->assertStringContainsString( 'INSERT INTO qlog1(a) VALUES(:a);', @@ -91,10 +96,12 @@ private function runExceptionTest(ConnectionInterface $db): void $thrown = false; try { - $db->createCommand( - 'SELECT * FROM qlog1 WHERE id=:a ORDER BY nonexistingcolumn;', - [':a' => 1] - )->queryAll(); + $db + ->createCommand( + 'SELECT * FROM qlog1 WHERE id=:a ORDER BY nonexistingcolumn;', + [':a' => 1] + ) + ->queryAll(); } catch (Exception $e) { $this->assertStringContainsString( 'SELECT * FROM qlog1 WHERE id=:a ORDER BY nonexistingcolumn;', @@ -155,31 +162,43 @@ public function testMasterSlave(): void $db = $this->prepareMasterSlave($masterCount, $slaveCount); $this->assertInstanceOf(Connection::class, $db->getSlave()); - $this->assertTrue($db->getSlave()->isActive()); + $this->assertTrue($db + ->getSlave() + ->isActive()); $this->assertFalse($db->isActive()); /* test SELECT uses slave */ - $this->assertEquals(2, $db->createCommand('SELECT COUNT(*) FROM profile')->queryScalar()); + $this->assertEquals(2, $db + ->createCommand('SELECT COUNT(*) FROM profile') + ->queryScalar()); $this->assertFalse($db->isActive()); /* test UPDATE uses master */ - $db->createCommand("UPDATE profile SET description='test' WHERE id=1")->execute(); + $db + ->createCommand("UPDATE profile SET description='test' WHERE id=1") + ->execute(); $this->assertTrue($db->isActive()); if ($masterCount > 0) { $this->assertInstanceOf(Connection::class, $db->getMaster()); - $this->assertTrue($db->getMaster()->isActive()); + $this->assertTrue($db + ->getMaster() + ->isActive()); } else { $this->assertNull($db->getMaster()); } $this->assertNotEquals( 'test', - $db->createCommand('SELECT description FROM profile WHERE id=1')->queryScalar() + $db + ->createCommand('SELECT description FROM profile WHERE id=1') + ->queryScalar() ); $result = $db->useMaster(static function (Connection $db) { - return $db->createCommand('SELECT description FROM profile WHERE id=1')->queryScalar(); + return $db + ->createCommand('SELECT description FROM profile WHERE id=1') + ->queryScalar(); }); $this->assertEquals('test', $result); @@ -200,8 +219,12 @@ public function testMastersShuffled(): void $db = $this->prepareMasterSlave($mastersCount, $slavesCount); $db->setShuffleMasters(true); - $hit_slaves[$db->getSlave()->getDsn()] = true; - $hit_masters[$db->getMaster()->getDsn()] = true; + $hit_slaves[$db + ->getSlave() + ->getDsn()] = true; + $hit_masters[$db + ->getMaster() + ->getDsn()] = true; if (\count($hit_slaves) === $slavesCount && \count($hit_masters) === $mastersCount) { break; @@ -226,8 +249,12 @@ public function testMastersSequential(): void $db = $this->prepareMasterSlave($mastersCount, $slavesCount); $db->setShuffleMasters(false); - $hit_slaves[$db->getSlave()->getDsn()] = true; - $hit_masters[$db->getMaster()->getDsn()] = true; + $hit_slaves[$db + ->getSlave() + ->getDsn()] = true; + $hit_masters[$db + ->getMaster() + ->getDsn()] = true; if (\count($hit_slaves) === $slavesCount) { break; @@ -308,12 +335,16 @@ public function testServerStatusCacheWorks(): void ['Yiisoft\Db\Connection\Connection::openFromPoolSequentially', $db->getDsn()] ); - $this->assertFalse($this->cache->psr()->has($cacheKey)); + $this->assertFalse($this->cache + ->psr() + ->has($cacheKey)); $db->open(); $this->assertFalse( - $this->cache->psr()->has($cacheKey), + $this->cache + ->psr() + ->has($cacheKey), 'Connection was successful – cache must not contain information about this DSN' ); @@ -338,7 +369,9 @@ public function testServerStatusCacheWorks(): void } $this->assertTrue( - $this->cache->psr()->has($cacheKey), + $this->cache + ->psr() + ->has($cacheKey), 'Connection was not successful – cache must contain information about this DSN' ); @@ -349,7 +382,9 @@ public function testServerStatusCacheCanBeDisabled(): void { $cacheKeyNormalizer = new CacheKeyNormalizer(); - $this->cache->psr()->clear(); + $this->cache + ->psr() + ->clear(); $db = $this->getConnection(); @@ -366,11 +401,15 @@ public function testServerStatusCacheCanBeDisabled(): void ['Yiisoft\Db\Connection\Connection::openFromPoolSequentially', $db->getDsn()] ); - $this->assertFalse($this->cache->psr()->has($cacheKey)); + $this->assertFalse($this->cache + ->psr() + ->has($cacheKey)); $db->open(); - $this->assertFalse($this->cache->psr()->has($cacheKey), 'Caching is disabled'); + $this->assertFalse($this->cache + ->psr() + ->has($cacheKey), 'Caching is disabled'); $db->close(); @@ -388,7 +427,9 @@ public function testServerStatusCacheCanBeDisabled(): void } catch (InvalidConfigException $e) { } - $this->assertFalse($this->cache->psr()->has($cacheKey), 'Caching is disabled'); + $this->assertFalse($this->cache + ->psr() + ->has($cacheKey), 'Caching is disabled'); $db->close(); } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index e3a05e2fe..f04fd62df 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -42,21 +42,26 @@ public function testBuildUnion(): void $secondQuery = new Query($db); - $secondQuery->select('id') + $secondQuery + ->select('id') ->from('TotalTotalExample t2') ->where('w > 5'); $thirdQuery = new Query($db); - $thirdQuery->select('id') + $thirdQuery + ->select('id') ->from('TotalTotalExample t3') ->where('w = 3'); - $query->select('id') + $query + ->select('id') ->from('TotalExample t1') ->where(['and', 'w > 0', 'x < 2']) ->union($secondQuery) ->union($thirdQuery, true); - [$actualQuerySql, $queryParams] = $this->getQueryBuilder()->build($query); + [$actualQuerySql, $queryParams] = $this + ->getQueryBuilder() + ->build($query); $this->assertEquals($expectedQuerySql, $actualQuerySql); $this->assertEquals([], $queryParams); @@ -93,7 +98,9 @@ public function testBuildWithQuery() ->withQuery($with2Query->union($with3Query), 'a2') ->from('a2'); - [$actualQuerySql, $queryParams] = $this->getQueryBuilder()->build($query); + [$actualQuerySql, $queryParams] = $this + ->getQueryBuilder() + ->build($query); $this->assertEquals($expectedQuerySql, $actualQuerySql); $this->assertEquals([], $queryParams); @@ -101,7 +108,9 @@ public function testBuildWithQuery() public function testRenameTable() { - $sql = $this->getQueryBuilder()->renameTable('table_from', 'table_to'); + $sql = $this + ->getQueryBuilder() + ->renameTable('table_from', 'table_to'); $this->assertEquals('ALTER TABLE `table_from` RENAME TO `table_to`', $sql); } @@ -193,7 +202,9 @@ static function (QueryBuilder $qb) use ($tableName, $name, $pkTableName) { */ public function testAddDropForeignKey(string $sql, Closure $builder): void { - $this->assertEqualsWithoutLE($this->getConnection()->quoteSql($sql), $builder($this->getQueryBuilder())); + $this->assertEqualsWithoutLE($this + ->getConnection() + ->quoteSql($sql), $builder($this->getQueryBuilder())); } public function batchInsertProvider(): array @@ -265,7 +276,9 @@ public function testBuildCondition($condition, string $expected, array $expected $query = (new Query($db))->where($condition); - [$sql, $params] = $this->getQueryBuilder()->build($query); + [$sql, $params] = $this + ->getQueryBuilder() + ->build($query); $this->assertEquals('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $this->replaceQuotes($expected)), $sql); $this->assertEquals($expectedParams, $params); @@ -287,7 +300,9 @@ public function testBuildFilterCondition(array $condition, string $expected, arr { $query = (new Query($this->getConnection()))->filterWhere($condition); - [$sql, $params] = $this->getQueryBuilder()->build($query); + [$sql, $params] = $this + ->getQueryBuilder() + ->build($query); $this->assertEquals('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $this->replaceQuotes($expected)), $sql); $this->assertEquals($expectedParams, $params); @@ -324,7 +339,9 @@ public function testBuildFrom(string $table, string $expected): void { $params = []; - $sql = $this->getQueryBuilder()->buildFrom([$table], $params); + $sql = $this + ->getQueryBuilder() + ->buildFrom([$table], $params); $this->assertEquals('FROM ' . $this->replaceQuotes($expected), $sql); } @@ -347,7 +364,9 @@ public function testBuildLikeCondition($condition, string $expected, array $expe $query = (new Query($db))->where($condition); - [$sql, $params] = $this->getQueryBuilder()->build($query); + [$sql, $params] = $this + ->getQueryBuilder() + ->build($query); $this->assertEquals('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $this->replaceQuotes($expected)), $sql); $this->assertEquals($expectedParams, $params); @@ -372,16 +391,20 @@ public function testBuildWhereExists(string $cond, string $expectedQuerySql): vo $subQuery = new Query($db); - $subQuery->select('1') + $subQuery + ->select('1') ->from('Website w'); $query = new Query($db); - $query->select('id') + $query + ->select('id') ->from('TotalExample t') ->where([$cond, $subQuery]); - [$actualQuerySql, $actualQueryParams] = $this->getQueryBuilder()->build($query); + [$actualQuerySql, $actualQueryParams] = $this + ->getQueryBuilder() + ->build($query); $this->assertEquals($expectedQuerySql, $actualQuerySql); $this->assertEquals($expectedQueryParams, $actualQueryParams); @@ -414,7 +437,9 @@ function (QueryBuilder $qb) use ($tableName, $indexName, $schemaName) { */ public function testCreateDropIndex(string $sql, Closure $builder): void { - $this->assertSame($this->getConnection()->quoteSql($sql), $builder($this->getQueryBuilder())); + $this->assertSame($this + ->getConnection() + ->quoteSql($sql), $builder($this->getQueryBuilder())); } /** @@ -434,7 +459,9 @@ public function testDelete(string $table, $condition, string $expectedSQL, array { $actualParams = []; - $actualSQL = $this->getQueryBuilder()->delete($table, $condition, $actualParams); + $actualSQL = $this + ->getQueryBuilder() + ->delete($table, $condition, $actualParams); $this->assertSame($expectedSQL, $actualSQL); $this->assertSame($expectedParams, $actualParams); @@ -458,7 +485,9 @@ public function testInsert(string $table, $columns, array $params, string $expec { $actualParams = $params; - $actualSQL = $this->getQueryBuilder()->insert($table, $columns, $actualParams); + $actualSQL = $this + ->getQueryBuilder() + ->insert($table, $columns, $actualParams); $this->assertSame($expectedSQL, $actualSQL); $this->assertSame($expectedParams, $actualParams); @@ -481,13 +510,15 @@ public function testInsert(string $table, $columns, array $params, string $expec public function testUpdate( string $table, array $columns, - $condition, + $condition, string $expectedSQL, array $expectedParams ): void { $actualParams = []; - $actualSQL = $this->getQueryBuilder()->update($table, $columns, $condition, $actualParams); + $actualSQL = $this + ->getQueryBuilder() + ->update($table, $columns, $condition, $actualParams); $this->assertSame($expectedSQL, $actualSQL); $this->assertSame($expectedParams, $actualParams); @@ -561,7 +592,8 @@ public function testUpsert(string $table, $insertColumns, $updateColumns, $expec { $actualParams = []; - $actualSQL = $this->getQueryBuilder() + $actualSQL = $this + ->getQueryBuilder() ->upsert($table, $insertColumns, $updateColumns, $actualParams); if (is_string($expectedSQL)) { diff --git a/tests/QueryTest.php b/tests/QueryTest.php index fcf0aa3b3..fc534c55a 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -21,7 +21,8 @@ public function testUnion(): void $query = new Query($db); - $query->select(['id', 'name']) + $query + ->select(['id', 'name']) ->from('item') ->union( (new Query($db)) @@ -37,7 +38,10 @@ public function testUnion(): void public function testLimitOffsetWithExpression(): void { - $query = (new Query($this->getConnection()))->from('customer')->select('id')->orderBy('id'); + $query = (new Query($this->getConnection())) + ->from('customer') + ->select('id') + ->orderBy('id'); $query ->limit(new Expression('1 + 1')) diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 0bd9c7cc0..93d2dc638 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -235,7 +235,9 @@ public function testGetTableNames(array $pdoAttributes): void $connection = $this->getConnection(true); foreach ($pdoAttributes as $name => $value) { - $connection->getPDO()->setAttribute($name, $value); + $connection + ->getPDO() + ->setAttribute($name, $value); } $schema = $connection->getSchema(); @@ -268,7 +270,9 @@ public function testGetTableSchemas(array $pdoAttributes): void $db = $this->getConnection(true); foreach ($pdoAttributes as $name => $value) { - $db->getPDO()->setAttribute($name, $value); + $db + ->getPDO() + ->setAttribute($name, $value); } $schema = $db->getSchema(); @@ -306,7 +310,9 @@ public function quoteTableNameDataProvider(): array */ public function testQuoteTableName($name, $expectedName): void { - $schema = $this->getConnection()->getSchema(); + $schema = $this + ->getConnection() + ->getSchema(); $quotedName = $schema->quoteTableName($name); $this->assertEquals($expectedName, $quotedName); } @@ -347,7 +353,9 @@ public function testTableSchemaConstraints(string $tableName, string $type, $exp $this->expectException(NotSupportedException::class); } - $constraints = $this->getConnection()->getSchema()->{'getTable' . ucfirst($type)}($tableName); + $constraints = $this + ->getConnection() + ->getSchema()->{'getTable' . ucfirst($type)}($tableName); $this->assertMetadataEquals($expected, $constraints); } @@ -370,7 +378,9 @@ public function testTableSchemaConstraintsWithPdoLowercase(string $tableName, st $connection = $this->getConnection(); - $connection->getSlavePdo()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); + $connection + ->getSlavePdo() + ->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); $constraints = $connection->getSchema()->{'getTable' . ucfirst($type)}($tableName, true); @@ -395,7 +405,9 @@ public function testTableSchemaConstraintsWithPdoUppercase(string $tableName, st $connection = $this->getConnection(); - $connection->getSlavePdo()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); + $connection + ->getSlavePdo() + ->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); $constraints = $connection->getSchema()->{'getTable' . ucfirst($type)}($tableName, true); @@ -419,7 +431,9 @@ public function testTableSchemaCacheWithTablePrefixes( string $testTableName ): void { $db = $this->getConnection(); - $schema = $this->getConnection()->getSchema(); + $schema = $this + ->getConnection() + ->getSchema(); $this->schemaCache->setEnable(true); @@ -467,32 +481,51 @@ public function testsCountIndexUnique(): void $schema = $db->getSchema(); if ($schema->getTableSchema($tableName) !== null) { - $db->createCommand()->dropTable($tableName)->execute(); + $db + ->createCommand() + ->dropTable($tableName) + ->execute(); } - $db->createCommand()->createTable($tableName, [ - 'int1' => 'integer not null', - 'int2' => 'integer not null', - ])->execute(); + $db + ->createCommand() + ->createTable($tableName, [ + 'int1' => 'integer not null', + 'int2' => 'integer not null', + ]) + ->execute(); $this->assertEmpty($schema->getTableIndexes($tableName, true)); $this->assertEmpty($schema->getTableUniques($tableName, true)); - $db->createCommand()->addUnique($name, $tableName, ['int1'])->execute(); + $db + ->createCommand() + ->addUnique($name, $tableName, ['int1']) + ->execute(); $this->assertCount(1, $schema->getTableIndexes($tableName, true)); $this->assertCount(1, $schema->getTableUniques($tableName, true)); - $this->assertEquals(['int1'], $schema->getTableUniques($tableName, true)[0]->getColumnNames()); + $this->assertEquals(['int1'], $schema + ->getTableUniques($tableName, true) + ->getColumnNames()); - $db->createCommand()->dropUnique($name, $tableName)->execute(); + $db + ->createCommand() + ->dropUnique($name, $tableName) + ->execute(); $this->assertEmpty($schema->getTableIndexes($tableName, true)); $this->assertEmpty($schema->getTableUniques($tableName, true)); - $db->createCommand()->addUnique($name, $tableName, ['int1', 'int2'])->execute(); + $db + ->createCommand() + ->addUnique($name, $tableName, ['int1', 'int2']) + ->execute(); $this->assertCount(1, $schema->getTableIndexes($tableName, true)); $this->assertCount(1, $schema->getTableUniques($tableName, true)); - $this->assertEquals(['int1', 'int2'], $schema->getTableUniques($tableName, true)[0]->getColumnNames()); + $this->assertEquals(['int1', 'int2'], $schema + ->getTableUniques($tableName, true) + ->getColumnNames()); } }