Skip to content

Commit 737fe6d

Browse files
authored
Refactor Dsn class (#378)
1 parent 5dbca2d commit 737fe6d

File tree

4 files changed

+58
-55
lines changed

4 files changed

+58
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- New #320: Realize `ColumnBuilder` class (@Tigrov)
1717
- Enh #321: Update according changes in `ColumnSchemaInterface` (@Tigrov)
1818
- New #322, #330, #340: Add `ColumnDefinitionBuilder` class (@Tigrov)
19-
- Enh #323: Refactor `Dsn` class (@Tigrov)
19+
- Enh #323, #378: Refactor `Dsn` class (@Tigrov)
2020
- Enh #324: Use constructor to create columns and initialize properties (@Tigrov)
2121
- Enh #327: Refactor `Schema::findColumns()` method (@Tigrov)
2222
- Enh #328: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)

src/Dsn.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,63 @@
44

55
namespace Yiisoft\Db\Mssql;
66

7-
use Yiisoft\Db\Connection\AbstractDsn;
7+
use Stringable;
88

99
/**
10-
* Implement a Data Source Name (DSN) for an MSSQL Server.
10+
* Represents a Data Source Name (DSN) for a MSSQL Server that's used to configure a {@see Driver} instance.
11+
*
12+
* To get DSN in string format, use the `(string)` type casting operator.
1113
*
1214
* @link https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php
1315
*/
14-
final class Dsn extends AbstractDsn
16+
final class Dsn implements Stringable
1517
{
1618
/**
19+
* @param string $driver The database driver name.
20+
* @param string $host The database host name or IP address.
21+
* @param string $databaseName The database name to connect to. Empty string if isn't set.
22+
* @param string $port The database port. Empty string if isn't set.
23+
* @param string[] $options The database connection options. Default value to an empty array.
24+
*
1725
* @psalm-param array<string,string> $options
1826
*/
1927
public function __construct(
20-
string $driver = 'sqlsrv',
21-
string $host = '127.0.0.1',
22-
string|null $databaseName = null,
23-
string|null $port = '1433',
24-
array $options = []
28+
public readonly string $driver = 'sqlsrv',
29+
public readonly string $host = '127.0.0.1',
30+
public readonly string $databaseName = '',
31+
public readonly string $port = '1433',
32+
public readonly array $options = [],
2533
) {
26-
parent::__construct($driver, $host, $databaseName, $port, $options);
2734
}
2835

2936
/**
3037
* @return string the Data Source Name, or DSN, has the information required to connect to the database.
3138
* Please refer to the [PHP manual](https://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
3239
*
33-
* The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
34-
* `key=value` and concatenated by `;`. For example:
40+
* The `driver` property is used as the driver prefix of the DSN. For example:
3541
*
3642
* ```php
3743
* $dsn = new Dsn('sqlsrv', 'localhost', 'yiitest', '1433');
38-
* $driver = new Driver($dsn->asString(), 'username', 'password');
44+
* $driver = new Driver($dsn, 'username', 'password');
3945
* $db = new Connection($driver, $schemaCache);
4046
* ```
4147
*
4248
* Will result in the DSN string `sqlsrv:Server=localhost,1433;Database=yiitest`.
4349
*/
44-
public function asString(): string
50+
public function __toString(): string
4551
{
46-
$driver = $this->getDriver();
47-
$host = $this->getHost();
48-
$port = $this->getPort();
49-
$databaseName = $this->getDatabaseName();
50-
$options = $this->getOptions();
51-
52-
$dsn = "$driver:Server=$host";
52+
$dsn = "$this->driver:Server=$this->host";
5353

54-
if (!empty($port)) {
55-
$dsn .= ",$port";
54+
if ($this->port !== '') {
55+
$dsn .= ",$this->port";
5656
}
5757

58-
if (!empty($databaseName)) {
59-
$dsn .= ";Database=$databaseName";
58+
if ($this->databaseName !== '') {
59+
$dsn .= ";Database=$this->databaseName";
6060
}
6161

62-
if (!empty($options)) {
63-
foreach ($options as $key => $value) {
64-
$dsn .= ";$key=$value";
65-
}
62+
foreach ($this->options as $key => $value) {
63+
$dsn .= ";$key=$value";
6664
}
6765

6866
return $dsn;

tests/DsnTest.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,39 @@
1414
*/
1515
final class DsnTest extends TestCase
1616
{
17-
public function testAsString(): void
17+
public function testConstruct(): void
1818
{
19-
$this->assertSame(
20-
'sqlsrv:Server=127.0.0.1,1433;Database=yiitest',
21-
(new Dsn('sqlsrv', '127.0.0.1', 'yiitest', '1433'))->asString(),
22-
);
19+
$dsn = new Dsn('sqlsrv', 'localhost', 'yiitest', '1434', ['Encrypt' => 'no']);
20+
21+
$this->assertSame('sqlsrv', $dsn->driver);
22+
$this->assertSame('localhost', $dsn->host);
23+
$this->assertSame('yiitest', $dsn->databaseName);
24+
$this->assertSame('1434', $dsn->port);
25+
$this->assertSame(['Encrypt' => 'no'], $dsn->options);
26+
$this->assertSame('sqlsrv:Server=localhost,1434;Database=yiitest;Encrypt=no', (string) $dsn);
2327
}
2428

25-
public function testAsStringWithDatabaseName(): void
29+
public function testConstructDefaults(): void
2630
{
27-
$this->assertSame('sqlsrv:Server=127.0.0.1,1433', (new Dsn('sqlsrv', '127.0.0.1'))->asString());
31+
$dsn = new Dsn();
32+
33+
$this->assertSame('sqlsrv', $dsn->driver);
34+
$this->assertSame('127.0.0.1', $dsn->host);
35+
$this->assertSame('', $dsn->databaseName);
36+
$this->assertSame('1433', $dsn->port);
37+
$this->assertSame([], $dsn->options);
38+
$this->assertSame('sqlsrv:Server=127.0.0.1,1433', (string) $dsn);
2839
}
2940

30-
public function testAsStringWithDatabaseNameWithEmptyString(): void
41+
public function testConstructWithEmptyPort(): void
3142
{
32-
$this->assertSame('sqlsrv:Server=127.0.0.1,1433', (new Dsn('sqlsrv', '127.0.0.1', ''))->asString());
33-
}
34-
35-
public function testAsStringWithDatabaseNameWithNull(): void
36-
{
37-
$this->assertSame('sqlsrv:Server=127.0.0.1,1433', (new Dsn('sqlsrv', '127.0.0.1', null))->asString());
38-
}
39-
40-
public function testAsStringWithEmptyPort(): void
41-
{
42-
$this->assertSame(
43-
'sqlsrv:Server=127.0.0.1;Database=yiitest',
44-
(new Dsn('sqlsrv', '127.0.0.1', 'yiitest', ''))->asString(),
45-
);
43+
$dsn = new Dsn('sqlsrv', 'localhost', port: '');
44+
45+
$this->assertSame('sqlsrv', $dsn->driver);
46+
$this->assertSame('localhost', $dsn->host);
47+
$this->assertSame('', $dsn->databaseName);
48+
$this->assertSame('', $dsn->port);
49+
$this->assertSame([], $dsn->options);
50+
$this->assertSame('sqlsrv:Server=localhost', (string) $dsn);
4651
}
4752
}

tests/Support/TestTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ protected function getConnection(bool $fixture = false): Connection
3636

3737
protected static function getDb(): Connection
3838
{
39-
$dsn = (new Dsn(
39+
$dsn = (string) new Dsn(
4040
host: self::getHost(),
4141
databaseName: self::getDatabaseName(),
4242
port: self::getPort(),
4343
options: ['Encrypt' => 'no']
44-
))->asString();
44+
);
4545

4646
return new Connection(
4747
new Driver($dsn, self::getUsername(), self::getPassword()),
@@ -52,12 +52,12 @@ protected static function getDb(): Connection
5252
protected function getDsn(): string
5353
{
5454
if ($this->dsn === '') {
55-
$this->dsn = (new Dsn(
55+
$this->dsn = (string) new Dsn(
5656
host: self::getHost(),
5757
databaseName: self::getDatabaseName(),
5858
port: self::getPort(),
5959
options: ['Encrypt' => 'no']
60-
))->asString();
60+
);
6161
}
6262

6363
return $this->dsn;

0 commit comments

Comments
 (0)