From d654f7b07d470daaf058d8ed8a42ab2e36fa1680 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Thu, 31 Oct 2024 20:27:42 +0500 Subject: [PATCH 1/3] Ease local testing --- .github/workflows/build.yml | 6 ++++ .github/workflows/mutation.yml | 5 +++ .gitignore | 4 --- composer.json | 4 ++- tests/.env | 6 ++++ tests/CommandTest.php | 8 +---- tests/PDODriverTest.php | 2 +- tests/Support/TestTrait.php | 56 +++++++++++++++++++++++++++++----- tests/bootstrap.php | 8 +++++ 9 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 tests/.env create mode 100644 tests/bootstrap.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b5632e7a5..0fa4efd43 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -93,6 +93,12 @@ jobs: - name: Run tests with phpunit with code coverage. run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always + env: + YII_PGSQL_DATABASE: yiitest + YII_PGSQL_HOST: 127.0.0.1 + YII_PGSQL_PORT: 5432 + YII_PGSQL_USER: root + YII_PGSQL_PASSWORD: root - name: Upload coverage to Codecov. uses: codecov/codecov-action@v3 diff --git a/.github/workflows/mutation.yml b/.github/workflows/mutation.yml index 843f69ab2..8709269f3 100644 --- a/.github/workflows/mutation.yml +++ b/.github/workflows/mutation.yml @@ -79,3 +79,8 @@ jobs: vendor/bin/roave-infection-static-analysis-plugin --threads=2 --ignore-msi-with-no-mutations --only-covered env: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} + YII_PGSQL_DATABASE: yiitest + YII_PGSQL_HOST: 127.0.0.1 + YII_PGSQL_PORT: 5432 + YII_PGSQL_USER: root + YII_PGSQL_PASSWORD: root diff --git a/.gitignore b/.gitignore index 3d694002b..d3a42a795 100644 --- a/.gitignore +++ b/.gitignore @@ -36,10 +36,6 @@ phpunit.phar /phpunit.xml /.phpunit.result.cache -# NPM packages -/node_modules -.env - #codeception /tests/_output c3.php diff --git a/composer.json b/composer.json index 41cd0180a..84515e84b 100644 --- a/composer.json +++ b/composer.json @@ -45,6 +45,7 @@ "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", "vimeo/psalm": "^5.25", + "vlucas/phpdotenv": "^5.6", "yiisoft/aliases": "^2.0", "yiisoft/cache-file": "^3.1", "yiisoft/var-dumper": "^1.5" @@ -52,7 +53,8 @@ "autoload": { "psr-4": { "Yiisoft\\Db\\Pgsql\\": "src" - } + }, + "files": ["tests/bootstrap.php"] }, "autoload-dev": { "psr-4": { diff --git a/tests/.env b/tests/.env new file mode 100644 index 000000000..23d184749 --- /dev/null +++ b/tests/.env @@ -0,0 +1,6 @@ +ENVIRONMENT=local +YII_PGSQL_DATABASE=yii +YII_PGSQL_HOST=postgres +YII_PGSQL_PORT=5432 +YII_PGSQL_USER=postgres +YII_PGSQL_PASSWORD=postgres diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 7ef9c81d2..3ae5b8c04 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -277,12 +277,6 @@ public function testinsertWithReturningPksUuid(): void public function testShowDatabases(): void { - $dsn = new Dsn('pgsql', '127.0.0.1'); - $db = new Connection(new Driver($dsn->asString(), 'root', 'root'), DbHelper::getSchemaCache()); - - $command = $db->createCommand(); - - $this->assertSame('pgsql:host=127.0.0.1;dbname=postgres;port=5432', $db->getDriver()->getDsn()); - $this->assertSame(['yiitest'], $command->showDatabases()); + $this->assertSame([self::getDatabaseName()], static::getDb()->createCommand()->showDatabases()); } } diff --git a/tests/PDODriverTest.php b/tests/PDODriverTest.php index c4ff1b495..a94e5b087 100644 --- a/tests/PDODriverTest.php +++ b/tests/PDODriverTest.php @@ -33,7 +33,7 @@ public function testConnectionCharset(): void $this->assertEqualsIgnoringCase('UTF8', array_values($charset)[0]); - $pdoDriver = new Driver('pgsql:host=127.0.0.1;dbname=yiitest;port=5432', 'root', 'root'); + $pdoDriver = $this->getDriver(); $pdoDriver->charset('latin1'); $pdo = $pdoDriver->createConnection(); $charset = $pdo->query('SHOW client_encoding', PDO::FETCH_ASSOC)->fetch(); diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 293cfa7de..8059f5657 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Pgsql\Tests\Support; use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; +use Yiisoft\Db\Driver\Pdo\PdoDriverInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Pgsql\Connection; @@ -23,9 +24,7 @@ trait TestTrait */ protected function getConnection(bool $fixture = false): PdoConnectionInterface { - $pdoDriver = new Driver($this->getDsn(), 'root', 'root'); - $pdoDriver->charset('utf8'); - $db = new Connection($pdoDriver, DbHelper::getSchemaCache()); + $db = new Connection($this->getDriver(), DbHelper::getSchemaCache()); if ($fixture) { DbHelper::loadFixture($db, __DIR__ . "/Fixture/$this->fixture"); @@ -36,15 +35,25 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface protected static function getDb(): PdoConnectionInterface { - $dsn = (new Dsn(databaseName: 'yiitest'))->asString(); - - return new Connection(new Driver($dsn, 'root', 'root'), DbHelper::getSchemaCache()); + $dsn = (new Dsn( + host: self::getHost(), + databaseName: self::getDatabaseName(), + port: self::getPort(), + ))->asString(); + $driver = new Driver($dsn, self::getUsername(), self::getPassword()); + $driver->charset('utf8'); + + return new Connection($driver, DbHelper::getSchemaCache()); } protected function getDsn(): string { if ($this->dsn === '') { - $this->dsn = (new Dsn(databaseName: 'yiitest'))->asString(); + $this->dsn = (new Dsn( + host: self::getHost(), + databaseName: self::getDatabaseName(), + port: self::getPort(), + ))->asString(); } return $this->dsn; @@ -73,4 +82,37 @@ public static function setUpBeforeClass(): void $db->close(); } + + private function getDriver(): PdoDriverInterface + { + $driver = new Driver($this->getDsn(), self::getUsername(), self::getPassword()); + $driver->charset('utf8'); + + return $driver; + } + + private static function getDatabaseName(): string + { + return getenv('YII_PGSQL_DATABASE'); + } + + private static function getHost(): string + { + return getenv('YII_PGSQL_HOST'); + } + + private static function getPort(): string + { + return getenv('YII_PGSQL_PORT'); + } + + private static function getUsername(): string + { + return getenv('YII_PGSQL_USER'); + } + + private static function getPassword(): string + { + return getenv('YII_PGSQL_PASSWORD'); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 000000000..0b8567eb8 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,8 @@ +load(); +} From c20b5550ecb5d0a4294ee58bfede45b36cf0cc89 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 31 Oct 2024 15:28:00 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- tests/CommandTest.php | 6 +----- tests/PDODriverTest.php | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 3ae5b8c04..1f311ff99 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -8,12 +8,8 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Pgsql\Connection; -use Yiisoft\Db\Pgsql\Dsn; -use Yiisoft\Db\Pgsql\Driver; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonCommandTest; -use Yiisoft\Db\Tests\Support\DbHelper; use function serialize; @@ -277,6 +273,6 @@ public function testinsertWithReturningPksUuid(): void public function testShowDatabases(): void { - $this->assertSame([self::getDatabaseName()], static::getDb()->createCommand()->showDatabases()); + $this->assertSame([self::getDatabaseName()], self::getDb()->createCommand()->showDatabases()); } } diff --git a/tests/PDODriverTest.php b/tests/PDODriverTest.php index a94e5b087..8285e9775 100644 --- a/tests/PDODriverTest.php +++ b/tests/PDODriverTest.php @@ -8,7 +8,6 @@ use PHPUnit\Framework\TestCase; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; -use Yiisoft\Db\Pgsql\Driver; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; /** From f20b47c11e2ebadb4dc2ef3de56816cda0f9bba9 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Mon, 2 Dec 2024 21:35:25 +0500 Subject: [PATCH 3/3] WIP --- tests/Support/TestTrait.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 8059f5657..0b2f4a1e5 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -93,26 +93,26 @@ private function getDriver(): PdoDriverInterface private static function getDatabaseName(): string { - return getenv('YII_PGSQL_DATABASE'); + return getenv('YII_PGSQL_DATABASE') ?: ''; } private static function getHost(): string { - return getenv('YII_PGSQL_HOST'); + return getenv('YII_PGSQL_HOST') ?: ''; } private static function getPort(): string { - return getenv('YII_PGSQL_PORT'); + return getenv('YII_PGSQL_PORT') ?: ''; } private static function getUsername(): string { - return getenv('YII_PGSQL_USER'); + return getenv('YII_PGSQL_USER') ?: ''; } private static function getPassword(): string { - return getenv('YII_PGSQL_PASSWORD'); + return getenv('YII_PGSQL_PASSWORD') ?: ''; } }