5
5
namespace Yiisoft \Db \Pgsql \Tests \Support ;
6
6
7
7
use Yiisoft \Db \Driver \Pdo \PdoConnectionInterface ;
8
+ use Yiisoft \Db \Driver \Pdo \PdoDriverInterface ;
8
9
use Yiisoft \Db \Exception \Exception ;
9
10
use Yiisoft \Db \Exception \InvalidConfigException ;
10
11
use Yiisoft \Db \Pgsql \Connection ;
@@ -23,9 +24,7 @@ trait TestTrait
23
24
*/
24
25
protected function getConnection (bool $ fixture = false ): PdoConnectionInterface
25
26
{
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 ());
29
28
30
29
if ($ fixture ) {
31
30
DbHelper::loadFixture ($ db , __DIR__ . "/Fixture/ $ this ->fixture " );
@@ -36,15 +35,25 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface
36
35
37
36
protected static function getDb (): PdoConnectionInterface
38
37
{
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 ());
42
47
}
43
48
44
49
protected function getDsn (): string
45
50
{
46
51
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 ();
48
57
}
49
58
50
59
return $ this ->dsn ;
@@ -73,4 +82,37 @@ public static function setUpBeforeClass(): void
73
82
74
83
$ db ->close ();
75
84
}
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
+ }
76
118
}
0 commit comments