Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ease local testing #367

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ phpunit.phar
/phpunit.xml
/.phpunit.result.cache

# NPM packages
/node_modules
.env

#codeception
/tests/_output
c3.php
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@
"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"
},
"autoload": {
"psr-4": {
"Yiisoft\\Db\\Pgsql\\": "src"
}
},
"files": ["tests/bootstrap.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
6 changes: 6 additions & 0 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -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
12 changes: 1 addition & 11 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
3 changes: 1 addition & 2 deletions tests/PDODriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
Expand Down
56 changes: 49 additions & 7 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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') ?: '';
}
}
8 changes: 8 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

if (getenv('ENVIRONMENT', local_only: true) === 'local') {
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();
}
Loading