diff --git a/composer.json b/composer.json index 49be70b6a..b58afefa5 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,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/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/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; } 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/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( 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); diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 8d811defb..10bd375e0 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -9,23 +9,23 @@ 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\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; 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 +35,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 +54,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 diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index 93a590538..585aa9fa1 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -22,7 +22,8 @@ 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\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\Column\DoubleColumn; @@ -39,11 +40,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 +166,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 +182,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 +269,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 +289,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')] @@ -410,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::getServerVersion(), '10', '<') + ? 'now()' + : 'CURRENT_TIMESTAMP', + ); + } } diff --git a/tests/CommandTest.php b/tests/CommandTest.php index e0a2b4bb6..31e5e5231 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -4,12 +4,14 @@ namespace Yiisoft\Db\Pgsql\Tests; +use Closure; use PHPUnit\Framework\Attributes\DataProviderExternal; use Throwable; 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\Pgsql\Tests\Support\TestConnection; use Yiisoft\Db\Tests\Common\CommonCommandTest; use function serialize; @@ -19,13 +21,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 +58,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 +92,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 +121,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 +143,7 @@ public function testDelete(): void public function testDropDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); @@ -161,11 +164,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->getConnection(); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $command = $command->insert( @@ -184,8 +188,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')] @@ -193,7 +195,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, @@ -202,14 +204,15 @@ public function testUpdate( } #[DataProviderExternal(CommandProvider::class, 'upsert')] - public function testUpsert(array $firstData, array $secondData): void + public function testUpsert(Closure|array $firstData, Closure|array $secondData): void { parent::testUpsert($firstData, $secondData); } public function testInsertReturningPksUuid(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $command = $db->createCommand(); $result = $command->insertReturningPks( @@ -231,7 +234,7 @@ public function testInsertReturningPksUuid(): void public function testShowDatabases(): void { - $db = TestConnection::get(); + $db = TestConnection::getShared(); $databases = $db->createCommand()->showDatabases(); @@ -246,4 +249,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/ConnectionTest.php b/tests/ConnectionTest.php index e10a52a25..073ac1964 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -8,10 +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\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; -use Yiisoft\Db\Tests\Support\DbHelper; use Yiisoft\Db\Transaction\TransactionInterface; /** @@ -19,11 +17,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 +33,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 +54,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 +84,8 @@ public function testTransactionIsolation(): void public function testTransactionShortcutCustom(): void { - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $this->assertTrue( $db->transaction( @@ -109,13 +108,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 +121,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(); - } } 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/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/PdoConnectionTest.php b/tests/PdoConnectionTest.php index 778acb03d..bd14ea8f6 100644 --- a/tests/PdoConnectionTest.php +++ b/tests/PdoConnectionTest.php @@ -4,32 +4,21 @@ 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\TestTrait; +use Yiisoft\Db\Pgsql\Tests\Support\IntegrationTestTrait; 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 +31,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 +46,5 @@ public function testGetLastInsertIDWithException(): void $this->expectExceptionMessage('PostgreSQL not support lastInsertId without sequence name.'); $db->getLastInsertId(); - - $db->close(); } } 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..4fd15581f 100644 --- a/tests/PDODriverTest.php +++ b/tests/PdoDriverTest.php @@ -5,34 +5,27 @@ 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\Pgsql\Tests\Support\TestConnection; +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 = TestConnection::createDriver(); $pdoDriver->charset('latin1'); $pdo = $pdoDriver->createConnection(); $charset = $pdo->query('SHOW client_encoding', PDO::FETCH_ASSOC)->fetch(); 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 { 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/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index d09de7a43..8c7307609 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -4,30 +4,28 @@ 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; -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\Support\TestTrait; -use Yiisoft\Db\Query\Query; +use Yiisoft\Db\Pgsql\Tests\Support\TestConnection; 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 [ @@ -64,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], '', []], @@ -93,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], @@ -104,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[]', []], @@ -180,7 +179,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( @@ -269,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']), @@ -281,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']), @@ -385,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] = 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]; + $data[] = [new ArrayValue([0, 1, 2, 7]), 1]; return $data; } @@ -456,11 +455,7 @@ public static function buildColumnDefinition(): array ColumnBuilder::string()->collation('C'), ]; - $db = self::getDb(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); - - 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)'; @@ -528,22 +523,21 @@ public static function prepareValue(): array public static function caseXBuilder(): array { - $data = parent::caseXBuilder(); - - $db = self::getDb(); - $serverVersion = $db->getServerInfo()->getVersion(); - $db->close(); + $data = iterator_to_array(parent::caseXBuilder()); - if (version_compare($serverVersion, '10', '<')) { + if (version_compare(TestConnection::getServerVersion(), '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)], - )), + $db->select( + new Expression( + ':pv2::text', + [':pv2' => $param], + ), + ), ), ), 'CASE WHEN "column_name" = 1 THEN :qp0 WHEN "column_name" = 2 THEN (SELECT :pv2::text) END', @@ -612,59 +606,62 @@ 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, ], ]; } - public static function multiOperandFunctionBuilder(): array + public static function multiOperandFunctionBuilder(): iterable { - $data = parent::multiOperandFunctionBuilder(); + $data = iterator_to_array(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', '<')) { + if (version_compare(TestConnection::getServerVersion(), '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['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 c0ba76b3e..6fc02da32 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -4,11 +4,11 @@ namespace Yiisoft\Db\Pgsql\Tests; +use Closure; use PHPUnit\Framework\Attributes\DataProviderExternal; 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 +20,8 @@ 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\Fixture\FixtureDump; +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 +29,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 +43,7 @@ public function getBuildColumnDefinitionProvider(): array public function testAddDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -103,7 +100,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 { @@ -133,7 +130,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 +139,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 +167,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 +195,6 @@ public function testCreateTable(): void ], ), ); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'delete')] @@ -213,7 +205,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 +215,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 +233,7 @@ public function testDropDefaultValue(): void public function testDropIndex(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $this->assertSame( @@ -285,14 +277,12 @@ public function testDropIndex(): void SQL, $qb->dropIndex('{{%schema.table}}', 'index'), ); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'insert')] public function testInsert( string $table, - array|QueryInterface $columns, + Closure|array|QueryInterface $columns, array $params, string $expectedSQL, array $expectedParams, @@ -303,17 +293,17 @@ 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 { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); @@ -323,13 +313,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 +342,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(FixtureDump::V12); - $db = $this->getConnection(true); $qb = $db->getQueryBuilder(); $this->assertSame( @@ -388,13 +373,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 +397,6 @@ public function testTruncateTable(): void SQL, $sql, ); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'update')] @@ -423,7 +404,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 = [], @@ -434,7 +415,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, @@ -445,7 +426,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, @@ -462,7 +443,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 +458,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,16 +473,18 @@ public function testJsonOverlapsBuilder(): void $sql, ); $this->assertSame([], $params); - - $db->close(); } #[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->getConnection(); + $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)) @@ -524,16 +505,18 @@ 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 + public function testOverlapsConditionOperator(Closure|iterable|ExpressionInterface $values, int $expectedCount): void { - $db = $this->getConnection(); + $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]) @@ -554,12 +537,10 @@ public function testOverlapsConditionOperator(iterable|ExpressionInterface $valu ->count(); $this->assertSame($expectedCount, $count); - - $db->close(); } #[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); } @@ -584,7 +565,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, @@ -594,7 +575,7 @@ public function testCaseXBuilder( #[DataProviderExternal(QueryBuilderProvider::class, 'lengthBuilder')] public function testLengthBuilder( - string|ExpressionInterface $operand, + Closure|string|ExpressionInterface $operand, string $expectedSql, int $expectedResult, array $expectedParams = [], @@ -605,7 +586,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 = [], @@ -628,7 +609,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 +630,6 @@ public function testArrayMergeWithTypeWithOrdering( $result = $db->select($arrayMerge)->scalar(); $this->assertSame($expectedResult, $result); - - $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'upsertWithMultiOperandFunctions')] 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 @@ +getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(); $selectExpression = "(customer.name || ' in ' || p.description) AS name"; @@ -39,18 +35,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 +65,5 @@ public function testBooleanValues(): void 1, (new Query($db))->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => false])->count(), ); - - $db->close(); } } 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 diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 67a3b80e3..3a9b4bd82 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -14,23 +14,26 @@ 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\Fixture\FixtureDump; +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; use Yiisoft\Db\Tests\Common\CommonSchemaTest; -use Yiisoft\Db\Tests\Support\DbHelper; +use Yiisoft\Db\Tests\Support\TestHelper; /** * @group pgsql */ 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,29 +47,23 @@ 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 + public function testColumns(array $columns, string $tableName, ?string $dump = null): void { - $db = $this->getConnection(); - - if (version_compare($db->getServerInfo()->getVersion(), '10', '>')) { + if (version_compare(TestConnection::getServerVersion(), '10', '>')) { if ($tableName === 'type') { $columns['timestamp_default']->defaultValue(new Expression('CURRENT_TIMESTAMP')); } } - $this->assertTableColumns($columns, $tableName); - - $db->close(); + $this->assertTableColumns($columns, $tableName, $dump); } 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(FixtureDump::V12); $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'; + $this->ensureMinPostgreSqlVersion('10.0'); - if (version_compare($this->getConnection()->getServerInfo()->getVersion(), '10.0', '<')) { - $this->markTestSkipped('PostgresSQL < 10.0 does not support PARTITION BY clause.'); - } - - $db = $this->getConnection(true); + $db = $this->getSharedConnection(); + $this->loadFixture(FixtureDump::V10); $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,49 +230,6 @@ public function testSequenceName(): void $this->assertNotNull($tableSchema); $this->assertEquals($sequenceName, $tableSchema->getSequenceName()); - - $db->close(); - } - - #[DataProviderExternal(SchemaProvider::class, 'tableSchemaCacheWithTablePrefixes')] - public function testTableSchemaCacheWithTablePrefixes( - string $tablePrefix, - string $tableName, - string $testTablePrefix, - string $testTableName, - ): void { - $db = $this->getConnection(); - - $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); - - $db->close(); } #[DataProviderExternal(SchemaProvider::class, 'constraints')] @@ -320,7 +257,7 @@ public function testTableSchemaWithDbSchemes( string $expectedTableName, string $expectedSchemaName = '', ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $commandMock = $this->createMock(CommandInterface::class); $commandMock->method('queryAll')->willReturn([]); @@ -341,10 +278,8 @@ function ($params) use ($expectedTableName, $expectedSchemaName) { ), ) ->willReturn($commandMock); - $schema = new Schema($mockDb, DbHelper::getSchemaCache()); + $schema = new Schema($mockDb, TestHelper::createMemorySchemaCache()); $schema->getTableSchema($tableName); - - $db->close(); } /** @@ -352,7 +287,7 @@ function ($params) use ($expectedTableName, $expectedSchemaName) { */ public function testTimestampNullDefaultValue(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -374,8 +309,6 @@ public function testTimestampNullDefaultValue(): void $this->assertNotNull($column); $this->assertNull($column->getDefaultValue()); - - $db->close(); } public function testWorkWithDefaultValueConstraint(): void @@ -421,19 +354,18 @@ 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 { $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.'); @@ -443,7 +375,7 @@ public function testNotConnectionPDO(): void public function testDomainType(): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $command = $db->createCommand(); $schema = $db->getSchema(); @@ -469,13 +401,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 +421,6 @@ public function testGetViewNames(): void ], $views, ); - - $db->close(); } #[DataProviderExternal(StructuredTypeProvider::class, 'columns')] @@ -502,13 +431,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(FixtureDump::V11); - $db = $this->getConnection(true); $schema = $db->getSchema(); $tableIndexes = $schema->getTableIndexes('table_index'); @@ -523,8 +450,6 @@ public function testTableIndexes(): void ], $tableIndexes, ); - - $db->close(); } #[DataProviderExternal(SchemaProvider::class, 'resultColumns')] 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); diff --git a/tests/StructuredValueBuilderTest.php b/tests/StructuredValueBuilderTest.php index f9c3e014f..bc83a184d 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; @@ -14,18 +13,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\Pgsql\Tests\Support\TestConnection; 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 +78,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 +138,7 @@ public function testBuild( string $expected, array $expectedParams, ): void { - $db = $this->getConnection(); + $db = $this->getSharedConnection(); $qb = $db->getQueryBuilder(); $params = []; 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 @@ +markTestSkipped( + "This test requires at least PostgreSQL version $version. Current version is $currentVersion.", + ); + } + } + + protected function replaceQuotes(string $sql): string + { + return str_replace(['\\[', '\\]'], ['[', ']'], preg_replace('/(\[\[)|((?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( @@ -32,7 +37,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 diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php deleted file mode 100644 index 1f996d937..000000000 --- a/tests/Support/TestTrait.php +++ /dev/null @@ -1,88 +0,0 @@ -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::get()->getServerInfo()->getVersion(); - if (version_compare($currentVersion, $version, '<')) { - $this->markTestSkipped( - "This test requires at least PostgreSQL version $version. Current version is $currentVersion.", - ); - } - } -}