Skip to content

Commit 2e2491d

Browse files
authored
Ease local testing (#367)
1 parent 2105ce3 commit 2e2491d

File tree

9 files changed

+79
-25
lines changed

9 files changed

+79
-25
lines changed

.github/workflows/build.yml

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ jobs:
9393

9494
- name: Run tests with phpunit with code coverage.
9595
run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always
96+
env:
97+
YII_PGSQL_DATABASE: yiitest
98+
YII_PGSQL_HOST: 127.0.0.1
99+
YII_PGSQL_PORT: 5432
100+
YII_PGSQL_USER: root
101+
YII_PGSQL_PASSWORD: root
96102

97103
- name: Upload coverage to Codecov.
98104
uses: codecov/codecov-action@v3

.github/workflows/mutation.yml

+5
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ jobs:
7979
vendor/bin/roave-infection-static-analysis-plugin --threads=2 --ignore-msi-with-no-mutations --only-covered
8080
env:
8181
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
82+
YII_PGSQL_DATABASE: yiitest
83+
YII_PGSQL_HOST: 127.0.0.1
84+
YII_PGSQL_PORT: 5432
85+
YII_PGSQL_USER: root
86+
YII_PGSQL_PASSWORD: root

.gitignore

-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ phpunit.phar
3636
/phpunit.xml
3737
/.phpunit.result.cache
3838

39-
# NPM packages
40-
/node_modules
41-
.env
42-
4339
#codeception
4440
/tests/_output
4541
c3.php

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@
4545
"roave/infection-static-analysis-plugin": "^1.16",
4646
"spatie/phpunit-watcher": "^1.23",
4747
"vimeo/psalm": "^5.25",
48+
"vlucas/phpdotenv": "^5.6",
4849
"yiisoft/aliases": "^2.0",
4950
"yiisoft/cache-file": "^3.1",
5051
"yiisoft/var-dumper": "^1.5"
5152
},
5253
"autoload": {
5354
"psr-4": {
5455
"Yiisoft\\Db\\Pgsql\\": "src"
55-
}
56+
},
57+
"files": ["tests/bootstrap.php"]
5658
},
5759
"autoload-dev": {
5860
"psr-4": {

tests/.env

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ENVIRONMENT=local
2+
YII_PGSQL_DATABASE=yii
3+
YII_PGSQL_HOST=postgres
4+
YII_PGSQL_PORT=5432
5+
YII_PGSQL_USER=postgres
6+
YII_PGSQL_PASSWORD=postgres

tests/CommandTest.php

+1-11
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
use Yiisoft\Db\Exception\Exception;
1010
use Yiisoft\Db\Exception\InvalidConfigException;
1111
use Yiisoft\Db\Exception\NotSupportedException;
12-
use Yiisoft\Db\Pgsql\Connection;
13-
use Yiisoft\Db\Pgsql\Dsn;
14-
use Yiisoft\Db\Pgsql\Driver;
1512
use Yiisoft\Db\Pgsql\Tests\Provider\CommandProvider;
1613
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
1714
use Yiisoft\Db\Tests\Common\CommonCommandTest;
18-
use Yiisoft\Db\Tests\Support\DbHelper;
1915

2016
use function serialize;
2117

@@ -279,13 +275,7 @@ public function testinsertWithReturningPksUuid(): void
279275

280276
public function testShowDatabases(): void
281277
{
282-
$dsn = new Dsn('pgsql', '127.0.0.1');
283-
$db = new Connection(new Driver($dsn->asString(), 'root', 'root'), DbHelper::getSchemaCache());
284-
285-
$command = $db->createCommand();
286-
287-
$this->assertSame('pgsql:host=127.0.0.1;dbname=postgres;port=5432', $db->getDriver()->getDsn());
288-
$this->assertSame(['yiitest'], $command->showDatabases());
278+
$this->assertSame([self::getDatabaseName()], self::getDb()->createCommand()->showDatabases());
289279
}
290280

291281
#[DataProviderExternal(CommandProvider::class, 'createIndex')]

tests/PDODriverTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPUnit\Framework\TestCase;
99
use Yiisoft\Db\Exception\Exception;
1010
use Yiisoft\Db\Exception\InvalidConfigException;
11-
use Yiisoft\Db\Pgsql\Driver;
1211
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
1312

1413
/**
@@ -33,7 +32,7 @@ public function testConnectionCharset(): void
3332

3433
$this->assertEqualsIgnoringCase('UTF8', array_values($charset)[0]);
3534

36-
$pdoDriver = new Driver('pgsql:host=127.0.0.1;dbname=yiitest;port=5432', 'root', 'root');
35+
$pdoDriver = $this->getDriver();
3736
$pdoDriver->charset('latin1');
3837
$pdo = $pdoDriver->createConnection();
3938
$charset = $pdo->query('SHOW client_encoding', PDO::FETCH_ASSOC)->fetch();

tests/Support/TestTrait.php

+49-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Pgsql\Tests\Support;
66

77
use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface;
8+
use Yiisoft\Db\Driver\Pdo\PdoDriverInterface;
89
use Yiisoft\Db\Exception\Exception;
910
use Yiisoft\Db\Exception\InvalidConfigException;
1011
use Yiisoft\Db\Pgsql\Connection;
@@ -23,9 +24,7 @@ trait TestTrait
2324
*/
2425
protected function getConnection(bool $fixture = false): PdoConnectionInterface
2526
{
26-
$pdoDriver = new Driver($this->getDsn(), 'root', 'root');
27-
$pdoDriver->charset('utf8');
28-
$db = new Connection($pdoDriver, DbHelper::getSchemaCache());
27+
$db = new Connection($this->getDriver(), DbHelper::getSchemaCache());
2928

3029
if ($fixture) {
3130
DbHelper::loadFixture($db, __DIR__ . "/Fixture/$this->fixture");
@@ -36,15 +35,25 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface
3635

3736
protected static function getDb(): PdoConnectionInterface
3837
{
39-
$dsn = (new Dsn(databaseName: 'yiitest'))->asString();
40-
41-
return new Connection(new Driver($dsn, 'root', 'root'), DbHelper::getSchemaCache());
38+
$dsn = (new Dsn(
39+
host: self::getHost(),
40+
databaseName: self::getDatabaseName(),
41+
port: self::getPort(),
42+
))->asString();
43+
$driver = new Driver($dsn, self::getUsername(), self::getPassword());
44+
$driver->charset('utf8');
45+
46+
return new Connection($driver, DbHelper::getSchemaCache());
4247
}
4348

4449
protected function getDsn(): string
4550
{
4651
if ($this->dsn === '') {
47-
$this->dsn = (new Dsn(databaseName: 'yiitest'))->asString();
52+
$this->dsn = (new Dsn(
53+
host: self::getHost(),
54+
databaseName: self::getDatabaseName(),
55+
port: self::getPort(),
56+
))->asString();
4857
}
4958

5059
return $this->dsn;
@@ -73,4 +82,37 @@ public static function setUpBeforeClass(): void
7382

7483
$db->close();
7584
}
85+
86+
private function getDriver(): PdoDriverInterface
87+
{
88+
$driver = new Driver($this->getDsn(), self::getUsername(), self::getPassword());
89+
$driver->charset('utf8');
90+
91+
return $driver;
92+
}
93+
94+
private static function getDatabaseName(): string
95+
{
96+
return getenv('YII_PGSQL_DATABASE') ?: '';
97+
}
98+
99+
private static function getHost(): string
100+
{
101+
return getenv('YII_PGSQL_HOST') ?: '';
102+
}
103+
104+
private static function getPort(): string
105+
{
106+
return getenv('YII_PGSQL_PORT') ?: '';
107+
}
108+
109+
private static function getUsername(): string
110+
{
111+
return getenv('YII_PGSQL_USER') ?: '';
112+
}
113+
114+
private static function getPassword(): string
115+
{
116+
return getenv('YII_PGSQL_PASSWORD') ?: '';
117+
}
76118
}

tests/bootstrap.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
if (getenv('ENVIRONMENT', local_only: true) === 'local') {
6+
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
7+
$dotenv->load();
8+
}

0 commit comments

Comments
 (0)