Skip to content

Commit

Permalink
feat: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
christyjacob4 committed Oct 26, 2022
1 parent 9a52423 commit 3a723fd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
19 changes: 14 additions & 5 deletions src/DSN/DSN.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class DSN
*/
protected ?string $query;

/**
* @var ?array
*/
protected ?array $params = null;

/**
* Construct
*
Expand Down Expand Up @@ -136,16 +141,20 @@ public function getQuery(): ?string
/**
* Return a query parameter by its key
*
* @return ?string
* @return string
*/
public function getParam(string $key): ?string
public function getParam(string $key, string $default = ''): string
{
if (isset($this->params[$key])) {
return $this->params[$key];
}

if (! $this->query) {
return null;
return $default;
}

parse_str($this->query, $params);
parse_str($this->query, $this->params);

return $params[$key] ?? null;
return $this->params[$key] ?? $default;
}
}
20 changes: 15 additions & 5 deletions tests/DSN/DSNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testSuccess(): void
$this->assertEquals('charset=utf8&timezone=UTC', $dsn->getQuery());
$this->assertEquals('utf8', $dsn->getParam('charset'));
$this->assertEquals('UTC', $dsn->getParam('timezone'));
$this->assertNull($dsn->getParam('doesNotExist'));
$this->assertEmpty($dsn->getParam('doesNotExist'));

$dsn = new DSN('mariadb://user@localhost:3306/database?charset=utf8&timezone=UTC');
$this->assertEquals('mariadb', $dsn->getScheme());
Expand All @@ -31,7 +31,7 @@ public function testSuccess(): void
$this->assertEquals('charset=utf8&timezone=UTC', $dsn->getQuery());
$this->assertEquals('utf8', $dsn->getParam('charset'));
$this->assertEquals('UTC', $dsn->getParam('timezone'));
$this->assertNull($dsn->getParam('doesNotExist'));
$this->assertEmpty($dsn->getParam('doesNotExist'));

$dsn = new DSN('mariadb://user@localhost/database?charset=utf8&timezone=UTC');
$this->assertEquals('mariadb', $dsn->getScheme());
Expand All @@ -43,7 +43,7 @@ public function testSuccess(): void
$this->assertEquals('charset=utf8&timezone=UTC', $dsn->getQuery());
$this->assertEquals('utf8', $dsn->getParam('charset'));
$this->assertEquals('UTC', $dsn->getParam('timezone'));
$this->assertNull($dsn->getParam('doesNotExist'));
$this->assertEmpty($dsn->getParam('doesNotExist'));

$dsn = new DSN('mariadb://user@localhost?charset=utf8&timezone=UTC');
$this->assertEquals('mariadb', $dsn->getScheme());
Expand All @@ -55,7 +55,7 @@ public function testSuccess(): void
$this->assertEquals('charset=utf8&timezone=UTC', $dsn->getQuery());
$this->assertEquals('utf8', $dsn->getParam('charset'));
$this->assertEquals('UTC', $dsn->getParam('timezone'));
$this->assertNull($dsn->getParam('doesNotExist'));
$this->assertEmpty($dsn->getParam('doesNotExist'));

$dsn = new DSN('mariadb://user@localhost');
$this->assertEquals('mariadb', $dsn->getScheme());
Expand Down Expand Up @@ -102,7 +102,17 @@ public function testSuccess(): void
$this->assertEquals('bucket', $dsn->getPath());
$this->assertEquals('region=us-east-1', $dsn->getQuery());
$this->assertEquals('us-east-1', $dsn->getParam('region'));
$this->assertNull($dsn->getParam('doesNotExist'));
$this->assertEmpty($dsn->getParam('doesNotExist'));
}

public function testGetParam(): void
{
$dsn = new DSN('mariadb://user:password@localhost:3306/database?charset=utf8&timezone=UTC');
$this->assertEquals('utf8', $dsn->getParam('charset'));
$this->assertEquals('UTC', $dsn->getParam('timezone'));
$this->assertEmpty($dsn->getParam('doesNotExist'));
$this->assertEquals('us-east-1', $dsn->getParam('region', 'us-east-1'));
$this->assertEquals('us-east-2', $dsn->getParam('region', 'us-east-2'));
}

public function testFail(): void
Expand Down

0 comments on commit 3a723fd

Please sign in to comment.