Skip to content

Commit 4685526

Browse files
Clean code in tests, refactor for PHPUNIT v.10. (#112)
1 parent 137b0a7 commit 4685526

20 files changed

+116
-348
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Yii Core version 2 Change Log
5151
- Enh #109: Clean tests for `PHPUnit` v.10 (@terabytesoftw)
5252
- Enh #110: Update database versions in build workflows `MariaDb 10.6`, `MySQL 8.0`, `Oracle 12c`, `PostgreSQL 12` (@terabytesoftw)
5353
- Enh #111: Refactor `ColumnSchema::class` in `MSSQL`, `MySQL`, `Oracle`, `PostgreSQL` (@terabytesoftw)
54+
- Enh #112: Clean code in tests, refactor for `PHPUNIT` v.10 (@terabytesoftw)
5455

5556
Yii Framework 2 Change Log
5657
==========================

tests/framework/db/CommandTest.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -800,14 +800,10 @@ public function testAddDropUnique()
800800
$this->assertEquals(['int1', 'int2'], $schema->getTableUniques($tableName, true)[0]->columnNames);
801801
}
802802

803-
public function testAddDropCheck()
803+
public function testAddDropCheck(): void
804804
{
805805
$db = $this->getConnection(false);
806806

807-
if (version_compare($db->getServerVersion(), '8.0.16', '<')) {
808-
$this->markTestSkipped('MySQL < 8.0.16 does not support CHECK constraints.');
809-
}
810-
811807
$tableName = 'test_ck';
812808
$name = 'test_ck_constraint';
813809
$schema = $db->getSchema();
@@ -1129,22 +1125,18 @@ public function testBindValuesSupportsDeprecatedPDOCastingFormat()
11291125
$this->assertTrue(true);
11301126
}
11311127

1132-
public function testBindValuesSupportsEnums()
1128+
public function testBindValuesSupportsEnums(): void
11331129
{
1134-
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
1135-
$db = $this->getConnection();
1136-
$command = $db->createCommand();
1130+
$db = $this->getConnection();
1131+
$command = $db->createCommand();
11371132

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

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

1144-
$command->setSql('SELECT :p1')->bindValues([':p1' => enums\StatusTypeInt::ACTIVE]);
1145-
$this->assertSame(1, $command->params[':p1']);
1146-
} else {
1147-
$this->markTestSkipped('Enums are not supported in PHP < 8.1');
1148-
}
1139+
$command->setSql('SELECT :p1')->bindValues([':p1' => enums\StatusTypeInt::ACTIVE]);
1140+
$this->assertSame(1, $command->params[':p1']);
11491141
}
11501142
}

tests/framework/db/SchemaTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ public function testGetSchemaNames()
4343

4444
$schemas = $schema->getSchemaNames();
4545

46-
if ($db->driverName === 'oci' && version_compare($db->serverVersion, '12.1', '<')) {
47-
$this->assertEmpty($schemas);
48-
} else {
49-
$this->assertNotEmpty($schemas);
50-
}
46+
$this->assertNotEmpty($schemas);
5147

5248
foreach ($this->expectedSchemas as $schema) {
5349
$this->assertContains($schema, $schemas);

tests/framework/db/mysql/ActiveRecordTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,8 @@ public function testCastValues()
4747
//$this->assertSame(false, $model->bool_col2);
4848
}
4949

