Skip to content

Commit

Permalink
Improve test coverage (prepareInsertSelectSubQuery) (#451)
Browse files Browse the repository at this point in the history
* Improve test coverage (prepareInsertSelectSubQuery)

* Improve test coverage for Command

* fix
  • Loading branch information
darkdef committed Jan 4, 2023
1 parent cfd508a commit 1eff36d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/QueryBuilder/DMLQueryBuilder.php
Expand Up @@ -185,10 +185,16 @@ protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $p
foreach ($select as $title => $field) {
if (is_string($title)) {
$names[] = $this->quoter->quoteColumnName($title);
} elseif (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $field, $matches)) {
$names[] = $this->quoter->quoteColumnName($matches[2]);
} else {
$names[] = $this->quoter->quoteColumnName($field);
if ($field instanceof ExpressionInterface) {
$field = $this->queryBuilder->buildExpression($field, $params);
}

if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $field, $matches)) {
$names[] = $this->quoter->quoteColumnName($matches[2]);
} else {
$names[] = $this->quoter->quoteColumnName($field);
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Common/CommonCommandTest.php
Expand Up @@ -111,6 +111,17 @@ public function testAddCommentOnTable(): void
$this->assertSame($commentText, $commentOnTable);
}

public function testResetSequenceSql(): void
{
$db = $this->getConnection(true);

$command = $db->createCommand();

$this->assertEmpty($command->getRawSql());
$command->resetSequence('{{%customer}}');
$this->assertNotEmpty($command->getRawSql());
}

/**
* @throws Exception
* @throws InvalidConfigException
Expand Down
21 changes: 21 additions & 0 deletions tests/Provider/AbstractQueryBuilderProvider.php
Expand Up @@ -995,6 +995,27 @@ public function insert(): array
),
[],
],
'query' => [
'customer',
(new Query($db))
->select([new Expression('email as email'), new Expression('name')])
->from('customer')
->where(
[
'email' => 'test@example.com',
],
),
[],
DbHelper::replaceQuotes(
<<<SQL
INSERT INTO [[customer]] ([[email]], [[name]]) SELECT email as email, name FROM [[customer]] WHERE [[email]]=:qp0
SQL,
$db->getName(),
),
[
':qp0' => 'test@example.com',
],
],
];
}

Expand Down

0 comments on commit 1eff36d

Please sign in to comment.