Skip to content

Commit

Permalink
Add SqlConfig test
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 31, 2022
1 parent 664945b commit 6ac65a3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"require-dev": {
"amphp/php-cs-fixer-config": "^2",
"amphp/phpunit-util": "^3",
"phpunit/phpunit": "^9",
"psalm/phar": "^5.4"
},
Expand Down
19 changes: 9 additions & 10 deletions src/SqlConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ abstract class SqlConfig
* Parses a connection string into an array of keys and values given.
*
* @param string $connectionString Connection string, e.g., "hostname=localhost username=sql password=default"
* @param string[] $keymap Map of alternative key names to canonical key names.
* @param array<non-empty-string, non-empty-string> $keymap Map of alternative key names to canonical key names.
*
* @return string[]
* @return array<non-empty-string, string>
*/
protected static function parseConnectionString(string $connectionString, array $keymap = self::KEY_MAP): array
{
Expand All @@ -42,18 +42,17 @@ protected static function parseConnectionString(string $connectionString, array

foreach ($params as $param) {
/** @psalm-suppress PossiblyInvalidArgument */
[$key, $value] = \array_map("trim", \explode("=", $param, 2) + [1 => ""]);

if (isset($keymap[$key])) {
$key = $keymap[$key];
[$key, $value] = \array_map(\trim(...), \explode("=", $param, 2) + [1 => ""]);
if ($key === '') {
throw new \ValueError("Empty key name in connection string");
}

$values[$key] = $value;
$values[$keymap[$key] ?? $key] = $value;
}

if (\preg_match('/^(.+):(\d{1,5})$/', $values["host"] ?? "", $matches)) {
$values["host"] = $matches[1];
$values["port"] = $matches[2];
if (\preg_match('/^(?<host>.+):(?<port>\d{1,5})$/', $values["host"] ?? "", $matches)) {
$values["host"] = $matches["host"];
$values["port"] = $matches["port"];
}

return $values;
Expand Down
8 changes: 4 additions & 4 deletions test/QueryErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Amp\Sql\Test;

use Amp\PHPUnit\AsyncTestCase;
use Amp\Sql\QueryError;
use PHPUnit\Framework\TestCase;

class QueryErrorTest extends AsyncTestCase
class QueryErrorTest extends TestCase
{
/**
* @test
Expand All @@ -14,7 +14,7 @@ public function testItPassesQueryAlong()
{
$error = new QueryError('error', 'SELECT * FROM foo');

$this->assertSame('SELECT * FROM foo', $error->getQuery());
$this->assertStringStartsWith("Amp\Sql\QueryError: error\nCurrent query was SELECT * FROM foo", (string) $error);
self::assertSame('SELECT * FROM foo', $error->getQuery());
self::assertStringStartsWith("Amp\Sql\QueryError: error\nCurrent query was SELECT * FROM foo", (string) $error);
}
}
67 changes: 67 additions & 0 deletions test/SqlConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);

namespace Amp\Sql\Test;

use Amp\Sql\SqlConfig;
use PHPUnit\Framework\TestCase;

class SqlConfigTest extends TestCase
{
private function createConfigFromString(string $connectionString): SqlConfig
{
return new class($connectionString) extends SqlConfig {
public function __construct(string $connectionString)
{
$parts = self::parseConnectionString($connectionString);

parent::__construct(
host: $parts["host"] ?? '',
port: (int) ($parts["port"] ?? 0),
user: $parts["user"] ?? "",
password: $parts["password"] ?? "",
database: $parts["db"] ?? "",
);
}
};
}

public function testPortInHost(): void
{
$config = $this->createConfigFromString("host=localhost:5432 user=user database=test");

self::assertSame("localhost", $config->getHost());
self::assertSame(5432, $config->getPort());
self::assertSame("user", $config->getUser());
self::assertSame("", $config->getPassword());
self::assertSame("test", $config->getDatabase());
}

public function testBasicSyntax(): void
{
$config = $this->createConfigFromString("host=localhost port=5432 user=user pass=test db=test");

self::assertSame("localhost", $config->getHost());
self::assertSame(5432, $config->getPort());
self::assertSame("user", $config->getUser());
self::assertSame("test", $config->getPassword());
self::assertSame("test", $config->getDatabase());
}

public function testAlternativeSyntax(): void
{
$config = $this->createConfigFromString("host=localhost;port=3306;user=user;password=test;db=test");

self::assertSame("localhost", $config->getHost());
self::assertSame(3306, $config->getPort());
self::assertSame("user", $config->getUser());
self::assertSame("test", $config->getPassword());
self::assertSame("test", $config->getDatabase());
}

public function testInvalidString(): void
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage("Empty key name in connection string");
$this->createConfigFromString("invalid =connection string");
}
}

0 comments on commit 6ac65a3

Please sign in to comment.