Skip to content

Commit

Permalink
Change methods to static QueryBuilderProvider::class. (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 17, 2023
1 parent 163313f commit 63d0653
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 60 deletions.
17 changes: 12 additions & 5 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Yiisoft\Db\Oracle\Tests;

use Closure;
use ReflectionException;
use Throwable;
use Yiisoft\Db\Exception\Exception;
Expand Down Expand Up @@ -64,7 +63,7 @@ public function testBatchInsert(
string $table,
array $columns,
array $values,
Closure $expected,
string $expected,
array $expectedParams = [],
int $insertedRow = 1
): void {
Expand Down Expand Up @@ -296,7 +295,7 @@ public function testExecuteWithTransaction(): void
* @throws InvalidConfigException
* @throws NotSupportedException
*/
public function testGetRawSql(string $sql, array $params, Closure $expectedRawSql): void
public function testGetRawSql(string $sql, array $params, string $expectedRawSql): void
{
parent::testGetRawSql($sql, $params, $expectedRawSql);
}
Expand Down Expand Up @@ -515,19 +514,27 @@ public function testNoTablenameReplacement(): void

/**
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandProvider::update
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testUpdate(
string $table,
array $columns,
array|string $conditions,
array $params,
Closure $expected
string $expected
): void {
parent::testUpdate($table, $columns, $conditions, $params, $expected);
}

/**
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandProvider::upsert
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testUpsert(array $firstData, array $secondData): void
{
Expand All @@ -545,7 +552,7 @@ public function testQueryScalarWithBlob(): void
{
$db = $this->getConnection(true);

$value = json_encode(['test']);
$value = json_encode(['test'], JSON_THROW_ON_ERROR);
$db->createCommand()->insert('{{%T_upsert_varbinary}}', ['id' => 1, 'blob_col' => $value])->execute();

$scalarValue = $db->createCommand('SELECT [[blob_col]] FROM {{%T_upsert_varbinary}}')->queryScalar();
Expand Down
28 changes: 15 additions & 13 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,52 @@

namespace Yiisoft\Db\Oracle\Tests\Provider;

use JsonException;
use PDO;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Support\DbHelper;

use function json_encode;
use function serialize;

final class CommandProvider extends \Yiisoft\Db\Tests\Provider\CommandProvider
{
use TestTrait;

protected static string $driverName = 'oci';

public static function batchInsert(): array
{
$batchInsert = parent::batchInsert();

$batchInsert['multirow']['expected'] = static fn (string $driverName): string => <<<SQL
$batchInsert['multirow']['expected'] = <<<SQL
INSERT ALL INTO "type" ("int_col", "float_col", "char_col", "bool_col") VALUES (:qp0, :qp1, :qp2, :qp3) INTO "type" ("int_col", "float_col", "char_col", "bool_col") VALUES (:qp4, :qp5, :qp6, :qp7) SELECT 1 FROM SYS.DUAL
SQL;
$batchInsert['multirow']['expectedParams'][':qp3'] = '1';
$batchInsert['multirow']['expectedParams'][':qp7'] = '0';

$issue11242 = $batchInsert['issue11242']['expected']('oci');
DbHelper::changeSqlForOracleBatchInsert($issue11242);
$batchInsert['issue11242']['expected'] = static fn (string $driverName): string => $issue11242;
DbHelper::changeSqlForOracleBatchInsert($batchInsert['issue11242']['expected']);
$batchInsert['issue11242']['expectedParams'][':qp3'] = '1';

$wrongBehavior = $batchInsert['wrongBehavior']['expected']('oci');
DbHelper::changeSqlForOracleBatchInsert($wrongBehavior);
$batchInsert['wrongBehavior']['expected'] = static fn (string $driverName): string => $wrongBehavior;

DbHelper::changeSqlForOracleBatchInsert($batchInsert['wrongBehavior']['expected']);
$batchInsert['wrongBehavior']['expectedParams'][':qp3'] = '0';

$batchInsertBinds = $batchInsert['batchInsert binds params from expression']['expected']('oci');
DbHelper::changeSqlForOracleBatchInsert($batchInsertBinds);
$batchInsert['batchInsert binds params from expression']['expected'] = static fn (string $driverName): string => $batchInsertBinds;
DbHelper::changeSqlForOracleBatchInsert($batchInsert['batchInsert binds params from expression']['expected']);
$batchInsert['batchInsert binds params from expression']['expectedParams'][':qp3'] = '0';

return $batchInsert;
}

/**
* @throws JsonException
*/
public static function insertVarbinary(): array
{
return [
[
json_encode(['string' => 'string', 'integer' => 1234]),
json_encode(['string' => 'string', 'integer' => 1234]),
json_encode(['string' => 'string', 'integer' => 1234], JSON_THROW_ON_ERROR),
json_encode(['string' => 'string', 'integer' => 1234], JSON_THROW_ON_ERROR),
],
[
serialize(['string' => 'string', 'integer' => 1234]),
Expand Down
40 changes: 16 additions & 24 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Yiisoft\Db\Oracle\Tests\Provider;

use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Exception;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
Expand All @@ -20,14 +19,15 @@ final class QueryBuilderProvider extends \Yiisoft\Db\Tests\Provider\QueryBuilder
{
use TestTrait;

protected string $likeEscapeCharSql = " ESCAPE '!'";
protected array $likeParameterReplacements = [
protected static string $driverName = 'oci';
protected static string $likeEscapeCharSql = " ESCAPE '!'";
protected static array $likeParameterReplacements = [
'\%' => '!%',
'\_' => '!_',
'!' => '!!',
];

public function addForeignKey(): array
public static function addForeignKey(): array
{
$addForeingKey = parent::addForeignKey();

Expand All @@ -41,7 +41,7 @@ public function addForeignKey(): array
return $addForeingKey;
}

public function batchInsert(): array
public static function batchInsert(): array
{
$batchInsert = parent::batchInsert();

Expand All @@ -59,7 +59,7 @@ public function batchInsert(): array
return $batchInsert;
}

public function buildCondition(): array
public static function buildCondition(): array
{
$buildCondition = parent::buildCondition();

Expand All @@ -76,31 +76,25 @@ public function buildCondition(): array
return $buildCondition;
}

/**
* @throws Exception
* @throws InvalidConfigException
*/
public function buildLikeCondition(): array
public static function buildLikeCondition(): array
{
$db = $this->getConnection();

/*
* Different pdo_oci8 versions may or may not implement PDO::quote(), so \Yiisoft\Db\Oracle\Quoter::quoteValue()
* may or may not quote \.
*/
try {
$encodedBackslash = substr($db->getQuoter()->quoteValue('\\\\'), 1, -1);
$encodedBackslash = substr(self::getDb()->quoteValue('\\\\'), 1, -1);

$this->likeParameterReplacements[$encodedBackslash] = '\\';
} catch (\Exception) {
self::$likeParameterReplacements[$encodedBackslash] = '\\';
} catch (Exception) {
// ignore
}


return parent::buildLikeCondition();
}

public function insert(): array
public static function insert(): array
{
$insert = parent::insert();

Expand All @@ -111,7 +105,7 @@ public function insert(): array
return $insert;
}

public function selectExist(): array
public static function selectExist(): array
{
$selectExist = parent::selectExist();

Expand All @@ -122,10 +116,8 @@ public function selectExist(): array
return $selectExist;
}

public function upsert(): array
public static function upsert(): array
{
$db = $this->getConnection();

$concreteData = [
'regular values' => [
3 => <<<SQL
Expand Down Expand Up @@ -185,7 +177,7 @@ public function upsert(): array
SQL,
],
'query, values and expressions with update part' => [
1 => (new Query($db))
1 => (new Query(self::getDb()))
->select(
[
'email' => new Expression(':phEmail', [':phEmail' => 'dynamic@example.com']),
Expand All @@ -198,7 +190,7 @@ public function upsert(): array
SQL,
],
'query, values and expressions without update part' => [
1 => (new Query($db))
1 => (new Query(self::getDb()))
->select(
[
'email' => new Expression(':phEmail', [':phEmail' => 'dynamic@example.com']),
Expand Down
Loading

0 comments on commit 63d0653

Please sign in to comment.