5
5
namespace Yiisoft \Db \Mssql \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 \Mssql \Connection ;
@@ -23,10 +24,7 @@ trait TestTrait
23
24
*/
24
25
protected function getConnection (bool $ fixture = false ): PdoConnectionInterface
25
26
{
26
- $ db = new Connection (
27
- new Driver ($ this ->getDsn (), 'SA ' , 'YourStrong!Passw0rd ' ),
28
- DbHelper::getSchemaCache ()
29
- );
27
+ $ db = new Connection ($ this ->getDriver (), DbHelper::getSchemaCache ());
30
28
31
29
if ($ fixture ) {
32
30
DbHelper::loadFixture ($ db , __DIR__ . "/Fixture/ $ this ->fixture " );
@@ -37,18 +35,28 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface
37
35
38
36
protected static function getDb (): PdoConnectionInterface
39
37
{
40
- $ dsn = (new Dsn (databaseName: 'yiitest ' , options: ['Encrypt ' => 'no ' ]))->asString ();
38
+ $ dsn = (new Dsn (
39
+ host: self ::getHost (),
40
+ databaseName: self ::getDatabaseName (),
41
+ port: self ::getPort (),
42
+ options: ['Encrypt ' => 'no ' ]
43
+ ))->asString ();
41
44
42
45
return new Connection (
43
- new Driver ($ dsn , ' SA ' , ' YourStrong!Passw0rd ' ),
46
+ new Driver ($ dsn , self :: getUsername (), self :: getPassword () ),
44
47
DbHelper::getSchemaCache (),
45
48
);
46
49
}
47
50
48
51
protected function getDsn (): string
49
52
{
50
53
if ($ this ->dsn === '' ) {
51
- $ this ->dsn = (new Dsn (databaseName: 'yiitest ' , options: ['Encrypt ' => 'no ' ]))->asString ();
54
+ $ this ->dsn = (new Dsn (
55
+ host: self ::getHost (),
56
+ databaseName: self ::getDatabaseName (),
57
+ port: self ::getPort (),
58
+ options: ['Encrypt ' => 'no ' ]
59
+ ))->asString ();
52
60
}
53
61
54
62
return $ this ->dsn ;
@@ -68,4 +76,34 @@ protected function setFixture(string $fixture): void
68
76
{
69
77
$ this ->fixture = $ fixture ;
70
78
}
79
+
80
+ private function getDriver (): PdoDriverInterface
81
+ {
82
+ return new Driver ($ this ->getDsn (), self ::getUsername (), self ::getPassword ());
83
+ }
84
+
85
+ private static function getDatabaseName (): string
86
+ {
87
+ return getenv ('YII_MSSQL_DATABASE ' ) ?? '' ;
88
+ }
89
+
90
+ private static function getHost (): string
91
+ {
92
+ return getenv ('YII_MSSQL_HOST ' ) ?? '' ;
93
+ }
94
+
95
+ private static function getPort (): string
96
+ {
97
+ return getenv ('YII_MSSQL_PORT ' ) ?? '' ;
98
+ }
99
+
100
+ private static function getUsername (): string
101
+ {
102
+ return getenv ('YII_MSSQL_USER ' ) ?? '' ;
103
+ }
104
+
105
+ private static function getPassword (): string
106
+ {
107
+ return getenv ('YII_MSSQL_PASSWORD ' ) ?? '' ;
108
+ }
71
109
}
0 commit comments