Skip to content

Commit

Permalink
Fix QueryBuilderTest::testBatchInsert() (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Dec 7, 2023
1 parent 199070f commit 447a0f0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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 in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov)

## 1.2.0 November 12, 2023

Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilder/AbstractDMLQueryBuilder.php
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
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
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
Expand Up @@ -145,7 +145,7 @@ public static function batchInsert(): array
SQL,
static::$driverName,
),
[':qp0' => 'test@example.com', ':qp1' => 'silverfire', ':qp2' => 'Kyiv {{city}}, Ukraine'],
'expectedParams' => [':qp0' => 'test@example.com', ':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

0 comments on commit 447a0f0

Please sign in to comment.