diff --git a/composer.json b/composer.json index c647b45b..2c335cf6 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,8 @@ "vimeo/psalm": "^5.26.1 || ^6.8.8", "vlucas/phpdotenv": "^5.6.1", "yiisoft/aliases": "^2.0", - "yiisoft/cache-file": "^3.2", + "yiisoft/psr-dummy-provider": "^1.0", + "yiisoft/test-support": "^3.0", "yiisoft/var-dumper": "^1.7" }, "provide": { diff --git a/tests/BatchQueryResultTest.php b/tests/BatchQueryResultTest.php index 6813a400..635b1a2d 100644 --- a/tests/BatchQueryResultTest.php +++ b/tests/BatchQueryResultTest.php @@ -4,15 +4,13 @@ namespace Yiisoft\Db\Oracle\Tests; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonBatchQueryResultTest; /** * @group oracle - * - * @psalm-suppress PropertyNotSetInConstructor */ final class BatchQueryResultTest extends CommonBatchQueryResultTest { - use TestTrait; + use IntegrationTestTrait; } diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php index 9f5a50e1..f24ad3f1 100644 --- a/tests/ColumnBuilderTest.php +++ b/tests/ColumnBuilderTest.php @@ -6,15 +6,15 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Oracle\Tests\Provider\ColumnBuilderProvider; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\AbstractColumnBuilderTest; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Common\CommonColumnBuilderTest; /** * @group oracle */ -class ColumnBuilderTest extends AbstractColumnBuilderTest +class ColumnBuilderTest extends CommonColumnBuilderTest { - use TestTrait; + use IntegrationTestTrait; #[DataProviderExternal(ColumnBuilderProvider::class, 'buildingMethods')] public function testBuildingMethods( diff --git a/tests/ColumnDefinitionParserTest.php b/tests/ColumnDefinitionParserTest.php index df02a28c..4d758587 100644 --- a/tests/ColumnDefinitionParserTest.php +++ b/tests/ColumnDefinitionParserTest.php @@ -4,17 +4,17 @@ namespace Yiisoft\Db\Oracle\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Oracle\Column\ColumnDefinitionParser; -use Yiisoft\Db\Tests\AbstractColumnDefinitionParserTest; +use Yiisoft\Db\Oracle\Tests\Provider\ColumnDefinitionParserProvider; +use Yiisoft\Db\Tests\Common\CommonColumnDefinitionParserTest; /** * @group oracle */ -final class ColumnDefinitionParserTest extends AbstractColumnDefinitionParserTest +final class ColumnDefinitionParserTest extends CommonColumnDefinitionParserTest { - /** - * @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnDefinitionParserProvider::parse - */ + #[DataProviderExternal(ColumnDefinitionParserProvider::class, 'parse')] public function testParse(string $definition, array $expected): void { parent::testParse($definition, $expected); diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index d7b7a94d..85a11831 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -7,16 +7,16 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Oracle\Column\ColumnFactory; use Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Schema\Column\ColumnInterface; -use Yiisoft\Db\Tests\AbstractColumnFactoryTest; +use Yiisoft\Db\Tests\Common\CommonColumnFactoryTest; /** * @group oracle */ -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 diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index 12e04abb..3dda064a 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -15,7 +15,9 @@ use Yiisoft\Db\Oracle\Column\ColumnBuilder; use Yiisoft\Db\Oracle\Column\JsonColumn; use Yiisoft\Db\Oracle\Tests\Provider\ColumnProvider; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\Fixture\FixtureDump; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Oracle\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\Column\DoubleColumn; @@ -33,21 +35,14 @@ */ final class ColumnTest extends CommonColumnTest { - use TestTrait; + use IntegrationTestTrait; public function testQueryWithTypecasting(): void { - $db = $this->getConnection(); - $version = $db->getServerInfo()->getVersion(); - $db->close(); - - $isOldVersion = version_compare($version, '21', '<'); - - if (!$isOldVersion) { - $this->fixture = 'oci21.sql'; - } + $db = $this->getSharedConnection(); + $isOldVersion = version_compare(TestConnection::getServerVersion(), '21', '<'); - $db = $this->getConnection(true); + $this->loadFixture($isOldVersion ? FixtureDump::DEFAULT : FixtureDump::OCI21); $this->insertTypeValues($db); @@ -66,17 +61,10 @@ public function testQueryWithTypecasting(): void public function testCommandWithPhpTypecasting(): void { - $db = $this->getConnection(); - $version = $db->getServerInfo()->getVersion(); - $db->close(); - - $isOldVersion = version_compare($version, '21', '<'); - - if (!$isOldVersion) { - $this->fixture = 'oci21.sql'; - } + $db = $this->getSharedConnection(); + $isOldVersion = version_compare(TestConnection::getServerVersion(), '21', '<'); - $db = $this->getConnection(true); + $this->loadFixture($isOldVersion ? FixtureDump::DEFAULT : FixtureDump::OCI21); $this->insertTypeValues($db); @@ -95,7 +83,7 @@ public function testCommandWithPhpTypecasting(): void public function testSelectWithPhpTypecasting(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $sql = "SELECT null, 1, 2.5, 'string' FROM DUAL"; @@ -141,27 +129,20 @@ public function testSelectWithPhpTypecasting(): void public function testPhpTypecast(): void { - $db = $this->getConnection(); - $version = $db->getServerInfo()->getVersion(); - $db->close(); + $db = $this->getSharedConnection(); + $isOldVersion = version_compare(TestConnection::getServerVersion(), '21', '<'); - if (version_compare($version, '21', '>=')) { - $this->fixture = 'oci21.sql'; - } + $this->loadFixture($isOldVersion ? FixtureDump::DEFAULT : FixtureDump::OCI21); parent::testPhpTypecast(); } public function testColumnInstance(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); + $isOldVersion = version_compare(TestConnection::getServerVersion(), '21', '<'); - if (version_compare($db->getServerInfo()->getVersion(), '21', '>=')) { - $this->fixture = 'oci21.sql'; - } - - $db->close(); - $db = $this->getConnection(true); + $this->loadFixture($isOldVersion ? FixtureDump::DEFAULT : FixtureDump::OCI21); $schema = $db->getSchema(); $tableSchema = $schema->getTableSchema('type'); @@ -221,7 +202,9 @@ public function testJsonColumn(): void public function testUniqueColumn(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $schema = $db->getSchema(); $this->assertTrue($schema->getTableSchema('T_constraints_1')?->getColumn('C_unique')->isUnique()); @@ -233,7 +216,7 @@ public function testUniqueColumn(): void public function testTimestampColumnOnDifferentTimezones(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); $schema = $db->getSchema(); $command = $db->createCommand(); $tableName = 'timestamp_column_test'; diff --git a/tests/CommandTest.php b/tests/CommandTest.php index fa20c70c..c5b1c44a 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Oracle\Tests; +use Closure; use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\PseudoType; @@ -14,7 +15,9 @@ use Yiisoft\Db\Oracle\Column\ColumnBuilder; use Yiisoft\Db\Oracle\IndexType; use Yiisoft\Db\Oracle\Tests\Provider\CommandProvider; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\Fixture\FixtureDump; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Oracle\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\Tests\Common\CommonCommandTest; @@ -30,13 +33,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(); @@ -59,13 +60,11 @@ public function testBatchInsert( array $expectedParams = [], int $insertedRow = 1, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); + $version = $db->getServerInfo()->getVersion(); + $isOldVersion = version_compare($version, '21', '<'); - if (version_compare($db->getServerInfo()->getVersion(), '21', '>=')) { - $this->fixture = 'oci21.sql'; - } - - $db->close(); + $this->loadFixture($isOldVersion ? FixtureDump::DEFAULT : FixtureDump::OCI21); parent::testBatchInsert($table, $values, $columns, $expected, $expectedParams, $insertedRow); } @@ -73,7 +72,7 @@ public function testBatchInsert( /** @link https://github.com/yiisoft/db-oracle/issues/284 */ public function testBatchInsertWithAutoincrement(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); try { @@ -101,7 +100,7 @@ public function testBatchInsertWithAutoincrement(): void public function testCLOBStringInsertion(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -130,7 +129,8 @@ public function testCLOBStringInsertion(): void public function testCreateTable(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -171,7 +171,7 @@ public function testCreateTable(): void public function testCreateView(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -229,7 +229,7 @@ public function testCreateView(): void public function testDropDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); @@ -243,7 +243,7 @@ public function testDropDefaultValue(): void public function testDropTableIfExists(): void { - $command = $this->getConnection()->createCommand(); + $command = $this->getSharedConnection()->createCommand(); $this->expectException(NotSupportedException::class); $this->expectExceptionMessage('Oracle doesn\'t support "IF EXISTS" option on drop table.'); @@ -268,7 +268,8 @@ public function testGetRawSql(string $sql, array $params, string $expectedRawSql public function testsInsertQueryAsColumnValue(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $time = (string) time(); @@ -304,7 +305,7 @@ public function testsInsertQueryAsColumnValue(): void public function testInsertReturningPksWithPrimaryKeyString(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -327,7 +328,7 @@ public function testInsertReturningPksWithPrimaryKeyString(): void public function testInsertReturningPksWithPrimaryKeySignedDecimal(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -358,7 +359,7 @@ public function testInsertReturningPksWithQuery(): void public function testInsertSelectAlias(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $command->delete('{{customer}}')->execute(); @@ -413,7 +414,8 @@ public function testInsertSelectAlias(): void #[DataProviderExternal(CommandProvider::class, 'insertVarbinary')] public function testInsertVarbinary(mixed $expectedData, mixed $testData): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command->delete('{{T_upsert_varbinary}}')->execute(); @@ -433,7 +435,8 @@ public function testInsertVarbinary(mixed $expectedData, mixed $testData): void public function testNoTablenameReplacement(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); @@ -485,7 +488,7 @@ public function testUpdate( string $table, array $columns, array|ExpressionInterface|string $conditions, - array|ExpressionInterface|string|null $from, + Closure|array|ExpressionInterface|string|null $from, array $params, array $expectedValues, int $expectedCount, @@ -498,7 +501,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); } @@ -506,13 +509,13 @@ public function testUpsert(array $firstData, array $secondData): void #[DataProviderExternal(CommandProvider::class, 'upsertReturning')] public function testUpsertReturning( string $table, - array|QueryInterface $insertColumns, + Closure|array|QueryInterface $insertColumns, array|bool $updateColumns, ?array $returnColumns, array $selectCondition, array $expectedValues, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $this->expectException(NotSupportedException::class); @@ -531,7 +534,7 @@ public function testUpsertReturningWithUnique(): void public function testUpsertReturningPks(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $this->expectException(NotSupportedException::class); @@ -542,7 +545,7 @@ public function testUpsertReturningPks(): void public function testUpsertReturningPksEmptyValues() { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $this->expectException(NotSupportedException::class); @@ -553,7 +556,7 @@ public function testUpsertReturningPksEmptyValues() public function testUpsertReturningPksWithPhpTypecasting(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->expectException(NotSupportedException::class); $this->expectExceptionMessage('Yiisoft\Db\Oracle\DMLQueryBuilder::upsertReturning() is not supported by Oracle.'); @@ -565,7 +568,8 @@ public function testUpsertReturningPksWithPhpTypecasting(): void public function testQueryScalarWithBlob(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $value = json_encode(['test'], JSON_THROW_ON_ERROR); $db->createCommand()->insert('{{%T_upsert_varbinary}}', ['id' => 1, 'blob_col' => $value])->execute(); @@ -586,7 +590,12 @@ public function testProfilerData(?string $sql = null): void public function testShowDatabases(): void { - $this->assertSame([self::getDatabaseName()], self::getDb()->createCommand()->showDatabases()); + $db = $this->getSharedConnection(); + + $this->assertSame( + [TestConnection::databaseName()], + $db->createCommand()->showDatabases(), + ); } #[DataProviderExternal(CommandProvider::class, 'createIndex')] @@ -597,9 +606,9 @@ public function testCreateIndex(array $columns, array $indexColumns, ?string $in public function testCreateSearchIndex() { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); - if (version_compare($db->getServerInfo()->getVersion(), '21', '<')) { + if (version_compare(TestConnection::getServerVersion(), '21', '<')) { $this->markTestSkipped('Search index is supported since Oracle 21'); } @@ -626,4 +635,9 @@ public function testCreateSearchIndex() $db->close(); } + + protected function getUpsertTestCharCast(): string + { + return 'CAST([[address]] AS VARCHAR(255))'; + } } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 3dae7049..07f84401 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -10,9 +10,10 @@ use Yiisoft\Db\Oracle\Column\ColumnBuilder; use Yiisoft\Db\Oracle\Column\ColumnFactory; use Yiisoft\Db\Oracle\Connection; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Oracle\Tests\Support\TestConnection; use Yiisoft\Db\Tests\Common\CommonConnectionTest; -use Yiisoft\Db\Tests\Support\DbHelper; +use Yiisoft\Db\Tests\Support\TestHelper; use Yiisoft\Db\Transaction\TransactionInterface; /** @@ -20,11 +21,11 @@ */ final class ConnectionTest extends CommonConnectionTest { - use TestTrait; + use IntegrationTestTrait; public function testSerialize(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $db->open(); $serialized = serialize($db); @@ -38,7 +39,7 @@ public function testSerialize(): void public function testSettingDefaultAttributes(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->assertSame(PDO::ERRMODE_EXCEPTION, $db->getActivePDO()?->getAttribute(PDO::ATTR_ERRMODE)); @@ -47,7 +48,7 @@ public function testSettingDefaultAttributes(): void public function testTransactionIsolation(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $transaction = $db->beginTransaction(TransactionInterface::READ_COMMITTED); $transaction->commit(); @@ -66,7 +67,8 @@ public function testTransactionIsolation(): void public function testTransactionShortcutCustom(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); @@ -97,21 +99,23 @@ static function (ConnectionInterface $db) { public function testSerialized(): void { - $connection = $this->getConnection(); + $connection = $this->createConnection(); $connection->open(); $serialized = serialize($connection); - $this->assertNotNull($connection->getPDO()); + $this->assertNotNull($connection->getPdo()); $unserialized = unserialize($serialized); $this->assertInstanceOf(PdoConnectionInterface::class, $unserialized); - $this->assertNull($unserialized->getPDO()); + $this->assertNull($unserialized->getPdo()); $this->assertEquals(123, $unserialized->createCommand('SELECT 123 FROM DUAL')->queryScalar()); - $this->assertNotNull($connection->getPDO()); + $this->assertNotNull($connection->getPdo()); + + $connection->close(); } public function getColumnBuilderClass(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); @@ -120,7 +124,7 @@ public function getColumnBuilderClass(): void public function testGetColumnFactory(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory()); @@ -131,7 +135,11 @@ public function testUserDefinedColumnFactory(): void { $columnFactory = new ColumnFactory(); - $db = new Connection($this->getDriver(), DbHelper::getSchemaCache(), $columnFactory); + $db = new Connection( + TestConnection::createDriver(), + TestHelper::createMemorySchemaCache(), + $columnFactory, + ); $this->assertSame($columnFactory, $db->getColumnFactory()); diff --git a/tests/DsnTest.php b/tests/DsnTest.php index 67c2e74d..14aa687b 100644 --- a/tests/DsnTest.php +++ b/tests/DsnTest.php @@ -9,8 +9,6 @@ /** * @group oracle - * - * @psalm-suppress PropertyNotSetInConstructor */ final class DsnTest extends TestCase { diff --git a/tests/PdoCommandTest.php b/tests/PdoCommandTest.php index 8a4ff82a..dc48675e 100644 --- a/tests/PdoCommandTest.php +++ b/tests/PdoCommandTest.php @@ -4,21 +4,19 @@ namespace Yiisoft\Db\Oracle\Tests; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use PHPUnit\Framework\Attributes\DataProviderExternal; +use Yiisoft\Db\Oracle\Tests\Provider\CommandPdoProvider; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonPdoCommandTest; /** * @group oracle - * - * @psalm-suppress PropertyNotSetInConstructor */ final class PdoCommandTest extends CommonPdoCommandTest { - use TestTrait; + use IntegrationTestTrait; - /** - * @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandPDOProvider::bindParam - */ + #[DataProviderExternal(CommandPdoProvider::class, 'bindParam')] public function testBindParam( string $field, string $name, @@ -31,9 +29,7 @@ public function testBindParam( parent::testBindParam($field, $name, $value, $dataType, $length, $driverOptions, $expected); } - /** - * @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandPDOProvider::bindParamsNonWhere - */ + #[DataProviderExternal(CommandPdoProvider::class, 'bindParamsNonWhere')] public function testBindParamsNonWhere(string $sql): void { parent::testBindParamsNonWhere($sql); diff --git a/tests/PdoConnectionTest.php b/tests/PdoConnectionTest.php index 7df8211f..ff92e261 100644 --- a/tests/PdoConnectionTest.php +++ b/tests/PdoConnectionTest.php @@ -4,33 +4,24 @@ namespace Yiisoft\Db\Oracle\Tests; -use Throwable; -use Yiisoft\Db\Exception\Exception; use InvalidArgumentException; -use Yiisoft\Db\Exception\InvalidCallException; -use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Oracle\ServerInfo; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonPdoConnectionTest; +use function strlen; + /** * @group oracle - * - * @psalm-suppress PropertyNotSetInConstructor */ final class PdoConnectionTest extends CommonPdoConnectionTest { - use TestTrait; - - /** - * @throws Exception - * @throws InvalidConfigException - * @throws InvalidCallException - * @throws Throwable - */ + use IntegrationTestTrait; + public function testGetLastInsertID(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command->insert('item', ['name' => 'Yii2 starter', 'category_id' => 1])->execute(); @@ -41,15 +32,10 @@ public function testGetLastInsertID(): void $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(); @@ -61,16 +47,10 @@ public function testGetLastInsertIDWithException(): void $db->getLastInsertId(); } - /** - * @throws Exception - * @throws InvalidConfigException - * @throws InvalidCallException - * @throws Throwable - */ public function testGetLastInsertIdWithTwoConnection() { - $db1 = $this->getConnection(); - $db2 = $this->getConnection(); + $db1 = $this->createConnection(); + $db2 = $this->createConnection(); $sql = 'INSERT INTO {{profile}}([[description]]) VALUES (\'non duplicate1\')'; $db1->createCommand($sql)->execute(); @@ -87,7 +67,7 @@ public function testGetLastInsertIdWithTwoConnection() public function testGetServerInfo(): void { - $db = $this->getConnection(); + $db = $this->createConnection(); $serverInfo = $db->getServerInfo(); $this->assertInstanceOf(ServerInfo::class, $serverInfo); diff --git a/tests/Provider/CommandPDOProvider.php b/tests/Provider/CommandPdoProvider.php similarity index 97% rename from tests/Provider/CommandPDOProvider.php rename to tests/Provider/CommandPdoProvider.php index 83a2d25a..c08d6b80 100644 --- a/tests/Provider/CommandPDOProvider.php +++ b/tests/Provider/CommandPdoProvider.php @@ -6,7 +6,7 @@ use PDO; -final class CommandPDOProvider +final class CommandPdoProvider { public static function bindParam(): array { diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index ceb3ac70..054266f4 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -4,13 +4,11 @@ namespace Yiisoft\Db\Oracle\Tests\Provider; -use JsonException; use PDO; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Oracle\Column\ColumnBuilder; use Yiisoft\Db\Oracle\IndexType; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\Support\DbHelper; +use Yiisoft\Db\Oracle\Tests\Support\OracleTestHelper; use function array_merge; use function json_encode; @@ -18,10 +16,6 @@ final class CommandProvider extends \Yiisoft\Db\Tests\Provider\CommandProvider { - use TestTrait; - - protected static string $driverName = 'oci'; - public static function batchInsert(): array { $batchInsert = parent::batchInsert(); @@ -81,7 +75,7 @@ public static function batchInsert(): array ]; foreach ($replaceParams as $key => $expectedParams) { - DbHelper::changeSqlForOracleBatchInsert($batchInsert[$key]['expected'], $expectedParams); + OracleTestHelper::changeSqlForBatchInsert($batchInsert[$key]['expected'], $expectedParams); $batchInsert[$key]['expectedParams'] = array_merge($batchInsert[$key]['expectedParams'], $expectedParams); } @@ -115,9 +109,6 @@ public static function batchInsert(): array return $batchInsert; } - /** - * @throws JsonException - */ public static function insertVarbinary(): array { return [ diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 446fbe19..9d449b6d 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -5,32 +5,26 @@ namespace Yiisoft\Db\Oracle\Tests\Provider; use Exception; +use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Constraint\ForeignKey; +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\Expression; -use Yiisoft\Db\Expression\Function\ArrayMerge; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Oracle\Column\ColumnBuilder; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; -use Yiisoft\Db\Query\Query; -use Yiisoft\Db\Tests\Support\DbHelper; +use Yiisoft\Db\Oracle\Tests\Support\OracleTestHelper; +use Yiisoft\Db\Oracle\Tests\Support\TestConnection; use function array_replace; -/** - * @psalm-suppress PropertyNotSetInConstructor - */ final class QueryBuilderProvider extends \Yiisoft\Db\Tests\Provider\QueryBuilderProvider { - use TestTrait; - - protected static string $driverName = 'oci'; protected static string $likeEscapeCharSql = " ESCAPE '!'"; protected static array $likeParameterReplacements = [ '\%' => '!%', @@ -70,8 +64,7 @@ public static function batchInsert(): array ]; foreach ($batchInsert as $key => &$value) { - DbHelper::changeSqlForOracleBatchInsert($value['expected'], $replaceParams[$key] ?? []); - + OracleTestHelper::changeSqlForBatchInsert($value['expected'], $replaceParams[$key] ?? []); foreach ($replaceParams[$key] ?? [] as $param => $val) { $value['expectedParams'][$param] = new Param($val, DataType::STRING); } @@ -108,7 +101,7 @@ public static function buildLikeCondition(): array * may or may not quote \. */ try { - $encodedBackslash = substr(self::getDb()->quoteValue('\\\\'), 1, -1); + $encodedBackslash = substr(TestConnection::getShared()->quoteValue('\\\\'), 1, -1); self::$likeParameterReplacements[$encodedBackslash] = '\\'; } catch (Exception) { @@ -205,7 +198,7 @@ public static function upsert(): array SQL, ], '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']), @@ -218,7 +211,7 @@ public static function upsert(): array SQL, ], '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']), @@ -386,9 +379,8 @@ public static function buildColumnDefinition(): array $values['reference($reference)'][0] = 'number(10) REFERENCES "ref_table" ("id") ON DELETE SET NULL'; $values['reference($referenceWithSchema)'][0] = 'number(10) REFERENCES "ref_schema"."ref_table" ("id") ON DELETE SET NULL'; - $db = self::getDb(); - - if (version_compare($db->getServerInfo()->getVersion(), '21', '>=')) { + $version = TestConnection::getServerVersion(); + if (version_compare($version, '21', '>=')) { $values['array()'][0] = 'json'; $values['structured()'][0] = 'json'; $values['json()'][0] = 'json'; @@ -412,8 +404,6 @@ public static function buildColumnDefinition(): array ]; } - $db->close(); - return [ ...$values, @@ -464,7 +454,7 @@ public static function prepareValue(): array public static function caseXBuilder(): array { - $data = parent::caseXBuilder(); + $data = iterator_to_array(parent::caseXBuilder()); $data['with case expression'] = [ new CaseX( @@ -513,7 +503,7 @@ public static function multiOperandFunctionClasses(): array public static function multiOperandFunctionBuilder(): array { - $data = parent::multiOperandFunctionBuilder(); + $data = iterator_to_array(parent::multiOperandFunctionBuilder()); $data['Greatest with 4 operands'][2] = 'GREATEST(1, 1.5, (1 + 2), (SELECT 10 FROM DUAL))'; $data['Least with 4 operands'][2] = 'LEAST(1, 1.5, (1 + 2), (SELECT 10 FROM DUAL))'; @@ -555,7 +545,12 @@ public static function multiOperandFunctionBuilder(): array ], 'ArrayMerge with 4 operands' => [ ArrayMerge::class, - [[1, 2, 3], new ArrayValue([5, 6, 7]), $stringParam, self::getDb()->select(new ArrayValue([9, 10]))], + static fn(ConnectionInterface $db) => [ + [1, 2, 3], + new ArrayValue([5, 6, 7]), + $stringParam, + $db->select(new ArrayValue([9, 10])), + ], '(SELECT JSON_ARRAYAGG(value) AS value FROM (' . "SELECT value FROM JSON_TABLE(:qp0, '$[*]' COLUMNS(value PATH '$'))" . " UNION SELECT value FROM JSON_TABLE(:qp1, '$[*]' COLUMNS(value PATH '$'))" diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index 07f79000..2efbaf4f 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -13,7 +13,7 @@ use Yiisoft\Db\Oracle\Column\BooleanColumn; use Yiisoft\Db\Oracle\Column\DateTimeColumn; use Yiisoft\Db\Oracle\Column\JsonColumn; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\TestConnection; use Yiisoft\Db\Schema\Column\DoubleColumn; use Yiisoft\Db\Schema\Column\IntegerColumn; use Yiisoft\Db\Schema\Column\StringColumn; @@ -21,13 +21,9 @@ final class SchemaProvider extends \Yiisoft\Db\Tests\Provider\SchemaProvider { - use TestTrait; - public static function columns(): array { - $db = self::getDb(); - $dbTimezone = self::getDb()->getServerInfo()->getTimezone(); - $db->close(); + $dbTimezone = TestConnection::getShared()->getServerInfo()->getTimezone(); return [ [ @@ -44,8 +40,8 @@ public static function columns(): array ), 'tinyint_col' => new IntegerColumn( dbType: 'number', - size: 3, scale: 0, + size: 3, defaultValue: 1, ), 'smallint_col' => new IntegerColumn( @@ -93,8 +89,8 @@ public static function columns(): array 'numeric_col' => new DoubleColumn( ColumnType::DECIMAL, dbType: 'number', - size: 5, scale: 2, + size: 5, defaultValue: 33.22, ), 'timestamp_col' => new DateTimeColumn( @@ -112,26 +108,26 @@ public static function columns(): array 'time_col' => new DateTimeColumn( ColumnType::TIME, dbType: 'interval day to second', - size: 0, scale: 0, + size: 0, defaultValue: new DateTimeImmutable('10:33:21', new DateTimeZone('UTC')), shouldConvertTimezone: true, ), 'interval_day_col' => new StringColumn( dbType: 'interval day to second', - size: 0, scale: 1, + size: 0, defaultValue: new Expression("INTERVAL '2 04:56:12' DAY(1) TO SECOND(0)"), ), 'bool_col' => new BooleanColumn( - dbType: 'char', check: '"bool_col" in (0,1)', + dbType: 'char', notNull: true, size: 1, ), 'bool_col2' => new BooleanColumn( - dbType: 'char', check: '"bool_col2" in (0,1)', + dbType: 'char', size: 1, defaultValue: true, ), @@ -144,14 +140,14 @@ public static function columns(): array 'bit_col' => new IntegerColumn( dbType: 'number', notNull: true, - size: 3, scale: 0, + size: 3, defaultValue: 130, // b'10000010' ), 'json_col' => new JsonColumn( + check: '"json_col" is json', dbType: 'clob', defaultValue: ['a' => 1], - check: '"json_col" is json', ), ], 'type', @@ -159,10 +155,10 @@ public static function columns(): array [ [ 'id' => new IntegerColumn( + autoIncrement: true, dbType: 'number', primaryKey: true, notNull: true, - autoIncrement: true, scale: 0, ), 'type' => new StringColumn( diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index ab4bc91e..81769533 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Oracle\Tests; +use Closure; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProviderExternal; use PHPUnit\Framework\Attributes\TestWith; @@ -17,7 +18,7 @@ use Yiisoft\Db\Expression\Function\ArrayMerge; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Oracle\Tests\Provider\QueryBuilderProvider; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\Schema\Column\ArrayColumn; @@ -31,7 +32,7 @@ */ final class QueryBuilderTest extends CommonQueryBuilderTest { - use TestTrait; + use IntegrationTestTrait; public function getBuildColumnDefinitionProvider(): array { @@ -40,7 +41,7 @@ public function getBuildColumnDefinitionProvider(): array public function testAddDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -67,7 +68,7 @@ public function testAddForeignKey( public function testAddForeignKeyUpdateException(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -108,7 +109,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 { @@ -126,7 +127,7 @@ public function testBuildLikeCondition( public function testBuildOrderByAndLimit(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $query = (new Query($db)) @@ -161,7 +162,7 @@ public function testBuildWithFrom(mixed $table, string $expectedSql, array $expe public function testBuildWithLimit(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $query = (new Query($db))->limit(10); @@ -182,7 +183,7 @@ public function testBuildWithLimit(): void public function testBuildWithOffset(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $query = (new Query($db))->offset(10); @@ -209,7 +210,7 @@ public function testBuildWithWhereExists(string $cond, string $expectedQuerySql) public function testCheckIntegrity(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -221,7 +222,7 @@ public function testCheckIntegrity(): void public function testCreateTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -258,7 +259,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(); @@ -274,7 +276,7 @@ public function testDropCommentFromColumn(): void public function testDropCommentFromTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -290,7 +292,8 @@ public function testDropCommentFromTable(): void public function testDropDefaultValue(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $qb = $db->getQueryBuilder(); @@ -304,7 +307,7 @@ public function testDropDefaultValue(): void public function testDropIndex(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -321,7 +324,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, @@ -332,24 +335,27 @@ 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 { + $db = $this->getSharedConnection(); + $this->loadFixture(); + + $queryBuilder = $db->getQueryBuilder(); + $this->expectException(NotSupportedException::class); $this->expectExceptionMessage( 'Yiisoft\Db\Oracle\DMLQueryBuilder::insertReturningPks is not supported by Oracle.', ); - $db = $this->getConnection(true); - $qb = $db->getQueryBuilder(); - $qb->insertReturningPks($table, $columns, $params); + $queryBuilder->insertReturningPks($table, $columns, $params); } public function testRenameTable(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -365,7 +371,8 @@ public function testRenameTable(): void public function testResetSequence(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $qb = $db->getQueryBuilder(); @@ -436,7 +443,9 @@ public function testResetSequence(): void public function testResetNonExistSequenceException(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $qb = $db->getQueryBuilder(); $this->expectException(InvalidArgumentException::class); @@ -450,7 +459,9 @@ public function testResetSequenceCompositeException(): void { self::markTestSkipped('Sequence name not found for composite primary key'); - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); + $qb = $db->getQueryBuilder(); $this->expectException(InvalidArgumentException::class); @@ -462,7 +473,7 @@ public function testResetSequenceCompositeException(): void public function testSelectExists(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $sql = 'SELECT 1 FROM "customer" WHERE "id" = 1'; @@ -477,7 +488,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 = [], @@ -492,7 +503,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, @@ -503,13 +514,13 @@ 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, array $expectedParams, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $this->expectException(NotSupportedException::class); @@ -520,7 +531,7 @@ public function testUpsertReturning( public function testDefaultValues(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $queryBuilder = $db->getQueryBuilder(); // Non-primary key columns should have DEFAULT as value @@ -537,7 +548,7 @@ public function testSelectScalar(array|bool|float|int|string $columns, string $e } #[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); } @@ -564,7 +575,7 @@ public function testPrepareValue(string $expected, mixed $value): void public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade): void { if ($ifExists) { - $qb = $this->getConnection()->getQueryBuilder(); + $qb = $this->getSharedConnection()->getQueryBuilder(); $this->expectException(NotSupportedException::class); $this->expectExceptionMessage('Oracle doesn\'t support "IF EXISTS" option on drop table.'); @@ -585,7 +596,7 @@ public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade) #[DataProviderExternal(QueryBuilderProvider::class, 'caseXBuilder')] public function testCaseXBuilder( - CaseX $case, + Closure|CaseX $case, string $expectedSql, array $expectedParams, string|int $expectedResult, @@ -595,7 +606,7 @@ public function testCaseXBuilder( #[DataProviderExternal(QueryBuilderProvider::class, 'lengthBuilder')] public function testLengthBuilder( - string|ExpressionInterface $operand, + Closure|string|ExpressionInterface $operand, string $expectedSql, int $expectedResult, array $expectedParams = [], @@ -606,7 +617,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 = [], @@ -629,7 +640,7 @@ public function testArrayMergeWithTypeWithOrdering( string $operandType, string $expectedResult, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $stringParam = new Param('[4,3,5]', DataType::STRING); @@ -637,7 +648,7 @@ public function testArrayMergeWithTypeWithOrdering( [2, 1, 3], new ArrayValue([6, 5, 7]), $stringParam, - self::getDb()->select(new ArrayValue([10, 9])), + $db->select(new ArrayValue([10, 9])), ))->type($type)->ordered(); $params = []; diff --git a/tests/QueryGetTableAliasTest.php b/tests/QueryGetTableAliasTest.php index 115ec341..12557261 100644 --- a/tests/QueryGetTableAliasTest.php +++ b/tests/QueryGetTableAliasTest.php @@ -4,15 +4,13 @@ namespace Yiisoft\Db\Oracle\Tests; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\AbstractQueryGetTableAliasTest; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Common\CommonQueryGetTableAliasTest; /** * @group oracle - * - * @psalm-suppress PropertyNotSetInConstructor */ -final class QueryGetTableAliasTest extends AbstractQueryGetTableAliasTest +final class QueryGetTableAliasTest extends CommonQueryGetTableAliasTest { - use TestTrait; + use IntegrationTestTrait; } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index a69aa712..3054492f 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -4,18 +4,16 @@ namespace Yiisoft\Db\Oracle\Tests; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Common\CommonQueryTest; /** * @group oracle - * - * @psalm-suppress PropertyNotSetInConstructor */ final class QueryTest extends CommonQueryTest { - use TestTrait; + use IntegrationTestTrait; /** * Ensure no ambiguous column error occurs on indexBy with JOIN. @@ -24,7 +22,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]] name"; diff --git a/tests/QuoterTest.php b/tests/QuoterTest.php index e2df60ba..c4c31f18 100644 --- a/tests/QuoterTest.php +++ b/tests/QuoterTest.php @@ -6,15 +6,15 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Oracle\Tests\Provider\QuoterProvider; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\AbstractQuoterTest; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Tests\Common\CommonQuoterTest; /** * @group oracle */ -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 diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index a1d494f9..b0f08e9e 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -13,10 +13,12 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Oracle\Schema; use Yiisoft\Db\Oracle\Tests\Provider\SchemaProvider; -use Yiisoft\Db\Oracle\Tests\Support\TestTrait; +use Yiisoft\Db\Oracle\Tests\Support\Fixture\FixtureDump; +use Yiisoft\Db\Oracle\Tests\Support\IntegrationTestTrait; +use Yiisoft\Db\Oracle\Tests\Support\TestConnection; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Tests\Common\CommonSchemaTest; -use Yiisoft\Db\Tests\Support\DbHelper; +use Yiisoft\Db\Tests\Support\TestHelper; use function version_compare; @@ -25,28 +27,27 @@ */ final class SchemaTest extends CommonSchemaTest { - use TestTrait; + use IntegrationTestTrait; #[DataProviderExternal(SchemaProvider::class, 'columns')] - public function testColumns(array $columns, string $tableName = 'type'): void + public function testColumns(array $columns, string $tableName = 'type', ?string $dump = null): void { - $db = $this->getConnection(); - $version21 = version_compare($db->getServerInfo()->getVersion(), '21', '>='); - $db->close(); + $db = $this->getSharedConnection(); + $version21 = version_compare(TestConnection::getServerVersion(), '21', '>='); if ($version21 && $tableName === 'type') { - $this->fixture = 'oci21.sql'; - + $dump = FixtureDump::OCI21; $columns['json_col']->dbType('json'); $columns['json_col']->check(null); } - parent::testColumns($columns, $tableName); + parent::testColumns($columns, $tableName, $dump); } public function testCompositeFk(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $table = $schema->getTableSchema('composite_fk'); @@ -70,7 +71,7 @@ public function testCompositeFk(): void public function testGetDefaultSchema(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $schema = $db->getSchema(); @@ -89,11 +90,12 @@ public function testGetSchemaDefaultValues(): void public function testGetSchemaNames(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); - if (version_compare($db->getServerInfo()->getVersion(), '12', '>')) { + if (version_compare(TestConnection::getServerVersion(), '12', '>')) { $this->assertContains('SYSBACKUP', $schema->getSchemaNames()); } else { $this->assertEmpty($schema->getSchemaNames()); @@ -104,7 +106,8 @@ public function testGetSchemaNames(): void public function testGetTableNamesWithSchema(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $tablesNames = $schema->getTableNames('SYSTEM'); @@ -149,7 +152,8 @@ public function testGetTableNamesWithSchema(): void public function testGetViewNames(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $views = $schema->getViewNames(); @@ -161,7 +165,8 @@ public function testGetViewNames(): void public function testGetViewNamesWithSchema(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $schema = $db->getSchema(); $views = $schema->getViewNames('SYSTEM'); @@ -195,7 +200,7 @@ public function testTableSchemaWithDbSchemes( string $expectedTableName, string $expectedSchemaName = '', ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $commandMock = $this->createMock(CommandInterface::class); $commandMock->method('queryAll')->willReturn([]); @@ -215,7 +220,7 @@ function ($params) use ($expectedTableName, $expectedSchemaName) { ), ) ->willReturn($commandMock); - $schema = new Schema($mockDb, DbHelper::getSchemaCache(), 'dbo'); + $schema = new Schema($mockDb, TestHelper::createMemorySchemaCache(), 'dbo'); $schema->getTablePrimaryKey($tableName, true); $db->close(); @@ -234,7 +239,7 @@ public function testWorkWithDefaultValueConstraint(): void public function testNotConnectionPDO(): void { $db = $this->createMock(ConnectionInterface::class); - $schema = new Schema($db, DbHelper::getSchemaCache(), 'system'); + $schema = new Schema($db, TestHelper::createMemorySchemaCache(), 'system'); $this->expectException(NotSupportedException::class); $this->expectExceptionMessage('Only PDO connections are supported.'); diff --git a/tests/SqlParserTest.php b/tests/SqlParserTest.php index bac0da11..056604a1 100644 --- a/tests/SqlParserTest.php +++ b/tests/SqlParserTest.php @@ -4,15 +4,17 @@ namespace Yiisoft\Db\Oracle\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Oracle\SqlParser; -use Yiisoft\Db\Tests\AbstractSqlParserTest; +use Yiisoft\Db\Oracle\Tests\Provider\SqlParserProvider; +use Yiisoft\Db\Tests\Common\CommonSqlParserTest; /** * @group oracle */ -final class SqlParserTest extends AbstractSqlParserTest +final class SqlParserTest extends CommonSqlParserTest { - /** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\SqlParserProvider::getNextPlaceholder */ + #[DataProviderExternal(SqlParserProvider::class, 'getNextPlaceholder')] public function testGetNextPlaceholder(string $sql, ?string $expectedPlaceholder, ?int $expectedPosition): void { parent::testGetNextPlaceholder($sql, $expectedPlaceholder, $expectedPosition); diff --git a/tests/Support/Fixture/FixtureDump.php b/tests/Support/Fixture/FixtureDump.php new file mode 100644 index 00000000..414b07c5 --- /dev/null +++ b/tests/Support/Fixture/FixtureDump.php @@ -0,0 +1,11 @@ + $value) { + $str = match ($value) { + '1' => preg_replace('/\bTRUE\b/i', $param, $str, 1), + '0' => preg_replace('/\bFALSE\b/i', $param, $str, 1), + default => $str, + }; + } + } +} diff --git a/tests/Support/TestConnection.php b/tests/Support/TestConnection.php new file mode 100644 index 00000000..35ae4679 --- /dev/null +++ b/tests/Support/TestConnection.php @@ -0,0 +1,78 @@ +getSchema()->refresh(); + return $db; + } + + public static function getServerVersion(): string + { + return self::getShared()->getServerInfo()->getVersion(); + } + + public static function dsn(): string + { + return self::$dsn ??= (string) new Dsn( + host: self::host(), + databaseName: self::sid(), + port: self::port(), + options: ['charset' => 'AL32UTF8'], + ); + } + + public static function create(?string $dsn = null): Connection + { + return new Connection(self::createDriver($dsn), TestHelper::createMemorySchemaCache()); + } + + public static function createDriver(?string $dsn = null): Driver + { + return new Driver($dsn ?? self::dsn(), self::username(), self::password()); + } + + public static function databaseName(): string + { + return getenv('YII_ORACLE_DATABASE') ?: 'YIITEST'; + } + + private static function sid(): string + { + return getenv('YII_ORACLE_SID') ?: 'XE'; + } + + private static function host(): string + { + return getenv('YII_ORACLE_HOST') ?: 'localhost'; + } + + private static function port(): string + { + return getenv('YII_ORACLE_PORT') ?: '1521'; + } + + private static function username(): string + { + return getenv('YII_ORACLE_USER') ?: 'system'; + } + + private static function password(): string + { + return getenv('YII_ORACLE_PASSWORD') ?: 'root'; + } +} diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php deleted file mode 100644 index 30ca9000..00000000 --- a/tests/Support/TestTrait.php +++ /dev/null @@ -1,115 +0,0 @@ -close(); - } - - protected function getConnection(bool $fixture = false): Connection - { - $db = new Connection($this->getDriver(), DbHelper::getSchemaCache()); - - if ($fixture) { - DbHelper::loadFixture($db, __DIR__ . "/Fixture/$this->fixture"); - } - - return $db; - } - - protected static function getDb(): Connection - { - $dsn = (string) new Dsn( - host: self::getHost(), - databaseName: self::getSid(), - port: self::getPort(), - options: ['charset' => 'AL32UTF8'], - ); - - return new Connection(new Driver($dsn, self::getUsername(), self::getPassword()), DbHelper::getSchemaCache()); - } - - protected function getDsn(): string - { - if ($this->dsn === '') { - $this->dsn = (string) new Dsn( - host: self::getHost(), - databaseName: self::getSid(), - port: self::getPort(), - options: ['charset' => 'AL32UTF8'], - ); - } - - return $this->dsn; - } - - protected static function getDriverName(): string - { - return 'oci'; - } - - protected static function replaceQuotes(string $sql): string - { - return str_replace(['[[', ']]'], '"', $sql); - } - - protected function setDsn(string $dsn): void - { - $this->dsn = $dsn; - } - - protected function getDriver(): Driver - { - return new Driver($this->getDsn(), self::getUsername(), self::getPassword()); - } - - private static function getSid(): string - { - return getenv('YII_ORACLE_SID') ?: 'XE'; - } - - private static function getDatabaseName(): string - { - return getenv('YII_ORACLE_DATABASE') ?: 'YIITEST'; - } - - private static function getHost(): string - { - return getenv('YII_ORACLE_HOST') ?: 'localhost'; - } - - private static function getPort(): string - { - return getenv('YII_ORACLE_PORT') ?: '1521'; - } - - private static function getUsername(): string - { - return getenv('YII_ORACLE_USER') ?: 'system'; - } - - private static function getPassword(): string - { - return getenv('YII_ORACLE_PASSWORD') ?: 'root'; - } -}