Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions docs/en/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
7 changes: 6 additions & 1 deletion tests/PDODriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/TestEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql\Tests\Support;

final class TestEnvironment
{
public static function getPostgreSqlHost(): string
{
$host = getenv('YIISOFT_DB_PGSQL_TEST_HOST');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need use const

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is necessary since that's the only place this string is used.

return $host === false ? '127.0.0.1' : $host;
}
}
4 changes: 2 additions & 2 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface

protected static function getDb(): PdoConnectionInterface
{
$dsn = (new Dsn('pgsql', '127.0.0.1', 'yiitest', '5432'))->asString();
$dsn = (new Dsn('pgsql', TestEnvironment::getPostgreSqlHost(), 'yiitest', '5432'))->asString();

return new Connection(new Driver($dsn, 'root', 'root'), DbHelper::getSchemaCache());
}

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;
Expand Down