50-
public function testJsonColumn()
50+
public function testJsonColumn(): void
5151
{
52-
if (version_compare($this->getConnection()->getSchema()->getServerVersion(), '5.7', '<')) {
53-
$this->markTestSkipped('JSON columns are not supported in MySQL < 5.7');
54-
}
55-
if (version_compare(PHP_VERSION, '5.6', '<')) {
56-
$this->markTestSkipped('JSON columns are not supported in PDO for PHP < 5.6');
57-
}
58-
5952
$data = [
6053
'obj' => ['a' => ['b' => ['c' => 2.7418]]],
6154
'array' => [1,2,null,3],

tests/framework/db/mysql/BaseActiveRecordTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@ class BaseActiveRecordTest extends \yiiunit\framework\db\BaseActiveRecordTest
1717
*
1818
* @dataProvider provideArrayValueWithChange
1919
*/
20-
public function testJsonDirtyAttributesWithDataChange($actual, $modified)
20+
public function testJsonDirtyAttributesWithDataChange($actual, $modified): void
2121
{
22-
if (version_compare($this->getConnection()->getSchema()->getServerVersion(), '5.7', '<')) {
23-
$this->markTestSkipped('JSON columns are not supported in MySQL < 5.7');
24-
}
25-
if (version_compare(PHP_VERSION, '5.6', '<')) {
26-
$this->markTestSkipped('JSON columns are not supported in PDO for PHP < 5.6');
27-
}
28-
2922
$createdStorage = new Storage(['data' => $actual]);
3023

3124
$createdStorage->save();

tests/framework/db/mysql/CommandTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ class CommandTest extends \yiiunit\framework\db\CommandTest
1515
{
1616
public $driverName = 'mysql';
1717

18-
public function testAddDropCheckSeveral()
18+
public function testAddDropCheckSeveral(): void
1919
{
2020
$db = $this->getConnection(false);
2121

22-
if (version_compare($db->getServerVersion(), '8.0.16', '<')) {
23-
$this->markTestSkipped('MySQL < 8.0.16 does not support CHECK constraints.');
24-
}
25-
2622
$tableName = 'test_ck_several';
2723
$schema = $db->getSchema();
2824

tests/framework/db/mysql/QueryBuilderTest.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,7 @@ public function columnTypes()
7070
],
7171
];
7272

73-
/*
74-
* TODO Remove in Yii 2.1
75-
*
76-
* Disabled due bug in MySQL extension
77-
* @link https://bugs.php.net/bug.php?id=70384
78-
*/
79-
if (version_compare(PHP_VERSION, '5.6', '>=')) {
80-
$columns[] = [
81-
Schema::TYPE_JSON,
82-
$this->json(),
83-
"json",
84-
];
85-
}
73+
$columns[] = [Schema::TYPE_JSON, $this->json(), "json"];
8674

8775
return array_merge(parent::columnTypes(), $this->columnTimeTypes(), $columns);
8876
}
@@ -125,28 +113,25 @@ public function columnTimeTypes()
125113
/**
126114
* @link https://github.com/yiisoft/yii2/issues/14367
127115
*/
128-
$mysqlVersion = $this->getDb()->getSlavePdo(true)->getAttribute(\PDO::ATTR_SERVER_VERSION);
129-
$supportsFractionalSeconds = version_compare($mysqlVersion,'5.6.4', '>=');
130-
if ($supportsFractionalSeconds) {
131-
$expectedValues = [
132-
'datetime(0) NOT NULL',
133-
'datetime(0)',
134-
'time(0) NOT NULL',
135-
'time(0)',
136-
'timestamp(0) NOT NULL',
137-
'timestamp(0) NULL DEFAULT NULL',
138-
];
116+
$expectedValues = [
117+
'datetime(0) NOT NULL',
118+
'datetime(0)',
119+
'time(0) NOT NULL',
120+
'time(0)',
121+
'timestamp(0) NOT NULL',
122+
'timestamp(0) NULL DEFAULT NULL',
123+
];
139124

140-
foreach ($expectedValues as $index => $expected) {
141-
$columns[$index][2] = $expected;
142-
}
125+
foreach ($expectedValues as $index => $expected) {
126+
$columns[$index][2] = $expected;
143127
}
144128

145129
/**
146130
* @link https://github.com/yiisoft/yii2/issues/14834
147131
*/
148132
$sqlModes = $this->getConnection(false)->createCommand('SELECT @@sql_mode')->queryScalar();
149133
$sqlModes = explode(',', $sqlModes);
134+
150135
if (in_array('NO_ZERO_DATE', $sqlModes, true)) {
151136
$this->markTestIncomplete(
152137
"MySQL doesn't allow the 'TIMESTAMP' column definition when the NO_ZERO_DATE mode enabled. " .
@@ -156,7 +141,7 @@ public function columnTimeTypes()
156141
$columns[] = [
157142
Schema::TYPE_TIMESTAMP,
158143
$this->timestamp(),
159-
$supportsFractionalSeconds ? 'timestamp(0)' : 'timestamp',
144+
'timestamp(0)',
160145
];
161146
}
162147

tests/framework/db/mysql/SchemaTest.php

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ class SchemaTest extends \yiiunit\framework\db\SchemaTest
2020
{
2121
public $driverName = 'mysql';
2222

23-
public function testLoadDefaultDatetimeColumn()
23+
public function testLoadDefaultDatetimeColumn(): void
2424
{
25-
if (!version_compare($this->getConnection()->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6', '>=')) {
26-
$this->markTestSkipped('Default datetime columns are supported since MySQL 5.6.');
27-
}
25+
2826
$sql = <<<SQL
29-
CREATE TABLE IF NOT EXISTS `datetime_test` (
30-
`id` int(11) NOT NULL AUTO_INCREMENT,
31-
`dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
32-
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
33-
PRIMARY KEY (`id`)
34-
) ENGINE=InnoDB DEFAULT CHARSET=utf8
35-
SQL;
27+
CREATE TABLE IF NOT EXISTS `datetime_test` (
28+
`id` int(11) NOT NULL AUTO_INCREMENT,
29+
`dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
30+
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
31+
PRIMARY KEY (`id`)
32+
) ENGINE=InnoDB DEFAULT CHARSET=utf8
33+
SQL;
3634

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

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

47-
public function testDefaultDatetimeColumnWithMicrosecs()
45+
public function testDefaultDatetimeColumnWithMicrosecs(): void
4846
{
49-
if (!version_compare($this->getConnection()->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6.4', '>=')) {
50-
$this->markTestSkipped('CURRENT_TIMESTAMP with microseconds as default column value is supported since MySQL 5.6.4.');
51-
}
5247
$sql = <<<SQL
53-
CREATE TABLE IF NOT EXISTS `current_timestamp_test` (
54-
`dt` datetime(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2),
55-
`ts` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
56-
) ENGINE=InnoDB DEFAULT CHARSET=utf8
57-
SQL;
48+
CREATE TABLE IF NOT EXISTS `current_timestamp_test` (
49+
`dt` datetime(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2),
50+
`ts` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
51+
) ENGINE=InnoDB DEFAULT CHARSET=utf8
52+
SQL;
5853

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

@@ -94,23 +89,15 @@ public function constraintsProvider()
9489
* @param string $type
9590
* @param mixed $expected
9691
*/
97-
public function testTableSchemaConstraints($tableName, $type, $expected)
92+
public function testTableSchemaConstraints($tableName, $type, $expected): void
9893
{
99-
$version = $this->getConnection(false)->getServerVersion();
100-
10194
if ($expected === false) {
10295
$this->expectException('yii\base\NotSupportedException');
10396
}
10497

98+
$version = $this->getConnection(false)->getServerVersion();
99+
105100
if (
106-
$this->driverName === 'mysql' &&
107-
\stripos($version, 'MariaDb') === false &&
108-
version_compare($version, '8.0.16', '<') &&
109-
$type === 'checks'
110-
) {
111-
$this->expectException('yii\base\NotSupportedException');
112-
} elseif (
113-
$this->driverName === 'mysql' &&
114101
\stripos($version, 'MariaDb') === false &&
115102
version_compare($version, '8.0.16', '>=') &&
116103
$tableName === 'T_constraints_1' &&
@@ -129,23 +116,15 @@ public function testTableSchemaConstraints($tableName, $type, $expected)
129116
* @param string $type
130117
* @param mixed $expected
131118
*/
132-
public function testTableSchemaConstraintsWithPdoUppercase($tableName, $type, $expected)
119+
public function testTableSchemaConstraintsWithPdoUppercase($tableName, $type, $expected): void
133120
{
134-
$version = $this->getConnection(false)->getServerVersion();
135-
136121
if ($expected === false) {
137122
$this->expectException('yii\base\NotSupportedException');
138123
}
139124

125+
$version = $this->getConnection(false)->getServerVersion();
126+
140127
if (
141-
$this->driverName === 'mysql' &&
142-
\stripos($version, 'MariaDb') === false &&
143-
version_compare($version, '8.0.16', '<') &&
144-
$type === 'checks'
145-
) {
146-
$this->expectException('yii\base\NotSupportedException');
147-
} elseif (
148-
$this->driverName === 'mysql' &&
149128
\stripos($version, 'MariaDb') === false &&
150129
version_compare($version, '8.0.16', '>=') &&
151130
$tableName === 'T_constraints_1' &&
@@ -166,23 +145,15 @@ public function testTableSchemaConstraintsWithPdoUppercase($tableName, $type, $e
166145
* @param string $type
167146
* @param mixed $expected
168147
*/
169-
public function testTableSchemaConstraintsWithPdoLowercase($tableName, $type, $expected)
148+
public function testTableSchemaConstraintsWithPdoLowercase($tableName, $type, $expected): void
170149
{
171-
$version = $this->getConnection(false)->getServerVersion();
172-
173150
if ($expected === false) {
174151
$this->expectException('yii\base\NotSupportedException');
175152
}
176153

154+
$version = $this->getConnection(false)->getServerVersion();
155+
177156
if (
178-
$this->driverName === 'mysql' &&
179-
\stripos($version, 'MariaDb') === false &&
180-
version_compare($version, '8.0.16', '<') &&
181-
$type === 'checks'
182-
) {
183-
$this->expectException('yii\base\NotSupportedException');
184-
} elseif (
185-
$this->driverName === 'mysql' &&
186157
\stripos($version, 'MariaDb') === false &&
187158
version_compare($version, '8.0.16', '>=') &&
188159
$tableName === 'T_constraints_1' &&
@@ -356,13 +327,6 @@ public function getExpectedColumns()
356327
$columns['bigint_col']['precision'] = null;
357328
}
358329

359-
if (version_compare($version, '5.7', '<') && \stripos($version, 'MariaDb') === false) {
360-
$columns['int_col3']['phpType'] = 'string';
361-
$columns['json_col']['type'] = 'text';
362-
$columns['json_col']['dbType'] = 'longtext';
363-
$columns['json_col']['phpType'] = 'string';
364-
}
365-
366330
return $columns;
367331
}
368332
}

tests/framework/db/mysql/connection/DeadLockTest.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,11 @@ public function testDeadlockException(): void
139139
);
140140
}
141141

142-
if (version_compare($this->getConnection()->getSchema()->getServerVersion(), '8.0', '<')) {
143-
$this->assertEquals(
144-
1,
145-
$deadlockHitCount,
146-
"exactly one child must hit deadlock; shared children log:\n" . $logContent
147-
);
148-
} else {
149-
$this->assertEquals(
150-
0,
151-
$deadlockHitCount,
152-
"exactly zero children must hit deadlock; shared children log:\n" . $logContent
153-
);
154-
}
142+
$this->assertEquals(
143+
0,
144+
$deadlockHitCount,
145+
"exactly zero children must hit deadlock; shared children log:\n" . $logContent
146+
);
155147
}
156148

157149
/**

tests/framework/db/pgsql/SchemaTest.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ public function getExpectedColumns()
7676
$columns['bool_col2']['precision'] = null;
7777
$columns['bool_col2']['scale'] = null;
7878
$columns['bool_col2']['defaultValue'] = true;
79-
if (version_compare($this->getConnection(false)->getServerVersion(), '10', '<')) {
80-
$columns['ts_default']['defaultValue'] = new Expression('now()');
81-
}
8279
$columns['bit_col']['dbType'] = 'bit';
8380
$columns['bit_col']['size'] = 8;
8481
$columns['bit_col']['precision'] = null;
@@ -207,12 +204,8 @@ public function testBooleanDefaultValues()
207204
$this->assertFalse($table->getColumn('default_false')->defaultValue);
208205
}
209206

210-
public function testGeneratedValues()
207+
public function testGeneratedValues(): void
211208
{
212-
if (version_compare($this->getConnection(false)->getServerVersion(), '12.0', '<')) {
213-
$this->markTestSkipped('PostgreSQL < 12.0 does not support GENERATED AS IDENTITY columns.');
214-
}
215-
216209
$config = $this->database;
217210
unset($config['fixture']);
218211
$this->prepareDatabase($config, realpath(__DIR__.'/../../../data') . '/postgres12.sql');
@@ -224,12 +217,8 @@ public function testGeneratedValues()
224217
$this->assertTrue($table->getColumn('id_default')->autoIncrement);
225218
}
226219

227-
public function testPartitionedTable()
220+
public function testPartitionedTable(): void
228221
{
229-
if (version_compare($this->getConnection(false)->getServerVersion(), '10.0', '<')) {
230-
$this->markTestSkipped('PostgreSQL < 10.0 does not support PARTITION BY clause.');
231-
}
232-
233222
$config = $this->database;
234223
unset($config['fixture']);
235224
$this->prepareDatabase($config, realpath(__DIR__.'/../../../data') . '/postgres10.sql');

0 commit comments

Comments
 (0)