Skip to content

Commit

Permalink
Improvements Dsn::class, fix phpdoc, add more tests. (#199)
Browse files Browse the repository at this point in the history
* Improvements Dsn::class, fix phpdoc, add more tests.
  • Loading branch information
terabytesoftw committed Dec 30, 2022
1 parent 0eb9be0 commit 40428ee
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/Dsn.php
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql;

use Yiisoft\Db\Connection\AbstractDsn;

/**
* The Dsn class is typically used to parse a DSN string, which is a string that contains all the necessary information
* to connect to a database SQL Server, such as the database driver, host, database name, port, options.
*
* It also allows you to access individual components of the DSN, such as the driver, host, database name or port.
*
* @link https://www.php.net/manual/en/ref.pdo-pgsql.connection.php
*/
final class Dsn extends AbstractDsn
{
/**
* @psalm-param string[] $options
*/
public function __construct(
private string $driver,
private string $host,
private string $databaseName,
private string $port = '5432',
private array $options = []
) {
parent::__construct($driver, $host, $databaseName, $port, $options);
}
}
40 changes: 40 additions & 0 deletions tests/DsnTest.php
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Pgsql\Dsn;

/**
* @group pgsql
*
* @psalm-suppress PropertyNotSetInConstructor
*/
final class DsnTest extends TestCase
{
public function testAsString(): void
{
$this->assertSame(
'pgsql:host=localhost;dbname=yiitest;port=5432',
(new Dsn('pgsql', 'localhost', 'yiitest'))->asString(),
);
}

public function testAsStringWithOptions(): void
{
$this->assertSame(
'pgsql:host=localhost;dbname=yiitest;port=5433;charset=utf8',
(new Dsn('pgsql', 'localhost', 'yiitest', '5433', ['charset' => 'utf8']))->asString(),
);
}

public function testAsStringWithPort(): void
{
$this->assertSame(
'pgsql:host=localhost;dbname=yiitest;port=5433',
(new Dsn('pgsql', 'localhost', 'yiitest', '5433'))->asString(),
);
}
}
15 changes: 12 additions & 3 deletions tests/Support/TestTrait.php
Expand Up @@ -8,12 +8,13 @@
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Pgsql\ConnectionPDO;
use Yiisoft\Db\Pgsql\Dsn;
use Yiisoft\Db\Pgsql\PDODriver;
use Yiisoft\Db\Tests\Support\DbHelper;

trait TestTrait
{
private string $dsn = 'pgsql:host=127.0.0.1;dbname=yiitest;port=5432';
private string $dsn = '';
private string $fixture = 'pgsql.sql';

/**
Expand All @@ -22,9 +23,8 @@ trait TestTrait
*/
protected function getConnection(bool $fixture = false): ConnectionPDOInterface
{
$pdoDriver = new PDODriver($this->dsn, 'root', 'root');
$pdoDriver = new PDODriver($this->getDsn(), 'root', 'root');
$pdoDriver->setCharset('utf8');

$db = new ConnectionPDO($pdoDriver, DbHelper::getQueryCache(), DbHelper::getSchemaCache());

if ($fixture) {
Expand All @@ -34,6 +34,15 @@ protected function getConnection(bool $fixture = false): ConnectionPDOInterface
return $db;
}

protected function getDsn(): string
{
if ($this->dsn === '') {
$this->dsn = (new Dsn('pgsql', '127.0.0.1', 'yiitest', '5432'))->asString();
}

return $this->dsn;
}

protected function getDriverName(): string
{
return 'pgsql';
Expand Down

0 comments on commit 40428ee

Please sign in to comment.