From 837ba7f6006cc18bf23ebf0b5a6d3441b8c93b17 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 17 Nov 2025 18:06:43 +0300 Subject: [PATCH 01/35] step 1 --- tests/CommandTest.php | 29 ++++++++++------- tests/Support/BaseTestTrait.php | 13 ++++++++ tests/Support/IntegrationTestTrait.php | 45 ++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 tests/Support/BaseTestTrait.php create mode 100644 tests/Support/IntegrationTestTrait.php diff --git a/tests/CommandTest.php b/tests/CommandTest.php index e0a2b4bb6..d7c8a7c79 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -9,7 +9,7 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Pgsql\Tests\Provider\CommandProvider; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonCommandTest; use function serialize; @@ -19,13 +19,11 @@ */ final class CommandTest extends CommonCommandTest { - use TestTrait; - - protected string $upsertTestCharCast = 'CAST([[address]] AS VARCHAR(255))'; + use IntegrationTestTrait; public function testAddDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); @@ -58,7 +56,8 @@ public function testBatchInsert( public function testBooleanValuesInsert(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture();; $command = $db->createCommand(); $command->insert('{{bool_values}}', ['bool_col' => true]); @@ -91,7 +90,8 @@ public function testBooleanValuesInsert(): void public function testBooleanValuesBatchInsert(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture();; $command = $db->createCommand(); $command->insertBatch('{{bool_values}}', [[true], [false]], ['bool_col']); @@ -119,7 +119,8 @@ public function testBooleanValuesBatchInsert(): void public function testDelete(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture();; $command = $db->createCommand(); $command->delete('{{customer}}', ['id' => 2])->execute(); @@ -140,7 +141,7 @@ public function testDelete(): void public function testDropDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); @@ -165,7 +166,7 @@ public function testGetRawSql(string $sql, array $params, string $expectedRawSql */ public function testSaveSerializedObject(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $command = $command->insert( @@ -209,7 +210,8 @@ public function testUpsert(array $firstData, array $secondData): void public function testInsertReturningPksUuid(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture();; $command = $db->createCommand(); $result = $command->insertReturningPks( @@ -246,4 +248,9 @@ public function testCreateIndex(array $columns, array $indexColumns, ?string $in { parent::testCreateIndex($columns, $indexColumns, $indexType, $indexMethod); } + + protected function getUpsertTestCharCast(): string + { + return 'CAST([[address]] AS VARCHAR(255))'; + } } diff --git a/tests/Support/BaseTestTrait.php b/tests/Support/BaseTestTrait.php new file mode 100644 index 000000000..3e117e4ab --- /dev/null +++ b/tests/Support/BaseTestTrait.php @@ -0,0 +1,13 @@ +charset('utf8'); + + $schemaCache = new SchemaCache( + new MemorySimpleCache(), + ); + + return new Connection($driver, $schemaCache); + } + + protected function getDefaultFixture(): string + { + return __DIR__ . '/Fixture/pgsql.sql'; + } +} From b9a7696661146d5b080d1013c7ce5a9dc080cbea Mon Sep 17 00:00:00 2001 From: vjik <525501+vjik@users.noreply.github.com> Date: Mon, 17 Nov 2025 15:08:54 +0000 Subject: [PATCH 02/35] Apply PHP CS Fixer and Rector changes (CI) --- tests/CommandTest.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index d7c8a7c79..98ba96728 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -57,7 +57,8 @@ public function testBatchInsert( public function testBooleanValuesInsert(): void { $db = $this->getSharedConnection(); - $this->loadFixture();; + $this->loadFixture(); + ; $command = $db->createCommand(); $command->insert('{{bool_values}}', ['bool_col' => true]); @@ -91,7 +92,8 @@ public function testBooleanValuesInsert(): void public function testBooleanValuesBatchInsert(): void { $db = $this->getSharedConnection(); - $this->loadFixture();; + $this->loadFixture(); + ; $command = $db->createCommand(); $command->insertBatch('{{bool_values}}', [[true], [false]], ['bool_col']); @@ -120,7 +122,8 @@ public function testBooleanValuesBatchInsert(): void public function testDelete(): void { $db = $this->getSharedConnection(); - $this->loadFixture();; + $this->loadFixture(); + ; $command = $db->createCommand(); $command->delete('{{customer}}', ['id' => 2])->execute(); @@ -211,7 +214,8 @@ public function testUpsert(array $firstData, array $secondData): void public function testInsertReturningPksUuid(): void { $db = $this->getSharedConnection(); - $this->loadFixture();; + $this->loadFixture(); + ; $command = $db->createCommand(); $result = $command->insertReturningPks( From 50a2f35f9854e5e378b75acf4780d359bb30b32c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 10:04:27 +0300 Subject: [PATCH 03/35] step 2 --- tests/SqlParserTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/SqlParserTest.php b/tests/SqlParserTest.php index 242209eeb..015e4f6a4 100644 --- a/tests/SqlParserTest.php +++ b/tests/SqlParserTest.php @@ -4,15 +4,17 @@ namespace Yiisoft\Db\Pgsql\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Pgsql\SqlParser; -use Yiisoft\Db\Tests\AbstractSqlParserTest; +use Yiisoft\Db\Pgsql\Tests\Provider\SqlParserProvider; +use Yiisoft\Db\Tests\Common\CommonSqlParserTest; /** * @group pgsql */ -final class SqlParserTest extends AbstractSqlParserTest +final class SqlParserTest extends CommonSqlParserTest { - /** @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\SqlParserProvider::getNextPlaceholder */ + #[DataProviderExternal(SqlParserProvider::class, 'getNextPlaceholder')] public function testGetNextPlaceholder(string $sql, ?string $expectedPlaceholder, ?int $expectedPosition): void { parent::testGetNextPlaceholder($sql, $expectedPlaceholder, $expectedPosition); From 33d82a68c89af71a3a40f60755b991567363a374 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 13:00:41 +0300 Subject: [PATCH 04/35] step 3 --- tests/SchemaTest.php | 100 ++++++++----------------- tests/Support/IntegrationTestTrait.php | 10 +++ 2 files changed, 42 insertions(+), 68 deletions(-) diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 67a3b80e3..138ddfbb4 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -14,7 +14,7 @@ use Yiisoft\Db\Pgsql\Schema; use Yiisoft\Db\Pgsql\Tests\Provider\SchemaProvider; use Yiisoft\Db\Pgsql\Tests\Provider\StructuredTypeProvider; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Schema\TableSchemaInterface; @@ -26,11 +26,12 @@ */ final class SchemaTest extends CommonSchemaTest { - use TestTrait; + use IntegrationTestTrait; public function testBooleanDefaultValues(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $table = $schema->getTableSchema('bool_values'); @@ -44,14 +45,12 @@ public function testBooleanDefaultValues(): void $this->assertNotNull($columnFalse); $this->assertTrue($columnTrue->getDefaultValue()); $this->assertFalse($columnFalse->getDefaultValue()); - - $db->close(); } #[DataProviderExternal(SchemaProvider::class, 'columns')] public function testColumns(array $columns, string $tableName): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); if (version_compare($db->getServerInfo()->getVersion(), '10', '>')) { if ($tableName === 'type') { @@ -60,13 +59,11 @@ public function testColumns(array $columns, string $tableName): void } $this->assertTableColumns($columns, $tableName); - - $db->close(); } public function testColumnTypeMapNoExist(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -82,19 +79,14 @@ public function testColumnTypeMapNoExist(): void $this->assertNotNull($table); $this->assertNotNull($table->getColumn('during')); $this->assertSame('string', $table->getColumn('during')?->getType()); - - $db->close(); } public function testGeneratedValues(): void { - $this->fixture = 'pgsql12.sql'; + $this->ensureMinPostgreSqlVersion('12.0'); - if (version_compare($this->getConnection()->getServerInfo()->getVersion(), '12.0', '<')) { - $this->markTestSkipped('PostgresSQL < 12.0 does not support GENERATED AS IDENTITY columns.'); - } - - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(__DIR__ . '/Support/Fixture/pgsql12.sql'); $schema = $db->getSchema(); $table = $schema->getTableSchema('generated'); @@ -104,24 +96,20 @@ public function testGeneratedValues(): void $this->assertTrue($table->getColumn('id_primary')?->isAutoIncrement()); $this->assertTrue($table->getColumn('id_primary')?->isAutoIncrement()); $this->assertTrue($table->getColumn('id_default')?->isAutoIncrement()); - - $db->close(); } public function testGetDefaultSchema(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $schema = $db->getSchema(); $this->assertSame('public', $schema->getDefaultSchema()); - - $db->close(); } public function testGetSchemaDefaultValues(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->expectException(NotSupportedException::class); $this->expectExceptionMessage( @@ -133,7 +121,8 @@ public function testGetSchemaDefaultValues(): void public function testGetSchemaNames(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $expectedSchemas = ['public', 'schema1', 'schema2']; $schema = $db->getSchema(); @@ -144,13 +133,12 @@ public function testGetSchemaNames(): void foreach ($expectedSchemas as $schema) { $this->assertContains($schema, $schemas); } - - $db->close(); } public function testGetTableSchemasNotSchemaDefault(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $tables = $schema->getTableSchemas('schema1'); @@ -160,8 +148,6 @@ public function testGetTableSchemasNotSchemaDefault(): void foreach ($tables as $table) { $this->assertInstanceOf(TableSchemaInterface::class, $table); } - - $db->close(); } /** @@ -169,7 +155,7 @@ public function testGetTableSchemasNotSchemaDefault(): void */ public function testParenthesisDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -197,30 +183,24 @@ public function testParenthesisDefaultValue(): void $this->assertTrue($column->isNotNull()); $this->assertEquals('numeric', $column->getDbType()); $this->assertEquals(0, $column->getDefaultValue()); - - $db->close(); } public function testPartitionedTable(): void { - $this->fixture = 'pgsql10.sql'; - - if (version_compare($this->getConnection()->getServerInfo()->getVersion(), '10.0', '<')) { - $this->markTestSkipped('PostgresSQL < 10.0 does not support PARTITION BY clause.'); - } + $this->ensureMinPostgreSqlVersion('10.0'); - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(__DIR__ . '/Support/Fixture/pgsql10.sql'); $schema = $db->getSchema(); $this->assertNotNull($schema->getTableSchema('partitioned')); - - $db->close(); } public function testSequenceName(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -250,8 +230,6 @@ public function testSequenceName(): void $this->assertNotNull($tableSchema); $this->assertEquals($sequenceName, $tableSchema->getSequenceName()); - - $db->close(); } #[DataProviderExternal(SchemaProvider::class, 'tableSchemaCacheWithTablePrefixes')] @@ -261,7 +239,7 @@ public function testTableSchemaCacheWithTablePrefixes( string $testTablePrefix, string $testTableName, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $schema = $db->getSchema(); $schema->enableCache(true); @@ -291,8 +269,6 @@ public function testTableSchemaCacheWithTablePrefixes( $this->assertInstanceOf(TableSchemaInterface::class, $testRefreshedTable); $this->assertSame($refreshedTable, $testRefreshedTable); $this->assertNotSame($testNoCacheTable, $testRefreshedTable); - - $db->close(); } #[DataProviderExternal(SchemaProvider::class, 'constraints')] @@ -320,7 +296,7 @@ public function testTableSchemaWithDbSchemes( string $expectedTableName, string $expectedSchemaName = '', ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $commandMock = $this->createMock(CommandInterface::class); $commandMock->method('queryAll')->willReturn([]); @@ -343,8 +319,6 @@ function ($params) use ($expectedTableName, $expectedSchemaName) { ->willReturn($commandMock); $schema = new Schema($mockDb, DbHelper::getSchemaCache()); $schema->getTableSchema($tableName); - - $db->close(); } /** @@ -352,7 +326,7 @@ function ($params) use ($expectedTableName, $expectedSchemaName) { */ public function testTimestampNullDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -374,8 +348,6 @@ public function testTimestampNullDefaultValue(): void $this->assertNotNull($column); $this->assertNull($column->getDefaultValue()); - - $db->close(); } public function testWorkWithDefaultValueConstraint(): void @@ -421,13 +393,12 @@ public function withIndexDataProvider(): array public function testCustomTypeInNonDefaultSchema() { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema()->getTableSchema('schema2.custom_type_test_table'); $this->assertEquals('my_type', $schema->getColumn('test_type')->getDbType()); $this->assertEquals('schema2.my_type2', $schema->getColumn('test_type2')->getDbType()); - - $db->close(); } public function testNotConnectionPDO(): void @@ -443,7 +414,7 @@ public function testNotConnectionPDO(): void public function testDomainType(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -469,13 +440,12 @@ public function testDomainType(): void $command->insert('test_domain_type', ['sex' => 'm'])->execute(); $sex = $command->setSql('SELECT sex FROM test_domain_type')->queryScalar(); $this->assertEquals('m', $sex); - - $db->close(); } public function testGetViewNames(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $views = $schema->getViewNames(); @@ -490,8 +460,6 @@ public function testGetViewNames(): void ], $views, ); - - $db->close(); } #[DataProviderExternal(StructuredTypeProvider::class, 'columns')] @@ -502,13 +470,11 @@ public function testStructuredTypeColumn(array $columns, string $tableName): voi public function testTableIndexes(): void { - $this->fixture = 'pgsql11.sql'; + $this->ensureMinPostgreSqlVersion('11.0'); - if (version_compare($this->getConnection()->getServerInfo()->getVersion(), '11.0', '<')) { - $this->markTestSkipped('PostgresSQL < 11.0 does not support INCLUDE clause.'); - } + $db = $this->getSharedConnection(); + $this->loadFixture(__DIR__ . '/Support/Fixture/pgsql11.sql'); - $db = $this->getConnection(true); $schema = $db->getSchema(); $tableIndexes = $schema->getTableIndexes('table_index'); @@ -523,8 +489,6 @@ public function testTableIndexes(): void ], $tableIndexes, ); - - $db->close(); } #[DataProviderExternal(SchemaProvider::class, 'resultColumns')] diff --git a/tests/Support/IntegrationTestTrait.php b/tests/Support/IntegrationTestTrait.php index ca907267b..8edb3d309 100644 --- a/tests/Support/IntegrationTestTrait.php +++ b/tests/Support/IntegrationTestTrait.php @@ -42,4 +42,14 @@ protected function getDefaultFixture(): string { return __DIR__ . '/Fixture/pgsql.sql'; } + + protected function ensureMinPostgreSqlVersion(string $version): void + { + $currentVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); + if (version_compare($currentVersion, $version, '<')) { + $this->markTestSkipped( + "This test requires at least PostgreSQL version $version. Current version is $currentVersion.", + ); + } + } } From e38d8ff6cabc40b3e770de3eae9f611defd6ae04 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 13:05:40 +0300 Subject: [PATCH 05/35] step 4 --- tests/QuoterTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/QuoterTest.php b/tests/QuoterTest.php index 4dcd448d5..fe83c0cb2 100644 --- a/tests/QuoterTest.php +++ b/tests/QuoterTest.php @@ -6,15 +6,15 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Pgsql\Tests\Provider\QuoterProvider; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\AbstractQuoterTest; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Common\CommonQuoterTest; /** * @group pgsql */ -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 From c68e90a9b739ff3984604799543c62ee4c656793 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 13:23:02 +0300 Subject: [PATCH 06/35] step 5 --- tests/QueryTest.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index c22b8013a..bf7f66725 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -7,18 +7,17 @@ use Throwable; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Common\CommonQueryTest; /** * @group pgsql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class QueryTest extends CommonQueryTest { - use TestTrait; + use IntegrationTestTrait; /** * Ensure no ambiguous column error occurs on indexBy with JOIN. @@ -27,7 +26,8 @@ final class QueryTest extends CommonQueryTest */ public function testAmbiguousColumnIndexBy(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $selectExpression = "(customer.name || ' in ' || p.description) AS name"; @@ -39,18 +39,12 @@ 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 testBooleanValues(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command->insertBatch('bool_values', [[true], [false]], ['bool_col'])->execute(); @@ -75,7 +69,5 @@ public function testBooleanValues(): void 1, (new Query($db))->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => false])->count(), ); - - $db->close(); } } From f8282f377d7a2d6c3b6bc8689c2462ea08ce76af Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 13:27:10 +0300 Subject: [PATCH 07/35] step 6 --- tests/QueryGetTableAliasTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/QueryGetTableAliasTest.php diff --git a/tests/QueryGetTableAliasTest.php b/tests/QueryGetTableAliasTest.php new file mode 100644 index 000000000..638d1c6c6 --- /dev/null +++ b/tests/QueryGetTableAliasTest.php @@ -0,0 +1,13 @@ + Date: Tue, 18 Nov 2025 13:47:48 +0300 Subject: [PATCH 08/35] step 7 --- tests/QueryBuilderTest.php | 79 ++++++++++++-------------------------- 1 file changed, 24 insertions(+), 55 deletions(-) diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index c0ba76b3e..79786789a 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -8,7 +8,6 @@ use PHPUnit\Framework\Attributes\TestWith; use Throwable; use Yiisoft\Db\Constant\DataType; -use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; use Yiisoft\Db\Exception\IntegrityException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\Value\ArrayValue; @@ -20,7 +19,7 @@ use Yiisoft\Db\Pgsql\Column\ArrayColumn; use Yiisoft\Db\Pgsql\Column\IntegerColumn; use Yiisoft\Db\Pgsql\Tests\Provider\QueryBuilderProvider; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\Condition\ArrayOverlaps; @@ -28,16 +27,12 @@ use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; -use function version_compare; - /** * @group pgsql */ final class QueryBuilderTest extends CommonQueryBuilderTest { - use TestTrait; - - protected PdoConnectionInterface $db; + use IntegrationTestTrait; public function getBuildColumnDefinitionProvider(): array { @@ -46,7 +41,7 @@ public function getBuildColumnDefinitionProvider(): array public function testAddDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -133,7 +128,7 @@ public function testBuildWithWhereExists(string $cond, string $expectedQuerySql) public function testCheckIntegrity(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $this->assertSame( @@ -142,13 +137,12 @@ public function testCheckIntegrity(): void SQL . ' ', $qb->checkIntegrity('public', 'item'), ); - - $db->close(); } public function testCheckIntegrityExecute(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $db->createCommand()->checkIntegrity('public', 'item', false)->execute(); $command = $db->createCommand( @@ -171,13 +165,11 @@ public function testCheckIntegrityExecute(): void 'SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "item" violates foreign key constraint "item_category_id_fkey"', $exception->getMessage(), ); - - $db->close(); } public function testCreateTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $this->assertSame( @@ -201,8 +193,6 @@ public function testCreateTable(): void ], ), ); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'delete')] @@ -213,7 +203,8 @@ public function testDelete(string $table, array|string $condition, string $expec public function testDropCommentFromColumn(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $qb = $db->getQueryBuilder(); $this->assertSame( @@ -222,13 +213,12 @@ public function testDropCommentFromColumn(): void SQL, $qb->dropCommentFromColumn('customer', 'id'), ); - - $db->close(); } public function testDropDefaultValue(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $qb = $db->getQueryBuilder(); $this->expectException(NotSupportedException::class); @@ -241,7 +231,7 @@ public function testDropDefaultValue(): void public function testDropIndex(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $this->assertSame( @@ -285,8 +275,6 @@ public function testDropIndex(): void SQL, $qb->dropIndex('{{%schema.table}}', 'index'), ); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'insert')] @@ -313,7 +301,7 @@ public function testInsertReturningPks( public function testRenameTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -323,13 +311,12 @@ public function testRenameTable(): void SQL, $qb->renameTable('alpha', 'alpha-test'), ); - - $db->close(); } public function testResetSequence(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $qb = $db->getQueryBuilder(); @@ -353,19 +340,15 @@ public function testResetSequence(): void SQL, $qb->resetSequence('item', '1'), ); - - $db->close(); } public function testResetSequencePgsql12(): void { - if (version_compare($this->getConnection()->getServerInfo()->getVersion(), '12.0', '<')) { - $this->markTestSkipped('PostgreSQL < 12.0 does not support GENERATED AS IDENTITY columns.'); - } + $this->ensureMinPostgreSqlVersion('12.0'); - $this->setFixture('pgsql12.sql'); + $db = $this->getSharedConnection(); + $this->loadFixture(__DIR__ . '/Support/Fixture/pgsql12.sql'); - $db = $this->getConnection(true); $qb = $db->getQueryBuilder(); $this->assertSame( @@ -388,13 +371,11 @@ public function testResetSequencePgsql12(): void SQL, $qb->resetSequence('item', '1'), ); - - $db->close(); } public function testTruncateTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $sql = $qb->truncateTable('customer'); @@ -414,8 +395,6 @@ public function testTruncateTable(): void SQL, $sql, ); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'update')] @@ -462,7 +441,7 @@ public function testSelectScalar(array|bool|float|int|string $columns, string $e public function testArrayOverlapsBuilder(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; @@ -477,13 +456,11 @@ public function testArrayOverlapsBuilder(): void $this->assertSame('column::int[] && ARRAY[1,2,3]::int[]', $sql); $this->assertSame([], $params); - - $db->close(); } public function testJsonOverlapsBuilder(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; @@ -494,14 +471,12 @@ public function testJsonOverlapsBuilder(): void $sql, ); $this->assertSame([], $params); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'overlapsCondition')] public function testOverlapsCondition(iterable|ExpressionInterface $values, int $expectedCount): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $query = new Query($db); $count = $query @@ -524,14 +499,12 @@ public function testOverlapsCondition(iterable|ExpressionInterface $values, int ->count(); $this->assertSame($expectedCount, $count); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'overlapsCondition')] public function testOverlapsConditionOperator(iterable|ExpressionInterface $values, int $expectedCount): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $query = new Query($db); $count = $query @@ -554,8 +527,6 @@ public function testOverlapsConditionOperator(iterable|ExpressionInterface $valu ->count(); $this->assertSame($expectedCount, $count); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'buildColumnDefinition')] @@ -628,7 +599,7 @@ public function testArrayMergeWithTypeWithOrdering( string $typeHint, string $expectedResult, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $stringParam = new Param('{4,3,5}', DataType::STRING); @@ -649,8 +620,6 @@ public function testArrayMergeWithTypeWithOrdering( $result = $db->select($arrayMerge)->scalar(); $this->assertSame($expectedResult, $result); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'upsertWithMultiOperandFunctions')] From fc9e666e671070ac570d0ef8d823e8b7de3cfc99 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 14:01:14 +0300 Subject: [PATCH 09/35] step 8 --- tests/PdoConnectionTest.php | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/tests/PdoConnectionTest.php b/tests/PdoConnectionTest.php index 778acb03d..0260685f2 100644 --- a/tests/PdoConnectionTest.php +++ b/tests/PdoConnectionTest.php @@ -9,27 +9,21 @@ use InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; use Yiisoft\Db\Exception\InvalidConfigException; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonPdoConnectionTest; /** * @group pgsql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class PdoConnectionTest extends CommonPdoConnectionTest { - use TestTrait; + use IntegrationTestTrait; - /** - * @throws Exception - * @throws InvalidConfigException - * @throws InvalidCallException - * @throws Throwable - */ public function testGetLastInsertID(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command->insert( @@ -42,19 +36,12 @@ public function testGetLastInsertID(): void )->execute(); $this->assertSame('4', $db->getLastInsertId('public.customer_id_seq')); - - $db->close(); } - /** - * @throws Exception - * @throws InvalidConfigException - * @throws InvalidCallException - * @throws Throwable - */ public function testGetLastInsertIDWithException(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command->insert('item', ['name' => 'Yii2 starter', 'category_id' => 1])->execute(); @@ -64,7 +51,5 @@ public function testGetLastInsertIDWithException(): void $this->expectExceptionMessage('PostgreSQL not support lastInsertId without sequence name.'); $db->getLastInsertId(); - - $db->close(); } } From fbb67264ba880574c71c8d6584a93462848efae6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 14:29:12 +0300 Subject: [PATCH 10/35] step 9 --- tests/ConnectionTest.php | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index e10a52a25..321073456 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -9,6 +9,7 @@ use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Column\ColumnFactory; use Yiisoft\Db\Pgsql\Connection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; use Yiisoft\Db\Tests\Support\DbHelper; @@ -19,11 +20,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(); @@ -35,7 +36,7 @@ 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)); @@ -56,7 +57,7 @@ public function testSettingDefaultAttributes(): void public function testTransactionIsolation(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); $transaction = $db->beginTransaction(); $transaction->setIsolationLevel(TransactionInterface::READ_UNCOMMITTED); @@ -86,7 +87,8 @@ public function testTransactionIsolation(): void public function testTransactionShortcutCustom(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $this->assertTrue( $db->transaction( @@ -109,13 +111,11 @@ static function (ConnectionInterface $db) { )->queryScalar(), 'profile should be inserted in transaction shortcut', ); - - $db->close(); } public function getColumnBuilderClass(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); @@ -124,21 +124,10 @@ public function getColumnBuilderClass(): void 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); - - $this->assertSame($columnFactory, $db->getColumnFactory()); - - $db->close(); - } } From 730c3ddba4cbbc8fb7eda171e7a13e6d5ff314fe Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 14:33:40 +0300 Subject: [PATCH 11/35] step 10 --- tests/ColumnTest.php | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index 93a590538..d74e5ca2d 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -22,7 +22,7 @@ use Yiisoft\Db\Pgsql\Column\IntegerColumn; use Yiisoft\Db\Pgsql\Column\StructuredColumn; use Yiisoft\Db\Pgsql\Tests\Provider\ColumnProvider; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\Column\DoubleColumn; @@ -39,11 +39,12 @@ */ final class ColumnTest extends CommonColumnTest { - use TestTrait; + use IntegrationTestTrait; public function testSelectWithPhpTypecasting(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $sql = <<queryColumn(); $this->assertSame([2.5, 3.3], $result); - - $db->close(); } public function testDbTypeCastJson(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $schema = $db->getSchema(); $tableSchema = $schema->getTableSchema('type'); $this->assertEquals(new JsonValue('', 'json'), $tableSchema->getColumn('json_col')->dbTypecast('')); $this->assertEquals(new JsonValue('', 'jsonb'), $tableSchema->getColumn('jsonb_col')->dbTypecast('')); - - $db->close(); } public function testBoolDefault(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -165,13 +165,13 @@ public function testBoolDefault(): void [null, true, true, true, true, true, true, false, false, false, false, false, false], $tableSchema->getColumn('default_array')->getDefaultValue(), ); - - $db->close(); } public function testPrimaryKeyOfView() { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $schema = $db->getSchema(); $tableSchema = $schema->getTableSchema('T_constraints_2_view'); @@ -181,13 +181,13 @@ public function testPrimaryKeyOfView() $this->assertFalse($tableSchema->getColumn('C_index_1')->isPrimaryKey()); $this->assertFalse($tableSchema->getColumn('C_index_2_1')->isPrimaryKey()); $this->assertFalse($tableSchema->getColumn('C_index_2_2')->isPrimaryKey()); - - $db->close(); } public function testStructuredType(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $command = $db->createCommand(); $schema = $db->getSchema(); $tableSchema = $schema->getTableSchema('test_structured_type'); @@ -268,13 +268,13 @@ public function testStructuredType(): void $priceArray2->dbTypecast([null, null]), 'Double array of null values', ); - - $db->close(); } public function testColumnInstance() { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $schema = $db->getSchema(); $tableSchema = $schema->getTableSchema('type'); @@ -288,8 +288,6 @@ public function testColumnInstance() $this->assertInstanceOf(ArrayColumn::class, $tableSchema->getColumn('intarray_col')); $this->assertInstanceOf(IntegerColumn::class, $tableSchema->getColumn('intarray_col')->getColumn()); $this->assertInstanceOf(JsonColumn::class, $tableSchema->getColumn('json_col')); - - $db->close(); } #[DataProviderExternal(ColumnProvider::class, 'predefinedTypes')] From 112d1235169064d587955d0a01da13c274314fd6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 14:54:24 +0300 Subject: [PATCH 12/35] step 11 --- tests/ColumnFactoryTest.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 8d811defb..4955f8cdb 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -9,23 +9,24 @@ use Yiisoft\Db\Pgsql\Column\ArrayColumn; use Yiisoft\Db\Pgsql\Column\ColumnFactory; use Yiisoft\Db\Pgsql\Tests\Provider\ColumnFactoryProvider; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Schema\Column\ColumnInterface; -use Yiisoft\Db\Tests\AbstractColumnFactoryTest; +use Yiisoft\Db\Tests\Common\CommonColumnFactoryTest; /** * @group pgsql */ -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 { parent::testFromDbType($dbType, $expectedType, $expectedInstanceOf); - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $columnFactory = $db->getColumnFactory(); // For array type @@ -35,8 +36,6 @@ public function testFromDbType(string $dbType, string $expectedType, string $exp $this->assertInstanceOf($expectedInstanceOf, $column->getColumn()); $this->assertSame($expectedType, $column->getColumn()->getType()); $this->assertSame($dbType, $column->getColumn()->getDbType()); - - $db->close(); } #[DataProviderExternal(ColumnFactoryProvider::class, 'definitions')] @@ -56,7 +55,7 @@ public function testFromType(string $type, string $expectedType, string $expecte { parent::testFromType($type, $expectedType, $expectedInstanceOf); - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $columnFactory = $db->getColumnFactory(); // For array type From 91fe245978ab25277e084684020c40e588f68b93 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 14:58:16 +0300 Subject: [PATCH 13/35] step 12 --- tests/ColumnDefinitionParserTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/ColumnDefinitionParserTest.php b/tests/ColumnDefinitionParserTest.php index 7b4142020..fbfa95e13 100644 --- a/tests/ColumnDefinitionParserTest.php +++ b/tests/ColumnDefinitionParserTest.php @@ -4,17 +4,17 @@ namespace Yiisoft\Db\Pgsql\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Pgsql\Column\ColumnDefinitionParser; -use Yiisoft\Db\Tests\AbstractColumnDefinitionParserTest; +use Yiisoft\Db\Pgsql\Tests\Provider\ColumnDefinitionParserProvider; +use Yiisoft\Db\Tests\Common\CommonColumnDefinitionParserTest; /** * @group pgsql */ -final class ColumnDefinitionParserTest extends AbstractColumnDefinitionParserTest +final class ColumnDefinitionParserTest extends CommonColumnDefinitionParserTest { - /** - * @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\ColumnDefinitionParserProvider::parse - */ + #[DataProviderExternal(ColumnDefinitionParserProvider::class, 'parse')] public function testParse(string $definition, array $expected): void { parent::testParse($definition, $expected); From 75bbf44c765639bb06904a41058baa7d23332a3d Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 15:11:00 +0300 Subject: [PATCH 14/35] step 13 --- tests/ColumnBuilderTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php index 12ed76527..b82327f1f 100644 --- a/tests/ColumnBuilderTest.php +++ b/tests/ColumnBuilderTest.php @@ -6,15 +6,15 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Pgsql\Tests\Provider\ColumnBuilderProvider; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\AbstractColumnBuilderTest; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Common\CommonColumnBuilderTest; /** * @group pgsql */ -final class ColumnBuilderTest extends AbstractColumnBuilderTest +final class ColumnBuilderTest extends CommonColumnBuilderTest { - use TestTrait; + use IntegrationTestTrait; #[DataProviderExternal(ColumnBuilderProvider::class, 'buildingMethods')] public function testBuildingMethods( From 3b04bc65b626764edce66d4e59ab36b4347ac736 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 15:13:20 +0300 Subject: [PATCH 15/35] step 14 --- tests/BatchQueryResultTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/BatchQueryResultTest.php b/tests/BatchQueryResultTest.php index e6e42202e..f1be09a97 100644 --- a/tests/BatchQueryResultTest.php +++ b/tests/BatchQueryResultTest.php @@ -4,15 +4,13 @@ namespace Yiisoft\Db\Pgsql\Tests; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonBatchQueryResultTest; /** * @group pgsql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class BatchQueryResultTest extends CommonBatchQueryResultTest { - use TestTrait; + use IntegrationTestTrait; } From 3a73ac2aa10b28f8138d082e32bbb1f290b0796d Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 15:39:45 +0300 Subject: [PATCH 16/35] step 15 --- tests/PdoCommandTest.php | 24 ++++++++----------- ...PDOProvider.php => CommandPdoProvider.php} | 2 +- 2 files changed, 11 insertions(+), 15 deletions(-) rename tests/Provider/{CommandPDOProvider.php => CommandPdoProvider.php} (87%) diff --git a/tests/PdoCommandTest.php b/tests/PdoCommandTest.php index b63f5c2fb..56ad76462 100644 --- a/tests/PdoCommandTest.php +++ b/tests/PdoCommandTest.php @@ -4,24 +4,22 @@ namespace Yiisoft\Db\Pgsql\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Psr\Log\LoggerAwareInterface; use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand; use Yiisoft\Db\Driver\Pdo\PdoCommandInterface; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Provider\CommandPdoProvider; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonPdoCommandTest; /** * @group pgsql - * - * @psalm-suppress PropertyNotSetInConstructor */ final class PdoCommandTest extends CommonPdoCommandTest { - use TestTrait; + use IntegrationTestTrait; - /** - * @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\CommandPDOProvider::bindParam - */ + #[DataProviderExternal(CommandPdoProvider::class, 'bindParam')] public function testBindParam( string $field, string $name, @@ -35,11 +33,12 @@ public function testBindParam( } /** - * {@link https://github.com/yiisoft/db-pgsql/issues/1} + * @link https://github.com/yiisoft/db-pgsql/issues/1 */ public function testInsertAndReadToArrayColumn(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $arrValue = [1, 2, 3, 4]; $insertedData = $db->createCommand()->insertReturningPks('{{%table_with_array_col}}', ['array_col' => $arrValue]); @@ -53,13 +52,12 @@ public function testInsertAndReadToArrayColumn(): void $column = $db->getTableSchema('{{%table_with_array_col}}')->getColumn('array_col'); $this->assertSame($arrValue, $column->phpTypecast($selectData['array_col'])); - - $db->close(); } public function testCommandLogging(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $sql = 'SELECT * FROM "customer" LIMIT 1'; @@ -92,7 +90,5 @@ public function testCommandLogging(): void $sql = 'INSERT INTO "customer" ("name", "email") VALUES (\'test\', \'email@email\') RETURNING "id"'; $command->setLogger($this->createQueryLogger($sql, ['Yiisoft\Db\Driver\Pdo\AbstractPdoCommand::insertReturningPks'])); $command->insertReturningPks('{{%customer}}', ['name' => 'test', 'email' => 'email@email']); - - $db->close(); } } diff --git a/tests/Provider/CommandPDOProvider.php b/tests/Provider/CommandPdoProvider.php similarity index 87% rename from tests/Provider/CommandPDOProvider.php rename to tests/Provider/CommandPdoProvider.php index bf4f27bb6..41164f9ef 100644 --- a/tests/Provider/CommandPDOProvider.php +++ b/tests/Provider/CommandPdoProvider.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Pgsql\Tests\Provider; -final class CommandPDOProvider extends \Yiisoft\Db\Tests\Provider\CommandPDOProvider +final class CommandPdoProvider extends \Yiisoft\Db\Tests\Provider\CommandPDOProvider { public static function bindParam(): array { From a65fe88db69c3ee67349922e7ebc204ad4f9dae7 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 16:42:53 +0300 Subject: [PATCH 17/35] step 16 --- tests/ArrayValueBuilderTest.php | 55 +++++++++++++------ tests/Column/DateMultiRangeColumnTest.php | 17 +++--- tests/Column/DateRangeColumnTest.php | 17 +++--- tests/Column/Int4MultiRangeColumnTest.php | 17 +++--- tests/Column/Int4RangeColumnTest.php | 17 +++--- tests/Column/Int8MultiRangeColumnTest.php | 17 +++--- tests/Column/Int8RangeColumnTest.php | 17 +++--- tests/Column/NumMultiRangeColumnTest.php | 17 +++--- tests/Column/NumRangeColumnTest.php | 17 +++--- tests/Column/TsMultiRangeColumnTest.php | 17 +++--- tests/Column/TsRangeColumnTest.php | 17 +++--- tests/Column/TsTzMultiRangeColumnTest.php | 17 +++--- tests/Column/TsTzRangeColumnTest.php | 17 +++--- tests/ColumnFactoryTest.php | 1 - tests/CommandTest.php | 14 +++-- tests/ConnectionTest.php | 3 - tests/JsonValueBuilderTest.php | 28 +++------- tests/PdoConnectionTest.php | 5 -- .../{PDODriverTest.php => PdoDriverTest.php} | 22 +++----- tests/QueryTest.php | 4 -- tests/SchemaTest.php | 6 +- tests/StructuredValueBuilderTest.php | 12 ++-- tests/Support/IntegrationTestTrait.php | 17 +++--- tests/Support/TestTrait.php | 2 +- tests/TestConnection.php | 6 +- 25 files changed, 184 insertions(+), 195 deletions(-) rename tests/{PDODriverTest.php => PdoDriverTest.php} (56%) diff --git a/tests/ArrayValueBuilderTest.php b/tests/ArrayValueBuilderTest.php index c64071a7f..57d3bf03f 100644 --- a/tests/ArrayValueBuilderTest.php +++ b/tests/ArrayValueBuilderTest.php @@ -6,7 +6,6 @@ use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\Value\ArrayValue; @@ -18,19 +17,20 @@ use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Data\LazyArray; use Yiisoft\Db\Pgsql\Data\StructuredLazyArray; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\Data\JsonLazyArray; use Yiisoft\Db\Schema\Data\LazyArrayInterface; use Yiisoft\Db\Tests\Support\Assert; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; /** * @group pgsql */ -final class ArrayValueBuilderTest extends TestCase +final class ArrayValueBuilderTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function buildProvider(): array { @@ -127,16 +127,6 @@ public static function buildProvider(): array 'ARRAY[NULL,ROW(11.11,:qp0),ROW(NULL,NULL)]', [':qp0' => new Param('USD', DataType::STRING)], ], - 'Query w/o type' => [ - (new Query(self::getDb()))->select('id')->from('users')->where(['active' => 1]), - null, - 'ARRAY(SELECT "id" FROM "users" WHERE "active" = 1)', - ], - 'Query' => [ - [(new Query(self::getDb()))->select('id')->from('users')->where(['active' => 1])], - 'integer[][]', - 'ARRAY[ARRAY(SELECT "id" FROM "users" WHERE "active" = 1)::integer[]]::integer[][]', - ], 'bool' => [ [[[true], [false, null]], [['t', 'f'], null], null], 'bool[][][]', @@ -169,7 +159,7 @@ public function testBuild( string $expected, array $expectedParams = [], ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; @@ -178,7 +168,40 @@ public function testBuild( $this->assertSame($expected, $builder->build($expression, $params)); Assert::arraysEquals($expectedParams, $params); + } + + public function testBuildWithQuery(): void + { + $db = $this->getSharedConnection(); + $queryBuilder = $db->getQueryBuilder(); + + $params = []; + $builder = new ArrayValueBuilder($queryBuilder); + $expression = new ArrayValue( + $db->select('id')->from('users')->where(['active' => 1]), + ); + + $result = $builder->build($expression, $params); + + $this->assertSame('ARRAY(SELECT "id" FROM "users" WHERE "active" = 1)', $result); + $this->assertSame([], $params); + } + + public function testBuildWithArrayOfQuery(): void + { + $db = $this->getSharedConnection(); + $queryBuilder = $db->getQueryBuilder(); + + $params = []; + $builder = new ArrayValueBuilder($queryBuilder); + $expression = new ArrayValue( + [$db->select('id')->from('users')->where(['active' => 1])], + 'integer[][]', + ); + + $result = $builder->build($expression, $params); - $db->close(); + $this->assertSame('ARRAY[ARRAY(SELECT "id" FROM "users" WHERE "active" = 1)::integer[]]::integer[][]', $result); + $this->assertSame([], $params); } } diff --git a/tests/Column/DateMultiRangeColumnTest.php b/tests/Column/DateMultiRangeColumnTest.php index 8fbe943ee..a5fcadd0e 100644 --- a/tests/Column/DateMultiRangeColumnTest.php +++ b/tests/Column/DateMultiRangeColumnTest.php @@ -6,16 +6,15 @@ use DateTimeImmutable; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\DateRangeValue; use Yiisoft\Db\Pgsql\Expression\MultiRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class DateMultiRangeColumnTest extends TestCase +final class DateMultiRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -105,7 +104,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['{[2024-01-01,2024-01-10),[2024-01-20,2024-01-30]}', '{[2024-02-01,2024-02-10)}']); + $db = $this->prepareConnection(['{[2024-01-01,2024-01-10),[2024-01-20,2024-01-30]}', '{[2024-02-01,2024-02-10)}']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -137,7 +136,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(array $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -147,9 +146,9 @@ public function testPhpTypecast(array $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $this->ensureMinPostgreSqlVersion('14.0'); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); diff --git a/tests/Column/DateRangeColumnTest.php b/tests/Column/DateRangeColumnTest.php index 9ea3eb452..bccb891d0 100644 --- a/tests/Column/DateRangeColumnTest.php +++ b/tests/Column/DateRangeColumnTest.php @@ -6,15 +6,14 @@ use DateTimeImmutable; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\DateRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class DateRangeColumnTest extends TestCase +final class DateRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -77,7 +76,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['[2024-01-01,2024-01-10)', '(2024-01-20,2024-01-30]']); + $db = $this->prepareConnection(['[2024-01-01,2024-01-10)', '(2024-01-20,2024-01-30]']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -107,7 +106,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(mixed $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -117,9 +116,9 @@ public function testPhpTypecast(mixed $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); $db->createCommand( diff --git a/tests/Column/Int4MultiRangeColumnTest.php b/tests/Column/Int4MultiRangeColumnTest.php index 4b6a607b6..f0adac9a8 100644 --- a/tests/Column/Int4MultiRangeColumnTest.php +++ b/tests/Column/Int4MultiRangeColumnTest.php @@ -5,16 +5,15 @@ namespace Yiisoft\Db\Pgsql\Tests\Column; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\Int4RangeValue; use Yiisoft\Db\Pgsql\Expression\MultiRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class Int4MultiRangeColumnTest extends TestCase +final class Int4MultiRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -53,7 +52,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['{[1,10),[20,30]}', '{[40,50)}']); + $db = $this->prepareConnection(['{[1,10),[20,30]}', '{[40,50)}']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -85,7 +84,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(array $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -95,9 +94,9 @@ public function testPhpTypecast(array $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $this->ensureMinPostgreSqlVersion('14.0'); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); diff --git a/tests/Column/Int4RangeColumnTest.php b/tests/Column/Int4RangeColumnTest.php index 3f2ece877..1f7295be0 100644 --- a/tests/Column/Int4RangeColumnTest.php +++ b/tests/Column/Int4RangeColumnTest.php @@ -5,15 +5,14 @@ namespace Yiisoft\Db\Pgsql\Tests\Column; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\Int4RangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class Int4RangeColumnTest extends TestCase +final class Int4RangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -43,7 +42,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['[1,10)', '(20,30]']); + $db = $this->prepareConnection(['[1,10)', '(20,30]']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -73,7 +72,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(mixed $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -83,9 +82,9 @@ public function testPhpTypecast(mixed $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); $db->createCommand( diff --git a/tests/Column/Int8MultiRangeColumnTest.php b/tests/Column/Int8MultiRangeColumnTest.php index 09f1cef3e..ec5883481 100644 --- a/tests/Column/Int8MultiRangeColumnTest.php +++ b/tests/Column/Int8MultiRangeColumnTest.php @@ -5,16 +5,15 @@ namespace Yiisoft\Db\Pgsql\Tests\Column; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\Int8RangeValue; use Yiisoft\Db\Pgsql\Expression\MultiRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class Int8MultiRangeColumnTest extends TestCase +final class Int8MultiRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -53,7 +52,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['{[1,10),[20,30]}', '{[40,50)}']); + $db = $this->prepareConnection(['{[1,10),[20,30]}', '{[40,50)}']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -85,7 +84,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(array $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -95,9 +94,9 @@ public function testPhpTypecast(array $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $this->ensureMinPostgreSqlVersion('14.0'); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); diff --git a/tests/Column/Int8RangeColumnTest.php b/tests/Column/Int8RangeColumnTest.php index efffb0567..59983c4b1 100644 --- a/tests/Column/Int8RangeColumnTest.php +++ b/tests/Column/Int8RangeColumnTest.php @@ -5,15 +5,14 @@ namespace Yiisoft\Db\Pgsql\Tests\Column; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\Int8RangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class Int8RangeColumnTest extends TestCase +final class Int8RangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -43,7 +42,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['[1,10)', '(20,30]']); + $db = $this->prepareConnection(['[1,10)', '(20,30]']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -73,7 +72,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(mixed $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -83,9 +82,9 @@ public function testPhpTypecast(mixed $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); $db->createCommand( diff --git a/tests/Column/NumMultiRangeColumnTest.php b/tests/Column/NumMultiRangeColumnTest.php index 1b187afaf..a668ad7a9 100644 --- a/tests/Column/NumMultiRangeColumnTest.php +++ b/tests/Column/NumMultiRangeColumnTest.php @@ -5,16 +5,15 @@ namespace Yiisoft\Db\Pgsql\Tests\Column; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\MultiRangeValue; use Yiisoft\Db\Pgsql\Expression\NumRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class NumMultiRangeColumnTest extends TestCase +final class NumMultiRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -53,7 +52,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['{[1.5,10.5),[20.5,30.5]}', '{[40.5,50.5)}']); + $db = $this->prepareConnection(['{[1.5,10.5),[20.5,30.5]}', '{[40.5,50.5)}']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -85,7 +84,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(array $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -95,9 +94,9 @@ public function testPhpTypecast(array $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $this->ensureMinPostgreSqlVersion('14.0'); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); diff --git a/tests/Column/NumRangeColumnTest.php b/tests/Column/NumRangeColumnTest.php index f1b43885e..687980fa6 100644 --- a/tests/Column/NumRangeColumnTest.php +++ b/tests/Column/NumRangeColumnTest.php @@ -5,15 +5,14 @@ namespace Yiisoft\Db\Pgsql\Tests\Column; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\NumRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class NumRangeColumnTest extends TestCase +final class NumRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -43,7 +42,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['[1.5,10.5)', '(20.5,30.5]']); + $db = $this->prepareConnection(['[1.5,10.5)', '(20.5,30.5]']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -73,7 +72,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(mixed $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -83,9 +82,9 @@ public function testPhpTypecast(mixed $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); $db->createCommand( diff --git a/tests/Column/TsMultiRangeColumnTest.php b/tests/Column/TsMultiRangeColumnTest.php index 3dff98d15..a616a1308 100644 --- a/tests/Column/TsMultiRangeColumnTest.php +++ b/tests/Column/TsMultiRangeColumnTest.php @@ -6,16 +6,15 @@ use DateTimeImmutable; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\MultiRangeValue; use Yiisoft\Db\Pgsql\Expression\TsRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class TsMultiRangeColumnTest extends TestCase +final class TsMultiRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -80,7 +79,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['{["2024-01-01 10:00:00","2024-01-01 20:00:00"),["2024-01-02 10:00:00","2024-01-02 20:00:00"]}', '{["2024-01-03 10:00:00","2024-01-03 20:00:00")}']); + $db = $this->prepareConnection(['{["2024-01-01 10:00:00","2024-01-01 20:00:00"),["2024-01-02 10:00:00","2024-01-02 20:00:00"]}', '{["2024-01-03 10:00:00","2024-01-03 20:00:00")}']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -112,7 +111,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(array $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -122,9 +121,9 @@ public function testPhpTypecast(array $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $this->ensureMinPostgreSqlVersion('14.0'); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); diff --git a/tests/Column/TsRangeColumnTest.php b/tests/Column/TsRangeColumnTest.php index ed994930b..6a7c02bb1 100644 --- a/tests/Column/TsRangeColumnTest.php +++ b/tests/Column/TsRangeColumnTest.php @@ -6,15 +6,14 @@ use DateTimeImmutable; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\TsRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class TsRangeColumnTest extends TestCase +final class TsRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -104,7 +103,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['["2024-01-01 10:00:00","2024-01-01 20:00:00")', '("2024-01-01 20:00:00","2024-01-01 23:00:00"]']); + $db = $this->prepareConnection(['["2024-01-01 10:00:00","2024-01-01 20:00:00")', '("2024-01-01 20:00:00","2024-01-01 23:00:00"]']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -134,7 +133,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(mixed $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -144,9 +143,9 @@ public function testPhpTypecast(mixed $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); $db->createCommand( diff --git a/tests/Column/TsTzMultiRangeColumnTest.php b/tests/Column/TsTzMultiRangeColumnTest.php index f38f4c6d7..1b2ea05ea 100644 --- a/tests/Column/TsTzMultiRangeColumnTest.php +++ b/tests/Column/TsTzMultiRangeColumnTest.php @@ -6,16 +6,15 @@ use DateTimeImmutable; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\MultiRangeValue; use Yiisoft\Db\Pgsql\Expression\TsTzRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class TsTzMultiRangeColumnTest extends TestCase +final class TsTzMultiRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -80,7 +79,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['{["2024-01-01 10:00:00+00","2024-01-01 20:00:00+00"),["2024-01-02 10:00:00+00","2024-01-02 20:00:00+00"]}', '{["2024-01-03 10:00:00+00","2024-01-03 20:00:00+00")}']); + $db = $this->prepareConnection(['{["2024-01-01 10:00:00+00","2024-01-01 20:00:00+00"),["2024-01-02 10:00:00+00","2024-01-02 20:00:00+00"]}', '{["2024-01-03 10:00:00+00","2024-01-03 20:00:00+00")}']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -112,7 +111,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(array $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -122,9 +121,9 @@ public function testPhpTypecast(array $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $this->ensureMinPostgreSqlVersion('14.0'); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); diff --git a/tests/Column/TsTzRangeColumnTest.php b/tests/Column/TsTzRangeColumnTest.php index cffa3aeb0..a2e6f2837 100644 --- a/tests/Column/TsTzRangeColumnTest.php +++ b/tests/Column/TsTzRangeColumnTest.php @@ -6,15 +6,14 @@ use DateTimeImmutable; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Expression\TsTzRangeValue; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; -final class TsTzRangeColumnTest extends TestCase +final class TsTzRangeColumnTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function dataBase(): iterable { @@ -104,7 +103,7 @@ public static function dataBase(): iterable #[DataProvider('dataBase')] public function testBase(mixed $expectedColumnValue, mixed $value): void { - $db = $this->createConnection(['["2024-01-01 10:00:00+00","2024-01-01 20:00:00+00")', '("2024-01-01 20:00:00+00","2024-01-01 23:00:00+00"]']); + $db = $this->prepareConnection(['["2024-01-01 10:00:00+00","2024-01-01 20:00:00+00")', '("2024-01-01 20:00:00+00","2024-01-01 23:00:00+00"]']); $db->createCommand()->insert('tbl_test', ['col' => $value])->execute(); @@ -134,7 +133,7 @@ public static function dataPhpTypecast(): iterable #[DataProvider('dataPhpTypecast')] public function testPhpTypecast(mixed $expected, string $value): void { - $db = $this->createConnection([$value]); + $db = $this->prepareConnection([$value]); $result = $db->select('col')->from('tbl_test')->where(['id' => 1])->withTypecasting()->one(); @@ -144,9 +143,9 @@ public function testPhpTypecast(mixed $expected, string $value): void /** * @psalm-param list $values */ - private function createConnection(array $values = []): Connection + private function prepareConnection(array $values = []): Connection { - $db = TestConnection::get(); + $db = $this->getSharedConnection(); $db->createCommand('DROP TABLE IF EXISTS tbl_test')->execute(); $db->createCommand( diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 4955f8cdb..10bd375e0 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -10,7 +10,6 @@ use Yiisoft\Db\Pgsql\Column\ColumnFactory; use Yiisoft\Db\Pgsql\Tests\Provider\ColumnFactoryProvider; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Tests\Common\CommonColumnFactoryTest; diff --git a/tests/CommandTest.php b/tests/CommandTest.php index d7c8a7c79..ba80cec02 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -57,7 +57,8 @@ public function testBatchInsert( public function testBooleanValuesInsert(): void { $db = $this->getSharedConnection(); - $this->loadFixture();; + $this->loadFixture(); + ; $command = $db->createCommand(); $command->insert('{{bool_values}}', ['bool_col' => true]); @@ -91,7 +92,8 @@ public function testBooleanValuesInsert(): void public function testBooleanValuesBatchInsert(): void { $db = $this->getSharedConnection(); - $this->loadFixture();; + $this->loadFixture(); + ; $command = $db->createCommand(); $command->insertBatch('{{bool_values}}', [[true], [false]], ['bool_col']); @@ -120,7 +122,8 @@ public function testBooleanValuesBatchInsert(): void public function testDelete(): void { $db = $this->getSharedConnection(); - $this->loadFixture();; + $this->loadFixture(); + ; $command = $db->createCommand(); $command->delete('{{customer}}', ['id' => 2])->execute(); @@ -211,7 +214,8 @@ public function testUpsert(array $firstData, array $secondData): void public function testInsertReturningPksUuid(): void { $db = $this->getSharedConnection(); - $this->loadFixture();; + $this->loadFixture(); + ; $command = $db->createCommand(); $result = $command->insertReturningPks( @@ -233,7 +237,7 @@ public function testInsertReturningPksUuid(): void public function testShowDatabases(): void { - $db = TestConnection::get(); + $db = TestConnection::getShared(); $databases = $db->createCommand()->showDatabases(); diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 321073456..073ac1964 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -8,11 +8,8 @@ use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Column\ColumnFactory; -use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; -use Yiisoft\Db\Tests\Support\DbHelper; use Yiisoft\Db\Transaction\TransactionInterface; /** diff --git a/tests/JsonValueBuilderTest.php b/tests/JsonValueBuilderTest.php index b88b6b6be..d9804fac4 100644 --- a/tests/JsonValueBuilderTest.php +++ b/tests/JsonValueBuilderTest.php @@ -6,7 +6,6 @@ use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\Value\JsonValue; @@ -15,16 +14,17 @@ use Yiisoft\Db\Pgsql\Column\IntegerColumn; use Yiisoft\Db\Pgsql\Data\LazyArray; use Yiisoft\Db\Pgsql\Data\StructuredLazyArray; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Data\JsonLazyArray; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; /** * @group pgsql */ -final class JsonValueBuilderTest extends TestCase +final class JsonValueBuilderTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function buildProvider(): array { @@ -50,7 +50,7 @@ public static function buildProvider(): array #[DataProvider('buildProvider')] public function testBuild(mixed $value, string $expected): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; @@ -59,13 +59,11 @@ public function testBuild(mixed $value, string $expected): void $this->assertSame(':qp0', $builder->build($expression, $params)); $this->assertEquals([':qp0' => new Param($expected, DataType::STRING)], $params); - - $db->close(); } public function testBuildArrayValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; @@ -80,13 +78,11 @@ public function testBuildArrayValue(): void $this->assertSame('array_to_json(ARRAY[1,2,3]::int[])::jsonb', $builder->build($expression, $params)); $this->assertSame([], $params); - - $db->close(); } public function testBuildNull(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; @@ -95,13 +91,11 @@ public function testBuildNull(): void $this->assertSame('NULL', $builder->build($expression, $params)); $this->assertSame([], $params); - - $db->close(); } public function testBuildQueryExpression(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; @@ -115,13 +109,11 @@ public function testBuildQueryExpression(): void $this->assertSame('(SELECT "json_field" FROM "json_table")::jsonb', $builder->build($expression, $params)); $this->assertSame([], $params); - - $db->close(); } public function testBuildWithType(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; @@ -130,7 +122,5 @@ public function testBuildWithType(): void $this->assertSame(':qp0::jsonb', $builder->build($expression, $params)); $this->assertEquals([':qp0' => new Param('[1,2,3]', DataType::STRING)], $params); - - $db->close(); } } diff --git a/tests/PdoConnectionTest.php b/tests/PdoConnectionTest.php index 0260685f2..bd14ea8f6 100644 --- a/tests/PdoConnectionTest.php +++ b/tests/PdoConnectionTest.php @@ -4,13 +4,8 @@ namespace Yiisoft\Db\Pgsql\Tests; -use Throwable; -use Yiisoft\Db\Exception\Exception; use InvalidArgumentException; -use Yiisoft\Db\Exception\InvalidCallException; -use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonPdoConnectionTest; /** diff --git a/tests/PDODriverTest.php b/tests/PdoDriverTest.php similarity index 56% rename from tests/PDODriverTest.php rename to tests/PdoDriverTest.php index 8285e9775..716e29d8a 100644 --- a/tests/PDODriverTest.php +++ b/tests/PdoDriverTest.php @@ -5,34 +5,26 @@ namespace Yiisoft\Db\Pgsql\Tests; use PDO; -use PHPUnit\Framework\TestCase; -use Yiisoft\Db\Exception\Exception; -use Yiisoft\Db\Exception\InvalidConfigException; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; /** * @group pgsql - * - * @psalm-suppress PropertyNotSetInConstructor */ -final class PDODriverTest extends TestCase +final class PdoDriverTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; - /** - * @throws Exception - * @throws InvalidConfigException - */ public function testConnectionCharset(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); - $pdo = $db->getActivePDO(); + $pdo = $db->getActivePdo(); $charset = $pdo->query('SHOW client_encoding', PDO::FETCH_ASSOC)->fetch(); $this->assertEqualsIgnoringCase('UTF8', array_values($charset)[0]); - $pdoDriver = $this->getDriver(); + $pdoDriver = $this->createDriver(); $pdoDriver->charset('latin1'); $pdo = $pdoDriver->createConnection(); $charset = $pdo->query('SHOW client_encoding', PDO::FETCH_ASSOC)->fetch(); diff --git a/tests/QueryTest.php b/tests/QueryTest.php index bf7f66725..cb7d4162b 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -4,11 +4,7 @@ namespace Yiisoft\Db\Pgsql\Tests; -use Throwable; -use Yiisoft\Db\Exception\Exception; -use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Common\CommonQueryTest; diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 138ddfbb4..29a7642e6 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -19,7 +19,7 @@ use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Schema\TableSchemaInterface; use Yiisoft\Db\Tests\Common\CommonSchemaTest; -use Yiisoft\Db\Tests\Support\DbHelper; +use Yiisoft\Db\Tests\Support\TestHelper; /** * @group pgsql @@ -317,7 +317,7 @@ function ($params) use ($expectedTableName, $expectedSchemaName) { ), ) ->willReturn($commandMock); - $schema = new Schema($mockDb, DbHelper::getSchemaCache()); + $schema = new Schema($mockDb, TestHelper::createMemorySchemaCache()); $schema->getTableSchema($tableName); } @@ -404,7 +404,7 @@ public function testCustomTypeInNonDefaultSchema() 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.'); diff --git a/tests/StructuredValueBuilderTest.php b/tests/StructuredValueBuilderTest.php index f9c3e014f..6ad54fa01 100644 --- a/tests/StructuredValueBuilderTest.php +++ b/tests/StructuredValueBuilderTest.php @@ -14,18 +14,20 @@ use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Data\LazyArray; use Yiisoft\Db\Pgsql\Data\StructuredLazyArray; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\AbstractStructuredColumn; use Yiisoft\Db\Schema\Data\JsonLazyArray; use Yiisoft\Db\Tests\Support\Assert; +use Yiisoft\Db\Tests\Support\IntegrationTestCase; /** * @group pgsql */ -final class StructuredValueBuilderTest extends TestCase +final class StructuredValueBuilderTest extends IntegrationTestCase { - use TestTrait; + use IntegrationTestTrait; public static function buildProvider(): array { @@ -77,13 +79,13 @@ public static function buildProvider(): array [':qp0' => new Param('USD', DataType::STRING)], ], 'Query w/o type' => [ - (new Query(self::getDb()))->select('price')->from('product')->where(['id' => 1]), + (new Query(TestConnection::getShared()))->select('price')->from('product')->where(['id' => 1]), null, '(SELECT "price" FROM "product" WHERE "id" = 1)', [], ], 'Query' => [ - (new Query(self::getDb()))->select('price')->from('product')->where(['id' => 1]), + (new Query(TestConnection::getShared()))->select('price')->from('product')->where(['id' => 1]), 'currency_money', '(SELECT "price" FROM "product" WHERE "id" = 1)::currency_money', [], @@ -137,7 +139,7 @@ public function testBuild( string $expected, array $expectedParams, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; diff --git a/tests/Support/IntegrationTestTrait.php b/tests/Support/IntegrationTestTrait.php index 8edb3d309..98e0d9481 100644 --- a/tests/Support/IntegrationTestTrait.php +++ b/tests/Support/IntegrationTestTrait.php @@ -4,17 +4,24 @@ namespace Yiisoft\Db\Pgsql\Tests\Support; -use Yiisoft\Db\Cache\SchemaCache; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Driver; use Yiisoft\Db\Pgsql\Dsn; -use Yiisoft\Test\Support\SimpleCache\MemorySimpleCache; +use Yiisoft\Db\Tests\Support\TestHelper; trait IntegrationTestTrait { use BaseTestTrait; protected function createConnection(): Connection + { + return new Connection( + $this->createDriver(), + TestHelper::createMemorySchemaCache(), + ); + } + + protected function createDriver(): Driver { $host = getenv('YII_PGSQL_HOST') ?: '127.0.0.1'; $databaseName = getenv('YII_PGSQL_DATABASE') ?: 'yiitest'; @@ -31,11 +38,7 @@ protected function createConnection(): Connection $driver = new Driver($dsn, $username, $password); $driver->charset('utf8'); - $schemaCache = new SchemaCache( - new MemorySimpleCache(), - ); - - return new Connection($driver, $schemaCache); + return $driver; } protected function getDefaultFixture(): string diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 1f996d937..48922ef88 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -78,7 +78,7 @@ protected function getDriver(): Driver protected function ensureMinPostgreSqlVersion(string $version): void { - $currentVersion = TestConnection::get()->getServerInfo()->getVersion(); + $currentVersion = TestConnection::getShared()->getServerInfo()->getVersion(); if (version_compare($currentVersion, $version, '<')) { $this->markTestSkipped( "This test requires at least PostgreSQL version $version. Current version is $currentVersion.", diff --git a/tests/TestConnection.php b/tests/TestConnection.php index 0d47b40f1..5d40bd2c6 100644 --- a/tests/TestConnection.php +++ b/tests/TestConnection.php @@ -7,14 +7,14 @@ use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Driver; use Yiisoft\Db\Pgsql\Dsn; -use Yiisoft\Db\Tests\Support\DbHelper; +use Yiisoft\Db\Tests\Support\TestHelper; final class TestConnection { private static ?string $dsn = null; private static ?Connection $connection = null; - public static function get(): Connection + public static function getShared(): Connection { $db = self::$connection ??= self::create(); $db->getSchema()->refresh(); @@ -32,7 +32,7 @@ public static function dsn(): string public static function create(?string $dsn = null): Connection { - return new Connection(self::createDriver($dsn), DbHelper::getSchemaCache()); + return new Connection(self::createDriver($dsn), TestHelper::createMemorySchemaCache()); } public static function createDriver(?string $dsn = null): Driver From 7eb747bdb1e5089800c98449ebb78b97bdf6a134 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 17:24:08 +0300 Subject: [PATCH 18/35] step 17 --- tests/CommandTest.php | 5 ++- tests/Provider/CommandProvider.php | 5 --- tests/StructuredValueBuilderTest.php | 2 - tests/Support/TestTrait.php | 55 ---------------------------- 4 files changed, 3 insertions(+), 64 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index ba80cec02..6233cf7ec 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Pgsql\Tests; +use Closure; use PHPUnit\Framework\Attributes\DataProviderExternal; use Throwable; use Yiisoft\Db\Exception\NotSupportedException; @@ -197,7 +198,7 @@ public function testUpdate( string $table, array $columns, array|ExpressionInterface|string $conditions, - array|ExpressionInterface|string|null $from, + Closure $from, array $params, array $expectedValues, int $expectedCount, @@ -206,7 +207,7 @@ 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); } diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 5b8a13760..32a7d7b2b 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -8,14 +8,9 @@ use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\IndexMethod; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; final class CommandProvider extends \Yiisoft\Db\Tests\Provider\CommandProvider { - use TestTrait; - - protected static string $driverName = 'pgsql'; - public static function batchInsert(): array { $batchInsert = parent::batchInsert(); diff --git a/tests/StructuredValueBuilderTest.php b/tests/StructuredValueBuilderTest.php index 6ad54fa01..aaf7b343a 100644 --- a/tests/StructuredValueBuilderTest.php +++ b/tests/StructuredValueBuilderTest.php @@ -6,7 +6,6 @@ use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Expression\Value\StructuredValue; @@ -15,7 +14,6 @@ use Yiisoft\Db\Pgsql\Data\LazyArray; use Yiisoft\Db\Pgsql\Data\StructuredLazyArray; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\AbstractStructuredColumn; use Yiisoft\Db\Schema\Data\JsonLazyArray; diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 48922ef88..41310058b 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -5,18 +5,13 @@ namespace Yiisoft\Db\Pgsql\Tests\Support; use Yiisoft\Db\Pgsql\Connection; -use Yiisoft\Db\Pgsql\Driver; use Yiisoft\Db\Pgsql\Tests\TestConnection; -use Yiisoft\Db\Tests\Support\DbHelper; use function preg_replace; use function str_replace; trait TestTrait { - private string $dsn = ''; - private string $fixture = 'pgsql.sql'; - public static function setUpBeforeClass(): void { $db = self::getDb(); @@ -26,63 +21,13 @@ public static function setUpBeforeClass(): void $db->close(); } - protected function getConnection(bool $fixture = false): Connection - { - $db = TestConnection::create($this->getDsn()); - - if ($fixture) { - DbHelper::loadFixture($db, __DIR__ . "/Fixture/$this->fixture"); - } - - return $db; - } - protected static function getDb(): Connection { return TestConnection::create(); } - protected function getDsn(): string - { - if ($this->dsn === '') { - $this->dsn = TestConnection::dsn(); - } - - return $this->dsn; - } - - protected static function getDriverName(): string - { - return 'pgsql'; - } - protected static function replaceQuotes(string $sql): string { return str_replace(['\\[', '\\]'], ['[', ']'], preg_replace('/(\[\[)|((?dsn = $dsn; - } - - protected function setFixture(string $fixture): void - { - $this->fixture = $fixture; - } - - protected function getDriver(): Driver - { - return TestConnection::createDriver(); - } - - protected function ensureMinPostgreSqlVersion(string $version): void - { - $currentVersion = TestConnection::getShared()->getServerInfo()->getVersion(); - if (version_compare($currentVersion, $version, '<')) { - $this->markTestSkipped( - "This test requires at least PostgreSQL version $version. Current version is $currentVersion.", - ); - } - } } From 50c8d5a745be006d5bde52269595e71dc782e0a5 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 18:10:16 +0300 Subject: [PATCH 19/35] tmp --- tests/Provider/QueryBuilderProvider.php | 9 +++---- tests/QueryBuilderTest.php | 19 +++++++------- tests/Support/TestTrait.php | 33 ------------------------- 3 files changed, 13 insertions(+), 48 deletions(-) delete mode 100644 tests/Support/TestTrait.php diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index d09de7a43..c854a5d4b 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Pgsql\Tests\Provider; +use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Expression\Statement\WhenThen; @@ -14,20 +15,16 @@ use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Column\IntegerColumn; -use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\QueryBuilder\Condition\Equals; use Yiisoft\Db\QueryBuilder\Condition\LikeConjunction; use function array_replace; +use function in_array; use function version_compare; final class QueryBuilderProvider extends \Yiisoft\Db\Tests\Provider\QueryBuilderProvider { - use TestTrait; - - protected static string $driverName = 'pgsql'; - public static function alterColumn(): array { return [ @@ -180,7 +177,7 @@ public static function insertReturningPks(): array ], 'carry passed params (query)' => [ 'customer', - (new Query(self::getDb())) + static fn(ConnectionInterface $db) => $db ->select(['email', 'name', 'address', 'is_active', 'related_id']) ->from('customer') ->where( diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 79786789a..4dafaa6dc 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Pgsql\Tests; +use Closure; use PHPUnit\Framework\Attributes\DataProviderExternal; use PHPUnit\Framework\Attributes\TestWith; use Throwable; @@ -98,7 +99,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 { @@ -280,7 +281,7 @@ public function testDropIndex(): void #[DataProviderExternal(QueryBuilderProvider::class, 'insert')] public function testInsert( string $table, - array|QueryInterface $columns, + Closure|array|QueryInterface $columns, array $params, string $expectedSQL, array $expectedParams, @@ -291,12 +292,12 @@ public function testInsert( #[DataProviderExternal(QueryBuilderProvider::class, 'insertReturningPks')] public function testInsertReturningPks( string $table, - array|QueryInterface $columns, + Closure|array|QueryInterface $columns, array $params, - string $expectedSQL, + string $expectedSql, array $expectedParams, ): void { - parent::testInsertReturningPks($table, $columns, $params, $expectedSQL, $expectedParams); + parent::testInsertReturningPks($table, $columns, $params, $expectedSql, $expectedParams); } public function testRenameTable(): void @@ -402,7 +403,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 = [], @@ -413,7 +414,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, @@ -424,7 +425,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, @@ -555,7 +556,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, diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php deleted file mode 100644 index 41310058b..000000000 --- a/tests/Support/TestTrait.php +++ /dev/null @@ -1,33 +0,0 @@ -close(); - } - - protected static function getDb(): Connection - { - return TestConnection::create(); - } - - protected static function replaceQuotes(string $sql): string - { - return str_replace(['\\[', '\\]'], ['[', ']'], preg_replace('/(\[\[)|((? Date: Tue, 18 Nov 2025 19:29:30 +0300 Subject: [PATCH 20/35] improve --- tests/Provider/QueryBuilderProvider.php | 119 +++++++++++++----------- tests/QueryBuilderTest.php | 2 +- 2 files changed, 66 insertions(+), 55 deletions(-) diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index c854a5d4b..89fda6469 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -13,6 +13,7 @@ use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\Function\ArrayMerge; use Yiisoft\Db\Expression\Value\Param; +use Yiisoft\Db\Expression\Value\Value; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Column\IntegerColumn; use Yiisoft\Db\Query\Query; @@ -61,10 +62,10 @@ public static function buildCondition(): array return [ ...parent::buildCondition(), /** - * adding conditions for ILIKE i.e. case insensitive LIKE. - * - * {@see https://www.postgresql.org/docs/8.3/static/functions-matching.html#FUNCTIONS-LIKE} - */ + * adding conditions for ILIKE i.e. case insensitive LIKE. + * + * {@see https://www.postgresql.org/docs/8.3/static/functions-matching.html#FUNCTIONS-LIKE} + */ /* empty values */ [['like', 'name', [], 'caseSensitive' => false], '0=1', []], [['not like', 'name', [], 'caseSensitive' => false], '', []], @@ -90,7 +91,8 @@ public static function buildCondition(): array ], [ ['like', 'name', ['heyho', 'abc'], 'conjunction' => LikeConjunction::Or, 'caseSensitive' => false], - '"name" ILIKE :qp0 OR "name" ILIKE :qp1', [':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)], + '"name" ILIKE :qp0 OR "name" ILIKE :qp1', + [':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)], ], [ ['not like', 'name', ['heyho', 'abc'], 'conjunction' => LikeConjunction::Or, 'caseSensitive' => false], @@ -101,10 +103,10 @@ public static function buildCondition(): array /* Checks to verity that operators work correctly */ [['@>', 'id', new ArrayValue([1])], '"id" @> ARRAY[1]::int[]', []], [['<@', 'id', new ArrayValue([1])], '"id" <@ ARRAY[1]::int[]', []], - [['=', 'id', new ArrayValue([1])], '"id" = ARRAY[1]::int[]', []], + [['=', 'id', new ArrayValue([1])], '"id" = ARRAY[1]::int[]', []], [['<>', 'id', new ArrayValue([1])], '"id" <> ARRAY[1]::int[]', []], - [['>', 'id', new ArrayValue([1])], '"id" > ARRAY[1]::int[]', []], - [['<', 'id', new ArrayValue([1])], '"id" < ARRAY[1]::int[]', []], + [['>', 'id', new ArrayValue([1])], '"id" > ARRAY[1]::int[]', []], + [['<', 'id', new ArrayValue([1])], '"id" < ARRAY[1]::int[]', []], [['>=', 'id', new ArrayValue([1])], '"id" >= ARRAY[1]::int[]', []], [['<=', 'id', new ArrayValue([1])], '"id" <= ARRAY[1]::int[]', []], [['&&', 'id', new ArrayValue([1])], '"id" && ARRAY[1]::int[]', []], @@ -382,9 +384,9 @@ public static function overlapsCondition(): array $data['null'][1] = 0; $data['expression'][0] = new Expression("'{0,1,2,7}'"); - $data['query expression'][0] = (new Query(self::getDb()))->select(new ArrayValue([0,1,2,7])); + $data['query expression'][0] = (new Query(self::getDb()))->select(new ArrayValue([0, 1, 2, 7])); $data[] = [new Expression('ARRAY[0,1,2,7]'), 1]; - $data[] = [new ArrayValue([0,1,2,7]), 1]; + $data[] = [new ArrayValue([0, 1, 2, 7]), 1]; return $data; } @@ -537,10 +539,12 @@ public static function caseXBuilder(): array when1: new WhenThen(['column_name' => 1], 'a'), when2: new WhenThen( new Equals('column_name', 2), - $db->select(new Expression( - ':pv2::text', - [':pv2' => $param = new Param('b', DataType::STRING)], - )), + $db->select( + new Expression( + ':pv2::text', + [':pv2' => $param = new Param('b', DataType::STRING)], + ) + ), ), ), 'CASE WHEN "column_name" = 1 THEN :qp0 WHEN "column_name" = 2 THEN (SELECT :pv2::text) END', @@ -616,52 +620,59 @@ public static function lengthBuilder(): array ]; } - public static function multiOperandFunctionBuilder(): array + public static function multiOperandFunctionBuilder(): iterable { - $data = parent::multiOperandFunctionBuilder(); - - $stringQuery = self::getDb()->select(new Expression("'longest'::text")); - $stringQuerySql = "(SELECT 'longest'::text)"; - $stringParam = new Param('{3,4,5}', DataType::STRING); - - $db = self::getDb(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); - - if (version_compare($serverVersion, '10', '<')) { - unset($data['Longest with 1 operand'], $data['Shortest with 1 operand']); - } - - $data['Longest with 3 operands'][1][1] = $stringQuery; - $data['Longest with 3 operands'][2] = "(SELECT value FROM (SELECT :qp0 AS value UNION SELECT $stringQuerySql" + $data = iterator_to_array(parent::multiOperandFunctionBuilder()); + +// $db = self::getDb(); +// $serverVersion = $db->getServerInfo()->getVersion(); +// $db->close(); +// +// if (version_compare($serverVersion, '10', '<')) { +// unset($data['Longest with 1 operand'], $data['Shortest with 1 operand']); +// } + + $data['Longest with 3 operands'][1] = static fn(ConnectionInterface $db) => [ + new Value('short'), + $db->select(new Expression("'longest'::text")), + new Param('string', DataType::STRING), + ]; + $data['Longest with 3 operands'][2] = "(SELECT value FROM (SELECT :qp0 AS value UNION SELECT (SELECT 'longest'::text)" . ' AS value UNION SELECT :qp1 AS value) AS t ORDER BY LENGTH(value) DESC LIMIT 1)'; - $data['Shortest with 3 operands'][1][1] = $stringQuery; - $data['Shortest with 3 operands'][2] = "(SELECT value FROM (SELECT :qp0 AS value UNION SELECT $stringQuerySql" + $data['Shortest with 3 operands'][1] = static fn(Connectioninterface $db) => [ + new Value('short'), + $db->select(new Expression("'longest'::text")), + new Param('string', DataType::STRING), + ]; + $data['Shortest with 3 operands'][2] = "(SELECT value FROM (SELECT :qp0 AS value UNION SELECT (SELECT 'longest'::text)" . ' AS value UNION SELECT :qp1 AS value) AS t ORDER BY LENGTH(value) ASC LIMIT 1)'; - return [ - ...$data, - 'ArrayMerge with 1 operand' => [ - ArrayMerge::class, - [[1, 2, 3]], - '(ARRAY[1,2,3]::int[])', + yield from $data; + yield 'ArrayMerge with 1 operand' => [ + ArrayMerge::class, + [[1, 2, 3]], + '(ARRAY[1,2,3]::int[])', + [1, 2, 3], + ]; + yield 'ArrayMerge with 2 operands' => [ + ArrayMerge::class, + [[1, 2, 3], new Param('{3,4,5}', DataType::STRING)], + 'ARRAY(SELECT DISTINCT UNNEST(ARRAY[1,2,3]::int[] || :qp0))', + [1, 2, 3, 4, 5], + [':qp0' => new Param('{3,4,5}', DataType::STRING)], + ]; + yield 'ArrayMerge with 4 operands' => [ + ArrayMerge::class, + static fn(ConnectionInterface $db) => [ [1, 2, 3], + new ArrayValue([5, 6, 7]), + new Param('{3,4,5}', DataType::STRING), + $db->select(new ArrayValue([9, 10])) ], - 'ArrayMerge with 2 operands' => [ - ArrayMerge::class, - [[1, 2, 3], $stringParam], - 'ARRAY(SELECT DISTINCT UNNEST(ARRAY[1,2,3]::int[] || :qp0))', - [1, 2, 3, 4, 5], - [':qp0' => $stringParam], - ], - 'ArrayMerge with 4 operands' => [ - ArrayMerge::class, - [[1, 2, 3], new ArrayValue([5, 6, 7]), $stringParam, self::getDb()->select(new ArrayValue([9, 10]))], - 'ARRAY(SELECT DISTINCT UNNEST(ARRAY[1,2,3]::int[] || ARRAY[5,6,7]::int[] || :qp0 || (SELECT ARRAY[9,10]::int[])))', - [1, 2, 3, 4, 5, 6, 7, 9, 10], - [ - ':qp0' => $stringParam, - ], + 'ARRAY(SELECT DISTINCT UNNEST(ARRAY[1,2,3]::int[] || ARRAY[5,6,7]::int[] || :qp0 || (SELECT ARRAY[9,10]::int[])))', + [1, 2, 3, 4, 5, 6, 7, 9, 10], + [ + ':qp0' => new Param('{3,4,5}', DataType::STRING), ], ]; } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 4dafaa6dc..9729d8fb3 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -577,7 +577,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 = [], From 013859302fdb5fd7f3049e028e059c488891e76d Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 20:03:45 +0300 Subject: [PATCH 21/35] improve --- tests/Provider/QueryBuilderProvider.php | 40 +++++++++++-------------- tests/QueryBuilderTest.php | 16 +++++++--- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 89fda6469..3f64ea35c 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -16,7 +16,7 @@ use Yiisoft\Db\Expression\Value\Value; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Column\IntegerColumn; -use Yiisoft\Db\Query\Query; +use Yiisoft\Db\Pgsql\Tests\TestConnection; use Yiisoft\Db\QueryBuilder\Condition\Equals; use Yiisoft\Db\QueryBuilder\Condition\LikeConjunction; @@ -268,7 +268,7 @@ public static function upsert(): array 3 => 'INSERT INTO "T_upsert" ("email", "ts") VALUES (:qp0, extract(epoch from now()) * 1000) ON CONFLICT DO NOTHING', ], 'query, values and expressions with update part' => [ - 1 => (new Query(self::getDb())) + 1 => static fn(ConnectionInterface $db) => $db ->select( [ 'email' => new Expression(':phEmail', [':phEmail' => 'dynamic@example.com']), @@ -280,7 +280,7 @@ public static function upsert(): array . 'ON CONFLICT ("email") DO UPDATE SET "ts"=0, "orders"=EXCLUDED.orders + 1', ], 'query, values and expressions without update part' => [ - 1 => (new Query(self::getDb())) + 1 => static fn(ConnectionInterface $db) => $db ->select( [ 'email' => new Expression(':phEmail', [':phEmail' => 'dynamic@example.com']), @@ -384,7 +384,7 @@ public static function overlapsCondition(): array $data['null'][1] = 0; $data['expression'][0] = new Expression("'{0,1,2,7}'"); - $data['query expression'][0] = (new Query(self::getDb()))->select(new ArrayValue([0, 1, 2, 7])); + $data['query expression'][0] = static fn(ConnectionInterface $db) => $db->select(new ArrayValue([0, 1, 2, 7])); $data[] = [new Expression('ARRAY[0,1,2,7]'), 1]; $data[] = [new ArrayValue([0, 1, 2, 7]), 1]; @@ -455,9 +455,7 @@ public static function buildColumnDefinition(): array ColumnBuilder::string()->collation('C'), ]; - $db = self::getDb(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); + $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); if (version_compare($serverVersion, '13', '<')) { $uuidExpression = "uuid_in(overlay(overlay(md5(now()::text || random()::text) placing '4' from 13) placing" @@ -529,21 +527,20 @@ public static function caseXBuilder(): array { $data = parent::caseXBuilder(); - $db = self::getDb(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); + $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); if (version_compare($serverVersion, '10', '<')) { + $param = new Param('b', DataType::STRING); $data['without case expression'] = [ - new CaseX( + static fn(ConnectionInterface $db) => new CaseX( when1: new WhenThen(['column_name' => 1], 'a'), when2: new WhenThen( new Equals('column_name', 2), $db->select( new Expression( ':pv2::text', - [':pv2' => $param = new Param('b', DataType::STRING)], - ) + [':pv2' => $param], + ), ), ), ), @@ -613,8 +610,8 @@ public static function lengthBuilder(): array return [ ...parent::lengthBuilder(), 'query' => [ - self::getDb()->select(new Expression("'four'::text")), - self::replaceQuotes("LENGTH((SELECT 'four'::text))"), + static fn(ConnectionInterface $db) => $db->select(new Expression("'four'::text")), + "LENGTH((SELECT 'four'::text))", 4, ], ]; @@ -624,13 +621,10 @@ public static function multiOperandFunctionBuilder(): iterable { $data = iterator_to_array(parent::multiOperandFunctionBuilder()); -// $db = self::getDb(); -// $serverVersion = $db->getServerInfo()->getVersion(); -// $db->close(); -// -// if (version_compare($serverVersion, '10', '<')) { -// unset($data['Longest with 1 operand'], $data['Shortest with 1 operand']); -// } + $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); + if (version_compare($serverVersion, '10', '<')) { + unset($data['Longest with 1 operand'], $data['Shortest with 1 operand']); + } $data['Longest with 3 operands'][1] = static fn(ConnectionInterface $db) => [ new Value('short'), @@ -667,7 +661,7 @@ public static function multiOperandFunctionBuilder(): iterable [1, 2, 3], new ArrayValue([5, 6, 7]), new Param('{3,4,5}', DataType::STRING), - $db->select(new ArrayValue([9, 10])) + $db->select(new ArrayValue([9, 10])), ], 'ARRAY(SELECT DISTINCT UNNEST(ARRAY[1,2,3]::int[] || ARRAY[5,6,7]::int[] || :qp0 || (SELECT ARRAY[9,10]::int[])))', [1, 2, 3, 4, 5, 6, 7, 9, 10], diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 9729d8fb3..ac855d93a 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -475,11 +475,15 @@ public function testJsonOverlapsBuilder(): void } #[DataProviderExternal(QueryBuilderProvider::class, 'overlapsCondition')] - public function testOverlapsCondition(iterable|ExpressionInterface $values, int $expectedCount): void + public function testOverlapsCondition(Closure|iterable|ExpressionInterface $values, int $expectedCount): void { $db = $this->getSharedConnection(); $query = new Query($db); + if ($values instanceof Closure) { + $values = $values($db); + } + $count = $query ->from('array_and_json_types') ->where(new ArrayOverlaps('intarray_col', $values)) @@ -503,11 +507,15 @@ public function testOverlapsCondition(iterable|ExpressionInterface $values, int } #[DataProviderExternal(QueryBuilderProvider::class, 'overlapsCondition')] - public function testOverlapsConditionOperator(iterable|ExpressionInterface $values, int $expectedCount): void + public function testOverlapsConditionOperator(Closure|iterable|ExpressionInterface $values, int $expectedCount): void { $db = $this->getSharedConnection(); $query = new Query($db); + if ($values instanceof Closure) { + $values = $values($db); + } + $count = $query ->from('array_and_json_types') ->where(['array overlaps', 'intarray_col', $values]) @@ -531,7 +539,7 @@ public function testOverlapsConditionOperator(iterable|ExpressionInterface $valu } #[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); } @@ -566,7 +574,7 @@ public function testCaseXBuilder( #[DataProviderExternal(QueryBuilderProvider::class, 'lengthBuilder')] public function testLengthBuilder( - string|ExpressionInterface $operand, + Closure|string|ExpressionInterface $operand, string $expectedSql, int $expectedResult, array $expectedParams = [], From 22aababf9bd1c18c952329cfe04e568d946b58a5 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 18 Nov 2025 22:44:15 +0300 Subject: [PATCH 22/35] improve --- tests/Support/BaseTestTrait.php | 13 ------------- tests/Support/IntegrationTestTrait.php | 7 +++++-- 2 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 tests/Support/BaseTestTrait.php diff --git a/tests/Support/BaseTestTrait.php b/tests/Support/BaseTestTrait.php deleted file mode 100644 index 3e117e4ab..000000000 --- a/tests/Support/BaseTestTrait.php +++ /dev/null @@ -1,13 +0,0 @@ - Date: Tue, 18 Nov 2025 22:48:19 +0300 Subject: [PATCH 23/35] improve --- tests/CommandTest.php | 1 + tests/Provider/QueryBuilderProvider.php | 8 ++++---- tests/StructuredValueBuilderTest.php | 1 + tests/{ => Support}/TestConnection.php | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) rename tests/{ => Support}/TestConnection.php (97%) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 6233cf7ec..655bdca59 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -11,6 +11,7 @@ use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Pgsql\Tests\Provider\CommandProvider; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\Tests\Common\CommonCommandTest; use function serialize; diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 3f64ea35c..2170834be 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -7,16 +7,16 @@ use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Constant\PseudoType; -use Yiisoft\Db\Expression\Statement\WhenThen; -use Yiisoft\Db\Expression\Value\ArrayValue; -use Yiisoft\Db\Expression\Statement\CaseX; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\Function\ArrayMerge; +use Yiisoft\Db\Expression\Statement\CaseX; +use Yiisoft\Db\Expression\Statement\WhenThen; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Expression\Value\Value; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Column\IntegerColumn; -use Yiisoft\Db\Pgsql\Tests\TestConnection; +use Yiisoft\Db\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\QueryBuilder\Condition\Equals; use Yiisoft\Db\QueryBuilder\Condition\LikeConjunction; diff --git a/tests/StructuredValueBuilderTest.php b/tests/StructuredValueBuilderTest.php index aaf7b343a..bc83a184d 100644 --- a/tests/StructuredValueBuilderTest.php +++ b/tests/StructuredValueBuilderTest.php @@ -14,6 +14,7 @@ use Yiisoft\Db\Pgsql\Data\LazyArray; use Yiisoft\Db\Pgsql\Data\StructuredLazyArray; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\AbstractStructuredColumn; use Yiisoft\Db\Schema\Data\JsonLazyArray; diff --git a/tests/TestConnection.php b/tests/Support/TestConnection.php similarity index 97% rename from tests/TestConnection.php rename to tests/Support/TestConnection.php index 5d40bd2c6..cedf847af 100644 --- a/tests/TestConnection.php +++ b/tests/Support/TestConnection.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Db\Pgsql\Tests; +namespace Yiisoft\Db\Pgsql\Tests\Support; use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Driver; From aa925921a84585704b93df3a4f66e796a458f69a Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 10:48:11 +0300 Subject: [PATCH 24/35] fix --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 49be70b6a..2b2e39e5e 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ "vimeo/psalm": "^5.26.1 || ^6.8.8", "vlucas/phpdotenv": "^5.6.1", "yiisoft/aliases": "^2.0", - "yiisoft/cache-file": "^3.2", + "yiisoft/test-support": "^3.0", "yiisoft/var-dumper": "^1.7" }, "provide": { From 03657119ad252f921aebfd3d6a6d5ef8311d5332 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 10:50:17 +0300 Subject: [PATCH 25/35] fix --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 2b2e39e5e..b58afefa5 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,7 @@ "vimeo/psalm": "^5.26.1 || ^6.8.8", "vlucas/phpdotenv": "^5.6.1", "yiisoft/aliases": "^2.0", + "yiisoft/psr-dummy-provider": "^1.0", "yiisoft/test-support": "^3.0", "yiisoft/var-dumper": "^1.7" }, From 8098723ac964e69cf1e399a5d71a4cc1aec725ec Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 10:53:08 +0300 Subject: [PATCH 26/35] fix --- tests/Provider/QueryBuilderProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 2170834be..b6c5b4d4e 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -525,7 +525,7 @@ public static function prepareValue(): array public static function caseXBuilder(): array { - $data = parent::caseXBuilder(); + $data = iterator_to_array(parent::caseXBuilder()); $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); From ee524fb9a45f2661fcb2744618026a7acb650358 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 10:56:46 +0300 Subject: [PATCH 27/35] fix --- tests/CommandTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 655bdca59..29f970f7f 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -167,11 +167,12 @@ public function testGetRawSql(string $sql, array $params, string $expectedRawSql } /** - * {@link https://github.com/yiisoft/yii2/issues/11498} + * @link https://github.com/yiisoft/yii2/issues/11498 */ public function testSaveSerializedObject(): void { $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command = $command->insert( @@ -190,8 +191,6 @@ public function testSaveSerializedObject(): void $command->update('{{type}}', ['blob_col' => serialize($db)], ['char_col' => 'serialize']); $this->assertSame(1, $command->execute()); - - $db->close(); } #[DataProviderExternal(CommandProvider::class, 'update')] From 6e1a651eec3f624c6b1eaf33c4e4761e8d6482c7 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 12:19:48 +0300 Subject: [PATCH 28/35] improve --- tests/PdoDriverTest.php | 3 ++- tests/Support/IntegrationTestTrait.php | 24 +----------------------- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/tests/PdoDriverTest.php b/tests/PdoDriverTest.php index 716e29d8a..4fd15581f 100644 --- a/tests/PdoDriverTest.php +++ b/tests/PdoDriverTest.php @@ -6,6 +6,7 @@ use PDO; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\Tests\Support\IntegrationTestCase; /** @@ -24,7 +25,7 @@ public function testConnectionCharset(): void $this->assertEqualsIgnoringCase('UTF8', array_values($charset)[0]); - $pdoDriver = $this->createDriver(); + $pdoDriver = TestConnection::createDriver(); $pdoDriver->charset('latin1'); $pdo = $pdoDriver->createConnection(); $charset = $pdo->query('SHOW client_encoding', PDO::FETCH_ASSOC)->fetch(); diff --git a/tests/Support/IntegrationTestTrait.php b/tests/Support/IntegrationTestTrait.php index 314b5146d..3f613148b 100644 --- a/tests/Support/IntegrationTestTrait.php +++ b/tests/Support/IntegrationTestTrait.php @@ -5,8 +5,6 @@ namespace Yiisoft\Db\Pgsql\Tests\Support; use Yiisoft\Db\Pgsql\Connection; -use Yiisoft\Db\Pgsql\Driver; -use Yiisoft\Db\Pgsql\Dsn; use Yiisoft\Db\Tests\Support\TestHelper; trait IntegrationTestTrait @@ -14,31 +12,11 @@ trait IntegrationTestTrait protected function createConnection(): Connection { return new Connection( - $this->createDriver(), + TestConnection::createDriver(), TestHelper::createMemorySchemaCache(), ); } - protected function createDriver(): Driver - { - $host = getenv('YII_PGSQL_HOST') ?: '127.0.0.1'; - $databaseName = getenv('YII_PGSQL_DATABASE') ?: 'yiitest'; - $port = getenv('YII_PGSQL_PORT') ?: '5432'; - $username = getenv('YII_PGSQL_USER') ?: 'root'; - $password = getenv('YII_PGSQL_PASSWORD') ?: 'root'; - - $dsn = new Dsn( - host: $host, - databaseName: $databaseName, - port: $port, - ); - - $driver = new Driver($dsn, $username, $password); - $driver->charset('utf8'); - - return $driver; - } - protected function getDefaultFixture(): string { return __DIR__ . '/Fixture/pgsql.sql'; From 14eca77066bfb9ad9bea5fbe6829824b23ae5525 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 15:47:51 +0300 Subject: [PATCH 29/35] fix --- tests/CommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 29f970f7f..b38f48589 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -198,7 +198,7 @@ public function testUpdate( string $table, array $columns, array|ExpressionInterface|string $conditions, - Closure $from, + Closure|array|ExpressionInterface|string|null $from, array $params, array $expectedValues, int $expectedCount, From 10ad173c72062cf36379bba6fca1140acb8b9768 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 16:28:09 +0300 Subject: [PATCH 30/35] fix --- tests/SchemaTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 29a7642e6..66c680fa9 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -48,7 +48,7 @@ public function testBooleanDefaultValues(): void } #[DataProviderExternal(SchemaProvider::class, 'columns')] - public function testColumns(array $columns, string $tableName): void + public function testColumns(array $columns, string $tableName, ?string $dump = null): void { $db = $this->getSharedConnection(); @@ -58,7 +58,7 @@ public function testColumns(array $columns, string $tableName): void } } - $this->assertTableColumns($columns, $tableName); + $this->assertTableColumns($columns, $tableName, $dump); } public function testColumnTypeMapNoExist(): void From 3eaff8aceb12257676602eaba0aff2b9c3898006 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 19 Nov 2025 16:40:48 +0300 Subject: [PATCH 31/35] cleanup --- tests/CommandTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index b38f48589..31e5e5231 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -60,7 +60,6 @@ public function testBooleanValuesInsert(): void { $db = $this->getSharedConnection(); $this->loadFixture(); - ; $command = $db->createCommand(); $command->insert('{{bool_values}}', ['bool_col' => true]); @@ -95,7 +94,6 @@ public function testBooleanValuesBatchInsert(): void { $db = $this->getSharedConnection(); $this->loadFixture(); - ; $command = $db->createCommand(); $command->insertBatch('{{bool_values}}', [[true], [false]], ['bool_col']); @@ -125,7 +123,6 @@ public function testDelete(): void { $db = $this->getSharedConnection(); $this->loadFixture(); - ; $command = $db->createCommand(); $command->delete('{{customer}}', ['id' => 2])->execute(); @@ -216,7 +213,6 @@ public function testInsertReturningPksUuid(): void { $db = $this->getSharedConnection(); $this->loadFixture(); - ; $command = $db->createCommand(); $result = $command->insertReturningPks( From 41f3dcfda68fed6b3a8ffedc2f2779d74a72c597 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 20 Nov 2025 15:16:22 +0300 Subject: [PATCH 32/35] improve --- tests/ColumnTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index d74e5ca2d..4a972cf1f 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -23,6 +23,7 @@ use Yiisoft\Db\Pgsql\Column\StructuredColumn; use Yiisoft\Db\Pgsql\Tests\Provider\ColumnProvider; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\Column\DoubleColumn; @@ -408,4 +409,13 @@ protected function assertTypecastedValues(array $result, bool $allTypecasted = f $this->assertSame([1, 2, 3], $result['jsonb_col']); $this->assertSame([[[',', 'null', true, 'false', 'f']]], $result['jsonarray_col']); } + + protected function createTimestampDefaultValue(): mixed + { + return new Expression( + version_compare(TestConnection::getShared()->getServerInfo()->getVersion(), '10', '<') + ? 'now()' + : 'CURRENT_TIMESTAMP', + ); + } } From 9703de2e3e2f7853ba6a3ef3f2b6c6f8d1a10b08 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 20 Nov 2025 15:24:54 +0300 Subject: [PATCH 33/35] improve --- tests/ColumnTest.php | 2 +- tests/Provider/QueryBuilderProvider.php | 11 +++-------- tests/SchemaTest.php | 5 ++--- tests/Support/IntegrationTestTrait.php | 2 +- tests/Support/TestConnection.php | 5 +++++ 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index 4a972cf1f..585aa9fa1 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -413,7 +413,7 @@ protected function assertTypecastedValues(array $result, bool $allTypecasted = f protected function createTimestampDefaultValue(): mixed { return new Expression( - version_compare(TestConnection::getShared()->getServerInfo()->getVersion(), '10', '<') + version_compare(TestConnection::getServerVersion(), '10', '<') ? 'now()' : 'CURRENT_TIMESTAMP', ); diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index b6c5b4d4e..8c7307609 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -455,9 +455,7 @@ public static function buildColumnDefinition(): array ColumnBuilder::string()->collation('C'), ]; - $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); - - if (version_compare($serverVersion, '13', '<')) { + if (version_compare(TestConnection::getServerVersion(), '13', '<')) { $uuidExpression = "uuid_in(overlay(overlay(md5(now()::text || random()::text) placing '4' from 13) placing" . ' to_hex(floor(4 * random() + 8)::int)::text from 17)::cstring)'; @@ -527,9 +525,7 @@ public static function caseXBuilder(): array { $data = iterator_to_array(parent::caseXBuilder()); - $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); - - if (version_compare($serverVersion, '10', '<')) { + if (version_compare(TestConnection::getServerVersion(), '10', '<')) { $param = new Param('b', DataType::STRING); $data['without case expression'] = [ static fn(ConnectionInterface $db) => new CaseX( @@ -621,8 +617,7 @@ public static function multiOperandFunctionBuilder(): iterable { $data = iterator_to_array(parent::multiOperandFunctionBuilder()); - $serverVersion = TestConnection::getShared()->getServerInfo()->getVersion(); - if (version_compare($serverVersion, '10', '<')) { + if (version_compare(TestConnection::getServerVersion(), '10', '<')) { unset($data['Longest with 1 operand'], $data['Shortest with 1 operand']); } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 66c680fa9..bdf2e5e37 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -15,6 +15,7 @@ use Yiisoft\Db\Pgsql\Tests\Provider\SchemaProvider; use Yiisoft\Db\Pgsql\Tests\Provider\StructuredTypeProvider; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Schema\TableSchemaInterface; @@ -50,9 +51,7 @@ public function testBooleanDefaultValues(): void #[DataProviderExternal(SchemaProvider::class, 'columns')] public function testColumns(array $columns, string $tableName, ?string $dump = null): void { - $db = $this->getSharedConnection(); - - if (version_compare($db->getServerInfo()->getVersion(), '10', '>')) { + if (version_compare(TestConnection::getServerVersion(), '10', '>')) { if ($tableName === 'type') { $columns['timestamp_default']->defaultValue(new Expression('CURRENT_TIMESTAMP')); } diff --git a/tests/Support/IntegrationTestTrait.php b/tests/Support/IntegrationTestTrait.php index 3f613148b..e4e15fd38 100644 --- a/tests/Support/IntegrationTestTrait.php +++ b/tests/Support/IntegrationTestTrait.php @@ -24,7 +24,7 @@ protected function getDefaultFixture(): string protected function ensureMinPostgreSqlVersion(string $version): void { - $currentVersion = $this->getSharedConnection()->getServerInfo()->getVersion(); + $currentVersion = TestConnection::getServerVersion(); if (version_compare($currentVersion, $version, '<')) { $this->markTestSkipped( "This test requires at least PostgreSQL version $version. Current version is $currentVersion.", diff --git a/tests/Support/TestConnection.php b/tests/Support/TestConnection.php index cedf847af..ab69ca6d9 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( From 002e1056319a94b6e15806ad2565e5f71c7589e6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 20 Nov 2025 15:52:32 +0300 Subject: [PATCH 34/35] improve --- tests/QueryBuilderTest.php | 3 ++- tests/SchemaTest.php | 7 ++++--- tests/Support/Fixture/FixtureDump.php | 13 +++++++++++++ tests/Support/IntegrationTestTrait.php | 3 ++- 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 tests/Support/Fixture/FixtureDump.php diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index ac855d93a..6fc02da32 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -20,6 +20,7 @@ use Yiisoft\Db\Pgsql\Column\ArrayColumn; use Yiisoft\Db\Pgsql\Column\IntegerColumn; use Yiisoft\Db\Pgsql\Tests\Provider\QueryBuilderProvider; +use Yiisoft\Db\Pgsql\Tests\Support\Fixture\FixtureDump; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; @@ -348,7 +349,7 @@ public function testResetSequencePgsql12(): void $this->ensureMinPostgreSqlVersion('12.0'); $db = $this->getSharedConnection(); - $this->loadFixture(__DIR__ . '/Support/Fixture/pgsql12.sql'); + $this->loadFixture(FixtureDump::V12); $qb = $db->getQueryBuilder(); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index bdf2e5e37..fbdc1690a 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -14,6 +14,7 @@ use Yiisoft\Db\Pgsql\Schema; use Yiisoft\Db\Pgsql\Tests\Provider\SchemaProvider; use Yiisoft\Db\Pgsql\Tests\Provider\StructuredTypeProvider; +use Yiisoft\Db\Pgsql\Tests\Support\Fixture\FixtureDump; use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\Schema\Column\ColumnInterface; @@ -85,7 +86,7 @@ public function testGeneratedValues(): void $this->ensureMinPostgreSqlVersion('12.0'); $db = $this->getSharedConnection(); - $this->loadFixture(__DIR__ . '/Support/Fixture/pgsql12.sql'); + $this->loadFixture(FixtureDump::V12); $schema = $db->getSchema(); $table = $schema->getTableSchema('generated'); @@ -189,7 +190,7 @@ public function testPartitionedTable(): void $this->ensureMinPostgreSqlVersion('10.0'); $db = $this->getSharedConnection(); - $this->loadFixture(__DIR__ . '/Support/Fixture/pgsql10.sql'); + $this->loadFixture(FixtureDump::V10); $schema = $db->getSchema(); @@ -472,7 +473,7 @@ public function testTableIndexes(): void $this->ensureMinPostgreSqlVersion('11.0'); $db = $this->getSharedConnection(); - $this->loadFixture(__DIR__ . '/Support/Fixture/pgsql11.sql'); + $this->loadFixture(FixtureDump::V11); $schema = $db->getSchema(); diff --git a/tests/Support/Fixture/FixtureDump.php b/tests/Support/Fixture/FixtureDump.php new file mode 100644 index 000000000..c74f74fb5 --- /dev/null +++ b/tests/Support/Fixture/FixtureDump.php @@ -0,0 +1,13 @@ + Date: Thu, 20 Nov 2025 16:17:28 +0300 Subject: [PATCH 35/35] fix --- tests/SchemaTest.php | 39 --------------------------------------- 1 file changed, 39 deletions(-) diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index fbdc1690a..3a9b4bd82 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -232,45 +232,6 @@ public function testSequenceName(): void $this->assertEquals($sequenceName, $tableSchema->getSequenceName()); } - #[DataProviderExternal(SchemaProvider::class, 'tableSchemaCacheWithTablePrefixes')] - public function testTableSchemaCacheWithTablePrefixes( - string $tablePrefix, - string $tableName, - string $testTablePrefix, - string $testTableName, - ): void { - $db = $this->getSharedConnection(); - - $schema = $db->getSchema(); - $schema->enableCache(true); - $db->setTablePrefix($tablePrefix); - $noCacheTable = $schema->getTableSchema($tableName, true); - - $this->assertInstanceOf(TableSchemaInterface::class, $noCacheTable); - - /* Compare */ - $db->setTablePrefix($testTablePrefix); - $testNoCacheTable = $schema->getTableSchema($testTableName); - - $this->assertSame($noCacheTable, $testNoCacheTable); - - $db->setTablePrefix($tablePrefix); - $schema->refreshTableSchema($tableName); - $refreshedTable = $schema->getTableSchema($tableName); - - $this->assertInstanceOf(TableSchemaInterface::class, $refreshedTable); - $this->assertNotSame($noCacheTable, $refreshedTable); - - /* Compare */ - $db->setTablePrefix($testTablePrefix); - $schema->refreshTableSchema($testTablePrefix); - $testRefreshedTable = $schema->getTableSchema($testTableName); - - $this->assertInstanceOf(TableSchemaInterface::class, $testRefreshedTable); - $this->assertSame($refreshedTable, $testRefreshedTable); - $this->assertNotSame($testNoCacheTable, $testRefreshedTable); - } - #[DataProviderExternal(SchemaProvider::class, 'constraints')] #[DataProviderExternal(SchemaProvider::class, 'constraintsOfView')] public function testTableSchemaConstraints(string $tableName, string $type, mixed $expected): void