Skip to content

Clean code in tests, refactor for PHPUNIT v.10. #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Yii Core version 2 Change Log
- Enh #109: Clean tests for `PHPUnit` v.10 (@terabytesoftw)
- Enh #110: Update database versions in build workflows `MariaDb 10.6`, `MySQL 8.0`, `Oracle 12c`, `PostgreSQL 12` (@terabytesoftw)
- Enh #111: Refactor `ColumnSchema::class` in `MSSQL`, `MySQL`, `Oracle`, `PostgreSQL` (@terabytesoftw)
- Enh #112: Clean code in tests, refactor for `PHPUNIT` v.10 (@terabytesoftw)

Yii Framework 2 Change Log
==========================
Expand Down
28 changes: 10 additions & 18 deletions tests/framework/db/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,14 +800,10 @@ public function testAddDropUnique()
$this->assertEquals(['int1', 'int2'], $schema->getTableUniques($tableName, true)[0]->columnNames);
}

public function testAddDropCheck()
public function testAddDropCheck(): void
{
$db = $this->getConnection(false);

if (version_compare($db->getServerVersion(), '8.0.16', '<')) {
$this->markTestSkipped('MySQL < 8.0.16 does not support CHECK constraints.');
}

$tableName = 'test_ck';
$name = 'test_ck_constraint';
$schema = $db->getSchema();
Expand Down Expand Up @@ -1129,22 +1125,18 @@ public function testBindValuesSupportsDeprecatedPDOCastingFormat()
$this->assertTrue(true);
}

public function testBindValuesSupportsEnums()
public function testBindValuesSupportsEnums(): void
{
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
$db = $this->getConnection();
$command = $db->createCommand();
$db = $this->getConnection();
$command = $db->createCommand();

$command->setSql('SELECT :p1')->bindValues([':p1' => enums\Status::ACTIVE]);
$this->assertSame('ACTIVE', $command->params[':p1']);
$command->setSql('SELECT :p1')->bindValues([':p1' => enums\Status::ACTIVE]);
$this->assertSame('ACTIVE', $command->params[':p1']);

$command->setSql('SELECT :p1')->bindValues([':p1' => enums\StatusTypeString::ACTIVE]);
$this->assertSame('active', $command->params[':p1']);
$command->setSql('SELECT :p1')->bindValues([':p1' => enums\StatusTypeString::ACTIVE]);
$this->assertSame('active', $command->params[':p1']);

$command->setSql('SELECT :p1')->bindValues([':p1' => enums\StatusTypeInt::ACTIVE]);
$this->assertSame(1, $command->params[':p1']);
} else {
$this->markTestSkipped('Enums are not supported in PHP < 8.1');
}
$command->setSql('SELECT :p1')->bindValues([':p1' => enums\StatusTypeInt::ACTIVE]);
$this->assertSame(1, $command->params[':p1']);
}
}
6 changes: 1 addition & 5 deletions tests/framework/db/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public function testGetSchemaNames()

$schemas = $schema->getSchemaNames();

if ($db->driverName === 'oci' && version_compare($db->serverVersion, '12.1', '<')) {
$this->assertEmpty($schemas);
} else {
$this->assertNotEmpty($schemas);
}
$this->assertNotEmpty($schemas);

foreach ($this->expectedSchemas as $schema) {
$this->assertContains($schema, $schemas);
Expand Down
9 changes: 1 addition & 8 deletions tests/framework/db/mysql/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,8 @@ public function testCastValues()
//$this->assertSame(false, $model->bool_col2);
}

