Skip to content

Commit

Permalink
Change methods to static CommandProvider::class. (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 17, 2023
1 parent d170f18 commit 3986271
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 124 deletions.
5 changes: 3 additions & 2 deletions tests/AbstractCommandTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Tests;

use Closure;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Command\ParamInterface;
Expand Down Expand Up @@ -101,13 +102,13 @@ public function testGetParams(): void
*
* {@see https://github.com/yiisoft/yii2/issues/8592}
*/
public function testGetRawSql(string $sql, array $params, string $expectedRawSql): void
public function testGetRawSql(string $sql, array $params, Closure $expectedRawSql): void
{
$db = $this->getConnection();

$command = $db->createCommand($sql, $params);

$this->assertSame($expectedRawSql, $command->getRawSql());
$this->assertSame($expectedRawSql($db->getName()), $command->getRawSql());
}

public function testGetSetSql(): void
Expand Down
30 changes: 21 additions & 9 deletions tests/Common/CommonCommandTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Tests\Common;

use Closure;
use ReflectionException;
use Throwable;
use Yiisoft\Db\Driver\PDO\AbstractCommandPDO;
Expand Down Expand Up @@ -306,7 +307,7 @@ public function testBatchInsert(
string $table,
array $columns,
array $values,
string $expected,
Closure $expected,
array $expectedParams = [],
int $insertedRow = 1
): void {
Expand All @@ -315,7 +316,7 @@ public function testBatchInsert(
$command = $db->createCommand();
$command->batchInsert($table, $columns, $values);

$this->assertSame($expected, $command->getSql());
$this->assertSame($expected($db->getName()), $command->getSql());
$this->assertSame($expectedParams, $command->getParams());

$command->prepare(false);
Expand Down Expand Up @@ -1888,14 +1889,14 @@ public function testUpdate(
array $columns,
array|string $conditions,
array $params,
string $expected
Closure $expected
): void {
$db = $this->getConnection();

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

$this->assertSame($expected, $sql);
$this->assertSame($expected($db->getName()), $sql);

$db->close();
}
Expand Down Expand Up @@ -1942,11 +1943,10 @@ public function testUpsert(array $firstData, array $secondData): void
public function testPrepareWithEmptySql()
{
$db = $this->createMock(ConnectionPDOInterface::class);
$db->expects(self::never())
->method('getActivePDO');
$db->expects(self::never())->method('getActivePDO');

$command = new class ($db) extends AbstractCommandPDO {
protected function internalExecute(?string $rawSql): void
protected function internalExecute(string|null $rawSql): void
{
}

Expand All @@ -1965,7 +1965,16 @@ public function queryBuilder(): QueryBuilderInterface
*/
protected function performAndCompareUpsertResult(ConnectionPDOInterface $db, array $data): void
{
$params = $data['params'];
$params = [];

foreach ($data['params'] as $param) {
if (is_callable($param)) {
$params[] = $param($db);
} else {
$params[] = $param;
}
}

$expected = $data['expected'] ?? $params[1];

$command = $db->createCommand();
Expand All @@ -1992,7 +2001,10 @@ public function testDecimalValue(): void
['customer_id' => 1, 'created_at' => 0, 'total' => $decimalValue]
);

$result = $db->createCommand('select * from {{%order}} where [[id]]=:id', ['id' => $inserted['id']])->queryOne();
$result = $db->createCommand(
'select * from {{%order}} where [[id]]=:id',
['id' => $inserted['id']]
)->queryOne();

$columnSchema = $db->getTableSchema('{{%order}}')->getColumn('total');
$phpTypecastValue = $columnSchema->phpTypecast($result['total']);
Expand Down
17 changes: 9 additions & 8 deletions tests/Db/Command/CommandTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Tests\Db\Command;

use Closure;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\AbstractCommandTest;
Expand Down Expand Up @@ -117,41 +118,41 @@ public function testAddForeignKeySql(
array|string $column2,
string|null $delete,
string|null $update,
string $expected
Closure $expected
): void {
$db = $this->getConnection();

$command = $db->createCommand();
$sql = $command->addForeignKey($name, $tableName, $column1, $tableName, $column2, $delete, $update)->getSql();

$this->assertSame($expected, $sql);
$this->assertSame($expected($db->getName()), $sql);
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::addPrimaryKeySql
*/
public function testAddPrimaryKeySql(string $name, string $tableName, array|string $column, string $expected): void
public function testAddPrimaryKeySql(string $name, string $tableName, array|string $column, Closure $expected): void
{
$db = $this->getConnection();

$command = $db->createCommand();
$sql = $command->addPrimaryKey($name, $tableName, $column)->getSql();


$this->assertSame($expected, $sql);
$this->assertSame($expected($db->getName()), $sql);
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::addUniqueSql
*/
public function testAddUniqueSql(string $name, string $tableName, array|string $column, string $expected): void
public function testAddUniqueSql(string $name, string $tableName, array|string $column, Closure $expected): void
{
$db = $this->getConnection();

$command = $db->createCommand();
$sql = $command->addUnique($name, $tableName, $column)->getSql();

$this->assertSame($expected, $sql);
$this->assertSame($expected($db->getName()), $sql);
}

public function testAlterColumn(): void
Expand Down Expand Up @@ -195,15 +196,15 @@ public function testCreateIndexSql(
array|string $column,
string $indexType,
string $indexMethod,
string $expected,
Closure $expected,
): void {
$db = $this->getConnection();

$command = $db->createCommand();

$sql = $command->createIndex($name, $table, $column, $indexType, $indexMethod)->getSql();

$this->assertSame($expected, $sql);
$this->assertSame($expected($db->getName()), $sql);
}

public function testCheckIntegrity(): void
Expand Down

0 comments on commit 3986271

Please sign in to comment.