Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Feb 6, 2024
1 parent 17eb937 commit b325bab
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 61 deletions.
2 changes: 1 addition & 1 deletion tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,7 @@ public function testUpdate(
$sql = $qb->quoter()->quoteSql($sql);

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

/**
Expand Down
21 changes: 15 additions & 6 deletions tests/Common/CommonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1888,16 +1888,25 @@ public function testUpdate(
array $columns,
array|string $conditions,
array $params,
string $expected,
array $expectedParams = [],
array $expectedValues,
int $expectedCount,
): void {
$db = $this->getConnection();
$db = $this->getConnection(true);

$command = $db->createCommand();
$sql = $command->update($table, $columns, $conditions, $params)->getSql();
$count = $command->update($table, $columns, $conditions, $params)->execute();

$this->assertSame($expected, $sql);
$this->assertSame($expectedParams, $command->getParams());
$this->assertSame($expectedCount, $count);

$values = (new Query($db))
->from($table)
->where($conditions, $params)
->limit(1)
->one();

foreach ($expectedValues as $name => $expectedValue) {
$this->assertEquals($expectedValue, $values[$name]);
}

$db->close();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Db/QueryBuilder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function testUpdate(
array $columns,
array|string $condition,
array $params,
string $expectedSQL,
string $expectedSql,
array $expectedParams
): void {
$db = $this->getConnection();
Expand All @@ -265,8 +265,8 @@ public function testUpdate(
$sql = $qb->update($table, $columns, $condition, $params);
$sql = $qb->quoter()->quoteSql($sql);

$this->assertSame($expectedSQL, $sql);
$this->assertSame($expectedParams, $params);
$this->assertSame($expectedSql, $sql);
$this->assertEquals($expectedParams, $params);
}

/**
Expand Down
78 changes: 27 additions & 51 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,71 +723,47 @@ public static function update(): array
{
return [
[
'{{table}}',
'{{customer}}',
['name' => '{{test}}'],
[],
[],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp0
SQL,
static::$driverName,
),
[
':qp0' => '{{test}}'
],
['name' => '{{test}}'],
3,
],
[
'{{table}}',
'{{customer}}',
['name' => '{{test}}'],
['id' => 1],
[],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp0 WHERE [[id]]=:qp1
SQL,
static::$driverName,
),
[
':qp0' => '{{test}}',
':qp1' => 1,
],
['name' => '{{test}}'],
1,
],
[
'{{table}}',
['{{table}}.name' => '{{test}}'],
'{{customer}}',
['{{customer}}.name' => '{{test}}'],
['id' => 1],
['id' => 'boolean'],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
SQL,
static::$driverName,
),
[
'id' => 'boolean',
':qp1' => '{{test}}',
':qp2' => 1,
],
[],
['name' => '{{test}}'],
1,
],
[
'{{table}}',
['name' => new Expression(
'[[name]] || :name',
['name' => new Expression('LOWER(:val)', ['val' => 'A'])]
'customer',
['status' => new Expression('1 + 2')],
['id' => 2],
[],
['status' => 3],
1,
],
[
'{{customer}}',
['status' => new Expression(
'1 + :val',
['val' => new Expression('2 + :val', ['val' => 3])]
)],
'[[name]] != :name',
['name' => new Expression('LOWER(:val)', ['val' => 'B'])],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=[[name]] || LOWER(:val) WHERE [[name]] != LOWER(:val_0)
SQL,
static::$driverName,
),
[
'val' => 'A',
'val_0' => 'B',
],
'[[name]] != :val',
['val' => new Expression('LOWER(:val)', ['val' => 'USER1'])],
['name' => 'user2', 'status' => 6],
2,
],
];
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,54 @@ public static function selectExist(): array
public static function update(): array
{
return [
[
'{{table}}',
['name' => '{{test}}'],
[],
[],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp0
SQL,
static::$driverName,
),
[
':qp0' => '{{test}}'
],
],
[
'{{table}}',
['name' => '{{test}}'],
['id' => 1],
[],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp0 WHERE [[id]]=:qp1
SQL,
static::$driverName,
),
[
':qp0' => '{{test}}',
':qp1' => 1,
],
],
[
'{{table}}',
['{{table}}.name' => '{{test}}'],
['id' => 1],
['id' => 'boolean'],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
SQL,
static::$driverName,
),
[
'id' => 'boolean',
':qp1' => '{{test}}',
':qp2' => 1,
],
],
[
'customer',
['status' => 1, 'updated_at' => new Expression('now()')],
Expand Down

0 comments on commit b325bab

Please sign in to comment.