Skip to content

Commit f901ace

Browse files
authored
Refactor Dsn class (#344)
1 parent 4f80770 commit f901ace

File tree

4 files changed

+50
-62
lines changed

4 files changed

+50
-62
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov)
1919
- New #282, #291, #299, #302: Add `ColumnDefinitionBuilder` class (@Tigrov)
2020
- Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov)
21-
- Enh #283: Refactor `Dsn` class (@Tigrov)
21+
- Enh #283, #344: Refactor `Dsn` class (@Tigrov)
2222
- Enh #286: Use constructor to create columns and initialize properties (@Tigrov)
2323
- Enh #288, #317: Refactor `Schema::findColumns()` method (@Tigrov)
2424
- Enh #289: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)

src/Dsn.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,53 @@
44

55
namespace Yiisoft\Db\Oracle;
66

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

99
/**
10-
* Implement a Data Source Name (DSN) for an Oracle Server.
10+
* Represents a Data Source Name (DSN) for an Oracle 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-oci.connection.php
1315
*/
14-
final class Dsn extends AbstractDsn
16+
final class Dsn implements Stringable
1517
{
1618
/**
1719
* @psalm-param array<string,string> $options
1820
*/
1921
public function __construct(
20-
string $driver = 'oci',
21-
string $host = '127.0.0.1',
22-
string|null $databaseName = null,
23-
string $port = '1521',
24-
array $options = []
22+
public readonly string $driver = 'oci',
23+
public readonly string $host = '127.0.0.1',
24+
public readonly string $databaseName = '',
25+
public readonly string $port = '1521',
26+
public readonly array $options = [],
2527
) {
26-
parent::__construct($driver, $host, $databaseName, $port, $options);
2728
}
2829

2930
/**
3031
* @return string The Data Source Name, or DSN, contains the information required to connect to the database.
3132
*
3233
* Please refer to the [PHP manual](https://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
3334
*
34-
* The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
35-
* `key=value` and concatenated by `;`. For example:
35+
* The `driver` property is used as the driver prefix of the DSN. For example:
3636
*
3737
* ```php
3838
* $dsn = new Dsn('oci', 'localhost', 'yiitest', '1521', ['charset' => 'AL32UTF8']);
39-
* $connection = new Connection($dsn->asString(), 'system', 'root');
39+
* $driver = new Driver($dsn, 'username', 'password');
40+
* $connection = new Connection($driver, 'system', 'root');
4041
* ```
4142
*
4243
* Will result in the DSN string `oci:dbname=localhost:1521/yiitest;charset=AL32UTF8`.
4344
*/
44-
public function asString(): string
45+
public function __toString(): string
4546
{
46-
$driver = $this->getDriver();
47-
$host = $this->getHost();
48-
$databaseName = $this->getDatabaseName();
49-
$port = $this->getPort();
50-
$options = $this->getOptions();
51-
52-
$dsn = "$driver:dbname=$host:$port";
47+
$dsn = "$this->driver:dbname=$this->host:$this->port";
5348

54-
if (!empty($databaseName)) {
55-
$dsn .= "/$databaseName";
49+
if ($this->databaseName !== '') {
50+
$dsn .= "/$this->databaseName";
5651
}
5752

58-
foreach ($options as $key => $value) {
53+
foreach ($this->options as $key => $value) {
5954
$dsn .= ";$key=$value";
6055
}
6156

tests/DsnTest.php

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

25-
public function testAsStringWithDatabaseNameWithEmptyString(): void
29+
public function testConstructDefaults(): void
2630
{
27-
$this->assertSame(
28-
'oci:dbname=localhost:1521;charset=AL32UTF8',
29-
(new Dsn('oci', 'localhost', '', '1521', ['charset' => 'AL32UTF8']))->asString(),
30-
);
31+
$dsn = new Dsn();
32+
33+
$this->assertSame('oci', $dsn->driver);
34+
$this->assertSame('127.0.0.1', $dsn->host);
35+
$this->assertSame('', $dsn->databaseName);
36+
$this->assertSame('1521', $dsn->port);
37+
$this->assertSame([], $dsn->options);
38+
$this->assertSame('oci:dbname=127.0.0.1:1521', (string) $dsn);
3139
}
3240

33-
public function testAsStringWithDatabaseNameWithNull(): void
41+
public function testConstructWithEmptyPort(): void
3442
{
35-
$this->assertSame(
36-
'oci:dbname=localhost:1521;charset=AL32UTF8',
37-
(new Dsn('oci', 'localhost', null, '1521', ['charset' => 'AL32UTF8']))->asString(),
38-
);
39-
}
40-
41-
/**
42-
* Oracle service name it support only in version 18 and higher, for docker image gvenzl/oracle-xe:18
43-
*/
44-
public function testAsStringWithService(): void
45-
{
46-
$this->assertSame(
47-
'oci:dbname=localhost:1521/yiitest;charset=AL32UTF8',
48-
(new Dsn('oci', 'localhost', 'yiitest', '1521', ['charset' => 'AL32UTF8']))->asString(),
49-
);
50-
}
51-
52-
public function testAsStringWithSID(): void
53-
{
54-
$this->assertSame(
55-
'oci:dbname=localhost:1521/XE;charset=AL32UTF8',
56-
(new Dsn('oci', 'localhost', 'XE', '1521', ['charset' => 'AL32UTF8']))->asString(),
57-
);
43+
$dsn = new Dsn('oci', 'localhost', port: '');
44+
45+
$this->assertSame('oci', $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('oci:dbname=localhost:', (string) $dsn);
5851
}
5952
}

tests/Support/TestTrait.php

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

3838
protected static function getDb(): Connection
3939
{
40-
$dsn = (new Dsn(
40+
$dsn = (string) new Dsn(
4141
host: self::getHost(),
4242
databaseName: self::getSid(),
4343
port: self::getPort(),
4444
options: ['charset' => 'AL32UTF8'],
45-
))->asString();
45+
);
4646

4747
return new Connection(new Driver($dsn, self::getUsername(), self::getPassword()), DbHelper::getSchemaCache());
4848
}
4949

5050
protected function getDsn(): string
5151
{
5252
if ($this->dsn === '') {
53-
$this->dsn = (new Dsn(
53+
$this->dsn = (string) new Dsn(
5454
host: self::getHost(),
5555
databaseName: self::getSid(),
5656
port: self::getPort(),
5757
options: ['charset' => 'AL32UTF8'],
58-
))->asString();
58+
);
5959
}
6060

6161
return $this->dsn;

0 commit comments

Comments
 (0)