From 029bf2ca02cc85ae16f73ac87408cf144e2a0955 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 23 Jun 2023 16:13:24 +0300 Subject: [PATCH 1/9] Add ability to change DB host and port for tests --- tests/CommandTest.php | 8 ++++++-- tests/PDODriverTest.php | 6 +++++- tests/Support/TestEnvironment.php | 20 ++++++++++++++++++++ tests/Support/TestTrait.php | 14 ++++++++++++-- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 tests/Support/TestEnvironment.php diff --git a/tests/CommandTest.php b/tests/CommandTest.php index d924a5503..46b0969df 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,15 @@ public function testinsertWithReturningPksUuid(): void public function testShowDatabases(): void { - $dsn = new Dsn('pgsql', '127.0.0.1'); + $dbHost = TestEnvironment::getPostgreSqlHost(); + $dbPort = TestEnvironment::getPostgreSqlPort(); + + $dsn = new Dsn('pgsql', $dbHost, port: $dbPort); $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=' . $dbPort, $db->getDriver()->getDsn()); $this->assertSame(['yiitest'], $command->showDatabases()); } } diff --git a/tests/PDODriverTest.php b/tests/PDODriverTest.php index c4ff1b495..865ee78cd 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,10 @@ 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'); + $dbHost = TestEnvironment::getPostgreSqlHost(); + $dbPort = TestEnvironment::getPostgreSqlPort(); + + $pdoDriver = new Driver('pgsql:host=' . $dbHost . ';dbname=yiitest;port=' . $dbPort, '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..c4418efb8 --- /dev/null +++ b/tests/Support/TestEnvironment.php @@ -0,0 +1,20 @@ +asString(); + $dsn = (new Dsn( + 'pgsql', + TestEnvironment::getPostgreSqlHost(), + 'yiitest', + TestEnvironment::getPostgreSqlPort() + ))->asString(); return new Connection(new Driver($dsn, 'root', 'root'), DbHelper::getSchemaCache()); } @@ -44,7 +49,12 @@ 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', + TestEnvironment::getPostgreSqlPort() + ))->asString(); } return $this->dsn; From a9edda9ff98cd7d0cd0e6e4d0f3fa83d860d2568 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 23 Jun 2023 17:34:09 +0300 Subject: [PATCH 2/9] remove port --- tests/CommandTest.php | 5 ++--- tests/PDODriverTest.php | 9 +++++---- tests/Support/TestEnvironment.php | 6 ------ tests/Support/TestTrait.php | 14 ++------------ 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 46b0969df..de787e035 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -324,14 +324,13 @@ public function testinsertWithReturningPksUuid(): void public function testShowDatabases(): void { $dbHost = TestEnvironment::getPostgreSqlHost(); - $dbPort = TestEnvironment::getPostgreSqlPort(); - $dsn = new Dsn('pgsql', $dbHost, port: $dbPort); + $dsn = new Dsn('pgsql', $dbHost); $db = new Connection(new Driver($dsn->asString(), 'root', 'root'), DbHelper::getSchemaCache()); $command = $db->createCommand(); - $this->assertSame('pgsql:host=' . $dbHost . ';dbname=postgres;port=' . $dbPort, $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 865ee78cd..06436a1e3 100644 --- a/tests/PDODriverTest.php +++ b/tests/PDODriverTest.php @@ -34,10 +34,11 @@ public function testConnectionCharset(): void $this->assertEqualsIgnoringCase('UTF8', array_values($charset)[0]); - $dbHost = TestEnvironment::getPostgreSqlHost(); - $dbPort = TestEnvironment::getPostgreSqlPort(); - - $pdoDriver = new Driver('pgsql:host=' . $dbHost . ';dbname=yiitest;port=' . $dbPort, '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 index c4418efb8..8e7199872 100644 --- a/tests/Support/TestEnvironment.php +++ b/tests/Support/TestEnvironment.php @@ -11,10 +11,4 @@ public static function getPostgreSqlHost(): string $host = getenv('YIISOFT_DB_PGSQL_TEST_HOST'); return $host === false ? '127.0.0.1' : $host; } - - public static function getPostgreSqlPort(): string - { - $port = getenv('YIISOFT_DB_PGSQL_TEST_PORT'); - return $port === false ? '5432' : $port; - } } diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index d43285477..e41426c9e 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -36,12 +36,7 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface protected static function getDb(): PdoConnectionInterface { - $dsn = (new Dsn( - 'pgsql', - TestEnvironment::getPostgreSqlHost(), - 'yiitest', - TestEnvironment::getPostgreSqlPort() - ))->asString(); + $dsn = (new Dsn('pgsql', TestEnvironment::getPostgreSqlHost(), 'yiitest', '5432'))->asString(); return new Connection(new Driver($dsn, 'root', 'root'), DbHelper::getSchemaCache()); } @@ -49,12 +44,7 @@ protected static function getDb(): PdoConnectionInterface protected function getDsn(): string { if ($this->dsn === '') { - $this->dsn = (new Dsn( - 'pgsql', - TestEnvironment::getPostgreSqlHost(), - 'yiitest', - TestEnvironment::getPostgreSqlPort() - ))->asString(); + $this->dsn = (new Dsn('pgsql', TestEnvironment::getPostgreSqlHost(), 'yiitest', '5432'))->asString(); } return $this->dsn; From e62e15dd7446c1757a7dcb529f66d2e42394f9ae Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 23 Jun 2023 17:49:25 +0300 Subject: [PATCH 3/9] improve docs --- docs/en/testing.md | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/en/testing.md b/docs/en/testing.md index 64ef8280d..1f9dc3308 100644 --- a/docs/en/testing.md +++ b/docs/en/testing.md @@ -6,32 +6,47 @@ 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) - -For running the docker containers you can use the following command: +For this you can use the [docker-compose.yml](https://docs.docker.com/compose/compose-file/) file with PostgreSQL 15 +that is in the root of package: ```shell docker compose up -d ``` +or run container directly via command: + +```shell +docker run --name pgsql -e POSTGRES_PASSWORD=root -e POSTGRES_USER=root -e POSTGRES_DB=yiitest -d postgres:15 +``` + ## 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 PostgreSQL database. 2. Install the dependencies of the project with composer. -3. Run the tests. +3. Run the tests with the command: ```shell vendor/bin/phpunit ``` +If 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` usage only for this package tests. It's not need and not work for +> your application or library that used `yiisoft/db-pgsql`. + ### Mutation testing The package tests are checked with [Infection](https://infection.github.io/) mutation framework with From 41acb3b6cdc50386d1713dd63f5f5f7a80cc1d90 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 23 Jun 2023 17:49:25 +0300 Subject: [PATCH 4/9] improve docs --- docs/en/testing.md | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/en/testing.md b/docs/en/testing.md index 64ef8280d..e3e8d8803 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) - -For running the docker containers you can use the following command: +For this you can use the [docker-compose.yml](https://docs.docker.com/compose/compose-file/) file with PostgreSQL 15 +that is in the root of package: ```shell docker compose up -d ``` +or run container directly via command: + +```shell +docker run --name pgsql -e POSTGRES_PASSWORD=root -e POSTGRES_USER=root -e POSTGRES_DB=yiitest -d postgres:15 +``` + +If 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` usage only for this package testing. It's not need and not work for +> your application or library that used `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 PostgreSQL database. 2. Install the dependencies of the project with composer. -3. Run the tests. +3. Run the tests with the command: ```shell vendor/bin/phpunit From 9e2854c31917c7a1c7238353326451221dc04757 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 25 Jun 2023 18:44:34 +0300 Subject: [PATCH 5/9] Update docs/en/testing.md Co-authored-by: Alexander Makarov --- docs/en/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/testing.md b/docs/en/testing.md index e3e8d8803..417975b86 100644 --- a/docs/en/testing.md +++ b/docs/en/testing.md @@ -10,7 +10,7 @@ All our packages have github actions by default, so you can test your [contribut 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 with PostgreSQL 15 +You can use the [docker-compose.yml](https://docs.docker.com/compose/compose-file/) file with PostgreSQL 15 that is in the root of package: ```shell From 5065ad7772bdd98bee79487593e8907bf04725b6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 25 Jun 2023 18:44:41 +0300 Subject: [PATCH 6/9] Update docs/en/testing.md Co-authored-by: Alexander Makarov --- docs/en/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/testing.md b/docs/en/testing.md index 417975b86..27333a08d 100644 --- a/docs/en/testing.md +++ b/docs/en/testing.md @@ -11,7 +11,7 @@ All our packages have github actions by default, so you can test your [contribut For greater ease it is recommended to use docker containers. You can use the [docker-compose.yml](https://docs.docker.com/compose/compose-file/) file with PostgreSQL 15 -that is in the root of package: +that is in the root of the package: ```shell docker compose up -d From 5436a69681273ad01d843ba9fd0a4f6b9ecd99d9 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 25 Jun 2023 18:44:47 +0300 Subject: [PATCH 7/9] Update docs/en/testing.md Co-authored-by: Alexander Makarov --- docs/en/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/testing.md b/docs/en/testing.md index 27333a08d..9e2d5dfe9 100644 --- a/docs/en/testing.md +++ b/docs/en/testing.md @@ -23,7 +23,7 @@ or run container directly via command: docker run --name pgsql -e POSTGRES_PASSWORD=root -e POSTGRES_USER=root -e POSTGRES_DB=yiitest -d postgres:15 ``` -If IP address at which the container with DB is accessible is different from `127.0.0.1`, then set environment variable +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 From 99f4a7c8d66d974b7aab48e0d96956cdc637b46d Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 25 Jun 2023 18:44:54 +0300 Subject: [PATCH 8/9] Update docs/en/testing.md Co-authored-by: Alexander Makarov --- docs/en/testing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/testing.md b/docs/en/testing.md index 9e2d5dfe9..e2c1dc865 100644 --- a/docs/en/testing.md +++ b/docs/en/testing.md @@ -30,8 +30,8 @@ If the IP address at which the container with DB is accessible is different from export YIISOFT_DB_PGSQL_TEST_HOST=172.17.0.3 ```` -> Environment variable `YIISOFT_DB_PGSQL_TEST_HOST` usage only for this package testing. It's not need and not work for -> your application or library that used `yiisoft/db-pgsql`. +> 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 From 5fd76d888328cb19d1ac848f1e27a90597da8411 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Mon, 26 Jun 2023 07:07:51 +0300 Subject: [PATCH 9/9] Apply suggestions from code review --- docs/en/testing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/en/testing.md b/docs/en/testing.md index e2c1dc865..9b5541761 100644 --- a/docs/en/testing.md +++ b/docs/en/testing.md @@ -14,7 +14,7 @@ You can use the [docker-compose.yml](https://docs.docker.com/compose/compose-fil that is in the root of the package: ```shell -docker compose up -d +docker-compose up -d ``` or run container directly via command: @@ -39,9 +39,9 @@ The package is tested with [PHPUnit](https://phpunit.de/). The following steps are required to run the tests: -1. Run the docker container with PostgreSQL database. +1. Run the docker container with DBMS. 2. Install the dependencies of the project with composer. -3. Run the tests with the command: +3. Run the tests: ```shell vendor/bin/phpunit