55namespace Yiisoft \Db \Pgsql \Tests \Support ;
66
77use Yiisoft \Db \Driver \Pdo \PdoConnectionInterface ;
8+ use Yiisoft \Db \Driver \Pdo \PdoDriverInterface ;
89use Yiisoft \Db \Exception \Exception ;
910use Yiisoft \Db \Exception \InvalidConfigException ;
1011use 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}
0 commit comments