diff --git a/docs/en/testing.md b/docs/en/testing.md index 64ef8280d..9b5541761 100644 --- a/docs/en/testing.md +++ b/docs/en/testing.md @@ -6,27 +6,42 @@ All our packages have github actions by default, so you can test your [contribut > Note: We recommend pull requesting in draft mode until all tests pass. -## Docker image +## Docker -For greater ease it is recommended to use docker containers, for this you can use the [docker-compose.yml](https://docs.docker.com/compose/compose-file/) file that is in the docs folder. +For greater ease it is recommended to use docker containers. -1. [PostgreSQL 15](/docker-compose.yml) +You can use the [docker-compose.yml](https://docs.docker.com/compose/compose-file/) file with PostgreSQL 15 +that is in the root of the package: -For running the docker containers you can use the following command: +```shell +docker-compose up -d +``` + +or run container directly via command: ```shell -docker compose up -d +docker run --name pgsql -e POSTGRES_PASSWORD=root -e POSTGRES_USER=root -e POSTGRES_DB=yiitest -d postgres:15 ``` +If the IP address at which the container with DB is accessible is different from `127.0.0.1`, then set environment variable +`YIISOFT_DB_PGSQL_TEST_HOST` to actual IP. For example: + +```shell +export YIISOFT_DB_PGSQL_TEST_HOST=172.17.0.3 +```` + +> Environment variable `YIISOFT_DB_PGSQL_TEST_HOST` is used only for this package testing. It's not needed for and isn't used in +> your application or package that uses `yiisoft/db-pgsql`. + ## Unit testing The package is tested with [PHPUnit](https://phpunit.de/). The following steps are required to run the tests: -1. Run the docker container for the dbms. +1. Run the docker container with DBMS. 2. Install the dependencies of the project with composer. -3. Run the tests. +3. Run the tests: ```shell vendor/bin/phpunit diff --git a/tests/CommandTest.php b/tests/CommandTest.php index d924a5503..de787e035 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -12,6 +12,7 @@ use Yiisoft\Db\Pgsql\Connection; use Yiisoft\Db\Pgsql\Dsn; use Yiisoft\Db\Pgsql\Driver; +use Yiisoft\Db\Pgsql\Tests\Support\TestEnvironment; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonCommandTest; use Yiisoft\Db\Tests\Support\DbHelper; @@ -322,12 +323,14 @@ public function testinsertWithReturningPksUuid(): void public function testShowDatabases(): void { - $dsn = new Dsn('pgsql', '127.0.0.1'); + $dbHost = TestEnvironment::getPostgreSqlHost(); + + $dsn = new Dsn('pgsql', $dbHost); $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('pgsql:host=' . $dbHost . ';dbname=postgres;port=5432', $db->getDriver()->getDsn()); $this->assertSame(['yiitest'], $command->showDatabases()); } } diff --git a/tests/PDODriverTest.php b/tests/PDODriverTest.php index c4ff1b495..06436a1e3 100644 --- a/tests/PDODriverTest.php +++ b/tests/PDODriverTest.php @@ -9,6 +9,7 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Pgsql\Driver; +use Yiisoft\Db\Pgsql\Tests\Support\TestEnvironment; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; /** @@ -33,7 +34,11 @@ 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 = new Driver( + 'pgsql:host=' . TestEnvironment::getPostgreSqlHost() . ';dbname=yiitest;port=5432', + 'root', + 'root' + ); $pdoDriver->charset('latin1'); $pdo = $pdoDriver->createConnection(); $charset = $pdo->query('SHOW client_encoding', PDO::FETCH_ASSOC)->fetch(); diff --git a/tests/Support/TestEnvironment.php b/tests/Support/TestEnvironment.php new file mode 100644 index 000000000..8e7199872 --- /dev/null +++ b/tests/Support/TestEnvironment.php @@ -0,0 +1,14 @@ +asString(); + $dsn = (new Dsn('pgsql', TestEnvironment::getPostgreSqlHost(), 'yiitest', '5432'))->asString(); return new Connection(new Driver($dsn, 'root', 'root'), DbHelper::getSchemaCache()); } @@ -44,7 +44,7 @@ protected static function getDb(): PdoConnectionInterface protected function getDsn(): string { if ($this->dsn === '') { - $this->dsn = (new Dsn('pgsql', '127.0.0.1', 'yiitest', '5432'))->asString(); + $this->dsn = (new Dsn('pgsql', TestEnvironment::getPostgreSqlHost(), 'yiitest', '5432'))->asString(); } return $this->dsn;