Skip to content

Commit

Permalink
Clean tests for PHPUnit v.10. (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Sep 27, 2024
1 parent 658aa1c commit 870f336
Show file tree
Hide file tree
Showing 69 changed files with 452 additions and 333 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Yii Core version 2 Change Log
- Enh #106: Move `extractAlias()` method to `Quoter::class` (@terabytesoftw)
- Enh #107: Add `SqlHelper::class` in `db` (@terabytesoftw)
- Enh #108: Add method `addSuffix()` in `SqlHelper::class` (@terabytesoftw)
- Enh #109: Clean tests for `PHPUnit` v.10 (@terabytesoftw)

Yii Framework 2 Change Log
==========================
Expand Down
2 changes: 1 addition & 1 deletion src/db/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ public function getServerVersion()
*/
public function getSequenceInfo(string $sequence): array|false
{
throw new NotSupportedException($this->db->getDriverName() . ' does not support getting sequence name.');
throw new NotSupportedException($this->db->getDriverName() . ' does not support getting sequence information.');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/db/pgsql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ public function resetAutoIncrementPK(string $tableName, int|null $value = null):
$value = $this->db->getSchema()->getNextAutoIncrementPKValue($tableSchema->fullName, $columnPK);
}

$sequenceName = $this->db->quoteValue($tableSchema->columns[$columnPK]->sequenceName);
$sequenceName = $this->quoteValue('"' . $tableSchema->columns[$columnPK]->sequenceName . '"');

$sql = <<<SQL
SELECT SETVAL($sequenceName,{$value},false)
SELECT SETVAL($sequenceName,$value,false)
SQL;

$this->db->createCommand($sql)->execute();
Expand Down
12 changes: 6 additions & 6 deletions tests/framework/db/command/AbstractBatchInsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace yiiunit\framework\db\command;

use yii\base\InvalidArgumentException;
use yii\db\Connection;
use yii\db\Query;
use yii\db\{Connection, Query};

abstract class AbstractBatchInsert extends \yiiunit\TestCase
{
Expand All @@ -26,23 +25,23 @@ public function tearDown(): void
* {@see https://github.com/yiisoft/yii2/issues/11242}
*/
public function testBatchInsert(
string $table,
string $tableName,
array $columns,
array $values,
string $expected,
array $expectedParams = [],
int $insertedRow = 1
): void {
$command = $this->db->createCommand();
$command->batchInsert($table, $columns, $values);
$command->batchInsert($tableName, $columns, $values);

$this->assertSame($expected, $command->getSql());
$this->assertSame($expectedParams, $command->params);

$command->prepare(false);
$command->execute();

$this->assertEquals($insertedRow, (new Query())->from($table)->count(db: $this->db));
$this->assertEquals($insertedRow, (new Query())->from($tableName)->count(db: $this->db));
}

/**
Expand Down Expand Up @@ -98,12 +97,13 @@ public function testBatchInsertDataTypesLocale(): void
$this->assertEquals('1', $data[0]['bool_col']);
$this->assertIsOneOf($data[1]['bool_col'], ['0', false]);
$this->assertIsOneOf($data[2]['bool_col'], ['0', false]);

} catch (\Exception $e) {
setlocale(LC_NUMERIC, $locale);

throw $e;
} catch (\Throwable $e) {
setlocale(LC_NUMERIC, $locale);

throw $e;
}

Expand Down
21 changes: 7 additions & 14 deletions tests/framework/db/command/AbstractCreateSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace yiiunit\framework\db\command;

use yii\db\Connection;
use yiiunit\support\DbHelper;

abstract class AbstractCreateSequence extends \yiiunit\TestCase
{
Expand All @@ -19,34 +20,26 @@ protected function tearDown(): void
}

public function testCreateSequence(
string $table,
string $sequence,
int $start,
int $increment,
array $options,
array $expectedSequenceInfo
): void {
$this->ensureNoTable($table);
DbHelper::ensureNoTable($this->db, $sequence);

$result = $this->db->createCommand()->createSequence($table, $start, $increment, $options)->execute();
$result = $this->db->createCommand()->createSequence($sequence, $start, $increment, $options)->execute();

$this->assertSame(0, $result);

$sequenceInfo = $this->db->getSchema()->getSequenceInfo($table);
$sequenceInfo = $this->db->getSchema()->getSequenceInfo($sequence);

$this->assertSame($expectedSequenceInfo, $sequenceInfo);

$result = $this->db->createCommand()->dropSequence($table)->execute();
$result = $this->db->createCommand()->dropSequence($sequence)->execute();

$this->assertSame(0, $result);

$this->ensureNoTable($table);
}

protected function ensureNoTable(string $tableName): void
{
if ($this->db->hasTable($tableName)) {
$this->db->createCommand()->dropTable($tableName)->execute();
$this->assertFalse($this->db->hasTable($tableName));
}
DbHelper::ensureNoTable($this->db, $sequence);
}
}
11 changes: 1 addition & 10 deletions tests/framework/db/command/AbstractInsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace yiiunit\framework\db\command;

use yii\db\Connection;
use yiiunit\TestCase;

abstract class AbstractInsert extends TestCase
abstract class AbstractInsert extends \yiiunit\TestCase
{
protected Connection|null $db = null;

Expand Down Expand Up @@ -44,12 +43,4 @@ public function testInsertWithReturningPksEmptyValuesAndNoPk(): void
$this->db->createCommand()->insertWithReturningPks('negative_default_values', []),
);
}

protected function ensureNoTable(string $tableName): void
{
if ($this->db->hasTable($tableName)) {
$this->db->createCommand()->dropTable($tableName)->execute();
$this->assertFalse($this->db->hasTable($tableName));
}
}
}
6 changes: 3 additions & 3 deletions tests/framework/db/command/AbstractUpsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace yiiunit\framework\db\command;

use yii\db\Connection;
use yii\db\Query;
use yii\db\QueryInterface;
use yii\db\{Connection, Query, QueryInterface};

use function is_resource;
use function json_encode;
use function stream_get_contents;

abstract class AbstractUpsert extends \yiiunit\TestCase
{
Expand Down
5 changes: 2 additions & 3 deletions tests/framework/db/connection/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace yiiunit\framework\db\connection;

use yii\db\Connection;
use yii\db\Schema;
use yii\db\{Connection, Schema};

abstract class AbstractConnection extends \yiiunit\TestCase
{
Expand All @@ -21,7 +20,7 @@ protected function tearDown(): void

public function testHasTable(): void
{
$tableName = 'T_table';
$tableName = '{{%T_has_table}}';

if ($this->db->hasTable($tableName)) {
$result = $this->db->createCommand()->dropTable($tableName)->execute();
Expand Down
6 changes: 3 additions & 3 deletions tests/framework/db/mssql/command/BatchInserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @group db
* @group mssql
* @group command
* @group batchinsert
* @group batch-insert
*/
final class BatchInserTest extends \yiiunit\framework\db\command\AbstractBatchInsert
{
Expand All @@ -25,13 +25,13 @@ protected function setUp(): void
* @dataProvider \yiiunit\framework\db\mssql\provider\CommandProvider::batchInsert
*/
public function testBatchInsert(
string $table,
string $tableName,
array $columns,
array $values,
string $expected,
array $expectedParams = [],
int $insertedRow = 1
): void {
parent::testBatchInsert($table, $columns, $values, $expected, $expectedParams, $insertedRow);
parent::testBatchInsert($tableName, $columns, $values, $expected, $expectedParams, $insertedRow);
}
}
4 changes: 2 additions & 2 deletions tests/framework/db/mssql/command/CreateSequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function setup(): void
* @dataProvider \yiiunit\framework\db\mssql\provider\CommandProvider::createSequence
*/
public function testCreateSequence(
string $table,
string $sequence,
int $start,
int $increment,
array $options,
array $expectedSequenceInfo
): void {
parent::testCreateSequence($table, $start, $increment, $options, $expectedSequenceInfo);
parent::testCreateSequence($sequence, $start, $increment, $options, $expectedSequenceInfo);
}
}
26 changes: 13 additions & 13 deletions tests/framework/db/mssql/command/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace yiiunit\framework\db\mssql\command;

use yii\db\Expression;
use yiiunit\support\MssqlConnection;
use yiiunit\support\{DbHelper, MssqlConnection};

/**
* @group db
Expand All @@ -26,7 +26,7 @@ public function testInsertUsingSequence(): void
{
$tableName = '{{%T_insert_using_sequence}}';

$this->ensureNoTable($tableName);
DbHelper::ensureNoTable($this->db, $tableName);

$result = $this->db->createCommand()->createTable(
$tableName,
Expand All @@ -38,15 +38,15 @@ public function testInsertUsingSequence(): void

$this->assertSame(0, $result);

$sequenceName = 'T_insert_using_sequence';
$sequence = 'T_insert_using_sequence';

$result = $this->db->createCommand()->createSequence($sequenceName)->execute();
$result = $this->db->createCommand()->createSequence($sequence)->execute();

$this->assertSame(0, $result);

$result = $this->db->createCommand()->insert(
$tableName,
['id' => new Expression("NEXT VALUE FOR {$sequenceName}_SEQ"), 'name' => 'test']
['id' => new Expression("NEXT VALUE FOR {$sequence}_SEQ"), 'name' => 'test']
)->execute();

$this->assertSame(1, $result);
Expand All @@ -60,18 +60,18 @@ public function testInsertUsingSequence(): void

$this->assertSame(['1'], $ids);

$result = $this->db->createCommand()->dropSequence($sequenceName)->execute();
$result = $this->db->createCommand()->dropSequence($sequence)->execute();

$this->assertSame(0, $result);

$this->ensureNoTable($tableName);
DbHelper::ensureNoTable($this->db, $tableName);
}

public function testInsertUsingSequenceWithOptions(): void
{
$tableName = '{{%T_insert_using_sequence_options}}';

$this->ensureNoTable($tableName);
DbHelper::ensureNoTable($this->db, $tableName);

$result = $this->db->createCommand()->createTable(
$tableName,
Expand All @@ -83,10 +83,10 @@ public function testInsertUsingSequenceWithOptions(): void

$this->assertSame(0, $result);

$sequenceName = 'T_insert_using_sequence_options';
$sequence = 'T_insert_using_sequence_options';

$result = $this->db->createCommand()->createSequence(
$sequenceName,
$sequence,
100,
2,
[
Expand All @@ -97,7 +97,7 @@ public function testInsertUsingSequenceWithOptions(): void

$this->assertSame(0, $result);

$idValue = new Expression("NEXT VALUE FOR {$sequenceName}_SEQ");
$idValue = new Expression("NEXT VALUE FOR {$sequence}_SEQ");

$result = $this->db->createCommand()->insert($tableName, ['id' => $idValue, 'name' => 'test'])->execute();

Expand All @@ -116,10 +116,10 @@ public function testInsertUsingSequenceWithOptions(): void

$this->assertSame(['100', '102'], $ids);

$result = $this->db->createCommand()->dropSequence($sequenceName)->execute();
$result = $this->db->createCommand()->dropSequence($sequence)->execute();

$this->assertSame(0, $result);

$this->ensureNoTable($tableName);
DbHelper::ensureNoTable($this->db, $tableName);
}
}
4 changes: 2 additions & 2 deletions tests/framework/db/mssql/provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static function resetAutoIncrementPK(): array
$rows = parent::resetAutoIncrementPK();

$rows['value with zero'] = [
'{{%reset_autoincrement_pk}}',
'{{%T_reset_autoincrement_pk}}',
[
['name' => 'name1'],
['name' => 'name2'],
Expand All @@ -22,7 +22,7 @@ public static function resetAutoIncrementPK(): array
];

$rows['value negative'] = [
'{{%reset_autoincrement_pk}}',
'{{%T_reset_autoincrement_pk}}',
[
['name' => 'name1'],
['name' => 'name2'],
Expand Down
8 changes: 4 additions & 4 deletions tests/framework/db/mssql/querybuilder/BatchInserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/**
* @group db
* @group mssql
* @group querybuilder
* @group batchinsert
* @group query-builder
* @group batch-insert
*/
final class BatchInserTest extends \yiiunit\framework\db\querybuilder\AbstractBatchInsert
{
Expand All @@ -24,8 +24,8 @@ protected function setUp(): void
/**
* @dataProvider \yiiunit\framework\db\mssql\provider\QueryBuilderProvider::batchInsert
*/
public function testBatchInsert(string $table, array $columns, iterable $rows, string $expected): void
public function testBatchInsert(string $tableName, array $columns, iterable $rows, string $expected): void
{
parent::testBatchInsert($table, $columns, $rows, $expected);
parent::testBatchInsert($tableName, $columns, $rows, $expected);
}
}
2 changes: 1 addition & 1 deletion tests/framework/db/mssql/querybuilder/BuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @group db
* @group mssql
* @group querybuilder
* @group query-builder
* @group build
*/
final class BuildTest extends \yiiunit\TestCase
Expand Down
6 changes: 3 additions & 3 deletions tests/framework/db/mssql/querybuilder/CreateSequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @group db
* @group mssql
* @group querybuilder
* @group query-builder
* @group create-sequence
*/
final class CreateSequenceTest extends \yiiunit\framework\db\querybuilder\AbstractCreateSequence
Expand All @@ -25,12 +25,12 @@ public function setup(): void
* @dataProvider \yiiunit\framework\db\mssql\provider\QueryBuilderProvider::createSequence
*/
public function testGenerateSQL(
string $sequenceName,
string $sequence,
int $start,
int $increment,
array $options,
string $expectedSQL
): void {
parent::testGenerateSQL($sequenceName, $start, $increment, $options, $expectedSQL);
parent::testGenerateSQL($sequence, $start, $increment, $options, $expectedSQL);
}
}
Loading

0 comments on commit 870f336

Please sign in to comment.