From c53d8bca6e9ef75ed31ff76433ebc1cdc1edf8f9 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 12:56:52 +0300 Subject: [PATCH 1/8] Refactor tests --- composer.json | 3 +- tests/BatchQueryResultTest.php | 6 +- tests/ColumnBuilderTest.php | 8 +- tests/ColumnFactoryTest.php | 10 +- tests/ColumnTest.php | 16 ++- tests/CommandTest.php | 32 +++-- tests/ConnectionTest.php | 61 +++++--- tests/DeadLockTest.php | 11 +- tests/PdoCommandTest.php | 6 +- tests/PdoConnectionTest.php | 30 ++-- .../{PDODriverTest.php => PdoDriverTest.php} | 21 ++- tests/Provider/CommandProvider.php | 3 - tests/Provider/QueryBuilderProvider.php | 27 ++-- tests/Provider/SchemaProvider.php | 12 +- tests/QueryBuilderTest.php | 109 ++++++++------ tests/QueryGetTableAliasTest.php | 10 +- tests/QueryTest.php | 28 ++-- tests/QuoterTest.php | 10 +- tests/SchemaTest.php | 44 +++--- tests/SqlParserTest.php | 8 +- tests/Support/IntegrationTestTrait.php | 29 ++++ tests/Support/TestConnection.php | 95 +++++++++++++ tests/Support/TestTrait.php | 133 ------------------ 23 files changed, 354 insertions(+), 358 deletions(-) rename tests/{PDODriverTest.php => PdoDriverTest.php} (68%) create mode 100644 tests/Support/IntegrationTestTrait.php create mode 100644 tests/Support/TestConnection.php delete mode 100644 tests/Support/TestTrait.php diff --git a/composer.json b/composer.json index b64b54f3c..77963fcef 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,8 @@ "vlucas/phpdotenv": "^5.6.1", "yiisoft/aliases": "^2.0", "yiisoft/log-target-file": "^2.0.1", - "yiisoft/cache-file": "^3.2", + "yiisoft/psr-dummy-provider": "^1.0", + "yiisoft/test-support": "^3.0", "yiisoft/var-dumper": "^1.7" }, "provide": { diff --git a/tests/BatchQueryResultTest.php b/tests/BatchQueryResultTest.php index adddfc63c..24792e2d4 100644 --- a/tests/BatchQueryResultTest.php +++ b/tests/BatchQueryResultTest.php @@ -4,15 +4,13 @@ namespace Yiisoft\Db\Mysql\Tests; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonBatchQueryResultTest; /** * @group mysql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class BatchQueryResultTest extends CommonBatchQueryResultTest { - use TestTrait; + use IntegrationTestTrait; } diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php index 3f6d7182e..42dd64123 100644 --- a/tests/ColumnBuilderTest.php +++ b/tests/ColumnBuilderTest.php @@ -6,15 +6,15 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Mysql\Tests\Provider\ColumnBuilderProvider; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\AbstractColumnBuilderTest; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Common\CommonColumnBuilderTest; /** * @group mysql */ -class ColumnBuilderTest extends AbstractColumnBuilderTest +class ColumnBuilderTest extends CommonColumnBuilderTest { - use TestTrait; + use IntegrationTestTrait; #[DataProviderExternal(ColumnBuilderProvider::class, 'buildingMethods')] public function testBuildingMethods( diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 824478ad0..51af2f56f 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -9,16 +9,16 @@ use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Mysql\Column\ColumnFactory; use Yiisoft\Db\Mysql\Tests\Provider\ColumnFactoryProvider; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Schema\Column\ColumnInterface; -use Yiisoft\Db\Tests\AbstractColumnFactoryTest; +use Yiisoft\Db\Tests\Common\CommonColumnFactoryTest; /** * @group mysql */ -final class ColumnFactoryTest extends AbstractColumnFactoryTest +final class ColumnFactoryTest extends CommonColumnFactoryTest { - use TestTrait; + use IntegrationTestTrait; #[DataProviderExternal(ColumnFactoryProvider::class, 'dbTypes')] public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void @@ -52,7 +52,7 @@ public function testFromTypeDefaultValueRaw(string $type, ?string $defaultValueR public function testExpressionDefaultValueRaw(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $columnFactory = $db->getColumnFactory(); $column = $columnFactory->fromType(ColumnType::DATETIME, ['defaultValueRaw' => 'now()', 'extra' => 'DEFAULT_GENERATED']); diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index 8d8f4b2f1..946737e01 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -12,7 +12,7 @@ use Yiisoft\Db\Mysql\Column\ColumnBuilder; use Yiisoft\Db\Mysql\Column\StringColumn; use Yiisoft\Db\Mysql\Tests\Provider\ColumnProvider; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\BinaryColumn; use Yiisoft\Db\Schema\Column\BooleanColumn; @@ -30,11 +30,11 @@ */ final class ColumnTest extends CommonColumnTest { - use TestTrait; + use IntegrationTestTrait; public function testSelectWithPhpTypecasting(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $sql = <<getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command->insert('negative_default_values', ['bigint_col' => $bigint]); @@ -105,7 +106,8 @@ public function testColumnBigInt(string $bigint): void public function testColumnInstance(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $tableSchema = $schema->getTableSchema('type'); @@ -121,7 +123,7 @@ public function testColumnInstance(): void public function testLongtextType(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); try { @@ -151,7 +153,7 @@ public function testLongtextType(): void public function testTimestampColumnOnDifferentTimezones(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); $schema = $db->getSchema(); $command = $db->createCommand(); $tableName = 'timestamp_column_test'; diff --git a/tests/CommandTest.php b/tests/CommandTest.php index f3da1fbde..b42288f70 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -4,24 +4,22 @@ namespace Yiisoft\Db\Mysql\Tests; +use Closure; use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Mysql\Tests\Provider\CommandProvider; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Mysql\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Common\CommonCommandTest; /** * @group mysql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class CommandTest extends CommonCommandTest { - use TestTrait; - - protected string $upsertTestCharCast = 'CONVERT([[address]], CHAR)'; + use IntegrationTestTrait; public function testAddCheck(): void { @@ -41,7 +39,8 @@ public function testAddDefaultValue(): void public function testAlterColumn(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command->alterColumn('{{customer}}', 'email', 'text')->execute(); @@ -84,7 +83,7 @@ public function testDropDefaultValue(): void public function testDropTableCascade(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $this->expectException(NotSupportedException::class); @@ -102,7 +101,9 @@ public function testGetRawSql(string $sql, array $params, string $expectedRawSql public function testInsertReturningPksWithSubqueryAndNoAutoincrement(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $command = $db->createCommand(); $query = (new Query($db))->select(['order_id' => 1, 'item_id' => 2, 'quantity' => 3, 'subtotal' => 4]); @@ -120,7 +121,7 @@ public function testUpdate( string $table, array $columns, array|ExpressionInterface|string $conditions, - array|ExpressionInterface|string|null $from, + Closure|array|ExpressionInterface|string|null $from, array $params, array $expectedValues, int $expectedCount, @@ -129,14 +130,16 @@ public function testUpdate( } #[DataProviderExternal(CommandProvider::class, 'upsert')] - public function testUpsert(array $firstData, array $secondData): void + public function testUpsert(Closure|array $firstData, Closure|array $secondData): void { parent::testUpsert($firstData, $secondData); } public function testShowDatabases(): void { - $this->assertSame([self::getDatabaseName()], self::getDb()->createCommand()->showDatabases()); + $db = $this->getSharedConnection(); + + $this->assertSame([TestConnection::databaseName()], $db->createCommand()->showDatabases()); } #[DataProviderExternal(CommandProvider::class, 'createIndex')] @@ -144,4 +147,9 @@ public function testCreateIndex(array $columns, array $indexColumns, ?string $in { parent::testCreateIndex($columns, $indexColumns, $indexType, $indexMethod); } + + protected function getUpsertTestCharCast(): string + { + return 'CONVERT([[address]], CHAR)'; + } } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index d9b467afc..50d7c40e5 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -5,14 +5,16 @@ namespace Yiisoft\Db\Mysql\Tests; use PDO; +use Throwable; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\IntegrityException; use Yiisoft\Db\Mysql\Column\ColumnBuilder; use Yiisoft\Db\Mysql\Column\ColumnFactory; use Yiisoft\Db\Mysql\Connection; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Mysql\Tests\Support\TestConnection; use Yiisoft\Db\Tests\Common\CommonConnectionTest; -use Yiisoft\Db\Tests\Support\DbHelper; +use Yiisoft\Db\Tests\Support\TestHelper; use Yiisoft\Db\Transaction\TransactionInterface; /** @@ -20,11 +22,11 @@ */ final class ConnectionTest extends CommonConnectionTest { - use TestTrait; + use IntegrationTestTrait; public function testInitConnection(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); $db->setEmulatePrepare(true); $db->open(); @@ -40,28 +42,29 @@ public function testInitConnection(): void public function testSettingDefaultAttributes(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); - $this->assertSame(PDO::ERRMODE_EXCEPTION, $db->getActivePDO()?->getAttribute(PDO::ATTR_ERRMODE)); + $this->assertSame(PDO::ERRMODE_EXCEPTION, $db->getActivePdo()?->getAttribute(PDO::ATTR_ERRMODE)); $db->close(); $db->setEmulatePrepare(true); $db->open(); - $this->assertEquals(true, $db->getActivePDO()?->getAttribute(PDO::ATTR_EMULATE_PREPARES)); + $this->assertEquals(true, $db->getActivePdo()?->getAttribute(PDO::ATTR_EMULATE_PREPARES)); $db->close(); $db->setEmulatePrepare(false); $db->open(); - $this->assertEquals(false, $db->getActivePDO()?->getAttribute(PDO::ATTR_EMULATE_PREPARES)); + $this->assertEquals(false, $db->getActivePdo()?->getAttribute(PDO::ATTR_EMULATE_PREPARES)); $db->close(); } public function testTransactionIsolation(): void { - $db = $this->getConnection(true); + $db = $this->createConnection(); + $this->loadFixture(db: $db); $transaction = $db->beginTransaction(TransactionInterface::READ_UNCOMMITTED); $transaction->commit(); @@ -83,7 +86,8 @@ public function testTransactionIsolation(): void public function testTransactionShortcutCustom(): void { - $db = $this->getConnection(true); + $db = $this->createConnection(); + $this->loadFixture(db: $db); $result = $db->transaction( static function (ConnectionInterface $db) { @@ -105,10 +109,12 @@ static function (ConnectionInterface $db) { $db->close(); } - /** @link https://github.com/yiisoft/db-mysql/issues/348 */ + /** + * @link https://github.com/yiisoft/db-mysql/issues/348 + */ public function testRestartConnectionOnTimeout(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); $db->createCommand('SET SESSION wait_timeout = 1')->execute(); @@ -123,44 +129,53 @@ public function testRestartConnectionOnTimeout(): void public function testNotRestartConnectionOnTimeoutInTransaction(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); $db->beginTransaction(); $db->createCommand('SET SESSION wait_timeout = 1')->execute(); sleep(2); - $this->expectException(IntegrityException::class); - $this->expectExceptionMessageMatches('/SQLSTATE\[HY000\]: General error: (?:2006|4031) /'); + $command = $db->createCommand('SELECT 1'); - $db->createCommand('SELECT 1')->queryScalar(); + $exception = null; + try { + $command->queryScalar(); + } catch (Throwable $exception) { + } + + $this->assertInstanceOf(IntegrityException::class, $exception); + $this->assertMatchesRegularExpression( + '/SQLSTATE\[HY000\]: General error: (?:2006|4031) /', + $exception->getMessage(), + ); $db->close(); } public function getColumnBuilderClass(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); - - $db->close(); } public function testGetColumnFactory(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory()); - - $db->close(); } public function testUserDefinedColumnFactory(): void { $columnFactory = new ColumnFactory(); - $db = new Connection($this->getDriver(), DbHelper::getSchemaCache(), $columnFactory); + $db = new Connection( + TestConnection::createDriver(), + TestHelper::createMemorySchemaCache(), + $columnFactory, + ); $this->assertSame($columnFactory, $db->getColumnFactory()); diff --git a/tests/DeadLockTest.php b/tests/DeadLockTest.php index e67627bc9..da93a6fcd 100644 --- a/tests/DeadLockTest.php +++ b/tests/DeadLockTest.php @@ -10,7 +10,6 @@ use Throwable; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; use Yiisoft\Db\Transaction\TransactionInterface; use function date; @@ -37,13 +36,9 @@ /** * @group mysql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class DeadLockTest extends TestCase { - use TestTrait; - private const CHILD_EXIT_CODE_DEADLOCK = 15; private string $logFile = ''; @@ -52,8 +47,8 @@ final class DeadLockTest extends TestCase * * Accident deadlock exception lost while rolling back a transaction or savepoint * - * {@link https://github.com/yiisoft/yii2/issues/12715} - * {@link https://github.com/yiisoft/yii2/pull/13346} + * @link https://github.com/yiisoft/yii2/issues/12715 + * @link https://github.com/yiisoft/yii2/pull/13346 */ public function testDeadlockException(): void { @@ -341,8 +336,6 @@ private function childrenUpdateLocked(): int * In case of error in child process its execution bubbles up to phpunit to continue all the rest tests. So, all * the rest tests in this case will run both in the child and parent processes. Such mess must be prevented with * child's own error handler. - * - * @throws ErrorException */ private function setErrorHandler(): void { diff --git a/tests/PdoCommandTest.php b/tests/PdoCommandTest.php index c62eccd72..c638ad690 100644 --- a/tests/PdoCommandTest.php +++ b/tests/PdoCommandTest.php @@ -4,15 +4,13 @@ namespace Yiisoft\Db\Mysql\Tests; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonPdoCommandTest; /** * @group mysql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class PdoCommandTest extends CommonPdoCommandTest { - use TestTrait; + use IntegrationTestTrait; } diff --git a/tests/PdoConnectionTest.php b/tests/PdoConnectionTest.php index 915c7f18e..59f037726 100644 --- a/tests/PdoConnectionTest.php +++ b/tests/PdoConnectionTest.php @@ -4,33 +4,21 @@ namespace Yiisoft\Db\Mysql\Tests; -use Throwable; use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; -use Yiisoft\Db\Exception\Exception; -use Yiisoft\Db\Exception\InvalidCallException; -use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Mysql\ServerInfo; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonPdoConnectionTest; /** * @group mysql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class PdoConnectionTest extends CommonPdoConnectionTest { - use TestTrait; - - /** - * @throws Exception - * @throws InvalidConfigException - * @throws InvalidCallException - * @throws Throwable - */ - public function testGetLastInsertID(): void + use IntegrationTestTrait; + + public function testGetLastInsertId(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $tableName = 'test'; @@ -96,9 +84,11 @@ public function testGetLastInsertID(): void $db->close(); } - public function testTransactionAutocommit() + public function testTransactionAutocommit(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $db->transaction(function (PdoConnectionInterface $db) { $this->assertTrue($db->getTransaction()->isActive()); @@ -115,7 +105,7 @@ public function testTransactionAutocommit() public function testGetServerInfo(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); $serverInfo = $db->getServerInfo(); $this->assertInstanceOf(ServerInfo::class, $serverInfo); diff --git a/tests/PDODriverTest.php b/tests/PdoDriverTest.php similarity index 68% rename from tests/PDODriverTest.php rename to tests/PdoDriverTest.php index e3a16ee61..670a71490 100644 --- a/tests/PDODriverTest.php +++ b/tests/PdoDriverTest.php @@ -5,41 +5,38 @@ namespace Yiisoft\Db\Mysql\Tests; use PDO; -use PHPUnit\Framework\TestCase; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Mysql\Tests\Support\TestConnection; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; /** * @group mysql - * - * @psalm-suppress PropertyNotSetInConstructor */ -final class PDODriverTest extends TestCase +final class PdoDriverTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public function testConnectionCharset(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); - $pdo = $db->getActivePDO(); + $pdo = $db->getActivePdo(); $charset = $pdo->query('SHOW VARIABLES LIKE \'character_set_client\'', PDO::FETCH_ASSOC)->fetch(); $this->assertEqualsIgnoringCase('utf8mb4', array_values($charset)[1]); - $pdoDriver = $this->getDriver(); + $pdoDriver = TestConnection::createDriver(); $newCharset = 'latin1'; $pdoDriver->charset($newCharset); $pdo = $pdoDriver->createConnection(); $charset = $pdo->query('SHOW VARIABLES LIKE \'character_set_client\'', PDO::FETCH_ASSOC)->fetch(); $this->assertEqualsIgnoringCase($newCharset, array_values($charset)[1]); - - $db->close(); } public function testCharsetDefault(): void { - $db = self::getDb(); + $db = $this->getSharedConnection(); $db->open(); $command = $db->createCommand(); diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index be148b62a..f4fd6debe 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -7,12 +7,9 @@ use Yiisoft\Db\Mysql\Column\ColumnBuilder; use Yiisoft\Db\Mysql\IndexMethod; use Yiisoft\Db\Mysql\IndexType; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; final class CommandProvider extends \Yiisoft\Db\Tests\Provider\CommandProvider { - use TestTrait; - public static function createIndex(): array { return [ diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 5cd705f08..c077d1947 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Mysql\Tests\Provider; +use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Constant\PseudoType; @@ -12,7 +13,8 @@ use Yiisoft\Db\Expression\Function\ArrayMerge; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Mysql\Column\ColumnBuilder; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; + +use Yiisoft\Db\Mysql\Tests\Support\TestConnection; use function array_replace; use function str_contains; @@ -20,10 +22,6 @@ final class QueryBuilderProvider extends \Yiisoft\Db\Tests\Provider\QueryBuilderProvider { - use TestTrait; - - protected static string $driverName = 'mysql'; - public static function alterColumn(): array { return [ @@ -140,7 +138,7 @@ public static function upsert(): array public static function upsertReturning(): array { $upsert = self::upsert(); - $quoter = self::getDb()->getQuoter(); + $quoter = TestConnection::getShared()->getQuoter(); foreach ($upsert as &$data) { array_splice($data, 3, 0, [['id']]); @@ -301,9 +299,7 @@ public static function buildColumnDefinition(): array $values[] = ["enum('a','b','c')", ColumnBuilder::string()->dbType("enum('a','b','c')")]; - $db = self::getDb(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); + $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); if (!str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '8', '<') @@ -350,7 +346,7 @@ public static function multiOperandFunctionClasses(): array public static function multiOperandFunctionBuilder(): array { - $data = parent::multiOperandFunctionBuilder(); + $data = iterator_to_array(parent::multiOperandFunctionBuilder()); $stringParam = new Param('[3,4,5]', DataType::STRING); @@ -359,9 +355,7 @@ public static function multiOperandFunctionBuilder(): array $data['Shortest with 2 operands'][2] = '(SELECT :qp0 AS value UNION SELECT :qp1 AS value ORDER BY LENGTH(value) ASC LIMIT 1)'; $data['Shortest with 3 operands'][2] = "(SELECT :qp0 AS value UNION SELECT (SELECT 'longest') AS value UNION SELECT :qp1 AS value ORDER BY LENGTH(value) ASC LIMIT 1)"; - $db = self::getDb(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); + $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); $isMariadb = str_contains($serverVersion, 'MariaDB'); @@ -398,7 +392,12 @@ public static function multiOperandFunctionBuilder(): array // MySQL does not support query parameters in JSON_TABLE() function. $data['ArrayMerge with 4 operands'] = [ ArrayMerge::class, - [[1, 2, 3], new ArrayValue([5, 6, 7]), $stringParam, self::getDb()->select(new ArrayValue([9, 10]))], + static fn(ConnectionInterface $db) => [ + [1, 2, 3], + new ArrayValue([5, 6, 7]), + $stringParam, + $db->select(new ArrayValue([9, 10])), + ], '(SELECT JSON_ARRAYAGG(value) AS value FROM (' . "SELECT value FROM JSON_TABLE(:qp0, '$[*]' COLUMNS(value json PATH '$')) AS t" . " UNION SELECT value FROM JSON_TABLE(:qp1, '$[*]' COLUMNS(value json PATH '$')) AS t" diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index d7d57b48b..3c7f3c4a5 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -10,7 +10,7 @@ use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Mysql\Column\DateTimeColumn; use Yiisoft\Db\Mysql\Column\StringColumn; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\TestConnection; use Yiisoft\Db\Schema\Column\BigIntColumn; use Yiisoft\Db\Schema\Column\BinaryColumn; use Yiisoft\Db\Schema\Column\BitColumn; @@ -22,13 +22,9 @@ final class SchemaProvider extends \Yiisoft\Db\Tests\Provider\SchemaProvider { - use TestTrait; - public static function columns(): array { - $db = self::getDb(); - $dbTimezone = self::getDb()->getServerInfo()->getTimezone(); - $db->close(); + $dbTimezone = TestConnection::getShared()->getServerInfo()->getTimezone(); return [ [ @@ -284,9 +280,7 @@ public static function constraints(): array public static function resultColumns(): array { - $db = self::getDb(); - $dbTimezone = self::getDb()->getServerInfo()->getTimezone(); - $db->close(); + $dbTimezone = TestConnection::getShared()->getServerInfo()->getTimezone(); return [ [null, []], diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index f488e7044..5e0b6eb5a 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Mysql\Tests; +use Closure; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProviderExternal; use PHPUnit\Framework\Attributes\TestWith; @@ -16,7 +17,7 @@ use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Mysql\Tests\Provider\QueryBuilderProvider; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\Condition\JsonOverlaps; @@ -34,16 +35,16 @@ */ final class QueryBuilderTest extends CommonQueryBuilderTest { - use TestTrait; + use IntegrationTestTrait; public function getBuildColumnDefinitionProvider(): array { return QueryBuilderProvider::buildColumnDefinition(); } - public function testAddcheck(): void + public function testAddCheck(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -57,7 +58,8 @@ public function testAddcheck(): void public function testAddCommentOnColumn(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $serverVersion = $db->getServerInfo()->getVersion(); $qb = $db->getQueryBuilder(); @@ -82,7 +84,7 @@ public function testAddCommentOnColumn(): void public function testAddCommentOnTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $sql = $qb->addCommentOnTable('customer', 'Customer table.'); @@ -99,7 +101,7 @@ public function testAddCommentOnTable(): void public function testAddDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -156,7 +158,7 @@ public function testBatchInsert( #[DataProviderExternal(QueryBuilderProvider::class, 'buildCondition')] public function testBuildCondition( - array|ExpressionInterface|string $condition, + Closure|array|ExpressionInterface|string $condition, ?string $expected, array $expectedParams, ): void { @@ -180,7 +182,7 @@ public function testBuildWithFrom(mixed $table, string $expectedSql, array $expe public function testBuildWithOffset(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $query = (new Query($db))->offset(10); @@ -207,7 +209,7 @@ public function testBuildWithWhereExists(string $cond, string $expectedQuerySql) public function testCheckIntegrity(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -223,7 +225,7 @@ public function testCheckIntegrity(): void public function testCreateTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -260,7 +262,7 @@ public function testDelete(string $table, array|string $condition, string $expec public function testDropCheck(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -277,7 +279,7 @@ public function testDropCheck(): void */ public function testDefaultValues(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -302,7 +304,8 @@ public function testDefaultValues(): void public function testDropCommentFromColumn(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $serverVersion = $db->getServerInfo()->getVersion(); $qb = $db->getQueryBuilder(); @@ -327,7 +330,7 @@ public function testDropCommentFromColumn(): void public function testDropCommentFromTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -343,7 +346,8 @@ public function testDropCommentFromTable(): void public function testDropDefaultValue(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $qb = $db->getQueryBuilder(); @@ -357,7 +361,7 @@ public function testDropDefaultValue(): void public function testDropForeignKey(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -373,7 +377,7 @@ public function testDropForeignKey(): void public function testDropPrimaryKey(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -389,7 +393,7 @@ public function testDropPrimaryKey(): void public function testDropUnique(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -406,7 +410,7 @@ public function testDropUnique(): void #[DataProviderExternal(QueryBuilderProvider::class, 'insert')] public function testInsert( string $table, - array|QueryInterface $columns, + Closure|array|QueryInterface $columns, array $params, string $expectedSQL, array $expectedParams, @@ -417,12 +421,12 @@ public function testInsert( #[DataProviderExternal(QueryBuilderProvider::class, 'insertReturningPks')] public function testInsertReturningPks( string $table, - array|QueryInterface $columns, + Closure|array|QueryInterface $columns, array $params, string $expectedSQL, array $expectedParams, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $this->expectException(NotSupportedException::class); @@ -440,7 +444,7 @@ public function testInsertReturningPks( */ public function testInsertInteger() { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); @@ -477,7 +481,7 @@ public function testInsertInteger() public function testRenameColumn(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $this->assertSame( @@ -506,7 +510,8 @@ public function testRenameColumn(): void public function testResetSequence(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $qb = $db->getQueryBuilder(); @@ -570,7 +575,7 @@ public function testUpdate( string $table, array $columns, array|ExpressionInterface|string $condition, - array|ExpressionInterface|string|null $from, + Closure|array|ExpressionInterface|string|null $from, array $params, string $expectedSql, array $expectedParams = [], @@ -581,7 +586,7 @@ public function testUpdate( #[DataProviderExternal(QueryBuilderProvider::class, 'upsert')] public function testUpsert( string $table, - array|QueryInterface $insertColumns, + Closure|array|QueryInterface $insertColumns, array|bool $updateColumns, string $expectedSql, array $expectedParams, @@ -592,7 +597,7 @@ public function testUpsert( #[DataProviderExternal(QueryBuilderProvider::class, 'upsertReturning')] public function testUpsertReturning( string $table, - array|QueryInterface $insertColumns, + Closure|array|QueryInterface $insertColumns, array|bool $updateColumns, ?array $returnColumns, string $expectedSql, @@ -608,7 +613,9 @@ public function testUpsertReturningWithUpdatingPrimaryKeyOrUnique( array $insertColumns, array $updateColumns, ): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $qb = $db->getQueryBuilder(); $this->expectException(NotSupportedException::class); @@ -621,7 +628,9 @@ public function testUpsertReturningWithUpdatingPrimaryKeyOrUnique( public function testUpsertReturningWithSameUpdatingPrimaryKeyOrUnique(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $qb = $db->getQueryBuilder(); $params = []; @@ -651,7 +660,8 @@ public function testUpsertReturningWithNullPrimaryKeyOrUnique( array $insertColumns, array $updateColumns, ): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $qb = $db->getQueryBuilder(); $this->expectException(NotSupportedException::class); @@ -664,7 +674,8 @@ public function testUpsertReturningWithNullPrimaryKeyOrUnique( public function testUpsertReturningWithSubqueryAndNoAutoincrement(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $qb = $db->getQueryBuilder(); $query = (new Query($db))->select(['order_id' => 1, 'item_id' => 2, 'quantity' => 3, 'subtotal' => 4]); @@ -685,7 +696,7 @@ public function testSelectScalar(array|bool|float|int|string $columns, string $e public function testJsonOverlapsBuilder(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $serverVersion = $db->getServerInfo()->getVersion(); if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) { @@ -713,11 +724,15 @@ public function testJsonOverlapsBuilder(): void } #[DataProviderExternal(QueryBuilderProvider::class, 'overlapsCondition')] - public function testJsonOverlaps(iterable|ExpressionInterface $values, int $expectedCount): void + public function testJsonOverlaps(Closure|iterable|ExpressionInterface $values, int $expectedCount): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $serverVersion = $db->getServerInfo()->getVersion(); + if ($values instanceof Closure) { + $values = $values($db); + } + if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) { $db->close(); self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.'); @@ -737,11 +752,15 @@ public function testJsonOverlaps(iterable|ExpressionInterface $values, int $expe } #[DataProviderExternal(QueryBuilderProvider::class, 'overlapsCondition')] - public function testJsonOverlapsOperator(iterable|ExpressionInterface $values, int $expectedCount): void + public function testJsonOverlapsOperator(Closure|iterable|ExpressionInterface $values, int $expectedCount): void { - $db = $this->getConnection(); - $serverVersion = $db->getServerInfo()->getVersion(); + $db = $this->getSharedConnection(); + + if ($values instanceof Closure) { + $values = $values($db); + } + $serverVersion = $db->getServerInfo()->getVersion(); if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) { $db->close(); self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.'); @@ -761,7 +780,7 @@ public function testJsonOverlapsOperator(iterable|ExpressionInterface $values, i } #[DataProviderExternal(QueryBuilderProvider::class, 'buildColumnDefinition')] - public function testBuildColumnDefinition(string $expected, ColumnInterface|string $column): void + public function testBuildColumnDefinition(string $expected, Closure|ColumnInterface|string $column): void { parent::testBuildColumnDefinition($expected, $column); } @@ -770,7 +789,7 @@ public function testBuildColumnDefinition(string $expected, ColumnInterface|stri public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade): void { if ($cascade) { - $qb = $this->getConnection()->getQueryBuilder(); + $qb = $this->getSharedConnection()->getQueryBuilder(); $this->expectException(NotSupportedException::class); $this->expectExceptionMessage('MySQL doesn\'t support cascade drop table.'); @@ -788,7 +807,7 @@ public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade) #[DataProviderExternal(QueryBuilderProvider::class, 'prepareValue')] public function testPrepareValue(string $expected, mixed $value): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $this->assertSame($expected, $qb->prepareValue($value)); @@ -796,7 +815,7 @@ public function testPrepareValue(string $expected, mixed $value): void #[DataProviderExternal(QueryBuilderProvider::class, 'caseXBuilder')] public function testCaseXBuilder( - CaseX $case, + Closure|CaseX $case, string $expectedSql, array $expectedParams, string|int $expectedResult, @@ -806,7 +825,7 @@ public function testCaseXBuilder( #[DataProviderExternal(QueryBuilderProvider::class, 'lengthBuilder')] public function testLengthBuilder( - string|ExpressionInterface $operand, + Closure|string|ExpressionInterface $operand, string $expectedSql, int $expectedResult, array $expectedParams = [], @@ -817,7 +836,7 @@ public function testLengthBuilder( #[DataProviderExternal(QueryBuilderProvider::class, 'multiOperandFunctionBuilder')] public function testMultiOperandFunctionBuilder( string $class, - array $operands, + Closure|array $operands, string $expectedSql, array|string|int $expectedResult, array $expectedParams = [], @@ -840,7 +859,7 @@ public function testArrayMergeWithTypeWithOrdering( string $operandType, string $expectedResult, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $serverVersion = $db->getServerInfo()->getVersion(); diff --git a/tests/QueryGetTableAliasTest.php b/tests/QueryGetTableAliasTest.php index 4a661d513..a2a8db268 100644 --- a/tests/QueryGetTableAliasTest.php +++ b/tests/QueryGetTableAliasTest.php @@ -4,15 +4,13 @@ namespace Yiisoft\Db\Mysql\Tests; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\AbstractQueryGetTableAliasTest; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Common\CommonQueryGetTableAliasTest; /** * @group mysql - * - * @psalm-suppress PropertyNotSetInConstructor */ -final class QueryGetTableAliasTest extends AbstractQueryGetTableAliasTest +final class QueryGetTableAliasTest extends CommonQueryGetTableAliasTest { - use TestTrait; + use IntegrationTestTrait; } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 52b24d75c..56e92fb92 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -8,7 +8,7 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Expression\Expression; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Common\CommonQueryTest; @@ -17,7 +17,7 @@ */ final class QueryTest extends CommonQueryTest { - use TestTrait; + use IntegrationTestTrait; /** * Ensure no ambiguous column error occurs on indexBy with JOIN. @@ -26,7 +26,8 @@ final class QueryTest extends CommonQueryTest */ public function testAmbiguousColumnIndexBy(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $selectExpression = "concat(customer.name,' in ', p.description) name"; @@ -38,18 +39,11 @@ public function testAmbiguousColumnIndexBy(): void ->column(); $this->assertSame([1 => 'user1 in profile customer 1', 3 => 'user3 in profile customer 3'], $result); - - $db->close(); } - /** - * @throws Exception - * @throws InvalidConfigException - * @throws Throwable - */ public function testQueryIndexHint(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $query = (new Query($db))->from([new Expression('{{%customer}} USE INDEX (primary)')]); @@ -62,14 +56,9 @@ public function testQueryIndexHint(): void $db->close(); } - /** - * @throws Exception - * @throws InvalidConfigException - * @throws Throwable - */ public function testLimitOffsetWithExpression(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $query = (new Query($db))->from('customer')->select('id')->orderBy('id'); @@ -88,7 +77,7 @@ public function testLimitOffsetWithExpression(): void public function testWithQuery(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $serverVersion = $db->getServerInfo()->getVersion(); $db->close(); @@ -104,9 +93,8 @@ public function testWithQuery(): void public function testWithQueryRecursive(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); if ( !str_contains($serverVersion, 'MariaDB') diff --git a/tests/QuoterTest.php b/tests/QuoterTest.php index 650b41e0d..12d8dd95e 100644 --- a/tests/QuoterTest.php +++ b/tests/QuoterTest.php @@ -6,15 +6,15 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Mysql\Tests\Provider\QuoterProvider; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\AbstractQuoterTest; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Common\CommonQuoterTest; /** * @group mysql */ -final class QuoterTest extends AbstractQuoterTest +final class QuoterTest extends CommonQuoterTest { - use TestTrait; + use IntegrationTestTrait; #[DataProviderExternal(QuoterProvider::class, 'tableNameParts')] public function testGetTableNameParts(string $tableName, array $expected): void @@ -45,7 +45,7 @@ public function testQuoteTableName(string $tableName, string $expected): void public function testQuoteValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $quoter = $db->getQuoter(); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index ccda7a171..ceea234d1 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -16,27 +16,27 @@ use Yiisoft\Db\Mysql\Column\ColumnBuilder; use Yiisoft\Db\Mysql\Schema; use Yiisoft\Db\Mysql\Tests\Provider\SchemaProvider; -use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Mysql\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Tests\Common\CommonSchemaTest; -use Yiisoft\Db\Tests\Support\DbHelper; + +use Yiisoft\Db\Tests\Support\TestHelper; use function version_compare; /** * @group mysql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class SchemaTest extends CommonSchemaTest { - use TestTrait; + use IntegrationTestTrait; #[DataProviderExternal(SchemaProvider::class, 'columns')] public function testColumns(array $columns, string $tableName): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $serverVersion = $db->getServerInfo()->getVersion(); $db->close(); @@ -77,7 +77,7 @@ public function testColumnWithTypeBit(array $columns): void public function testDefaultValueDatetimeColumn(): void { $tableName = '{{%datetime_test}}'; - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $serverVersion = $db->getServerInfo()->getVersion(); $oldMySQL = !( @@ -128,7 +128,7 @@ public function testDefaultValueDatetimeColumn(): void public function testDefaultValueDatetimeColumnWithMicrosecs(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -184,11 +184,11 @@ public function testGetSchemaDefaultValues(): void public function testGetSchemaNames(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $schema = $db->getSchema(); - $this->assertSame([self::getDatabaseName()], $schema->getSchemaNames()); + $this->assertSame([TestConnection::databaseName()], $schema->getSchemaNames()); $db->close(); } @@ -205,10 +205,11 @@ public function testGetTableChecks(): void public function testGetTableNamesWithSchema(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); - $tablesNames = $schema->getTableNames(self::getDatabaseName()); + $tablesNames = $schema->getTableNames(TestConnection::databaseName()); $expectedTableNames = [ 'alpha', @@ -254,7 +255,8 @@ public function testGetTableNamesWithSchema(): void public function testGetViewNames(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $views = $schema->getViewNames(); @@ -293,7 +295,7 @@ public function testTableSchemaWithDbSchemes( string $expectedTableName, string $expectedSchemaName = '', ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $commandMock = $this->createMock(CommandInterface::class); $commandMock->method('queryAll')->willReturn([]); @@ -314,7 +316,7 @@ function ($params) use ($expectedTableName, $expectedSchemaName) { ) ->willReturn($commandMock); - $schema = new Schema($mockDb, DbHelper::getSchemaCache()); + $schema = new Schema($mockDb, TestHelper::createMemorySchemaCache()); $schema->getTablePrimaryKey($tableName, true); $db->close(); @@ -346,7 +348,7 @@ public function testWorkWithPrimaryKeyConstraint(): void $constraintName = 't_constraint'; $columnName = 't_field'; - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->createTableForIndexAndConstraintTests($db, $tableName, $columnName); $db->createCommand()->addPrimaryKey($tableName, $constraintName, $columnName)->execute(); @@ -369,7 +371,9 @@ public function testWorkWithPrimaryKeyConstraint(): void public function testTinyInt1() { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $tableName = '{{%tinyint}}'; if ($db->getTableSchema($tableName)) { @@ -398,7 +402,7 @@ public function testTinyInt1() public function testNotConnectionPDO(): void { $db = $this->createMock(ConnectionInterface::class); - $schema = new Schema($db, DbHelper::getSchemaCache()); + $schema = new Schema($db, TestHelper::createMemorySchemaCache()); $this->expectException(NotSupportedException::class); $this->expectExceptionMessage('Only PDO connections are supported.'); @@ -408,7 +412,9 @@ public function testNotConnectionPDO(): void public function testInsertDefaultValues() { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $command = $db->createCommand(); $command->insert('negative_default_values', [])->execute(); diff --git a/tests/SqlParserTest.php b/tests/SqlParserTest.php index 32f21890e..63b6c9ec8 100644 --- a/tests/SqlParserTest.php +++ b/tests/SqlParserTest.php @@ -4,15 +4,17 @@ namespace Yiisoft\Db\Mysql\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Mysql\SqlParser; -use Yiisoft\Db\Tests\AbstractSqlParserTest; +use Yiisoft\Db\Mysql\Tests\Provider\SqlParserProvider; +use Yiisoft\Db\Tests\Common\CommonSqlParserTest; /** * @group mysql */ -final class SqlParserTest extends AbstractSqlParserTest +final class SqlParserTest extends CommonSqlParserTest { - /** @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\SqlParserProvider::getNextPlaceholder */ + #[DataProviderExternal(SqlParserProvider::class, 'getNextPlaceholder')] public function testGetNextPlaceholder(string $sql, ?string $expectedPlaceholder, ?int $expectedPosition): void { parent::testGetNextPlaceholder($sql, $expectedPlaceholder, $expectedPosition); diff --git a/tests/Support/IntegrationTestTrait.php b/tests/Support/IntegrationTestTrait.php new file mode 100644 index 000000000..29cb0cbd9 --- /dev/null +++ b/tests/Support/IntegrationTestTrait.php @@ -0,0 +1,29 @@ +getSchema()->refresh(); + return $db; + } + + public static function dsn(): string + { + return self::$dsn ??= (string) new Dsn( + host: self::host(), + databaseName: self::databaseName(), + port: self::port(), + options: ['charset' => 'utf8mb4'], + ); + } + + public static function create(?string $dsn = null): Connection + { + return new Connection(self::createDriver($dsn), TestHelper::createMemorySchemaCache()); + } + + public static function createDriver(?string $dsn = null): Driver + { + $driver = new Driver($dsn ?? self::dsn(), self::username(), self::password()); + $driver->charset('utf8mb4'); + return $driver; + } + + public static function databaseName(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_DATABASE') ?: 'yiitest'; + } + + return getenv('YII_MYSQL_DATABASE') ?: 'yiitest'; + } + + private static function host(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_HOST') ?: '127.0.0.1'; + } + + return getenv('YII_MYSQL_HOST') ?: '127.0.0.1'; + } + + private static function port(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_PORT') ?: '3306'; + } + + return getenv('YII_MYSQL_PORT') ?: '3306'; + } + + private static function username(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_USER') ?: 'root'; + } + + return getenv('YII_MYSQL_USER') ?: 'root'; + } + + private static function password(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_PASSWORD') ?: ''; + } + + return getenv('YII_MYSQL_PASSWORD') ?: ''; + } + + private static function isMariadb(): bool + { + return getenv('YII_MYSQL_TYPE') === 'mariadb'; + } +} diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php deleted file mode 100644 index 49dfca79d..000000000 --- a/tests/Support/TestTrait.php +++ /dev/null @@ -1,133 +0,0 @@ -close(); - } - - protected function getConnection(bool $fixture = false): Connection - { - $db = new Connection($this->getDriver(), DbHelper::getSchemaCache()); - - if ($fixture) { - DbHelper::loadFixture($db, __DIR__ . '/Fixture/mysql.sql'); - } - - return $db; - } - - protected static function getDb(): Connection - { - $dsn = (string) new Dsn( - host: self::getHost(), - databaseName: self::getDatabaseName(), - port: self::getPort(), - options: ['charset' => 'utf8mb4'], - ); - - return new Connection(new Driver($dsn, self::getUsername(), self::getPassword()), DbHelper::getSchemaCache()); - } - - protected function getDsn(): string - { - if ($this->dsn === '') { - $this->dsn = (string) new Dsn( - host: self::getHost(), - databaseName: self::getDatabaseName(), - port: self::getPort(), - options: ['charset' => 'utf8mb4'], - ); - } - - return $this->dsn; - } - - protected static function getDriverName(): string - { - return 'mysql'; - } - - protected static function replaceQuotes(string $sql): string - { - return str_replace(['[[', ']]'], '`', $sql); - } - - protected function setDsn(string $dsn): void - { - $this->dsn = $dsn; - } - - protected function getDriver(): Driver - { - return new Driver($this->getDsn(), self::getUsername(), self::getPassword()); - } - - private static function getDatabaseName(): string - { - if (self::isMariadb()) { - return getenv('YII_MARIADB_DATABASE') ?: 'yiitest'; - } - - return getenv('YII_MYSQL_DATABASE') ?: 'yiitest'; - } - - private static function getHost(): string - { - if (self::isMariadb()) { - return getenv('YII_MARIADB_HOST') ?: '127.0.0.1'; - } - - return getenv('YII_MYSQL_HOST') ?: '127.0.0.1'; - } - - private static function getPort(): string - { - if (self::isMariadb()) { - return getenv('YII_MARIADB_PORT') ?: '3306'; - } - - return getenv('YII_MYSQL_PORT') ?: '3306'; - } - - private static function getUsername(): string - { - if (self::isMariadb()) { - return getenv('YII_MARIADB_USER') ?: 'root'; - } - - return getenv('YII_MYSQL_USER') ?: 'root'; - } - - private static function getPassword(): string - { - if (self::isMariadb()) { - return getenv('YII_MARIADB_PASSWORD') ?: ''; - } - - return getenv('YII_MYSQL_PASSWORD') ?: ''; - } - - private static function isMariadb(): bool - { - return getenv('YII_MYSQL_TYPE') === 'mariadb'; - } -} From 132e7de27e91cb8bab06287a7236c500bc3833a1 Mon Sep 17 00:00:00 2001 From: vjik <525501+vjik@users.noreply.github.com> Date: Wed, 19 Nov 2025 09:58:01 +0000 Subject: [PATCH 2/8] Apply PHP CS Fixer and Rector changes (CI) --- tests/Provider/QueryBuilderProvider.php | 1 - tests/QueryTest.php | 3 --- tests/SchemaTest.php | 1 - 3 files changed, 5 deletions(-) diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index c077d1947..c8e5a1832 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -13,7 +13,6 @@ use Yiisoft\Db\Expression\Function\ArrayMerge; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Mysql\Column\ColumnBuilder; - use Yiisoft\Db\Mysql\Tests\Support\TestConnection; use function array_replace; diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 56e92fb92..211197e2f 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -4,9 +4,6 @@ namespace Yiisoft\Db\Mysql\Tests; -use Throwable; -use Yiisoft\Db\Exception\Exception; -use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index ceea234d1..3cb410828 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -21,7 +21,6 @@ use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Tests\Common\CommonSchemaTest; - use Yiisoft\Db\Tests\Support\TestHelper; use function version_compare; From 67842e6778460ea2e79b5f9874fcad1c7797d73f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 13:02:49 +0300 Subject: [PATCH 3/8] fix --- tests/DeadLockTest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/DeadLockTest.php b/tests/DeadLockTest.php index da93a6fcd..129f82371 100644 --- a/tests/DeadLockTest.php +++ b/tests/DeadLockTest.php @@ -5,11 +5,12 @@ namespace Yiisoft\Db\Mysql\Tests; use ErrorException; -use PHPUnit\Framework\TestCase; use RuntimeException; use Throwable; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; +use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; use Yiisoft\Db\Transaction\TransactionInterface; use function date; @@ -37,8 +38,10 @@ /** * @group mysql */ -final class DeadLockTest extends TestCase +final class DeadLockTest extends IntegrationTestCase { + use IntegrationTestTrait; + private const CHILD_EXIT_CODE_DEADLOCK = 15; private string $logFile = ''; @@ -172,7 +175,7 @@ private function childrenSelectAndAccidentUpdate(int $pidSecond): int try { $this->log('child 1: connect'); - $first = $this->getConnection(); + $first = $this->createConnection(); $this->log('child 1: delete'); @@ -286,8 +289,8 @@ private function childrenUpdateLocked(): int $this->log('child 2: connect'); - /* @var ConnectionInteface $second */ - $second = $this->getConnection(true); + $second = $this->createConnection(); + $this->loadFixture(db: $second); $second->open(); /* sleep(1); */ From 0872b18dbbed14c0cd191f9884fef7ee67f0e48f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 13:13:26 +0300 Subject: [PATCH 4/8] test --- tests/DeadLockTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/DeadLockTest.php b/tests/DeadLockTest.php index 129f82371..68d8b8344 100644 --- a/tests/DeadLockTest.php +++ b/tests/DeadLockTest.php @@ -53,7 +53,7 @@ final class DeadLockTest extends IntegrationTestCase * @link https://github.com/yiisoft/yii2/issues/12715 * @link https://github.com/yiisoft/yii2/pull/13346 */ - public function testDeadlockException(): void + public function t1estDeadlockException(): void { if (!function_exists('pcntl_fork')) { $this->markTestSkipped('pcntl_fork() is not available'); From 21d3fd0a9fb8d436c6c1ade6a03855307eae39aa Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 13:40:51 +0300 Subject: [PATCH 5/8] improve --- tests/DeadLockTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/DeadLockTest.php b/tests/DeadLockTest.php index 68d8b8344..215b5bb9e 100644 --- a/tests/DeadLockTest.php +++ b/tests/DeadLockTest.php @@ -53,7 +53,7 @@ final class DeadLockTest extends IntegrationTestCase * @link https://github.com/yiisoft/yii2/issues/12715 * @link https://github.com/yiisoft/yii2/pull/13346 */ - public function t1estDeadlockException(): void + public function testDeadlockException(): void { if (!function_exists('pcntl_fork')) { $this->markTestSkipped('pcntl_fork() is not available'); @@ -248,6 +248,8 @@ private function childrenSelectAndAccidentUpdate(int $pidSecond): int ); return 1; + } finally { + $first->close(); } $this->log('child 1: exit'); @@ -326,6 +328,8 @@ private function childrenUpdateLocked(): int ); return 1; + } finally { + $second->close(); } $this->log('child 2: exit'); From 7e52cd7ecbf868936d052f32a578f26828ef148b Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 14:16:58 +0300 Subject: [PATCH 6/8] improve --- tests/ColumnFactoryTest.php | 2 -- tests/PdoDriverTest.php | 2 ++ tests/QueryTest.php | 9 ++++----- tests/SchemaTest.php | 4 +--- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 51af2f56f..a3b028bde 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -58,8 +58,6 @@ public function testExpressionDefaultValueRaw(): void $column = $columnFactory->fromType(ColumnType::DATETIME, ['defaultValueRaw' => 'now()', 'extra' => 'DEFAULT_GENERATED']); $this->assertEquals(new Expression('now()'), $column->getDefaultValue()); - - $db->close(); } protected function getColumnFactoryClass(): string diff --git a/tests/PdoDriverTest.php b/tests/PdoDriverTest.php index 670a71490..d0f180bd3 100644 --- a/tests/PdoDriverTest.php +++ b/tests/PdoDriverTest.php @@ -32,6 +32,8 @@ public function testConnectionCharset(): void $charset = $pdo->query('SHOW VARIABLES LIKE \'character_set_client\'', PDO::FETCH_ASSOC)->fetch(); $this->assertEqualsIgnoringCase($newCharset, array_values($charset)[1]); + + unset($pdo); } public function testCharsetDefault(): void diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 211197e2f..c7028dff0 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -41,6 +41,7 @@ public function testAmbiguousColumnIndexBy(): void public function testQueryIndexHint(): void { $db = $this->getSharedConnection(); + $this->loadFixture(); $query = (new Query($db))->from([new Expression('{{%customer}} USE INDEX (primary)')]); @@ -56,6 +57,7 @@ public function testQueryIndexHint(): void public function testLimitOffsetWithExpression(): void { $db = $this->getSharedConnection(); + $this->loadFixture(); $query = (new Query($db))->from('customer')->select('id')->orderBy('id'); @@ -74,9 +76,7 @@ public function testLimitOffsetWithExpression(): void public function testWithQuery(): void { - $db = $this->getSharedConnection(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); + $serverVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); if ( !str_contains($serverVersion, 'MariaDB') @@ -90,8 +90,7 @@ public function testWithQuery(): void public function testWithQueryRecursive(): void { - $db = $this->getSharedConnection(); - $serverVersion = $db->getServerInfo()->getVersion(); + $serverVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); if ( !str_contains($serverVersion, 'MariaDB') diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 3cb410828..3512e2f61 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -35,9 +35,7 @@ final class SchemaTest extends CommonSchemaTest #[DataProviderExternal(SchemaProvider::class, 'columns')] public function testColumns(array $columns, string $tableName): void { - $db = $this->getSharedConnection(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); + $serverVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); if ( version_compare($serverVersion, '8.0.17', '>') From 2bf3a5ebcf772c11fac8502be37d0eb34a896da6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 16:33:47 +0300 Subject: [PATCH 7/8] fix --- tests/SchemaTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 3512e2f61..8845594b3 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -33,7 +33,7 @@ final class SchemaTest extends CommonSchemaTest use IntegrationTestTrait; #[DataProviderExternal(SchemaProvider::class, 'columns')] - public function testColumns(array $columns, string $tableName): void + public function testColumns(array $columns, string $tableName, ?string $dump = null): void { $serverVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); @@ -62,7 +62,7 @@ public function testColumns(array $columns, string $tableName): void } } - parent::testColumns($columns, $tableName); + parent::testColumns($columns, $tableName, $dump); } #[DataProviderExternal(SchemaProvider::class, 'columnsTypeBit')] From 3de255b88b95ed9143b6146ac73e54cad483e718 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 20 Nov 2025 15:27:22 +0300 Subject: [PATCH 8/8] improve --- tests/Provider/QueryBuilderProvider.php | 5 ++--- tests/QueryBuilderTest.php | 13 +++++++------ tests/QueryTest.php | 5 +++-- tests/SchemaTest.php | 6 +++--- tests/Support/TestConnection.php | 5 +++++ 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index c8e5a1832..cd315c4d1 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -298,7 +298,7 @@ public static function buildColumnDefinition(): array $values[] = ["enum('a','b','c')", ColumnBuilder::string()->dbType("enum('a','b','c')")]; - $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); if (!str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '8', '<') @@ -354,8 +354,7 @@ public static function multiOperandFunctionBuilder(): array $data['Shortest with 2 operands'][2] = '(SELECT :qp0 AS value UNION SELECT :qp1 AS value ORDER BY LENGTH(value) ASC LIMIT 1)'; $data['Shortest with 3 operands'][2] = "(SELECT :qp0 AS value UNION SELECT (SELECT 'longest') AS value UNION SELECT :qp1 AS value ORDER BY LENGTH(value) ASC LIMIT 1)"; - $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); - + $serverVersion = TestConnection::getServerVersion(); $isMariadb = str_contains($serverVersion, 'MariaDB'); if ( diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 5e0b6eb5a..a7f676581 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -18,6 +18,7 @@ use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Mysql\Tests\Provider\QueryBuilderProvider; use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Mysql\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\Condition\JsonOverlaps; @@ -60,7 +61,7 @@ public function testAddCommentOnColumn(): void { $db = $this->getSharedConnection(); $this->loadFixture(); - $serverVersion = $db->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); $qb = $db->getQueryBuilder(); $sql = <<getSharedConnection(); $this->loadFixture(); - $serverVersion = $db->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); $qb = $db->getQueryBuilder(); $sql = <<getSharedConnection(); - $serverVersion = $db->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) { self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.'); @@ -727,7 +728,7 @@ public function testJsonOverlapsBuilder(): void public function testJsonOverlaps(Closure|iterable|ExpressionInterface $values, int $expectedCount): void { $db = $this->getSharedConnection(); - $serverVersion = $db->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); if ($values instanceof Closure) { $values = $values($db); @@ -760,7 +761,7 @@ public function testJsonOverlapsOperator(Closure|iterable|ExpressionInterface $v $values = $values($db); } - $serverVersion = $db->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) { $db->close(); self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.'); @@ -861,7 +862,7 @@ public function testArrayMergeWithTypeWithOrdering( ): void { $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); - $serverVersion = $db->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); $isMariadb = str_contains($serverVersion, 'MariaDB'); diff --git a/tests/QueryTest.php b/tests/QueryTest.php index c7028dff0..fee34ab83 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -6,6 +6,7 @@ use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Mysql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Mysql\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Common\CommonQueryTest; @@ -76,7 +77,7 @@ public function testLimitOffsetWithExpression(): void public function testWithQuery(): void { - $serverVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); if ( !str_contains($serverVersion, 'MariaDB') @@ -90,7 +91,7 @@ public function testWithQuery(): void public function testWithQueryRecursive(): void { - $serverVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); if ( !str_contains($serverVersion, 'MariaDB') diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 8845594b3..0978f02cf 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -35,7 +35,7 @@ final class SchemaTest extends CommonSchemaTest #[DataProviderExternal(SchemaProvider::class, 'columns')] public function testColumns(array $columns, string $tableName, ?string $dump = null): void { - $serverVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); if ( version_compare($serverVersion, '8.0.17', '>') @@ -75,7 +75,7 @@ public function testDefaultValueDatetimeColumn(): void { $tableName = '{{%datetime_test}}'; $db = $this->getSharedConnection(); - $serverVersion = $db->getServerInfo()->getVersion(); + $serverVersion = TestConnection::getServerVersion(); $oldMySQL = !( version_compare($serverVersion, '8.0.0', '>') @@ -258,7 +258,7 @@ public function testGetViewNames(): void $schema = $db->getSchema(); $views = $schema->getViewNames(); - $viewExpected = match (str_contains($db->getServerInfo()->getVersion(), 'MariaDB')) { + $viewExpected = match (str_contains(TestConnection::getServerVersion(), 'MariaDB')) { true => ['animal_view', 'user'], default => ['animal_view'], }; diff --git a/tests/Support/TestConnection.php b/tests/Support/TestConnection.php index 1d6593f58..bdd2fc97d 100644 --- a/tests/Support/TestConnection.php +++ b/tests/Support/TestConnection.php @@ -21,6 +21,11 @@ public static function getShared(): Connection return $db; } + public static function getServerVersion(): string + { + return self::getShared()->getServerInfo()->getVersion(); + } + public static function dsn(): string { return self::$dsn ??= (string) new Dsn(