Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix QueryBuilderTest::testBatchInsert() #789

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Enh #784: Specify result type of `ConstraintSchemaInterface::getTableIndexes()` method to `IndexConstraint[]` (@vjik)
- Enh #784: Remove unused code in `AbstractSchema::getTableIndexes()` (@vjik)
- Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov)
- Enh #789: Remove unnecessary type casting to `(array)` (@Tigrov)
vjik marked this conversation as resolved.
Show resolved Hide resolved

## 1.2.0 November 12, 2023

Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilder/AbstractDMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ static function (Constraint $constraint) use ($quoter, $columns, &$columnNames):
$result = empty(array_diff($constraintColumnNames, $columns));

if ($result) {
$columnNames = array_merge((array) $columnNames, $constraintColumnNames);
$columnNames = array_merge($columnNames, $constraintColumnNames);
}

return $result;
Expand Down
15 changes: 11 additions & 4 deletions tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,21 @@ public function testAlterColumn(): void
*
* @psalm-param array<array-key, string> $columns
*/
public function testBatchInsert(string $table, array $columns, iterable $rows, string $expected): void
{
public function testBatchInsert(
string $table,
array $columns,
iterable $rows,
string $expected,
array $expectedParams = [],
): void {
$db = $this->getConnection();

$qb = $db->getQueryBuilder();
$sql = $qb->batchInsert($table, $columns, $rows);

$params = [];
$sql = $qb->batchInsert($table, $columns, $rows, $params);

$this->assertSame($expected, $sql);
$this->assertSame($expectedParams, $params);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions tests/Db/QueryBuilder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,22 @@ public function testAddDefaultValue(): void
/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::batchInsert
*/
public function testBatchInsert(string $table, array $columns, iterable $rows, string $expected): void
{
public function testBatchInsert(
string $table,
array $columns,
iterable $rows,
string $expected,
array $expectedParams = [],
): void {
$db = $this->getConnection();

$schemaMock = $this->createMock(Schema::class);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock);
$params = [];

try {
$this->assertSame($expected, $qb->batchInsert($table, $columns, $rows));
$this->assertSame($expected, $qb->batchInsert($table, $columns, $rows, $params));
$this->assertSame($expectedParams, $params);
} catch (InvalidArgumentException|Exception) {
}
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static function batchInsert(): array
SQL,
static::$driverName,
),
[':qp0' => '[email protected]', ':qp1' => 'silverfire', ':qp2' => 'Kyiv {{city}}, Ukraine'],
'expectedParams' => [':qp0' => '[email protected]', ':qp1' => 'silverfire', ':qp2' => 'Kyiv {{city}}, Ukraine'],
],
'escape-danger-chars' => [
'customer',
Expand All @@ -157,7 +157,7 @@ public static function batchInsert(): array
SQL,
static::$driverName,
),
[':qp0' => "SQL-danger chars are escaped: '); --"],
'expectedParams' => [':qp0' => "SQL-danger chars are escaped: '); --"],
],
'customer2' => [
'customer',
Expand All @@ -175,7 +175,7 @@ public static function batchInsert(): array
SQL,
static::$driverName,
),
[':qp0' => 'no columns passed'],
'expectedParams' => [':qp0' => 'no columns passed'],
],
'bool-false, bool2-null' => [
'type',
Expand All @@ -187,7 +187,7 @@ public static function batchInsert(): array
SQL,
static::$driverName,
),
[':qp0' => 0, ':qp1' => null],
'expectedParams' => [':qp0' => false, ':qp1' => null],
],
'wrong' => [
'{{%type}}',
Expand All @@ -199,7 +199,7 @@ public static function batchInsert(): array
SQL,
static::$driverName,
),
[':qp0' => null, ':qp1' => null],
'expectedParams' => [':qp0' => null, ':qp1' => null],
],
'bool-false, time-now()' => [
'{{%type}}',
Expand All @@ -211,7 +211,7 @@ public static function batchInsert(): array
SQL,
static::$driverName,
),
[':qp0' => null],
'expectedParams' => [':qp0' => false],
],
'column table names are not checked' => [
'{{%type}}',
Expand All @@ -223,7 +223,7 @@ public static function batchInsert(): array
SQL,
static::$driverName,
),
[':qp0' => null, ':qp1' => null],
'expectedParams' => [':qp0' => true, ':qp1' => false],
],
'empty-sql' => [
'{{%type}}',
Expand Down
Loading