Skip to content

Commit

Permalink
Fix self-signed cert for MSSQL CI (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 23, 2022
1 parent b3e06bc commit 6ca86cd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ jobs:
- name: "Run tests: MSSQL"
env:
DB_DSN: "sqlsrv:host=mssql;dbname=master"
DB_DSN: "sqlsrv:host=mssql;dbname=master;driverOptions[TrustServerCertificate]=1"
DB_USER: sa
DB_PASSWORD: atk4_pass
run: |
Expand Down
9 changes: 8 additions & 1 deletion src/Persistence/Sql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ public static function normalizeDsn($dsn, $user = null, $password = null)
} else {
foreach (explode(';', $parts[1] ?? '') as $part) {
[$k, $v] = str_contains($part, '=') ? explode('=', $part, 2) : [$part, null];
$dsn[$k] = $v;
if ($k === 'query' || str_contains($part, '[')) {
parse_str($k === 'query' ? $v : $part, $partRes);
foreach ($partRes as $pK => $pV) {
$dsn[$pK] = $pV;
}
} else {
$dsn[$k] = $v;
}
}
if (isset($dsn['host']) && str_contains($dsn['host'], ':')) {
[$dsn['host'], $port] = explode(':', $dsn['host'], 2);
Expand Down
7 changes: 7 additions & 0 deletions tests/Persistence/Sql/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public function testDsnNormalize(): void
$dsn = Connection::normalizeDsn('mysql:host=localhost:1234;dbname=db');
$this->assertSame(['driver' => 'mysqli', 'host' => 'localhost', 'dbname' => 'db', 'port' => '1234'], $dsn);

// driverOptions array
$dsn = Connection::normalizeDsn('pdo-sqlsrv://localhost:1234/db?driverOptions[TrustServerCertificate]=1');
$this->assertSame(['driver' => 'pdo_sqlsrv', 'host' => 'localhost', 'port' => '1234', 'driverOptions' => ['TrustServerCertificate' => '1'], 'dbname' => 'db'], $dsn);

$dsn = Connection::normalizeDsn('pdo_sqlsrv:host=localhost:1234;dbname=db;driverOptions[TrustServerCertificate]=1');
$this->assertSame(['driver' => 'pdo_sqlsrv', 'host' => 'localhost', 'dbname' => 'db', 'driverOptions' => ['TrustServerCertificate' => '1'], 'port' => '1234'], $dsn);

// full PDO and native driver names
$dsn = Connection::normalizeDsn('pdo-mysql://root:pass@localhost/db');
$this->assertSame(['driver' => 'pdo_mysql', 'host' => 'localhost', 'user' => 'root', 'password' => 'pass', 'dbname' => 'db'], $dsn);
Expand Down

0 comments on commit 6ca86cd

Please sign in to comment.