diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b5632e7a..0fa4efd4 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 843f69ab..8709269f 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 3d694002..d3a42a79 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 41cd0180..84515e84 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 00000000..23d18474 --- /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 7ef9c81d..1f311ff9 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,12 +273,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()], self::getDb()->createCommand()->showDatabases()); } } diff --git a/tests/PDODriverTest.php b/tests/PDODriverTest.php index c4ff1b49..8285e977 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; /** @@ -33,7 +32,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 293cfa7d..0b2f4a1e 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 00000000..0b8567eb --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,8 @@ +load(); +}