public function testJsonColumn()
public function testJsonColumn(): void
{
if (version_compare($this->getConnection()->getSchema()->getServerVersion(), '5.7', '<')) {
$this->markTestSkipped('JSON columns are not supported in MySQL < 5.7');
}
if (version_compare(PHP_VERSION, '5.6', '<')) {
$this->markTestSkipped('JSON columns are not supported in PDO for PHP < 5.6');
}

$data = [
'obj' => ['a' => ['b' => ['c' => 2.7418]]],
'array' => [1,2,null,3],
Expand Down
9 changes: 1 addition & 8 deletions tests/framework/db/mysql/BaseActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@ class BaseActiveRecordTest extends \yiiunit\framework\db\BaseActiveRecordTest
*
* @dataProvider provideArrayValueWithChange
*/
public function testJsonDirtyAttributesWithDataChange($actual, $modified)
public function testJsonDirtyAttributesWithDataChange($actual, $modified): void
{
if (version_compare($this->getConnection()->getSchema()->getServerVersion(), '5.7', '<')) {
$this->markTestSkipped('JSON columns are not supported in MySQL < 5.7');
}
if (version_compare(PHP_VERSION, '5.6', '<')) {
$this->markTestSkipped('JSON columns are not supported in PDO for PHP < 5.6');
}

$createdStorage = new Storage(['data' => $actual]);

$createdStorage->save();
Expand Down
6 changes: 1 addition & 5 deletions tests/framework/db/mysql/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ class CommandTest extends \yiiunit\framework\db\CommandTest
{
public $driverName = 'mysql';

public function testAddDropCheckSeveral()
public function testAddDropCheckSeveral(): void
{
$db = $this->getConnection(false);

if (version_compare($db->getServerVersion(), '8.0.16', '<')) {
$this->markTestSkipped('MySQL < 8.0.16 does not support CHECK constraints.');
}

$tableName = 'test_ck_several';
$schema = $db->getSchema();

Expand Down
41 changes: 13 additions & 28 deletions tests/framework/db/mysql/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,7 @@ public function columnTypes()
],
];

/*
* TODO Remove in Yii 2.1
*
* Disabled due bug in MySQL extension
* @link https://bugs.php.net/bug.php?id=70384
*/
if (version_compare(PHP_VERSION, '5.6', '>=')) {
$columns[] = [
Schema::TYPE_JSON,
$this->json(),
"json",
];
}
$columns[] = [Schema::TYPE_JSON, $this->json(), "json"];

return array_merge(parent::columnTypes(), $this->columnTimeTypes(), $columns);
}
Expand Down Expand Up @@ -125,28 +113,25 @@ public function columnTimeTypes()
/**
* @link https://github.com/yiisoft/yii2/issues/14367
*/
$mysqlVersion = $this->getDb()->getSlavePdo(true)->getAttribute(\PDO::ATTR_SERVER_VERSION);
$supportsFractionalSeconds = version_compare($mysqlVersion,'5.6.4', '>=');
if ($supportsFractionalSeconds) {
$expectedValues = [
'datetime(0) NOT NULL',
'datetime(0)',
'time(0) NOT NULL',
'time(0)',
'timestamp(0) NOT NULL',
'timestamp(0) NULL DEFAULT NULL',
];
$expectedValues = [
'datetime(0) NOT NULL',
'datetime(0)',
'time(0) NOT NULL',
'time(0)',
'timestamp(0) NOT NULL',
'timestamp(0) NULL DEFAULT NULL',
];

foreach ($expectedValues as $index => $expected) {
$columns[$index][2] = $expected;
}
foreach ($expectedValues as $index => $expected) {
$columns[$index][2] = $expected;
}

/**
* @link https://github.com/yiisoft/yii2/issues/14834
*/
$sqlModes = $this->getConnection(false)->createCommand('SELECT @@sql_mode')->queryScalar();
$sqlModes = explode(',', $sqlModes);

if (in_array('NO_ZERO_DATE', $sqlModes, true)) {
$this->markTestIncomplete(
"MySQL doesn't allow the 'TIMESTAMP' column definition when the NO_ZERO_DATE mode enabled. " .
Expand All @@ -156,7 +141,7 @@ public function columnTimeTypes()
$columns[] = [
Schema::TYPE_TIMESTAMP,
$this->timestamp(),
$supportsFractionalSeconds ? 'timestamp(0)' : 'timestamp',
'timestamp(0)',
];
}

Expand Down
84 changes: 24 additions & 60 deletions tests/framework/db/mysql/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@ class SchemaTest extends \yiiunit\framework\db\SchemaTest
{
public $driverName = 'mysql';

public function testLoadDefaultDatetimeColumn()
public function testLoadDefaultDatetimeColumn(): void
{
if (!version_compare($this->getConnection()->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6', '>=')) {
$this->markTestSkipped('Default datetime columns are supported since MySQL 5.6.');
}

$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `datetime_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SQL;
CREATE TABLE IF NOT EXISTS `datetime_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SQL;

$this->getConnection()->createCommand($sql)->execute();

Expand All @@ -44,17 +42,14 @@ public function testLoadDefaultDatetimeColumn()
$this->assertEquals('CURRENT_TIMESTAMP', (string)$dt->defaultValue);
}

public function testDefaultDatetimeColumnWithMicrosecs()
public function testDefaultDatetimeColumnWithMicrosecs(): void
{
if (!version_compare($this->getConnection()->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6.4', '>=')) {
$this->markTestSkipped('CURRENT_TIMESTAMP with microseconds as default column value is supported since MySQL 5.6.4.');
}
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `current_timestamp_test` (
`dt` datetime(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2),
`ts` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SQL;
CREATE TABLE IF NOT EXISTS `current_timestamp_test` (
`dt` datetime(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2),
`ts` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SQL;

$this->getConnection()->createCommand($sql)->execute();

Expand Down Expand Up @@ -94,23 +89,15 @@ public function constraintsProvider()
* @param string $type
* @param mixed $expected
*/
public function testTableSchemaConstraints($tableName, $type, $expected)
public function testTableSchemaConstraints($tableName, $type, $expected): void
{
$version = $this->getConnection(false)->getServerVersion();

if ($expected === false) {
$this->expectException('yii\base\NotSupportedException');
}

$version = $this->getConnection(false)->getServerVersion();

if (
$this->driverName === 'mysql' &&
\stripos($version, 'MariaDb') === false &&
version_compare($version, '8.0.16', '<') &&
$type === 'checks'
) {
$this->expectException('yii\base\NotSupportedException');
} elseif (
$this->driverName === 'mysql' &&
\stripos($version, 'MariaDb') === false &&
version_compare($version, '8.0.16', '>=') &&
$tableName === 'T_constraints_1' &&
Expand All @@ -129,23 +116,15 @@ public function testTableSchemaConstraints($tableName, $type, $expected)
* @param string $type
* @param mixed $expected
*/
public function testTableSchemaConstraintsWithPdoUppercase($tableName, $type, $expected)
public function testTableSchemaConstraintsWithPdoUppercase($tableName, $type, $expected): void
{
$version = $this->getConnection(false)->getServerVersion();

if ($expected === false) {
$this->expectException('yii\base\NotSupportedException');
}

$version = $this->getConnection(false)->getServerVersion();

if (
$this->driverName === 'mysql' &&
\stripos($version, 'MariaDb') === false &&
version_compare($version, '8.0.16', '<') &&
$type === 'checks'
) {
$this->expectException('yii\base\NotSupportedException');
} elseif (
$this->driverName === 'mysql' &&
\stripos($version, 'MariaDb') === false &&
version_compare($version, '8.0.16', '>=') &&
$tableName === 'T_constraints_1' &&
Expand All @@ -166,23 +145,15 @@ public function testTableSchemaConstraintsWithPdoUppercase($tableName, $type, $e
* @param string $type
* @param mixed $expected
*/
public function testTableSchemaConstraintsWithPdoLowercase($tableName, $type, $expected)
public function testTableSchemaConstraintsWithPdoLowercase($tableName, $type, $expected): void
{
$version = $this->getConnection(false)->getServerVersion();

if ($expected === false) {
$this->expectException('yii\base\NotSupportedException');
}

$version = $this->getConnection(false)->getServerVersion();

if (
$this->driverName === 'mysql' &&
\stripos($version, 'MariaDb') === false &&
version_compare($version, '8.0.16', '<') &&
$type === 'checks'
) {
$this->expectException('yii\base\NotSupportedException');
} elseif (
$this->driverName === 'mysql' &&
\stripos($version, 'MariaDb') === false &&
version_compare($version, '8.0.16', '>=') &&
$tableName === 'T_constraints_1' &&
Expand Down Expand Up @@ -356,13 +327,6 @@ public function getExpectedColumns()
$columns['bigint_col']['precision'] = null;
}

if (version_compare($version, '5.7', '<') && \stripos($version, 'MariaDb') === false) {
$columns['int_col3']['phpType'] = 'string';
$columns['json_col']['type'] = 'text';
$columns['json_col']['dbType'] = 'longtext';
$columns['json_col']['phpType'] = 'string';
}

return $columns;
}
}
18 changes: 5 additions & 13 deletions tests/framework/db/mysql/connection/DeadLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,11 @@ public function testDeadlockException(): void
);
}

if (version_compare($this->getConnection()->getSchema()->getServerVersion(), '8.0', '<')) {
$this->assertEquals(
1,
$deadlockHitCount,
"exactly one child must hit deadlock; shared children log:\n" . $logContent
);
} else {
$this->assertEquals(
0,
$deadlockHitCount,
"exactly zero children must hit deadlock; shared children log:\n" . $logContent
);
}
$this->assertEquals(
0,
$deadlockHitCount,
"exactly zero children must hit deadlock; shared children log:\n" . $logContent
);
}

/**
Expand Down
15 changes: 2 additions & 13 deletions tests/framework/db/pgsql/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ public function getExpectedColumns()
$columns['bool_col2']['precision'] = null;
$columns['bool_col2']['scale'] = null;
$columns['bool_col2']['defaultValue'] = true;
if (version_compare($this->getConnection(false)->getServerVersion(), '10', '<')) {
$columns['ts_default']['defaultValue'] = new Expression('now()');
}
$columns['bit_col']['dbType'] = 'bit';
$columns['bit_col']['size'] = 8;
$columns['bit_col']['precision'] = null;
Expand Down Expand Up @@ -207,12 +204,8 @@ public function testBooleanDefaultValues()
$this->assertFalse($table->getColumn('default_false')->defaultValue);
}

public function testGeneratedValues()
public function testGeneratedValues(): void
{
if (version_compare($this->getConnection(false)->getServerVersion(), '12.0', '<')) {
$this->markTestSkipped('PostgreSQL < 12.0 does not support GENERATED AS IDENTITY columns.');
}

$config = $this->database;
unset($config['fixture']);
$this->prepareDatabase($config, realpath(__DIR__.'/../../../data') . '/postgres12.sql');
Expand All @@ -224,12 +217,8 @@ public function testGeneratedValues()
$this->assertTrue($table->getColumn('id_default')->autoIncrement);
}

public function testPartitionedTable()
public function testPartitionedTable(): void
{
if (version_compare($this->getConnection(false)->getServerVersion(), '10.0', '<')) {
$this->markTestSkipped('PostgreSQL < 10.0 does not support PARTITION BY clause.');
}

$config = $this->database;
unset($config['fixture']);
$this->prepareDatabase($config, realpath(__DIR__.'/../../../data') . '/postgres10.sql');
Expand Down
Loading