Skip to content

Commit 447a0f0

Browse files
authored
Fix QueryBuilderTest::testBatchInsert() (#789)
1 parent 199070f commit 447a0f0

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Enh #784: Specify result type of `ConstraintSchemaInterface::getTableIndexes()` method to `IndexConstraint[]` (@vjik)
1616
- Enh #784: Remove unused code in `AbstractSchema::getTableIndexes()` (@vjik)
1717
- Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov)
18+
- Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov)
1819

1920
## 1.2.0 November 12, 2023
2021

src/QueryBuilder/AbstractDMLQueryBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static function (Constraint $constraint) use ($quoter, $columns, &$columnNames):
380380
$result = empty(array_diff($constraintColumnNames, $columns));
381381

382382
if ($result) {
383-
$columnNames = array_merge((array) $columnNames, $constraintColumnNames);
383+
$columnNames = array_merge($columnNames, $constraintColumnNames);
384384
}
385385

386386
return $result;

tests/AbstractQueryBuilderTest.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,21 @@ public function testAlterColumn(): void
210210
*
211211
* @psalm-param array<array-key, string> $columns
212212
*/
213-
public function testBatchInsert(string $table, array $columns, iterable $rows, string $expected): void
214-
{
213+
public function testBatchInsert(
214+
string $table,
215+
array $columns,
216+
iterable $rows,
217+
string $expected,
218+
array $expectedParams = [],
219+
): void {
215220
$db = $this->getConnection();
216-
217221
$qb = $db->getQueryBuilder();
218-
$sql = $qb->batchInsert($table, $columns, $rows);
222+
223+
$params = [];
224+
$sql = $qb->batchInsert($table, $columns, $rows, $params);
219225
220226
$this->assertSame($expected, $sql);
227+
$this->assertSame($expectedParams, $params);
221228
}
222229
223230
/**

tests/Db/QueryBuilder/QueryBuilderTest.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,22 @@ public function testAddDefaultValue(): void
4747
/**
4848
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::batchInsert
4949
*/
50-
public function testBatchInsert(string $table, array $columns, iterable $rows, string $expected): void
51-
{
50+
public function testBatchInsert(
51+
string $table,
52+
array $columns,
53+
iterable $rows,
54+
string $expected,
55+
array $expectedParams = [],
56+
): void {
5257
$db = $this->getConnection();
5358

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

5763
try {
58-
$this->assertSame($expected, $qb->batchInsert($table, $columns, $rows));
64+
$this->assertSame($expected, $qb->batchInsert($table, $columns, $rows, $params));
65+
$this->assertSame($expectedParams, $params);
5966
} catch (InvalidArgumentException|Exception) {
6067
}
6168
}

tests/Provider/QueryBuilderProvider.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static function batchInsert(): array
145145
SQL,
146146
static::$driverName,
147147
),
148-
[':qp0' => '[email protected]', ':qp1' => 'silverfire', ':qp2' => 'Kyiv {{city}}, Ukraine'],
148+
'expectedParams' => [':qp0' => '[email protected]', ':qp1' => 'silverfire', ':qp2' => 'Kyiv {{city}}, Ukraine'],
149149
],
150150
'escape-danger-chars' => [
151151
'customer',
@@ -157,7 +157,7 @@ public static function batchInsert(): array
157157
SQL,
158158
static::$driverName,
159159
),
160-
[':qp0' => "SQL-danger chars are escaped: '); --"],
160+
'expectedParams' => [':qp0' => "SQL-danger chars are escaped: '); --"],
161161
],
162162
'customer2' => [
163163
'customer',
@@ -175,7 +175,7 @@ public static function batchInsert(): array
175175
SQL,
176176
static::$driverName,
177177
),
178-
[':qp0' => 'no columns passed'],
178+
'expectedParams' => [':qp0' => 'no columns passed'],
179179
],
180180
'bool-false, bool2-null' => [
181181
'type',
@@ -187,7 +187,7 @@ public static function batchInsert(): array
187187
SQL,
188188
static::$driverName,
189189
),
190-
[':qp0' => 0, ':qp1' => null],
190+
'expectedParams' => [':qp0' => false, ':qp1' => null],
191191
],
192192
'wrong' => [
193193
'{{%type}}',
@@ -199,7 +199,7 @@ public static function batchInsert(): array
199199
SQL,
200200
static::$driverName,
201201
),
202-
[':qp0' => null, ':qp1' => null],
202+
'expectedParams' => [':qp0' => null, ':qp1' => null],
203203
],
204204
'bool-false, time-now()' => [
205205
'{{%type}}',
@@ -211,7 +211,7 @@ public static function batchInsert(): array
211211
SQL,
212212
static::$driverName,
213213
),
214-
[':qp0' => null],
214+
'expectedParams' => [':qp0' => false],
215215
],
216216
'column table names are not checked' => [
217217
'{{%type}}',
@@ -223,7 +223,7 @@ public static function batchInsert(): array
223223
SQL,
224224
static::$driverName,
225225
),
226-
[':qp0' => null, ':qp1' => null],
226+
'expectedParams' => [':qp0' => true, ':qp1' => false],
227227
],
228228
'empty-sql' => [
229229
'{{%type}}',

0 commit comments

Comments
 (0)