Skip to content

Commit

Permalink
Ease local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Oct 31, 2024
1 parent 4f85a22 commit d654f7b
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 20 deletions.
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
8 changes: 1 addition & 7 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,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()], static::getDb()->createCommand()->showDatabases());
}
}
2 changes: 1 addition & 1 deletion tests/PDODriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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();
}

0 comments on commit d654f7b

Please sign in to comment.